SUMO - Simulation of Urban MObility
TraCI_Person.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // C++ TraCI client API implementation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSNet.h>
35 #include <utils/geom/GeomHelper.h>
37 #include "TraCI_VehicleType.h"
38 #include "TraCI_Person.h"
39 
40 
41 // ===========================================================================
42 // member definitions
43 // ===========================================================================
44 
45 std::vector<std::string>
48  std::vector<std::string> ids;
49  for (MSTransportableControl::constVehIt i = c.loadedBegin(); i != c.loadedEnd(); ++i) {
50  if (i->second->getCurrentStageType() != MSTransportable::WAITING_FOR_DEPART) {
51  ids.push_back(i->first);
52  }
53  }
54  return std::move(ids);
55 }
56 
57 
58 int
61 }
62 
63 
65 TraCI_Person::getPosition(const std::string& personID) {
66  MSTransportable* p = getPerson(personID);
67  TraCIPosition pos;
68  pos.x = p->getPosition().x();
69  pos.y = p->getPosition().y();
70  pos.z = p->getPosition().z();
71  return pos;
72 }
73 
74 
75 double
76 TraCI_Person::getAngle(const std::string& personID){
77  return GeomHelper::naviDegree(getPerson(personID)->getAngle());
78 }
79 
80 
81 double
82 TraCI_Person::getSpeed(const std::string& personID){
83  return getPerson(personID)->getSpeed();
84 }
85 
86 
87 std::string
88 TraCI_Person::getRoadID(const std::string& personID){
89  return getPerson(personID)->getEdge()->getID();
90 }
91 
92 
93 double
94 TraCI_Person::getLanePosition(const std::string& personID) {
95  return getPerson(personID)->getEdgePos();
96 }
97 
98 
100 TraCI_Person::getColor(const std::string& personID){
101  const RGBColor& col = getPerson(personID)->getParameter().color;
102  TraCIColor tcol;
103  tcol.r = col.red();
104  tcol.g = col.green();
105  tcol.b = col.blue();
106  tcol.a = col.alpha();
107  return tcol;
108 }
109 
110 
111 std::string
112 TraCI_Person::getTypeID(const std::string& personID) {
113  return getPerson(personID)->getVehicleType().getID();
114 }
115 
116 
117 double
118 TraCI_Person::getWaitingTime(const std::string& personID) {
119  return getPerson(personID)->getWaitingSeconds();
120 }
121 
122 
123 std::string
124 TraCI_Person::getNextEdge(const std::string& personID) {
125  return dynamic_cast<MSPerson*>(getPerson(personID))->getNextEdge();
126 }
127 
128 
129 std::vector<std::string>
130 TraCI_Person::getEdges(const std::string& personID, int nextStageIndex) {
131  MSTransportable* p = getPerson(personID);
132  if (nextStageIndex >= p->getNumRemainingStages()) {
133  throw TraCIException("The stage index must be lower than the number of remaining stages.");
134  }
135  if (nextStageIndex < (p->getNumRemainingStages() - p->getNumStages())) {
136  throw TraCIException("The negative stage index must refer to a valid previous stage.");
137  }
138  std::vector<std::string> edgeIDs;
139  for(auto& e : p->getEdges(nextStageIndex)) {
140  edgeIDs.push_back(e->getID());
141  }
142  return edgeIDs;
143 }
144 
145 
146 int
147 TraCI_Person::getStage(const std::string& personID, int nextStageIndex) {
148  MSTransportable* p = getPerson(personID);
149  if (nextStageIndex >= p->getNumRemainingStages()) {
150  throw TraCIException("The stage index must be lower than the number of remaining stages.");
151  }
152  if (nextStageIndex < (p->getNumRemainingStages() - p->getNumStages())) {
153  throw TraCIException("The negative stage index must refer to a valid previous stage.");
154  }
155  return p->getStageType(nextStageIndex);
156 }
157 
158 
159 int
160 TraCI_Person::getRemainingStages(const std::string& personID) {
161  return getPerson(personID)->getNumRemainingStages();
162 }
163 
164 
165 std::string
166 TraCI_Person::getVehicle(const std::string& personID) {
167  const SUMOVehicle* veh = getPerson(personID)->getVehicle();
168  if (veh == nullptr) {
169  return "";
170  } else {
171  return veh->getID();
172  }
173 }
174 
175 
176 std::string
177 TraCI_Person::getParameter(const std::string& personID, const std::string& param) {
178  return getPerson(personID)->getParameter().getParameter(param, "");
179 }
180 
181 
182 
183 
184 void
185 TraCI_Person::setSpeed(const std::string& personID, double speed) {
186  getPerson(personID)->setSpeed(speed);
187 }
188 
189 
190 void
191 TraCI_Person::setType(const std::string& personID, const std::string& typeID) {
192  MSVehicleType* vehicleType = MSNet::getInstance()->getVehicleControl().getVType(typeID);
193  if (vehicleType == 0) {
194  throw TraCIException("The vehicle type '" + typeID + "' is not known.");
195  }
196  getPerson(personID)->replaceVehicleType(vehicleType);
197 }
198 
199 
200 void
201 TraCI_Person::add(const std::string& personID, const std::string& edgeID, double pos, double departInSecs, const std::string typeID) {
202  MSTransportable* p;
203  try {
204  p = getPerson(personID);
205  } catch (TraCIException&) {
206  p = nullptr;
207  }
208 
209  if (p != nullptr) {
210  throw TraCIException("The person " + personID + " to add already exists.");
211  }
212 
213  SUMOTime depart = TIME2STEPS(departInSecs);
214  SUMOVehicleParameter vehicleParams;
215  vehicleParams.id = personID;
216 
217  MSVehicleType* vehicleType = MSNet::getInstance()->getVehicleControl().getVType(typeID);
218  if (!vehicleType) {
219  throw TraCIException("Invalid type '" + typeID + "' for person '" + personID + "'");
220  }
221 
222  const MSEdge* edge = MSEdge::dictionary(edgeID);
223  if (!edge) {
224  throw TraCIException("Invalid edge '" + edgeID + "' for person: '" + personID + "'");
225  }
226 
227  if (depart < 0) {
228  const int proc = (int)-depart;
229  if (proc >= static_cast<int>(DEPART_DEF_MAX)) {
230  throw TraCIException("Invalid departure time.");
231  }
232  vehicleParams.departProcedure = (DepartDefinition)proc;
233  vehicleParams.depart = MSNet::getInstance()->getCurrentTimeStep();
234  } else if (depart < MSNet::getInstance()->getCurrentTimeStep()) {
235  vehicleParams.depart = MSNet::getInstance()->getCurrentTimeStep();
236  WRITE_WARNING("Departure time " + toString(departInSecs) + " for person '" + personID
237  + "' is in the past; using current time " + time2string(vehicleParams.depart) + " instead.");
238  } else {
239  vehicleParams.depart = depart;
240  }
241 
242  vehicleParams.departPosProcedure = DEPART_POS_GIVEN;
243  if (fabs(pos) > edge->getLength()) {
244  throw TraCIException("Invalid departure position.");
245  }
246  if (pos < 0) {
247  pos += edge->getLength();
248  }
249  vehicleParams.departPos = pos;
250 
251  SUMOVehicleParameter* params = new SUMOVehicleParameter(vehicleParams);
253  plan->push_back(new MSTransportable::Stage_Waiting(*edge, 0, depart, pos, "awaiting departure", true));
254 
255  try {
256  MSTransportable* person = MSNet::getInstance()->getPersonControl().buildPerson(params, vehicleType, plan, 0);
258  } catch (ProcessError& e) {
259  delete params;
260  delete plan;
261  throw TraCIException(e.what());
262  }
263 }
264 
265 
266 void
267 TraCI_Person::appendDrivingStage(const std::string& personID, const std::string& toEdge, const std::string& lines, const std::string& stopID) {
268  MSTransportable* p = getPerson(personID);
269  const MSEdge* edge = MSEdge::dictionary(toEdge);
270  if (!edge) {
271  throw TraCIException("Invalid edge '" + toEdge + "' for person: '" + personID + "'");
272  }
273  if (lines.size() == 0) {
274  return throw TraCIException("Empty lines parameter for person: '" + personID + "'");
275  }
276  MSStoppingPlace* bs = 0;
277  if (stopID != "") {
279  if (bs == 0) {
280  throw TraCIException("Invalid stopping place id '" + stopID + "' for person: '" + personID + "'");
281  }
282  }
283  p->appendStage(new MSPerson::MSPersonStage_Driving(*edge, bs, -NUMERICAL_EPS, StringTokenizer(lines).getVector()));
284 }
285 
286 
287 void
288 TraCI_Person::appendWaitingStage(const std::string& personID, double duration, const std::string& description, const std::string& stopID) {
289  MSTransportable* p = getPerson(personID);
290  if (duration < 0) {
291  throw TraCIException("Duration for person: '" + personID + "' must not be negative");
292  }
293  MSStoppingPlace* bs = 0;
294  if (stopID != "") {
296  if (bs == 0) {
297  throw TraCIException("Invalid stopping place id '" + stopID + "' for person: '" + personID + "'");
298  }
299  }
300  p->appendStage(new MSTransportable::Stage_Waiting(*p->getArrivalEdge(), TIME2STEPS(duration), 0, p->getArrivalPos(), description, false));
301 }
302 
303 
304 void
305 TraCI_Person::appendWalkingStage(const std::string& personID, const std::vector<std::string>& edgeIDs, double arrivalPos, double duration, double speed, const std::string& stopID) {
306  MSTransportable* p = getPerson(personID);
307  ConstMSEdgeVector edges;
308  try {
309  MSEdge::parseEdgesList(edgeIDs, edges, "<unknown>");
310  } catch (ProcessError& e) {
311  throw TraCIException(e.what());
312  }
313  if (edges.empty()) {
314  throw TraCIException("Empty edge list for walking stage of person '" + personID+ "'.");
315  }
316  if (fabs(arrivalPos) > edges.back()->getLength()) {
317  throw TraCIException("Invalid arrivalPos for walking stage of person '" + personID + "'.");
318  }
319  if (arrivalPos < 0) {
320  arrivalPos += edges.back()->getLength();
321  }
322  if (speed < 0) {
323  speed = p->getVehicleType().getMaxSpeed();
324  }
325  MSStoppingPlace* bs = 0;
326  if (stopID != "") {
328  if (bs == 0) {
329  throw TraCIException("Invalid stopping place id '" + stopID + "' for person: '" + personID + "'");
330  }
331  }
332  p->appendStage(new MSPerson::MSPersonStage_Walking(p->getID(), edges, bs, TIME2STEPS(duration), speed, p->getArrivalPos(), arrivalPos, 0));
333 }
334 
335 
336 void
337 TraCI_Person::removeStage(const std::string& personID, int nextStageIndex) {
338  MSTransportable* p = getPerson(personID);
339  if (nextStageIndex >= p->getNumRemainingStages()) {
340  throw TraCIException("The stage index must be lower than the number of remaining stages.");
341  }
342  if (nextStageIndex < 0) {
343  throw TraCIException("The stage index may not be negative.");
344  }
345  p->removeStage(nextStageIndex);
346 }
347 
348 
349 void
350 TraCI_Person::rerouteTraveltime(const std::string& personID) {
351  MSTransportable* p = getPerson(personID);
353  throw TraCIException("Person '" + personID + "' is not currenlty walking.");
354  }
355  const MSEdge* from = p->getEdge();
356  double departPos = p->getEdgePos();
357  const MSEdge* to = p->getArrivalEdge();
358  double arrivalPos = p->getArrivalPos();
359  double speed = p->getVehicleType().getMaxSpeed();
360  ConstMSEdgeVector newEdges;
361  MSNet::getInstance()->getPedestrianRouter().compute(from, to, departPos, arrivalPos, speed, 0, 0, newEdges);
362  if (newEdges.empty()) {
363  throw TraCIException("Could not find new route for person '" + personID + "'.");
364  }
365  ConstMSEdgeVector oldEdges = p->getEdges(0);
366  assert(!oldEdges.empty());
367  if (oldEdges.front()->getFunction() != EDGEFUNC_NORMAL) {
368  oldEdges.erase(oldEdges.begin());
369  }
370  if (newEdges == oldEdges) {
371  return;
372  }
373  if (newEdges.front() != from) {
374  // @note: maybe this should be done automatically by the router
375  newEdges.insert(newEdges.begin(), from);
376  }
377  //std::cout << " from=" << from->getID() << " to=" << to->getID() << " newEdges=" << toString(newEdges) << "\n";
378  MSPerson::MSPersonStage_Walking* newStage = new MSPerson::MSPersonStage_Walking(p->getID(), newEdges, 0, -1, speed, departPos, arrivalPos, 0);
379  if (p->getNumRemainingStages() == 1) {
380  // Do not remove the last stage (a waiting stage would be added otherwise)
381  p->appendStage(newStage);
382  //std::cout << "case a: remaining=" << p->getNumRemainingStages() << "\n";
383  p->removeStage(0);
384  } else {
385  p->removeStage(0);
386  p->appendStage(newStage);
387  //std::cout << "case b: remaining=" << p->getNumRemainingStages() << "\n";
388  }
389 }
390 
391 
394 void
395 TraCI_Person::setParameter(const std::string& personID, const std::string& key, const std::string& value) {
396  MSTransportable* p = getPerson(personID);
397  ((SUMOVehicleParameter&) p->getParameter()).setParameter(key, value);
398 }
399 
400 void
401 TraCI_Person::setLength(const std::string& personID, double length) {
403 }
404 
405 void
406 TraCI_Person::setWidth(const std::string& personID, double width) {
408 }
409 
410 void
411 TraCI_Person::setHeight(const std::string& personID, double height) {
413 }
414 
415 void
416 TraCI_Person::setMinGap(const std::string& personID, double minGap) {
418 }
419 
420 void
421 TraCI_Person::setColor(const std::string& personID, const TraCIColor& c) {
423 }
424 
425 
426 
427 /******** private functions *************/
428 
430 TraCI_Person::getPerson(const std::string& personID) {
432  MSTransportable* p = c.get(personID);
433  if (p == 0) {
434  throw TraCIException("Person '" + personID + "' is not known");
435  }
436  return p;
437 }
438 
439 std::string
440 TraCI_Person::getSingularVType(const std::string& personID) {
441  return getPerson(personID)->getSingularType().getID();
442 }
443 
444 
445 /****************************************************************************/
static TraCIPosition getPosition(const std::string &personID)
double x
Definition: TraCIDefs.h:71
void setMinGap(const double &minGap)
Set a new value for this type&#39;s minimum gap.
double getLength() const
return the length of the edge
Definition: MSEdge.h:569
double z() const
Returns the z-position.
Definition: Position.h:72
RGBColor color
The vehicle&#39;s color, TraCI may change this.
virtual double getEdgePos() const
Return the position on the edge.
std::map< std::string, MSTransportable * >::const_iterator constVehIt
Definition of the internal transportables map iterator.
static void setLength(const std::string &personID, double length)
static double getLanePosition(const std::string &personID)
A lane area vehicles can halt at.
virtual double getWaitingSeconds() const
the time this transportable spent waiting in seconds
void setSpeed(double speed)
sets the walking speed (ignored in other stages)
const SUMOVehicleParameter & getParameter() const
double x() const
Returns the x-position.
Definition: Position.h:62
The position is given.
static void setHeight(const std::string &personID, double height)
SUMOVehicle * getVehicle() const
The vehicle associated with this transportable.
unsigned char g
Definition: TraCIDefs.h:78
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
int getNumRemainingStages() const
Return the number of remaining stages (including the current)
MSPedestrianRouterDijkstra & getPedestrianRouter(const MSEdgeVector &prohibited=MSEdgeVector()) const
Definition: MSNet.cpp:925
static int getStage(const std::string &personID, int nextStageIndex=0)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
static std::vector< std::string > getIDList()
const std::string & getID() const
returns the id of the transportable
unsigned char r
Definition: TraCIDefs.h:78
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:744
static MSTransportable * getPerson(const std::string &id)
void setLength(const double &length)
Set a new value for this type&#39;s length.
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
static int getIDCount()
static std::string getParameter(const std::string &routeID, const std::string &param)
double compute(const E *from, const E *to, double departPos, double arrivalPos, double speed, SUMOTime msTime, const N *onlyNode, std::vector< const E * > &into, bool allEdges=false)
Builds the route between the given edges using the minimum effort at the given time The definition of...
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
virtual MSTransportable * buildPerson(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportable::MSTransportablePlan *plan, std::mt19937 *rng) const
Builds a new person.
unsigned char a
Definition: TraCIDefs.h:78
Tag for the last element in the enum for safe int casting.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
The car-following model and parameter.
Definition: MSVehicleType.h:72
static std::string getNextEdge(const std::string &personID)
virtual double getSpeed() const
the current speed of the transportable
static double getWaitingTime(const std::string &personID)
int size() const
Returns the number of known transportables.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:82
void removeStage(int next)
removes the nth next stage
static void setWidth(const std::string &personID, double width)
double z
Definition: TraCIDefs.h:71
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:860
static std::string getRoadID(const std::string &personID)
static void appendWaitingStage(const std::string &personID, double duration, const std::string &description="waiting", const std::string &stopID="")
MSVehicleType & getSingularType()
Replaces the current vehicle type with a new one used by this vehicle only.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
static double naviDegree(const double angle)
Definition: GeomHelper.cpp:186
const MSVehicleType & getVehicleType() const
static std::vector< std::string > getEdges(const std::string &personID, int nextStageIndex=0)
static std::string getSingularVType(const std::string &personID)
static void appendDrivingStage(const std::string &personID, const std::string &toEdge, const std::string &lines, const std::string &stopID="")
static std::string getTypeID(const std::string &personID)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
const MSEdge * getEdge() const
Returns the current edge.
static void setParameter(const std::string &personID, const std::string &key, const std::string &value)
Representation of a vehicle.
Definition: SUMOVehicle.h:66
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
A 3D-position.
Definition: TraCIDefs.h:70
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:765
StageType getCurrentStageType() const
the current stage type of the transportable
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:89
void setHeight(const double &height)
Set a new value for this type&#39;s height.
static std::string getVehicle(const std::string &personID)
double y() const
Returns the y-position.
Definition: Position.h:67
SUMOTime depart
The vehicle&#39;s departure time.
static void setSpeed(const std::string &personID, double speed)
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static double getAngle(const std::string &personID)
static void rerouteTraveltime(const std::string &personID)
static void setMinGap(const std::string &personID, double minGap)
static int getRemainingStages(const std::string &personID)
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=0)
Returns the named vehicle type or a sample from the named distribution.
constVehIt loadedEnd() const
Returns the end of the internal transportables map.
void appendStage(Stage *stage, int next=-1)
Appends the given stage to the current plan.
static TraCIColor getColor(const std::string &personID)
double departPos
(optional) The position the vehicle shall depart from
Structure representing possible vehicle parameter.
static double getSpeed(const std::string &personID)
static void setColor(const std::string &personID, const TraCIColor &c)
static void add(const std::string &personID, const std::string &edgeID, double pos, double depart=DEPARTFLAG_NOW, const std::string typeID="DEFAULT_PEDTYPE")
A color.
Definition: TraCIDefs.h:77
StageType getStageType(int next) const
the stage type for the nth next stage
bool add(MSTransportable *transportable)
Adds a single transportable, returns false if an id clash occured.
void setWidth(const double &width)
Set a new value for this type&#39;s width.
ConstMSEdgeVector getEdges(int next) const
Return the edges of the nth next stage.
const std::string & getID() const
Returns the name of the vehicle type.
void replaceVehicleType(MSVehicleType *type)
Replaces the current vehicle type by the one given.
virtual Position getPosition() const
Return the Network coordinate of the transportable.
static void removeStage(const std::string &personID, int nextStageIndex)
double getArrivalPos() const
returns the final arrival pos
static void setType(const std::string &personID, const std::string &typeID)
unsigned char b
Definition: TraCIDefs.h:78
void setColor(const RGBColor &color)
Set a new value for this type&#39;s color.
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:75
int getNumStages() const
Return the total number stages in this persons plan.
constVehIt loadedBegin() const
Returns the begin of the internal transportables map.
long long int SUMOTime
Definition: TraCIDefs.h:51
#define NUMERICAL_EPS
Definition: config.h:151
double y
Definition: TraCIDefs.h:71
static void appendWalkingStage(const std::string &personID, const std::vector< std::string > &edgeIDs, double arrivalPos, double duration=-1, double speed=-1, const std::string &stopID="")
static MSVehicleType * getVType(std::string id)
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:68
const MSEdge * getArrivalEdge() const
returns the final arrival edge
static void parseEdgesList(const std::string &desc, ConstMSEdgeVector &into, const std::string &rid)
Parses the given string assuming it contains a list of edge ids divided by spaces.
Definition: MSEdge.cpp:801
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
DepartDefinition
Possible ways to depart.
std::string id
The vehicle&#39;s id.