SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIAPI.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // C++ TraCI client API implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2012-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef TraCIAPI_h
23 #define TraCIAPI_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <vector>
36 #include <string>
37 #include <foreign/tcpip/socket.h>
38 #include <utils/common/SUMOTime.h>
39 
40 
41 // ===========================================================================
42 // global definitions
43 // ===========================================================================
44 #define DEFAULT_VIEW "View #0"
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
54 class TraCIAPI {
55 public:
58 
62  struct TraCIPosition {
63  double x, y, z;
64  };
65 
69  struct TraCIColor {
70  int r, g, b, a;
71  };
72 
76  typedef std::vector<TraCIPosition> TraCIPositionVector;
77 
81  struct TraCIBoundary {
82  double xMin, yMin, zMin;
83  double xMax, yMax, zMax;
84  };
85 
86 
87 
88  class TraCIPhase {
89  public:
90  TraCIPhase(const SUMOTime _duration, const SUMOTime _duration1, const SUMOTime _duration2, const std::string& _phase)
91  : duration(_duration), duration1(_duration1), duration2(_duration2), phase(_phase) {}
93 
95  std::string phase;
96  };
97 
98 
99  class TraCILogic {
100  public:
101  TraCILogic(const std::string& _subID, int _type, const std::map<std::string, SUMOReal>& _subParameter, unsigned int _currentPhaseIndex, const std::vector<TraCIPhase>& _phases)
102  : subID(_subID), type(_type), subParameter(_subParameter), currentPhaseIndex(_currentPhaseIndex), phases(_phases) {}
104 
105  std::string subID;
106  int type;
107  std::map<std::string, SUMOReal> subParameter;
108  unsigned int currentPhaseIndex;
109  std::vector<TraCIPhase> phases;
110  };
111 
112  class TraCILink {
113  public:
114  TraCILink(const std::string& _from, const std::string& _via, const std::string& _to)
115  : from(_from), via(_via), to(_to) {}
117 
118  std::string from;
119  std::string via;
120  std::string to;
121  };
122 
124 
125 
126 
129  TraCIAPI();
130 
131 
133  ~TraCIAPI();
134 
135 
138 
144  void connect(const std::string& host, int port);
145 
146 
148  void close();
150 
151 
152 
155 
156  SUMOTime getSUMOTime(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
157  int getUnsignedByte(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
158  int getByte(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
159  int getInt(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
160  SUMOReal getFloat(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
161  SUMOReal getDouble(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
162  TraCIBoundary getBoundingBox(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
163  TraCIPositionVector getPolygon(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
164  TraCIPosition getPosition(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
165  std::string getString(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
166  std::vector<std::string> getStringVector(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
167  TraCIColor getColor(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
169 
170 
171 
178  public:
182  TraCIScopeWrapper(TraCIAPI& parent) : myParent(parent) {}
183 
185  virtual ~TraCIScopeWrapper() {}
186 
187 
188  protected:
191 
192 
193  private:
196 
199 
200  };
201 
202 
203 
204 
205 
209  class EdgeScope : public TraCIScopeWrapper {
210  public:
211  EdgeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
212  virtual ~EdgeScope() {}
213 
214  std::vector<std::string> getIDList() const;
215  unsigned int getIDCount() const;
216  SUMOReal getAdaptedTraveltime(const std::string& edgeID, SUMOTime time) const;
217  SUMOReal getEffort(const std::string& edgeID, SUMOTime time) const;
218  SUMOReal getCO2Emission(const std::string& edgeID) const;
219  SUMOReal getCOEmission(const std::string& edgeID) const;
220  SUMOReal getHCEmission(const std::string& edgeID) const;
221  SUMOReal getPMxEmission(const std::string& edgeID) const;
222  SUMOReal getNOxEmission(const std::string& edgeID) const;
223  SUMOReal getFuelConsumption(const std::string& edgeID) const;
224  SUMOReal getNoiseEmission(const std::string& edgeID) const;
225  SUMOReal getLastStepMeanSpeed(const std::string& edgeID) const;
226  SUMOReal getLastStepOccupancy(const std::string& edgeID) const;
227  SUMOReal getLastStepLength(const std::string& edgeID) const;
228  SUMOReal getTraveltime(const std::string& edgeID) const;
229  unsigned int getLastStepVehicleNumber(const std::string& edgeID) const;
230  SUMOReal getLastStepHaltingNumber(const std::string& edgeID) const;
231  std::vector<std::string> getLastStepVehicleIDs(const std::string& edgeID) const;
232 
233  void adaptTraveltime(const std::string& edgeID, SUMOReal time) const;
234  void setEffort(const std::string& edgeID, SUMOReal effort) const;
235  void setMaxSpeed(const std::string& edgeID, SUMOReal speed) const;
236 
237  private:
239  EdgeScope(const EdgeScope& src);
240 
242  EdgeScope& operator=(const EdgeScope& src);
243 
244  };
245 
246 
247 
248 
249 
253  class GUIScope : public TraCIScopeWrapper {
254  public:
255  GUIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
256  virtual ~GUIScope() {}
257 
258  std::vector<std::string> getIDList() const;
259  SUMOReal getZoom(const std::string& viewID = DEFAULT_VIEW) const;
260  TraCIPosition getOffset(const std::string& viewID = DEFAULT_VIEW) const;
261  std::string getSchema(const std::string& viewID = DEFAULT_VIEW) const;
262  TraCIBoundary getBoundary(const std::string& viewID = DEFAULT_VIEW) const;
263  void setZoom(const std::string& viewID, SUMOReal zoom) const;
264  void setOffset(const std::string& viewID, SUMOReal x, SUMOReal y) const;
265  void setSchema(const std::string& viewID, const std::string& schemeName) const;
266  void setBoundary(const std::string& viewID, SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax) const;
267  void screenshot(const std::string& viewID, const std::string& filename) const;
268  void trackVehicle(const std::string& viewID, const std::string& vehID) const;
269 
270  private:
272  GUIScope(const GUIScope& src);
273 
275  GUIScope& operator=(const GUIScope& src);
276 
277  };
278 
279 
280 
281 
282 
287  public:
289  virtual ~InductionLoopScope() {}
290 
291  std::vector<std::string> getIDList() const;
292  SUMOReal getPosition(const std::string& loopID) const;
293  std::string getLaneID(const std::string& loopID) const;
294  unsigned int getLastStepVehicleNumber(const std::string& loopID) const;
295  SUMOReal getLastStepMeanSpeed(const std::string& loopID) const;
296  std::vector<std::string> getLastStepVehicleIDs(const std::string& loopID) const;
297  SUMOReal getLastStepOccupancy(const std::string& loopID) const;
298  SUMOReal getLastStepMeanLength(const std::string& loopID) const;
299  SUMOReal getTimeSinceDetection(const std::string& loopID) const;
300  unsigned int getVehicleData(const std::string& loopID) const;
301 
302  private:
305 
308 
309  };
310 
311 
312 
313 
314 
319  public:
321  virtual ~JunctionScope() {}
322 
323  std::vector<std::string> getIDList() const;
324  TraCIPosition getPosition(const std::string& junctionID) const;
325 
326  private:
328  JunctionScope(const JunctionScope& src);
329 
332 
333  };
334 
335 
336 
337 
338 
342  class LaneScope : public TraCIScopeWrapper {
343  public:
344  LaneScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
345  virtual ~LaneScope() {}
346 
347  std::vector<std::string> getIDList() const;
348  SUMOReal getLength(const std::string& laneID) const;
349  SUMOReal getMaxSpeed(const std::string& laneID) const;
350  SUMOReal getWidth(const std::string& laneID) const;
351  std::vector<std::string> getAllowed(const std::string& laneID) const;
352  std::vector<std::string> getDisallowed(const std::string& laneID) const;
353  unsigned int getLinkNumber(const std::string& laneID) const;
354  TraCIPositionVector getShape(const std::string& laneID) const;
355  std::string getEdgeID(const std::string& laneID) const;
356  SUMOReal getCO2Emission(const std::string& laneID) const;
357  SUMOReal getCOEmission(const std::string& laneID) const;
358  SUMOReal getHCEmission(const std::string& laneID) const;
359  SUMOReal getPMxEmission(const std::string& laneID) const;
360  SUMOReal getNOxEmission(const std::string& laneID) const;
361  SUMOReal getFuelConsumption(const std::string& laneID) const;
362  SUMOReal getNoiseEmission(const std::string& laneID) const;
363  SUMOReal getLastStepMeanSpeed(const std::string& laneID) const;
364  SUMOReal getLastStepOccupancy(const std::string& laneID) const;
365  SUMOReal getLastStepLength(const std::string& laneID) const;
366  SUMOReal getTraveltime(const std::string& laneID) const;
367  unsigned int getLastStepVehicleNumber(const std::string& laneID) const;
368  unsigned int getLastStepHaltingNumber(const std::string& laneID) const;
369  std::vector<std::string> getLastStepVehicleIDs(const std::string& laneID) const;
370 
371  void setAllowed(const std::string& laneID, const std::vector<std::string>& allowedClasses) const;
372  void setDisallowed(const std::string& laneID, const std::vector<std::string>& disallowedClasses) const;
373  void setMaxSpeed(const std::string& laneID, SUMOReal speed) const;
374  void setLength(const std::string& laneID, SUMOReal length) const;
375 
376  private:
378  LaneScope(const LaneScope& src);
379 
381  LaneScope& operator=(const LaneScope& src);
382 
383  };
384 
385 
389  class AreaScope : public TraCIScopeWrapper {
390  public:
391  AreaScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
392  virtual ~AreaScope() {}
393 
394  std::vector<std::string> getIDList() const;
395  int getJamLengthVehicle(const std::string& laneID) const;
396  SUMOReal getJamLengthMeters(const std::string& laneID) const;
397 
398  private:
400  AreaScope(const AreaScope& src);
401 
403  AreaScope& operator=(const AreaScope& src);
404 
405  };
406 
407 
411  class MeMeScope : public TraCIScopeWrapper {
412  public:
413  MeMeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
414  virtual ~MeMeScope() {}
415 
416  std::vector<std::string> getIDList() const;
417  unsigned int getLastStepVehicleNumber(const std::string& detID) const;
418  SUMOReal getLastStepMeanSpeed(const std::string& detID) const;
419  std::vector<std::string> getLastStepVehicleIDs(const std::string& detID) const;
420  unsigned int getLastStepHaltingNumber(const std::string& detID) const;
421 
422  private:
424  MeMeScope(const MeMeScope& src);
425 
427  MeMeScope& operator=(const MeMeScope& src);
428 
429  };
430 
431 
432 
433 
434 
438  class POIScope : public TraCIScopeWrapper {
439  public:
440  POIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
441  virtual ~POIScope() {}
442 
443  std::vector<std::string> getIDList() const;
444  std::string getType(const std::string& poiID) const;
445  TraCIPosition getPosition(const std::string& poiID) const;
446  TraCIColor getColor(const std::string& poiID) const;
447 
448  void setType(const std::string& poiID, const std::string& setType) const;
449  void setPosition(const std::string& poiID, SUMOReal x, SUMOReal y) const;
450  void setColor(const std::string& poiID, const TraCIColor& c) const;
451  void add(const std::string& poiID, SUMOReal x, SUMOReal y, const TraCIColor& c, const std::string& type, int layer) const;
452  void remove(const std::string& poiID, int layer = 0) const;
453 
454  private:
456  POIScope(const POIScope& src);
457 
459  POIScope& operator=(const POIScope& src);
460 
461  };
462 
463 
464 
465 
466 
471  public:
473  virtual ~PolygonScope() {}
474 
475  std::vector<std::string> getIDList() const;
476  std::string getType(const std::string& polygonID) const;
477  TraCIPositionVector getShape(const std::string& polygonID) const;
478  TraCIColor getColor(const std::string& polygonID) const;
479  void setType(const std::string& polygonID, const std::string& setType) const;
480  void setShape(const std::string& polygonID, const TraCIPositionVector& shape) const;
481  void setColor(const std::string& polygonID, const TraCIColor& c) const;
482  void add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& c, bool fill, const std::string& type, int layer) const;
483  void remove(const std::string& polygonID, int layer = 0) const;
484 
485  private:
487  PolygonScope(const PolygonScope& src);
488 
490  PolygonScope& operator=(const PolygonScope& src);
491 
492  };
493 
494 
495 
496 
497 
501  class RouteScope : public TraCIScopeWrapper {
502  public:
503  RouteScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
504  virtual ~RouteScope() {}
505 
506  std::vector<std::string> getIDList() const;
507  std::vector<std::string> getEdges(const std::string& routeID) const;
508 
509  void add(const std::string& routeID, const std::vector<std::string>& edges) const;
510 
511  private:
513  RouteScope(const RouteScope& src);
514 
516  RouteScope& operator=(const RouteScope& src);
517 
518  };
519 
520 
521 
522 
523 
528  public:
530  virtual ~SimulationScope() {}
531 
532  SUMOTime getCurrentTime() const;
533  unsigned int getLoadedNumber() const;
534  std::vector<std::string> getLoadedIDList() const;
535  unsigned int getDepartedNumber() const;
536  std::vector<std::string> getDepartedIDList() const;
537  unsigned int getArrivedNumber() const;
538  std::vector<std::string> getArrivedIDList() const;
539  unsigned int getStartingTeleportNumber() const;
540  std::vector<std::string> getStartingTeleportIDList() const;
541  unsigned int getEndingTeleportNumber() const;
542  std::vector<std::string> getEndingTeleportIDList() const;
543  SUMOTime getDeltaT() const;
545  unsigned int getMinExpectedNumber() const;
546 
547  private:
549  SimulationScope(const SimulationScope& src);
550 
553 
554  };
555 
556 
557 
558 
559 
564  public:
566  virtual ~TrafficLightScope() {}
567 
568  std::vector<std::string> getIDList() const;
569  std::string getRedYellowGreenState(const std::string& tlsID) const;
570  std::vector<TraCIAPI::TraCILogic> getCompleteRedYellowGreenDefinition(const std::string& tlsID) const;
571  std::vector<std::string> getControlledLanes(const std::string& tlsID) const;
572  std::vector<TraCIAPI::TraCILink> getControlledLinks(const std::string& tlsID) const;
573  std::string getProgram(const std::string& tlsID) const;
574  unsigned int getPhase(const std::string& tlsID) const;
575  unsigned int getNextSwitch(const std::string& tlsID) const;
576 
577  void setRedYellowGreenState(const std::string& tlsID, const std::string& state) const;
578  void setPhase(const std::string& tlsID, unsigned int index) const;
579  void setProgram(const std::string& tlsID, const std::string& programID) const;
580  void setPhaseDuration(const std::string& tlsID, unsigned int phaseDuration) const;
581  void setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCIAPI::TraCILogic& logic) const;
582 
583  private:
586 
589 
590  };
591 
592 
593 
594 
595 
600  public:
602  virtual ~VehicleTypeScope() {}
603 
604  std::vector<std::string> getIDList() const;
605  SUMOReal getLength(const std::string& typeID) const;
606  SUMOReal getMaxSpeed(const std::string& typeID) const;
607  SUMOReal getSpeedFactor(const std::string& typeID) const;
608  SUMOReal getSpeedDeviation(const std::string& typeID) const;
609  SUMOReal getAccel(const std::string& typeID) const;
610  SUMOReal getDecel(const std::string& typeID) const;
611  SUMOReal getImperfection(const std::string& typeID) const;
612  SUMOReal getTau(const std::string& typeID) const;
613  std::string getVehicleClass(const std::string& typeID) const;
614  std::string getEmissionClass(const std::string& typeID) const;
615  std::string getShapeClass(const std::string& typeID) const;
616  SUMOReal getMinGap(const std::string& typeID) const;
617  SUMOReal getWidth(const std::string& typeID) const;
618  TraCIColor getColor(const std::string& typeID) const;
619 
620  void setLength(const std::string& typeID, SUMOReal length) const;
621  void setMaxSpeed(const std::string& typeID, SUMOReal speed) const;
622  void setVehicleClass(const std::string& typeID, const std::string& clazz) const;
623  void setSpeedFactor(const std::string& typeID, SUMOReal factor) const;
624  void setSpeedDeviation(const std::string& typeID, SUMOReal deviation) const;
625  void setEmissionClass(const std::string& typeID, const std::string& clazz) const;
626  void setWidth(const std::string& typeID, SUMOReal width) const;
627  void setMinGap(const std::string& typeID, SUMOReal minGap) const;
628  void setShapeClass(const std::string& typeID, const std::string& clazz) const;
629  void setAccel(const std::string& typeID, SUMOReal accel) const;
630  void setDecel(const std::string& typeID, SUMOReal decel) const;
631  void setImperfection(const std::string& typeID, SUMOReal imperfection) const;
632  void setTau(const std::string& typeID, SUMOReal tau) const;
633  void setColor(const std::string& typeID, const TraCIColor& c) const;
634 
635  private:
638 
641 
642  };
643 
644 public:
669 
670 
671 protected:
674 
677  void send_commandSimulationStep(SUMOTime time) const;
678 
679 
682  void send_commandClose() const;
683 
684 
691  void send_commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* add = 0) const;
692 
693 
700  void send_commandSetValue(int domID, int varID, const std::string& objID, tcpip::Storage& content) const;
701 
702 
710  void send_commandSubscribeObjectVariable(int domID, const std::string& objID, int beginTime, int endTime, const std::vector<int>& vars) const;
711 
712 
722  void send_commandSubscribeObjectContext(int domID, const std::string& objID, int beginTime, int endTime,
723  int domain, SUMOReal range, const std::vector<int>& vars) const;
725 
726 
727 
730 
737  void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0) const;
738 
739  void check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
740 
741  void processGET(tcpip::Storage& inMsg, int command, int expectedType, bool ignoreCommandId = false) const;
743 
744 
745 protected:
748 
749 
750 };
751 
752 
753 #endif
754 
755 /****************************************************************************/
756