SUMO - Simulation of Urban MObility
MSPModel_NonInteracting.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The pedestrian following model (prototype)
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <cmath>
24 #include <algorithm>
26 #include <utils/geom/GeomHelper.h>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSEdge.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSJunction.h>
35 
36 
37 // ===========================================================================
38 // DEBUGGING HELPERS
39 // ===========================================================================
40 //
41 #define DEBUG1 "disabled"
42 #define DEBUG2 "disabled"
43 #define DEBUGCOND(PEDID) (PEDID == DEBUG1 || PEDID == DEBUG2)
44 
45 // ===========================================================================
46 // named (internal) constants
47 // ===========================================================================
48 
49 
50 // ===========================================================================
51 // static members
52 // ===========================================================================
53 
54 
55 // ===========================================================================
56 // MSPModel_NonInteracting method definitions
57 // ===========================================================================
58 
60  myNet(net) {
61  assert(myNet != 0);
62  UNUSED_PARAMETER(oc);
63 }
64 
65 
67 }
68 
69 
72  MoveToNextEdge* cmd = new MoveToNextEdge(person, *stage);
73  PState* state = new PState(cmd);
74  const SUMOTime firstEdgeDuration = state->computeWalkingTime(nullptr, *stage, now);
75  myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + firstEdgeDuration);
76 
77  //if DEBUGCOND(person->getID()) std::cout << SIMTIME << " " << person->getID() << " inserted on " << stage->getEdge()->getID() << "\n";
78  return state;
79 }
80 
81 
82 void
84  dynamic_cast<PState*>(state)->getCommand()->abortWalk();
85 }
86 
87 
90  if (myPerson == nullptr) {
91  return 0; // descheduled
92  }
93  PState* state = dynamic_cast<PState*>(myParent.getPedestrianState());
94  const MSEdge* old = myParent.getEdge();
95  const bool arrived = myParent.moveToNextEdge(myPerson, currentTime);
96  if (arrived) {
97  // walk finished
98  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " arrived on " << old->getID() << "\n";
99  return 0;
100  } else {
101  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " moves to " << myParent.getEdge()->getID() << "\n";
102  return state->computeWalkingTime(old, myParent, currentTime);
103  }
104 }
105 
106 
107 SUMOTime
109  myLastEntryTime = currentTime;
110  const MSEdge* edge = stage.getEdge();
111  const MSEdge* next = stage.getNextRouteEdge();
112  int dir = UNDEFINED_DIRECTION;
113  if (prev == nullptr) {
114  myCurrentBeginPos = stage.getDepartPos();
115  } else {
116  // default to FORWARD if not connected
117  dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
118  myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
119  }
120  if (next == nullptr) {
121  myCurrentEndPos = stage.getArrivalPos();
122  } else {
123  if (dir == UNDEFINED_DIRECTION) {
124  // default to FORWARD if not connected
125  dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
126  }
127  myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
128  }
129  // ensure that a result > 0 is returned even if the walk ends immediately
130  // adding 0.5ms is done to ensure proper rounding
131  myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed(myCommand->getPerson())));
132  //std::cout << std::setprecision(8) << SIMTIME << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed(myCommand->getPerson()) << " dur=" << myCurrentDuration << "\n";
133  // round to the next timestep to avoid systematic higher walking speed
134  if ((myCurrentDuration % DELTA_T) > 0) {
135  myCurrentDuration += DELTA_T;
136  }
137  return myCurrentDuration;
138 }
139 
140 
141 double
143  //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
144  return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime);
145 }
146 
147 
148 Position
150  const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
151  if (lane == nullptr) {
152  //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
153  // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
154  //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
155  // throw ProcessError(error);
156  //}
157  lane = stage.getEdge()->getLanes().front();
158  }
159  const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
160  * (MSNet::getInstance()->lefthand() ? -1 : 1));
161  return stage.getLanePosition(lane, getEdgePos(stage, now), lateral_offset);
162 }
163 
164 
165 double
167  //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
168  double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
169  if (angle > M_PI) {
170  angle -= 2 * M_PI;
171  }
172  return angle;
173 }
174 
175 
176 SUMOTime
178  return 0;
179 }
180 
181 
182 double
184  return stage.getMaxSpeed(myCommand->getPerson());
185 }
186 
187 
188 const MSEdge*
190  return stage.getNextRouteEdge();
191 }
192 
193 /****************************************************************************/
const MSEdge * getNextEdge(const MSPerson::MSPersonStage_Walking &stage) const
return the list of internal edges if the pedestrian is on an intersection
long long int SUMOTime
Definition: SUMOTime.h:36
is a pedestrian
SUMOTime computeWalkingTime(const MSEdge *prev, const MSPerson::MSPersonStage_Walking &stage, SUMOTime currentTime)
compute walking time on edge and update state members
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.cpp:88
static const int FORWARD
Definition: MSPModel.h:100
double getEdgePos(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
abstract methods inherited from PedestrianState
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:162
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
T MAX2(T a, T b)
Definition: StdDefs.h:76
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
abstract base class for managing callbacks to retrieve various state information from the model ...
static const double SIDEWALK_OFFSET
the offset for computing person positions when walking on edges without a sidewalk ...
Definition: MSPModel.h:111
double getAngle(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the direction in which the person faces in degrees
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
double getLength() const
return the length of the edge
Definition: MSEdge.h:568
const MSJunction * getToJunction() const
Definition: MSEdge.h:347
PedestrianState * add(MSPerson *person, MSPerson::MSPersonStage_Walking *stage, SUMOTime now)
register the given person as a pedestrian
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
void remove(PedestrianState *state)
remove the specified person from the pedestrian simulation
The simulated network and simulation perfomer.
Definition: MSNet.h:84
A road/street connecting two junctions.
Definition: MSEdge.h:75
static const int UNDEFINED_DIRECTION
Definition: MSPModel.h:105
MSPModel_NonInteracting(const OptionsCont &oc, MSNet *net)
Constructor (it should not be necessary to construct more than one instance)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
double getSpeed(const MSPerson::MSPersonStage_Walking &stage) const
return the current speed of the person
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:409
MSNet * myNet
the net to which to issue moveToNextEdge commands
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
double getMaxSpeed(const MSTransportable *const person) const
accessors to be used by MSPModel
Definition: MSPerson.cpp:349
double getArrivalPos() const
Definition: MSPerson.h:154
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:615
SUMOTime getWaitingTime(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the time the person spent standing
abstract base class for managing callbacks to retrieve various state information from the model ...
Definition: MSPModel.h:128
Position getPosition(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the network coordinate of the person
#define M_PI
Definition: odrSpiral.cpp:40
const MSJunction * getFromJunction() const
Definition: MSEdge.h:343
A storage for options typed value containers)
Definition: OptionsCont.h:92
bool allowsVehicleClass(SUMOVehicleClass vclass) const
Definition: MSLane.h:772
static const int BACKWARD
Definition: MSPModel.h:104
double getEdgeAngle(const MSEdge *e, double at) const
get angle of the edge at a certain position
const MSEdge * getNextRouteEdge() const
Definition: MSPerson.h:165
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
SUMOTime execute(SUMOTime currentTime)
Executes the command.