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 /****************************************************************************/
7 // C++ TraCI client API implementation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef TraCIAPI_h
21 #define TraCIAPI_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <vector>
34 #include <string>
35 #include <foreign/tcpip/socket.h>
36 #include <utils/common/SUMOTime.h>
37 
38 
39 // ===========================================================================
40 // global definitions
41 // ===========================================================================
42 #define DEFAULT_VIEW "View #0"
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
52 class TraCIAPI {
53 public:
56 
60  struct TraCIPosition {
61  double x, y, z;
62  };
63 
67  struct TraCIColor {
68  int r, g, b, a;
69  };
70 
74  typedef std::vector<TraCIPosition> TraCIPositionVector;
75 
79  struct TraCIBoundary {
80  double xMin, yMin, zMin;
81  double xMax, yMax, zMax;
82  };
83 
84 
85 
86  class TraCIPhase {
87  public:
88  TraCIPhase(const SUMOTime _duration, const SUMOTime _duration1, const SUMOTime _duration2, const std::string& _phase)
89  : duration(_duration), duration1(_duration1), duration2(_duration2), phase(_phase) {}
91 
93  std::string phase;
94  };
95 
96 
97  class TraCILogic {
98  public:
99  TraCILogic(const std::string& _subID, int _type, const std::map<std::string, SUMOReal>& _subParameter, unsigned int _currentPhaseIndex, const std::vector<TraCIPhase>& _phases)
100  : subID(_subID), type(_type), subParameter(_subParameter), currentPhaseIndex(_currentPhaseIndex), phases(_phases) {}
102 
103  std::string subID;
104  int type;
105  std::map<std::string, SUMOReal> subParameter;
106  unsigned int currentPhaseIndex;
107  std::vector<TraCIPhase> phases;
108  };
109 
110  class TraCILink {
111  public:
112  TraCILink(const std::string& _from, const std::string& _via, const std::string& _to)
113  : from(_from), via(_via), to(_to) {}
115 
116  std::string from;
117  std::string via;
118  std::string to;
119  };
120 
122 
123 
124 
127  TraCIAPI();
128 
129 
131  ~TraCIAPI();
132 
133 
136 
142  void connect(const std::string& host, int port);
143 
144 
146  void close();
148 
149 
150 
153 
154  SUMOTime getSUMOTime(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
155  int getUnsignedByte(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
156  int getByte(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
157  int getInt(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
158  SUMOReal getFloat(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
159  SUMOReal getDouble(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
160  TraCIBoundary getBoundingBox(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
161  TraCIPositionVector getPolygon(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
162  TraCIPosition getPosition(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
163  std::string getString(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
164  std::vector<std::string> getStringVector(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
165  TraCIColor getColor(int cmd, int var, const std::string& id, tcpip::Storage* add = 0);
167 
168 
169 
176  public:
180  TraCIScopeWrapper(TraCIAPI& parent) : myParent(parent) {}
181 
183  virtual ~TraCIScopeWrapper() {}
184 
185 
186  protected:
189 
190 
191  private:
194 
197 
198  };
199 
200 
201 
202 
203 
207  class EdgeScope : public TraCIScopeWrapper {
208  public:
209  EdgeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
210  virtual ~EdgeScope() {}
211 
212  std::vector<std::string> getIDList() const;
213  unsigned int getIDCount() const;
214  SUMOReal getAdaptedTraveltime(const std::string& edgeID, SUMOTime time) const;
215  SUMOReal getEffort(const std::string& edgeID, SUMOTime time) const;
216  SUMOReal getCO2Emission(const std::string& edgeID) const;
217  SUMOReal getCOEmission(const std::string& edgeID) const;
218  SUMOReal getHCEmission(const std::string& edgeID) const;
219  SUMOReal getPMxEmission(const std::string& edgeID) const;
220  SUMOReal getNOxEmission(const std::string& edgeID) const;
221  SUMOReal getFuelConsumption(const std::string& edgeID) const;
222  SUMOReal getNoiseEmission(const std::string& edgeID) const;
223  SUMOReal getLastStepMeanSpeed(const std::string& edgeID) const;
224  SUMOReal getLastStepOccupancy(const std::string& edgeID) const;
225  SUMOReal getLastStepLength(const std::string& edgeID) const;
226  SUMOReal getTraveltime(const std::string& edgeID) const;
227  unsigned int getLastStepVehicleNumber(const std::string& edgeID) const;
228  SUMOReal getLastStepHaltingNumber(const std::string& edgeID) const;
229  std::vector<std::string> getLastStepVehicleIDs(const std::string& edgeID) const;
230 
231  void adaptTraveltime(const std::string& edgeID, SUMOReal time) const;
232  void setEffort(const std::string& edgeID, SUMOReal effort) const;
233  void setMaxSpeed(const std::string& edgeID, SUMOReal speed) const;
234 
235  private:
237  EdgeScope(const EdgeScope& src);
238 
240  EdgeScope& operator=(const EdgeScope& src);
241 
242  };
243 
244 
245 
246 
247 
251  class GUIScope : public TraCIScopeWrapper {
252  public:
253  GUIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
254  virtual ~GUIScope() {}
255 
256  std::vector<std::string> getIDList() const;
257  SUMOReal getZoom(const std::string& viewID = DEFAULT_VIEW) const;
258  TraCIPosition getOffset(const std::string& viewID = DEFAULT_VIEW) const;
259  std::string getSchema(const std::string& viewID = DEFAULT_VIEW) const;
260  TraCIBoundary getBoundary(const std::string& viewID = DEFAULT_VIEW) const;
261  void setZoom(const std::string& viewID, SUMOReal zoom) const;
262  void setOffset(const std::string& viewID, SUMOReal x, SUMOReal y) const;
263  void setSchema(const std::string& viewID, const std::string& schemeName) const;
264  void setBoundary(const std::string& viewID, SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax) const;
265  void screenshot(const std::string& viewID, const std::string& filename) const;
266  void trackVehicle(const std::string& viewID, const std::string& vehID) const;
267 
268  private:
270  GUIScope(const GUIScope& src);
271 
273  GUIScope& operator=(const GUIScope& src);
274 
275  };
276 
277 
278 
279 
280 
285  public:
287  virtual ~InductionLoopScope() {}
288 
289  std::vector<std::string> getIDList() const;
290  SUMOReal getPosition(const std::string& loopID) const;
291  std::string getLaneID(const std::string& loopID) const;
292  unsigned int getLastStepVehicleNumber(const std::string& loopID) const;
293  SUMOReal getLastStepMeanSpeed(const std::string& loopID) const;
294  std::vector<std::string> getLastStepVehicleIDs(const std::string& loopID) const;
295  SUMOReal getLastStepOccupancy(const std::string& loopID) const;
296  SUMOReal getLastStepMeanLength(const std::string& loopID) const;
297  SUMOReal getTimeSinceDetection(const std::string& loopID) const;
298  unsigned int getVehicleData(const std::string& loopID) const;
299 
300  private:
303 
306 
307  };
308 
309 
310 
311 
312 
317  public:
319  virtual ~JunctionScope() {}
320 
321  std::vector<std::string> getIDList() const;
322  TraCIPosition getPosition(const std::string& junctionID) const;
323 
324  private:
326  JunctionScope(const JunctionScope& src);
327 
330 
331  };
332 
333 
334 
335 
336 
340  class LaneScope : public TraCIScopeWrapper {
341  public:
342  LaneScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
343  virtual ~LaneScope() {}
344 
345  std::vector<std::string> getIDList() const;
346  SUMOReal getLength(const std::string& laneID) const;
347  SUMOReal getMaxSpeed(const std::string& laneID) const;
348  SUMOReal getWidth(const std::string& laneID) const;
349  std::vector<std::string> getAllowed(const std::string& laneID) const;
350  std::vector<std::string> getDisallowed(const std::string& laneID) const;
351  unsigned int getLinkNumber(const std::string& laneID) const;
352  TraCIPositionVector getShape(const std::string& laneID) const;
353  std::string getEdgeID(const std::string& laneID) const;
354  SUMOReal getCO2Emission(const std::string& laneID) const;
355  SUMOReal getCOEmission(const std::string& laneID) const;
356  SUMOReal getHCEmission(const std::string& laneID) const;
357  SUMOReal getPMxEmission(const std::string& laneID) const;
358  SUMOReal getNOxEmission(const std::string& laneID) const;
359  SUMOReal getFuelConsumption(const std::string& laneID) const;
360  SUMOReal getNoiseEmission(const std::string& laneID) const;
361  SUMOReal getLastStepMeanSpeed(const std::string& laneID) const;
362  SUMOReal getLastStepOccupancy(const std::string& laneID) const;
363  SUMOReal getLastStepLength(const std::string& laneID) const;
364  SUMOReal getTraveltime(const std::string& laneID) const;
365  unsigned int getLastStepVehicleNumber(const std::string& laneID) const;
366  unsigned int getLastStepHaltingNumber(const std::string& laneID) const;
367  std::vector<std::string> getLastStepVehicleIDs(const std::string& laneID) const;
368 
369  void setAllowed(const std::string& laneID, const std::vector<std::string>& allowedClasses) const;
370  void setDisallowed(const std::string& laneID, const std::vector<std::string>& disallowedClasses) const;
371  void setMaxSpeed(const std::string& laneID, SUMOReal speed) const;
372  void setLength(const std::string& laneID, SUMOReal length) const;
373 
374  private:
376  LaneScope(const LaneScope& src);
377 
379  LaneScope& operator=(const LaneScope& src);
380 
381  };
382 
383 
387  class AreaScope : public TraCIScopeWrapper {
388  public:
389  AreaScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
390  virtual ~AreaScope() {}
391 
392  std::vector<std::string> getIDList() const;
393  int getJamLengthVehicle(const std::string& laneID) const;
394  SUMOReal getJamLengthMeters(const std::string& laneID) const;
395 
396  private:
398  AreaScope(const AreaScope& src);
399 
401  AreaScope& operator=(const AreaScope& src);
402 
403  };
404 
405 
409  class MeMeScope : public TraCIScopeWrapper {
410  public:
411  MeMeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
412  virtual ~MeMeScope() {}
413 
414  std::vector<std::string> getIDList() const;
415  unsigned int getLastStepVehicleNumber(const std::string& detID) const;
416  SUMOReal getLastStepMeanSpeed(const std::string& detID) const;
417  std::vector<std::string> getLastStepVehicleIDs(const std::string& detID) const;
418  unsigned int getLastStepHaltingNumber(const std::string& detID) const;
419 
420  private:
422  MeMeScope(const MeMeScope& src);
423 
425  MeMeScope& operator=(const MeMeScope& src);
426 
427  };
428 
429 
430 
431 
432 
436  class POIScope : public TraCIScopeWrapper {
437  public:
438  POIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
439  virtual ~POIScope() {}
440 
441  std::vector<std::string> getIDList() const;
442  std::string getType(const std::string& poiID) const;
443  TraCIPosition getPosition(const std::string& poiID) const;
444  TraCIColor getColor(const std::string& poiID) const;
445 
446  void setType(const std::string& poiID, const std::string& setType) const;
447  void setPosition(const std::string& poiID, SUMOReal x, SUMOReal y) const;
448  void setColor(const std::string& poiID, const TraCIColor& c) const;
449  void add(const std::string& poiID, SUMOReal x, SUMOReal y, const TraCIColor& c, const std::string& type, int layer) const;
450  void remove(const std::string& poiID, int layer = 0) const;
451 
452  private:
454  POIScope(const POIScope& src);
455 
457  POIScope& operator=(const POIScope& src);
458 
459  };
460 
461 
462 
463 
464 
469  public:
471  virtual ~PolygonScope() {}
472 
473  std::vector<std::string> getIDList() const;
474  std::string getType(const std::string& polygonID) const;
475  TraCIPositionVector getShape(const std::string& polygonID) const;
476  TraCIColor getColor(const std::string& polygonID) const;
477  void setType(const std::string& polygonID, const std::string& setType) const;
478  void setShape(const std::string& polygonID, const TraCIPositionVector& shape) const;
479  void setColor(const std::string& polygonID, const TraCIColor& c) const;
480  void add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& c, bool fill, const std::string& type, int layer) const;
481  void remove(const std::string& polygonID, int layer = 0) const;
482 
483  private:
485  PolygonScope(const PolygonScope& src);
486 
488  PolygonScope& operator=(const PolygonScope& src);
489 
490  };
491 
492 
493 
494 
495 
499  class RouteScope : public TraCIScopeWrapper {
500  public:
501  RouteScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
502  virtual ~RouteScope() {}
503 
504  std::vector<std::string> getIDList() const;
505  std::vector<std::string> getEdges(const std::string& routeID) const;
506 
507  void add(const std::string& routeID, const std::vector<std::string>& edges) const;
508 
509  private:
511  RouteScope(const RouteScope& src);
512 
514  RouteScope& operator=(const RouteScope& src);
515 
516  };
517 
518 
519 
520 
521 
526  public:
528  virtual ~SimulationScope() {}
529 
530  SUMOTime getCurrentTime() const;
531  unsigned int getLoadedNumber() const;
532  std::vector<std::string> getLoadedIDList() const;
533  unsigned int getDepartedNumber() const;
534  std::vector<std::string> getDepartedIDList() const;
535  unsigned int getArrivedNumber() const;
536  std::vector<std::string> getArrivedIDList() const;
537  unsigned int getStartingTeleportNumber() const;
538  std::vector<std::string> getStartingTeleportIDList() const;
539  unsigned int getEndingTeleportNumber() const;
540  std::vector<std::string> getEndingTeleportIDList() const;
541  SUMOTime getDeltaT() const;
543  unsigned int getMinExpectedNumber() const;
544 
545  private:
547  SimulationScope(const SimulationScope& src);
548 
551 
552  };
553 
554 
555 
556 
557 
562  public:
564  virtual ~TrafficLightScope() {}
565 
566  std::vector<std::string> getIDList() const;
567  std::string getRedYellowGreenState(const std::string& tlsID) const;
568  std::vector<TraCIAPI::TraCILogic> getCompleteRedYellowGreenDefinition(const std::string& tlsID) const;
569  std::vector<std::string> getControlledLanes(const std::string& tlsID) const;
570  std::vector<TraCIAPI::TraCILink> getControlledLinks(const std::string& tlsID) const;
571  std::string getProgram(const std::string& tlsID) const;
572  unsigned int getPhase(const std::string& tlsID) const;
573  unsigned int getNextSwitch(const std::string& tlsID) const;
574 
575  void setRedYellowGreenState(const std::string& tlsID, const std::string& state) const;
576  void setPhase(const std::string& tlsID, unsigned int index) const;
577  void setProgram(const std::string& tlsID, const std::string& programID) const;
578  void setPhaseDuration(const std::string& tlsID, unsigned int phaseDuration) const;
579  void setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCIAPI::TraCILogic& logic) const;
580 
581  private:
584 
587 
588  };
589 
590 
591 
592 
593 
598  public:
600  virtual ~VehicleTypeScope() {}
601 
602  std::vector<std::string> getIDList() const;
603  SUMOReal getLength(const std::string& typeID) const;
604  SUMOReal getMaxSpeed(const std::string& typeID) const;
605  SUMOReal getSpeedFactor(const std::string& typeID) const;
606  SUMOReal getSpeedDeviation(const std::string& typeID) const;
607  SUMOReal getAccel(const std::string& typeID) const;
608  SUMOReal getDecel(const std::string& typeID) const;
609  SUMOReal getImperfection(const std::string& typeID) const;
610  SUMOReal getTau(const std::string& typeID) const;
611  std::string getVehicleClass(const std::string& typeID) const;
612  std::string getEmissionClass(const std::string& typeID) const;
613  std::string getShapeClass(const std::string& typeID) const;
614  SUMOReal getMinGap(const std::string& typeID) const;
615  SUMOReal getWidth(const std::string& typeID) const;
616  TraCIColor getColor(const std::string& typeID) const;
617 
618  void setLength(const std::string& typeID, SUMOReal length) const;
619  void setMaxSpeed(const std::string& typeID, SUMOReal speed) const;
620  void setVehicleClass(const std::string& typeID, const std::string& clazz) const;
621  void setSpeedFactor(const std::string& typeID, SUMOReal factor) const;
622  void setSpeedDeviation(const std::string& typeID, SUMOReal deviation) const;
623  void setEmissionClass(const std::string& typeID, const std::string& clazz) const;
624  void setWidth(const std::string& typeID, SUMOReal width) const;
625  void setMinGap(const std::string& typeID, SUMOReal minGap) const;
626  void setShapeClass(const std::string& typeID, const std::string& clazz) const;
627  void setAccel(const std::string& typeID, SUMOReal accel) const;
628  void setDecel(const std::string& typeID, SUMOReal decel) const;
629  void setImperfection(const std::string& typeID, SUMOReal imperfection) const;
630  void setTau(const std::string& typeID, SUMOReal tau) const;
631  void setColor(const std::string& typeID, const TraCIColor& c) const;
632 
633  private:
636 
639 
640  };
641 
642 public:
667 
668 
669 protected:
672 
675  void send_commandSimulationStep(SUMOTime time) const;
676 
677 
680  void send_commandClose() const;
681 
682 
689  void send_commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* add = 0) const;
690 
691 
698  void send_commandSetValue(int domID, int varID, const std::string& objID, tcpip::Storage& content) const;
699 
700 
708  void send_commandSubscribeObjectVariable(int domID, const std::string& objID, int beginTime, int endTime, const std::vector<int>& vars) const;
709 
710 
720  void send_commandSubscribeObjectContext(int domID, const std::string& objID, int beginTime, int endTime,
721  int domain, SUMOReal range, const std::vector<int>& vars) const;
723 
724 
725 
728 
735  void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0) const;
736 
737  void check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
738 
739  void processGET(tcpip::Storage& inMsg, int command, int expectedType, bool ignoreCommandId = false) const;
741 
742 
743 protected:
746 
747 
748 };
749 
750 
751 #endif
752 
753 /****************************************************************************/
754