Eclipse SUMO - Simulation of Urban MObility
libtraci/Simulation.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-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19// C++ TraCI client API implementation
20/****************************************************************************/
21#include <config.h>
22#include <cstdlib>
23
25#define LIBTRACI 1
26#include "Connection.h"
27#include "Domain.h"
29#include <libsumo/GUI.h>
30#include <libsumo/Simulation.h>
31
32
33namespace libtraci {
34
35typedef Domain<libsumo::CMD_GET_SIM_VARIABLE, libsumo::CMD_SET_SIM_VARIABLE> Dom;
36
37
38// ===========================================================================
39// static member definitions
40// ===========================================================================
41std::pair<int, std::string>
42Simulation::init(int port, int numRetries, const std::string& host, const std::string& label, FILE* const pipe) {
43 Connection::connect(host, port, numRetries, label, pipe);
44 switchConnection(label);
45 return getVersion();
46}
47
48
49std::pair<int, std::string>
50Simulation::start(const std::vector<std::string>& cmd, int port, int numRetries, const std::string& label, const bool verbose,
51 const std::string& /* traceFile */, bool /* traceGetters */, void* /* _stdout */) {
52 if (port == -1) {
54 }
55 std::ostringstream oss;
56 for (const std::string& s : cmd) {
57 oss << s << " ";
58 }
59 oss << "--remote-port " << port << " 2>&1";
60#ifndef WIN32
61 oss << " &";
62#endif
63 if (verbose) {
64 std::cout << "Calling " << oss.str() << std::endl;
65 }
66#ifdef WIN32
67 FILE* pipe = _popen(oss.str().c_str(), "r");
68#else
69 FILE* pipe = popen(oss.str().c_str(), "r");
70#endif
71 return init(port, numRetries, "localhost", label, pipe);
72}
73
74
75bool
76Simulation::isLibsumo() {
77 return false;
78}
79
80
81bool
82Simulation::hasGUI() {
83 try {
84 GUI::getIDList();
85 return true;
86 } catch (libsumo::TraCIException&) {
87 return false;
88 }
89}
90
91
92void
93Simulation::switchConnection(const std::string& label) {
95}
96
97
98const std::string&
99Simulation::getLabel() {
101}
102
103
104void
105Simulation::setOrder(int order) {
107}
108
109
110void
111Simulation::load(const std::vector<std::string>& args) {
112 tcpip::Storage content;
114 content.writeStringList(args);
116}
117
118
119bool
120Simulation::isLoaded() {
121 return Connection::isActive();
122}
123
124
125void
126Simulation::step(const double time) {
128}
129
130
131void
132Simulation::close(const std::string& /* reason */) {
134}
135
136
137std::pair<int, std::string>
138Simulation::getVersion() {
140 inMsg.readUnsignedByte(); // msg length
141 inMsg.readUnsignedByte(); // libsumo::CMD_GETVERSION again, see #7284
142 const int traciVersion = inMsg.readInt(); // to fix evaluation order
143 return std::make_pair(traciVersion, inMsg.readString());
144}
145
146
147std::string
148Simulation::getOption(const std::string& option) {
149 return Dom::getString(libsumo::VAR_OPTION, option);
150}
151
152
153int
154Simulation::getCurrentTime() {
156}
157
158
159double
160Simulation::getTime() {
162}
163
164
165double
166Simulation::getEndTime() {
168}
169
170
171int
172Simulation::getLoadedNumber() {
174}
175
176
177std::vector<std::string>
178Simulation::getLoadedIDList() {
180}
181
182
183int
184Simulation::getDepartedNumber() {
186}
187
188
189std::vector<std::string>
190Simulation::getDepartedIDList() {
192}
193
194
195int
196Simulation::getArrivedNumber() {
198}
199
200
201std::vector<std::string>
202Simulation::getArrivedIDList() {
204}
205
206
207int
208Simulation::getParkingStartingVehiclesNumber() {
210}
211
212
213std::vector<std::string>
214Simulation::getParkingStartingVehiclesIDList() {
216}
217
218
219int
220Simulation::getParkingEndingVehiclesNumber() {
222}
223
224
225std::vector<std::string>
226Simulation::getParkingEndingVehiclesIDList() {
228}
229
230
231int
232Simulation::getStopStartingVehiclesNumber() {
234}
235
236
237std::vector<std::string>
238Simulation::getStopStartingVehiclesIDList() {
240}
241
242
243int
244Simulation::getStopEndingVehiclesNumber() {
246}
247
248
249std::vector<std::string>
250Simulation::getStopEndingVehiclesIDList() {
252}
253
254
255int
256Simulation::getCollidingVehiclesNumber() {
258}
259
260
261std::vector<std::string>
262Simulation::getCollidingVehiclesIDList() {
264}
265
266
267int
268Simulation::getEmergencyStoppingVehiclesNumber() {
270}
271
272
273std::vector<std::string>
274Simulation::getEmergencyStoppingVehiclesIDList() {
276}
277
278
279int
280Simulation::getStartingTeleportNumber() {
282}
283
284
285std::vector<std::string>
286Simulation::getStartingTeleportIDList() {
288}
289
290
291int
292Simulation::getEndingTeleportNumber() {
294}
295
296
297std::vector<std::string>
298Simulation::getEndingTeleportIDList() {
300}
301
302
303int
304Simulation::getDepartedPersonNumber() {
306}
307
308
309std::vector<std::string>
310Simulation::getDepartedPersonIDList() {
312}
313
314
315int
316Simulation::getArrivedPersonNumber() {
318}
319
320
321std::vector<std::string>
322Simulation::getArrivedPersonIDList() {
324}
325
326
327std::vector<std::string>
328Simulation::getBusStopIDList() {
330}
331
332int
333Simulation::getBusStopWaiting(const std::string& stopID) {
335}
336
337std::vector<std::string>
338Simulation::getBusStopWaitingIDList(const std::string& stopID) {
340}
341
342
343std::vector<std::string>
344Simulation::getPendingVehicles() {
346}
347
348std::vector<libsumo::TraCICollision>
349Simulation::getCollisions() {
350 std::vector<libsumo::TraCICollision> result;
351 return result;
352}
353
354
355double
356Simulation::getScale() {
358}
359
360
361double
362Simulation::getDeltaT() {
364}
365
366
368Simulation::getNetBoundary() {
370}
371
372
373int
374Simulation::getMinExpectedNumber() {
376}
377
378
380Simulation::convert2D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
381 tcpip::Storage content;
382 StoHelp::writeCompound(content, 2);
384 content.writeString(edgeID);
385 content.writeDouble(pos);
386 content.writeUnsignedByte(laneIndex);
389 return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, toGeo);
390}
391
392
394Simulation::convert3D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
395 tcpip::Storage content;
396 StoHelp::writeCompound(content, 2);
398 content.writeString(edgeID);
399 content.writeDouble(pos);
400 content.writeUnsignedByte(laneIndex);
403 return Dom::getPos3D(libsumo::POSITION_CONVERSION, "", &content, toGeo);
404}
405
406
408Simulation::convertRoad(double x, double y, bool isGeo, const std::string& vClass) {
409 tcpip::Storage content;
410 StoHelp::writeCompound(content, 3);
412 content.writeDouble(x);
413 content.writeDouble(y);
416 StoHelp::writeTypedString(content, vClass);
419 result.edgeID = ret.readString();
420 result.pos = ret.readDouble();
421 result.laneIndex = ret.readByte();
422 return result;
423}
424
425
427Simulation::convertGeo(double x, double y, bool fromGeo) {
428 tcpip::Storage content;
429 StoHelp::writeCompound(content, 2);
431 content.writeDouble(x);
432 content.writeDouble(y);
435 return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, !fromGeo);
436}
437
438
439double
440Simulation::getDistance2D(double x1, double y1, double x2, double y2, bool isGeo, bool isDriving) {
441 tcpip::Storage content;
442 StoHelp::writeCompound(content, 3);
444 content.writeDouble(x1);
445 content.writeDouble(y1);
447 content.writeDouble(x2);
448 content.writeDouble(y2);
450 return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
451}
452
453
454double
455Simulation::getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving) {
456 tcpip::Storage content;
457 StoHelp::writeCompound(content, 3);
459 content.writeString(edgeID1);
460 content.writeDouble(pos1);
461 content.writeUnsignedByte(0);
463 content.writeString(edgeID2);
464 content.writeDouble(pos2);
465 content.writeUnsignedByte(0);
467 return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
468}
469
470
472Simulation::findRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& vType, const double depart, const int routingMode) {
473 tcpip::Storage content;
474 StoHelp::writeCompound(content, 5);
475 StoHelp::writeTypedString(content, fromEdge);
476 StoHelp::writeTypedString(content, toEdge);
477 StoHelp::writeTypedString(content, vType);
478 StoHelp::writeTypedDouble(content, depart);
479 StoHelp::writeTypedInt(content, routingMode);
480 return Dom::getTraCIStage(libsumo::FIND_ROUTE, "", &content);
481}
482
483
484std::vector<libsumo::TraCIStage>
485Simulation::findIntermodalRoute(const std::string& fromEdge, const std::string& toEdge,
486 const std::string& modes, double depart, const int routingMode, double speed, double walkFactor,
487 double departPos, double arrivalPos, const double departPosLat,
488 const std::string& pType, const std::string& vType, const std::string& destStop) {
489 tcpip::Storage content;
490 StoHelp::writeCompound(content, 13);
491 StoHelp::writeTypedString(content, fromEdge);
492 StoHelp::writeTypedString(content, toEdge);
493 StoHelp::writeTypedString(content, modes);
494 StoHelp::writeTypedDouble(content, depart);
495 StoHelp::writeTypedInt(content, routingMode);
496 StoHelp::writeTypedDouble(content, speed);
497 StoHelp::writeTypedDouble(content, walkFactor);
498 StoHelp::writeTypedDouble(content, departPos);
499 StoHelp::writeTypedDouble(content, arrivalPos);
500 StoHelp::writeTypedDouble(content, departPosLat);
501 StoHelp::writeTypedString(content, pType);
502 StoHelp::writeTypedString(content, vType);
503 StoHelp::writeTypedString(content, destStop);
505 int numStages = result.readInt();
506 std::vector<libsumo::TraCIStage> ret;
507 while (numStages-- > 0) {
509 StoHelp::readCompound(result, 13);
510 s.type = StoHelp::readTypedInt(result);
512 s.line = StoHelp::readTypedString(result);
516 s.cost = StoHelp::readTypedDouble(result);
523 ret.emplace_back(s);
524 }
525 return ret;
526}
527
529
530void
531Simulation::setScale(double value) {
533}
534
535void
536Simulation::clearPending(const std::string& routeID) {
538}
539
540
541void
542Simulation::saveState(const std::string& fileName) {
544}
545
546double
547Simulation::loadState(const std::string& fileName) {
549 return 0.;
550}
551
552void
553Simulation::writeMessage(const std::string& msg) {
555}
556
557
558void
559Simulation::subscribe(const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) {
560 libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_SIM_VARIABLE, "", begin, end, -1, -1, varIDs, params);
561}
562
563
565Simulation::getSubscriptionResults() {
567}
568
569
571
572}
573
574
575/****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:77
static void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:50
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static void writeTypedInt(tcpip::Storage &content, int value)
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:138
std::string intended
id of the intended vehicle for public transport ride
Definition: TraCIDefs.h:545
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:529
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:535
double length
length in m
Definition: TraCIDefs.h:543
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:539
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:549
std::string description
arbitrary description string
Definition: TraCIDefs.h:553
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:533
double cost
effort needed
Definition: TraCIDefs.h:541
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:547
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:537
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:551
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:531
void simulationStep(double time)
Sends a SimulationStep command.
Definition: Connection.cpp:123
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition: Connection.h:51
static bool isActive()
Definition: Connection.h:59
void close()
ends the simulation and closes the connection
Definition: Connection.cpp:92
static Connection & getActive()
Definition: Connection.h:55
void setOrder(int order)
Sends a SetOrder command.
Definition: Connection.cpp:151
libsumo::SubscriptionResults getAllSubscriptionResults(const int domain)
Definition: Connection.h:74
void subscribe(int domID, const std::string &objID, double beginTime, double endTime, int domain, double range, const std::vector< int > &vars, const libsumo::TraCIResults &params)
Sends a SubscribeContext or a SubscribeVariable request.
Definition: Connection.cpp:204
tcpip::Storage & doCommand(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.cpp:328
const std::string & getLabel()
Definition: Connection.h:67
static void switchCon(const std::string &label)
Definition: Connection.h:63
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:220
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:150
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:171
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:167
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:125
static libsumo::TraCIStage getTraCIStage(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:189
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:133
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:158
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:129
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:227
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:111
static int getFreeSocketPort()
Returns an free port on the system.
Definition: socket.cpp:118
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:247
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual int readByte()
Definition: storage.cpp:128
virtual double readDouble()
Definition: storage.cpp:362
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int VAR_MIN_EXPECTED_VEHICLES
TRACI_CONST int CMD_SAVE_SIMSTATE
TRACI_CONST int VAR_STOP_ENDING_VEHICLES_IDS
TRACI_CONST int CMD_SUBSCRIBE_SIM_VARIABLE
TRACI_CONST int CMD_LOAD
TRACI_CONST int POSITION_3D
TRACI_CONST int POSITION_ROADMAP
TRACI_CONST int VAR_ARRIVED_VEHICLES_NUMBER
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_NUMBER
TRACI_CONST int VAR_SCALE
TRACI_CONST int VAR_DEPARTED_VEHICLES_NUMBER
TRACI_CONST int CMD_LOAD_SIMSTATE
TRACI_CONST int VAR_COLLIDING_VEHICLES_NUMBER
TRACI_CONST int CMD_CLEAR_PENDING_VEHICLES
TRACI_CONST int VAR_PARKING_ENDING_VEHICLES_IDS
TRACI_CONST int VAR_PARKING_STARTING_VEHICLES_IDS
TRACI_CONST int FIND_INTERMODAL_ROUTE
TRACI_CONST int VAR_DEPARTED_PERSONS_NUMBER
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int VAR_END
TRACI_CONST int POSITION_2D
TRACI_CONST int VAR_ARRIVED_PERSONS_IDS
TRACI_CONST int VAR_TIME
TRACI_CONST int VAR_NET_BOUNDING_BOX
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int VAR_PENDING_VEHICLES
TRACI_CONST int VAR_BUS_STOP_ID_LIST
TRACI_CONST int POSITION_LON_LAT
TRACI_CONST int VAR_EMERGENCYSTOPPING_VEHICLES_IDS
TRACI_CONST int VAR_BUS_STOP_WAITING_IDS
TRACI_CONST int VAR_DEPARTED_VEHICLES_IDS
TRACI_CONST int VAR_TELEPORT_ENDING_VEHICLES_NUMBER
TRACI_CONST int CMD_MESSAGE
TRACI_CONST int VAR_PARKING_ENDING_VEHICLES_NUMBER
TRACI_CONST int VAR_LOADED_VEHICLES_IDS
TRACI_CONST int VAR_DELTA_T
TRACI_CONST int REQUEST_DRIVINGDIST
TRACI_CONST int VAR_DEPARTED_PERSONS_IDS
TRACI_CONST int VAR_ARRIVED_PERSONS_NUMBER
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_IDS
TRACI_CONST int FIND_ROUTE
TRACI_CONST int VAR_STOP_ENDING_VEHICLES_NUMBER
TRACI_CONST int POSITION_CONVERSION
TRACI_CONST int DISTANCE_REQUEST
TRACI_CONST int VAR_LOADED_VEHICLES_NUMBER
TRACI_CONST int VAR_TELEPORT_ENDING_VEHICLES_IDS
TRACI_CONST int VAR_COLLIDING_VEHICLES_IDS
TRACI_CONST int VAR_TELEPORT_STARTING_VEHICLES_NUMBER
TRACI_CONST int VAR_TELEPORT_STARTING_VEHICLES_IDS
TRACI_CONST int CMD_GETVERSION
TRACI_CONST int REQUEST_AIRDIST
TRACI_CONST int VAR_BUS_STOP_WAITING
TRACI_CONST int VAR_TIME_STEP
TRACI_CONST int VAR_ARRIVED_VEHICLES_IDS
TRACI_CONST int RESPONSE_SUBSCRIBE_SIM_VARIABLE
TRACI_CONST int VAR_PARKING_STARTING_VEHICLES_NUMBER
TRACI_CONST int POSITION_LON_LAT_ALT
TRACI_CONST int VAR_EMERGENCYSTOPPING_VEHICLES_NUMBER
std::map< int, std::shared_ptr< libsumo::TraCIResult > > TraCIResults
{variable->value}
Definition: TraCIDefs.h:298
TRACI_CONST int VAR_OPTION
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
A 3D-position.
Definition: TraCIDefs.h:172
A list of positions.
Definition: TraCIDefs.h:215
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:184