Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libtraci/Simulation.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2024 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 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
113 tcpip::Storage content;
115 content.writeStringList(args);
117}
118
119
120bool
121Simulation::isLoaded() {
122 return Connection::isActive();
123}
124
125
126void
127Simulation::step(const double time) {
129}
130
131
132void
133Simulation::executeMove() {
134 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
136}
137
138
139void
140Simulation::close(const std::string& /* reason */) {
142}
143
144
145std::pair<int, std::string>
146Simulation::getVersion() {
147 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
149 inMsg.readUnsignedByte(); // msg length
150 inMsg.readUnsignedByte(); // libsumo::CMD_GETVERSION again, see #7284
151 const int traciVersion = inMsg.readInt(); // to fix evaluation order
152 return std::make_pair(traciVersion, inMsg.readString());
153}
154
155
156std::string
157Simulation::getOption(const std::string& option) {
158 return Dom::getString(libsumo::VAR_OPTION, option);
159}
160
161
162int
163Simulation::getCurrentTime() {
165}
166
167
168double
169Simulation::getTime() {
171}
172
173
174double
175Simulation::getEndTime() {
177}
178
179
180int
181Simulation::getLoadedNumber() {
183}
184
185
186std::vector<std::string>
187Simulation::getLoadedIDList() {
189}
190
191
192int
193Simulation::getDepartedNumber() {
195}
196
197
198std::vector<std::string>
199Simulation::getDepartedIDList() {
201}
202
203
204int
205Simulation::getArrivedNumber() {
207}
208
209
210std::vector<std::string>
211Simulation::getArrivedIDList() {
213}
214
215
216int
217Simulation::getParkingStartingVehiclesNumber() {
219}
220
221
222std::vector<std::string>
223Simulation::getParkingStartingVehiclesIDList() {
225}
226
227
228int
229Simulation::getParkingEndingVehiclesNumber() {
231}
232
233
234std::vector<std::string>
235Simulation::getParkingEndingVehiclesIDList() {
237}
238
239
240int
241Simulation::getStopStartingVehiclesNumber() {
243}
244
245
246std::vector<std::string>
247Simulation::getStopStartingVehiclesIDList() {
249}
250
251
252int
253Simulation::getStopEndingVehiclesNumber() {
255}
256
257
258std::vector<std::string>
259Simulation::getStopEndingVehiclesIDList() {
261}
262
263
264int
265Simulation::getCollidingVehiclesNumber() {
267}
268
269
270std::vector<std::string>
271Simulation::getCollidingVehiclesIDList() {
273}
274
275
276int
277Simulation::getEmergencyStoppingVehiclesNumber() {
279}
280
281
282std::vector<std::string>
283Simulation::getEmergencyStoppingVehiclesIDList() {
285}
286
287
288int
289Simulation::getStartingTeleportNumber() {
291}
292
293
294std::vector<std::string>
295Simulation::getStartingTeleportIDList() {
297}
298
299
300int
301Simulation::getEndingTeleportNumber() {
303}
304
305
306std::vector<std::string>
307Simulation::getEndingTeleportIDList() {
309}
310
311
312int
313Simulation::getDepartedPersonNumber() {
315}
316
317
318std::vector<std::string>
319Simulation::getDepartedPersonIDList() {
321}
322
323
324int
325Simulation::getArrivedPersonNumber() {
327}
328
329
330std::vector<std::string>
331Simulation::getArrivedPersonIDList() {
333}
334
335
336std::vector<std::string>
337Simulation::getBusStopIDList() {
339}
340
341int
342Simulation::getBusStopWaiting(const std::string& stopID) {
344}
345
346std::vector<std::string>
347Simulation::getBusStopWaitingIDList(const std::string& stopID) {
349}
350
351
352std::vector<std::string>
353Simulation::getPendingVehicles() {
355}
356
357
358std::vector<libsumo::TraCICollision>
359Simulation::getCollisions() {
360 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
362 std::vector<libsumo::TraCICollision> result;
364 int numCollisions = ret.readInt();
365 while (numCollisions-- > 0) {
376 result.emplace_back(c);
377 }
378 return result;
379}
380
381
382double
383Simulation::getScale() {
385}
386
387
388double
389Simulation::getDeltaT() {
391}
392
393
395Simulation::getNetBoundary() {
397}
398
399
400int
401Simulation::getMinExpectedNumber() {
403}
404
405
407Simulation::convert2D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
408 tcpip::Storage content;
409 StoHelp::writeCompound(content, 2);
411 content.writeString(edgeID);
412 content.writeDouble(pos);
413 content.writeUnsignedByte(laneIndex);
416 return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, toGeo);
417}
418
419
421Simulation::convert3D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
422 tcpip::Storage content;
423 StoHelp::writeCompound(content, 2);
425 content.writeString(edgeID);
426 content.writeDouble(pos);
427 content.writeUnsignedByte(laneIndex);
430 return Dom::getPos3D(libsumo::POSITION_CONVERSION, "", &content, toGeo);
431}
432
433
435Simulation::convertRoad(double x, double y, bool isGeo, const std::string& vClass) {
436 tcpip::Storage content;
437 StoHelp::writeCompound(content, 3);
439 content.writeDouble(x);
440 content.writeDouble(y);
443 StoHelp::writeTypedString(content, vClass);
444 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
447 result.edgeID = ret.readString();
448 result.pos = ret.readDouble();
449 result.laneIndex = ret.readByte();
450 return result;
451}
452
453
455Simulation::convertGeo(double x, double y, bool fromGeo) {
456 tcpip::Storage content;
457 StoHelp::writeCompound(content, 2);
459 content.writeDouble(x);
460 content.writeDouble(y);
463 return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, !fromGeo);
464}
465
466
467double
468Simulation::getDistance2D(double x1, double y1, double x2, double y2, bool isGeo, bool isDriving) {
469 tcpip::Storage content;
470 StoHelp::writeCompound(content, 3);
472 content.writeDouble(x1);
473 content.writeDouble(y1);
475 content.writeDouble(x2);
476 content.writeDouble(y2);
478 return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
479}
480
481
482double
483Simulation::getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving) {
484 tcpip::Storage content;
485 StoHelp::writeCompound(content, 3);
487 content.writeString(edgeID1);
488 content.writeDouble(pos1);
489 content.writeUnsignedByte(0);
491 content.writeString(edgeID2);
492 content.writeDouble(pos2);
493 content.writeUnsignedByte(0);
495 return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
496}
497
498
500Simulation::findRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& vType, const double depart, const int routingMode) {
501 tcpip::Storage content;
502 StoHelp::writeCompound(content, 5);
503 StoHelp::writeTypedString(content, fromEdge);
504 StoHelp::writeTypedString(content, toEdge);
505 StoHelp::writeTypedString(content, vType);
506 StoHelp::writeTypedDouble(content, depart);
507 StoHelp::writeTypedInt(content, routingMode);
508 return Dom::getTraCIStage(libsumo::FIND_ROUTE, "", &content);
509}
510
511
512std::vector<libsumo::TraCIStage>
513Simulation::findIntermodalRoute(const std::string& fromEdge, const std::string& toEdge,
514 const std::string& modes, double depart, const int routingMode, double speed, double walkFactor,
515 double departPos, double arrivalPos, const double departPosLat,
516 const std::string& pType, const std::string& vType, const std::string& destStop) {
517 tcpip::Storage content;
518 StoHelp::writeCompound(content, 13);
519 StoHelp::writeTypedString(content, fromEdge);
520 StoHelp::writeTypedString(content, toEdge);
521 StoHelp::writeTypedString(content, modes);
522 StoHelp::writeTypedDouble(content, depart);
523 StoHelp::writeTypedInt(content, routingMode);
524 StoHelp::writeTypedDouble(content, speed);
525 StoHelp::writeTypedDouble(content, walkFactor);
526 StoHelp::writeTypedDouble(content, departPos);
527 StoHelp::writeTypedDouble(content, arrivalPos);
528 StoHelp::writeTypedDouble(content, departPosLat);
529 StoHelp::writeTypedString(content, pType);
530 StoHelp::writeTypedString(content, vType);
531 StoHelp::writeTypedString(content, destStop);
532 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
534 int numStages = result.readInt();
535 std::vector<libsumo::TraCIStage> ret;
536 while (numStages-- > 0) {
538 StoHelp::readCompound(result, 13);
539 s.type = StoHelp::readTypedInt(result);
541 s.line = StoHelp::readTypedString(result);
545 s.cost = StoHelp::readTypedDouble(result);
552 ret.emplace_back(s);
553 }
554 return ret;
555}
556
558
559void
560Simulation::setScale(double value) {
562}
563
564void
565Simulation::clearPending(const std::string& routeID) {
567}
568
569
570void
571Simulation::saveState(const std::string& fileName) {
573}
574
575double
576Simulation::loadState(const std::string& fileName) {
578 return 0.;
579}
580
581void
582Simulation::writeMessage(const std::string& msg) {
584}
585
586
587void
588Simulation::subscribe(const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) {
589 libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_SIM_VARIABLE, "", begin, end, -1, -1, varIDs, params);
590}
591
592
594Simulation::getSubscriptionResults() {
596}
597
598
600
601}
602
603
604/****************************************************************************/
#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="")
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
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="")
An error which allows to continue.
Definition TraCIDefs.h:144
std::string intended
id of the intended vehicle for public transport ride
Definition TraCIDefs.h:582
int type
The type of stage (walking, driving, ...)
Definition TraCIDefs.h:566
std::string destStop
The id of the destination stop.
Definition TraCIDefs.h:572
double length
length in m
Definition TraCIDefs.h:580
double travelTime
duration of the stage in seconds
Definition TraCIDefs.h:576
double departPos
position on the lane when starting the stage
Definition TraCIDefs.h:586
std::string description
arbitrary description string
Definition TraCIDefs.h:590
std::string line
The line or the id of the vehicle type.
Definition TraCIDefs.h:570
double cost
effort needed
Definition TraCIDefs.h:578
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition TraCIDefs.h:584
std::vector< std::string > edges
The sequence of edges to travel.
Definition TraCIDefs.h:574
double arrivalPos
position on the lane when ending the stage
Definition TraCIDefs.h:588
std::string vType
The vehicle type when using a private car or bike.
Definition TraCIDefs.h:568
void simulationStep(double time)
Sends a SimulationStep command.
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition Connection.h:53
static bool isActive()
Definition Connection.h:64
void close()
ends the simulation and closes the connection
static Connection & getActive()
Definition Connection.h:57
std::mutex & getMutex() const
Definition Connection.h:76
void setOrder(int order)
Sends a SetOrder command.
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.
libsumo::SubscriptionResults & getAllSubscriptionResults(const int domain)
Definition Connection.h:83
tcpip::Storage & doCommand(int command, int var=-1, const std::string &id="", tcpip::Storage *add=nullptr, int expectedType=-1)
static void switchCon(const std::string &label)
Definition Connection.h:68
const std::string & getLabel() const
Definition Connection.h:72
static void setDouble(int var, const std::string &id, double value)
Definition Domain.h:231
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition Domain.h:153
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:177
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:172
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:198
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:135
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition Domain.h:162
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:130
static void setString(int var, const std::string &id, const std::string &value)
Definition Domain.h:238
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 VAR_COLLISIONS
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 CMD_EXECUTEMOVE
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:335
TRACI_CONST int VAR_OPTION
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
std::string lane
The lane where the collision happended.
Definition TraCIDefs.h:643
std::string type
The type of collision.
Definition TraCIDefs.h:641
std::string collider
The ids of the participating vehicles and persons.
Definition TraCIDefs.h:634
double pos
The position of the collision along the lane.
Definition TraCIDefs.h:645
std::string colliderType
Definition TraCIDefs.h:636
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:178
A list of positions.
Definition TraCIDefs.h:234
An edgeId, position and laneIndex.
Definition TraCIDefs.h:197