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.sourceforge.net/
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(SUMOTime _duration, const std::string& _phase, SUMOTime _duration1, SUMOTime _duration2)
89  : duration(_duration), phase(_phase), duration1(_duration1), duration2(_duration2) {}
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 
384 
385 
386 
390  class MeMeScope : public TraCIScopeWrapper {
391  public:
392  MeMeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
393  virtual ~MeMeScope() {}
394 
395  std::vector<std::string> getIDList() const;
396  unsigned int getLastStepVehicleNumber(const std::string& detID) const;
397  SUMOReal getLastStepMeanSpeed(const std::string& detID) const;
398  std::vector<std::string> getLastStepVehicleIDs(const std::string& detID) const;
399  unsigned int getLastStepHaltingNumber(const std::string& detID) const;
400 
401  private:
403  MeMeScope(const MeMeScope& src);
404 
406  MeMeScope& operator=(const MeMeScope& src);
407 
408  };
409 
410 
411 
412 
413 
417  class POIScope : public TraCIScopeWrapper {
418  public:
419  POIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
420  virtual ~POIScope() {}
421 
422  std::vector<std::string> getIDList() const;
423  std::string getType(const std::string& poiID) const;
424  TraCIPosition getPosition(const std::string& poiID) const;
425  TraCIColor getColor(const std::string& poiID) const;
426 
427  void setType(const std::string& poiID, const std::string& setType) const;
428  void setPosition(const std::string& poiID, SUMOReal x, SUMOReal y) const;
429  void setColor(const std::string& poiID, const TraCIColor& c) const;
430  void add(const std::string& poiID, SUMOReal x, SUMOReal y, const TraCIColor& c, const std::string& type, int layer) const;
431  void remove(const std::string& poiID, int layer = 0) const;
432 
433  private:
435  POIScope(const POIScope& src);
436 
438  POIScope& operator=(const POIScope& src);
439 
440  };
441 
442 
443 
444 
445 
450  public:
452  virtual ~PolygonScope() {}
453 
454  std::vector<std::string> getIDList() const;
455  std::string getType(const std::string& polygonID) const;
456  TraCIPositionVector getShape(const std::string& polygonID) const;
457  TraCIColor getColor(const std::string& polygonID) const;
458  void setType(const std::string& polygonID, const std::string& setType) const;
459  void setShape(const std::string& polygonID, const TraCIPositionVector& shape) const;
460  void setColor(const std::string& polygonID, const TraCIColor& c) const;
461  void add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& c, bool fill, const std::string& type, int layer) const;
462  void remove(const std::string& polygonID, int layer = 0) const;
463 
464  private:
466  PolygonScope(const PolygonScope& src);
467 
469  PolygonScope& operator=(const PolygonScope& src);
470 
471  };
472 
473 
474 
475 
476 
480  class RouteScope : public TraCIScopeWrapper {
481  public:
482  RouteScope(TraCIAPI& parent) : TraCIScopeWrapper(parent) {}
483  virtual ~RouteScope() {}
484 
485  std::vector<std::string> getIDList() const;
486  std::vector<std::string> getEdges(const std::string& routeID) const;
487 
488  void add(const std::string& routeID, const std::vector<std::string>& edges) const;
489 
490  private:
492  RouteScope(const RouteScope& src);
493 
495  RouteScope& operator=(const RouteScope& src);
496 
497  };
498 
499 
500 
501 
502 
507  public:
509  virtual ~SimulationScope() {}
510 
511  SUMOTime getCurrentTime() const;
512  unsigned int getLoadedNumber() const;
513  std::vector<std::string> getLoadedIDList() const;
514  unsigned int getDepartedNumber() const;
515  std::vector<std::string> getDepartedIDList() const;
516  unsigned int getArrivedNumber() const;
517  std::vector<std::string> getArrivedIDList() const;
518  unsigned int getStartingTeleportNumber() const;
519  std::vector<std::string> getStartingTeleportIDList() const;
520  unsigned int getEndingTeleportNumber() const;
521  std::vector<std::string> getEndingTeleportIDList() const;
522  SUMOTime getDeltaT() const;
524  unsigned int getMinExpectedNumber() const;
525 
526  private:
528  SimulationScope(const SimulationScope& src);
529 
532 
533  };
534 
535 
536 
537 
538 
543  public:
545  virtual ~TrafficLightScope() {}
546 
547  std::vector<std::string> getIDList() const;
548  std::string getRedYellowGreenState(const std::string& tlsID) const;
549  std::vector<TraCIAPI::TraCILogic> getCompleteRedYellowGreenDefinition(const std::string& tlsID) const;
550  std::vector<std::string> getControlledLanes(const std::string& tlsID) const;
551  std::vector<TraCIAPI::TraCILink> getControlledLinks(const std::string& tlsID) const;
552  std::string getProgram(const std::string& tlsID) const;
553  unsigned int getPhase(const std::string& tlsID) const;
554  unsigned int getNextSwitch(const std::string& tlsID) const;
555 
556  void setRedYellowGreenState(const std::string& tlsID, const std::string& state) const;
557  void setPhase(const std::string& tlsID, unsigned int index) const;
558  void setProgram(const std::string& tlsID, const std::string& programID) const;
559  void setPhaseDuration(const std::string& tlsID, unsigned int phaseDuration) const;
560  void setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCIAPI::TraCILogic& logic) const;
561 
562  private:
565 
568 
569  };
570 
571 
572 
573 
574 
579  public:
581  virtual ~VehicleTypeScope() {}
582 
583  std::vector<std::string> getIDList() const;
584  SUMOReal getLength(const std::string& typeID) const;
585  SUMOReal getMaxSpeed(const std::string& typeID) const;
586  SUMOReal getSpeedFactor(const std::string& typeID) const;
587  SUMOReal getSpeedDeviation(const std::string& typeID) const;
588  SUMOReal getAccel(const std::string& typeID) const;
589  SUMOReal getDecel(const std::string& typeID) const;
590  SUMOReal getImperfection(const std::string& typeID) const;
591  SUMOReal getTau(const std::string& typeID) const;
592  std::string getVehicleClass(const std::string& typeID) const;
593  std::string getEmissionClass(const std::string& typeID) const;
594  std::string getShapeClass(const std::string& typeID) const;
595  SUMOReal getMinGap(const std::string& typeID) const;
596  SUMOReal getWidth(const std::string& typeID) const;
597  TraCIColor getColor(const std::string& typeID) const;
598 
599  void setLength(const std::string& typeID, SUMOReal length) const;
600  void setMaxSpeed(const std::string& typeID, SUMOReal speed) const;
601  void setVehicleClass(const std::string& typeID, const std::string& clazz) const;
602  void setSpeedFactor(const std::string& typeID, SUMOReal factor) const;
603  void setSpeedDeviation(const std::string& typeID, SUMOReal deviation) const;
604  void setEmissionClass(const std::string& typeID, const std::string& clazz) const;
605  void setWidth(const std::string& typeID, SUMOReal width) const;
606  void setMinGap(const std::string& typeID, SUMOReal minGap) const;
607  void setShapeClass(const std::string& typeID, const std::string& clazz) const;
608  void setAccel(const std::string& typeID, SUMOReal accel) const;
609  void setDecel(const std::string& typeID, SUMOReal decel) const;
610  void setImperfection(const std::string& typeID, SUMOReal imperfection) const;
611  void setTau(const std::string& typeID, SUMOReal tau) const;
612  void setColor(const std::string& typeID, const TraCIColor& c) const;
613 
614  private:
617 
620 
621  };
622 
623 public:
648 
649 
650 protected:
653 
656  void send_commandSimulationStep(SUMOTime time) const;
657 
658 
661  void send_commandClose() const;
662 
663 
670  void send_commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* add = 0) const;
671 
672 
679  void send_commandSetValue(int domID, int varID, const std::string& objID, tcpip::Storage& content) const;
680 
681 
689  void send_commandSubscribeObjectVariable(int domID, const std::string& objID, int beginTime, int endTime, const std::vector<int>& vars) const;
690 
691 
701  void send_commandSubscribeObjectContext(int domID, const std::string& objID, int beginTime, int endTime,
702  int domain, SUMOReal range, const std::vector<int>& vars) const;
704 
705 
706 
709 
716  void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0) const;
717 
718  void check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
719 
720  void processGET(tcpip::Storage& inMsg, int command, int expectedType, bool ignoreCommandId = false) const;
722 
723 
724 protected:
727 
728 
729 };
730 
731 
732 #endif
733 
734 /****************************************************************************/
735