Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libtraci/TrafficLight.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/****************************************************************************/
21// C++ TraCI client API implementation
22/****************************************************************************/
23#include <config.h>
24
25#define LIBTRACI 1
29#include "Domain.h"
30
31
32namespace libtraci {
33
34typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
35
36// ===========================================================================
37// static member definitions
38// ===========================================================================
39std::vector<std::string>
40TrafficLight::getIDList() {
42}
43
44
45int
46TrafficLight::getIDCount() {
48}
49
50
51std::string
52TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
54}
55
56
57std::vector<libsumo::TraCILogic>
58TrafficLight::getAllProgramLogics(const std::string& tlsID) {
59 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
61 std::vector<libsumo::TraCILogic> result;
62 int numLogics = ret.readInt();
63 while (numLogics-- > 0) {
67 logic.type = StoHelp::readTypedInt(ret);
69 int numPhases = StoHelp::readCompound(ret);
70 while (numPhases-- > 0) {
74 phase->state = StoHelp::readTypedString(ret);
75 phase->minDur = StoHelp::readTypedDouble(ret);
76 phase->maxDur = StoHelp::readTypedDouble(ret);
77 int numNext = StoHelp::readCompound(ret);
78 while (numNext-- > 0) {
79 phase->next.push_back(StoHelp::readTypedInt(ret));
80 }
81 phase->name = StoHelp::readTypedString(ret);
82 logic.phases.emplace_back(phase);
83 }
84 int numParams = StoHelp::readCompound(ret);
85 while (numParams-- > 0) {
86 const std::vector<std::string> key_value = StoHelp::readTypedStringList(ret);
87 logic.subParameter[key_value[0]] = key_value[1];
88 }
89 result.emplace_back(logic);
90 }
91 return result;
92}
93
94
95std::vector<std::string>
96TrafficLight::getControlledJunctions(const std::string& tlsID) {
98}
99
100
101std::vector<std::string>
102TrafficLight::getControlledLanes(const std::string& tlsID) {
104}
105
106
107std::vector<std::vector<libsumo::TraCILink> >
108TrafficLight::getControlledLinks(const std::string& tlsID) {
109 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
111 std::vector< std::vector<libsumo::TraCILink> > result;
112 ret.readInt();
113 int numSignals = StoHelp::readTypedInt(ret);
114 while (numSignals-- > 0) {
115 std::vector<libsumo::TraCILink> controlledLinks;
116 int numLinks = StoHelp::readTypedInt(ret);
117 while (numLinks-- > 0) {
118 std::vector<std::string> link = StoHelp::readTypedStringList(ret);
119 controlledLinks.emplace_back(link[0], link[2], link[1]);
120 }
121 result.emplace_back(controlledLinks);
122 }
123 return result;
124}
125
126
127std::string
128TrafficLight::getProgram(const std::string& tlsID) {
130}
131
132
133int
134TrafficLight::getPhase(const std::string& tlsID) {
136}
137
138
139std::string
140TrafficLight::getPhaseName(const std::string& tlsID) {
141 return Dom::getString(libsumo::VAR_NAME, tlsID);
142}
143
144
145double
146TrafficLight::getPhaseDuration(const std::string& tlsID) {
148}
149
150
151double
152TrafficLight::getNextSwitch(const std::string& tlsID) {
154}
155
156
157double
158TrafficLight::getSpentDuration(const std::string& tlsID) {
160}
161
162
163int
164TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
165 tcpip::Storage content;
167 content.writeInt(index);
168 return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
169}
170
171std::vector<std::string>
172TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
173 tcpip::Storage content;
175 content.writeInt(linkIndex);
177}
178
179std::vector<std::string>
180TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
181 tcpip::Storage content;
183 content.writeInt(linkIndex);
184 return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
185}
186
187std::vector<std::string>
188TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
189 tcpip::Storage content;
191 content.writeInt(linkIndex);
193}
194
195std::vector<libsumo::TraCISignalConstraint>
196TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
197 std::vector<libsumo::TraCISignalConstraint> result;
198 tcpip::Storage content;
199 StoHelp::writeTypedString(content, tripId);
200 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
201 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
202 ret.readInt(); // components
203 // number of items
204 ret.readUnsignedByte();
205 const int n = ret.readInt();
206 for (int i = 0; i < n; ++i) {
214 c.mustWait = StoHelp::readTypedByte(ret) != 0;
215 c.active = StoHelp::readTypedByte(ret) != 0;
216 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
217 for (int j = 0; j < (int)paramItems.size(); j += 2) {
218 c.param[paramItems[j]] = paramItems[j + 1];
219 }
220 result.push_back(c);
221 }
222 return result;
223}
224
225std::vector<libsumo::TraCISignalConstraint>
226TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
227 std::vector<libsumo::TraCISignalConstraint> result;
228 tcpip::Storage content;
229 StoHelp::writeTypedString(content, foeId);
230 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
231 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
232 ret.readInt(); // components
233 // number of items
234 ret.readUnsignedByte();
235 const int n = ret.readInt();
236 for (int i = 0; i < n; ++i) {
244 c.mustWait = StoHelp::readTypedByte(ret) != 0;
245 c.active = StoHelp::readTypedByte(ret) != 0;
246 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
247 for (int j = 0; j < (int)paramItems.size(); j += 2) {
248 c.param[paramItems[j]] = paramItems[j + 1];
249 }
250 result.push_back(c);
251 }
252 return result;
253}
254
256
257void
258TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
260}
261
262
263void
264TrafficLight::setPhase(const std::string& tlsID, const int index) {
266}
267
268
269void
270TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
271 Dom::setString(libsumo::VAR_NAME, tlsID, name);
272}
273
274
275void
276TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
277 Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
278}
279
280
281void
282TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
283 Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
284}
285
286
287void
288TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
289 tcpip::Storage content;
290 StoHelp::writeCompound(content, 5);
291 StoHelp::writeTypedString(content, logic.programID);
292 StoHelp::writeTypedInt(content, logic.type);
294 StoHelp::writeCompound(content, (int)logic.phases.size());
295 for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
296 StoHelp::writeCompound(content, 6);
297 StoHelp::writeTypedDouble(content, phase->duration);
298 StoHelp::writeTypedString(content, phase->state);
299 StoHelp::writeTypedDouble(content, phase->minDur);
300 StoHelp::writeTypedDouble(content, phase->maxDur);
301 StoHelp::writeCompound(content, (int)phase->next.size());
302 for (int n : phase->next) {
303 StoHelp::writeTypedInt(content, n);
304 }
305 StoHelp::writeTypedString(content, phase->name);
306 }
307 StoHelp::writeCompound(content, (int)logic.subParameter.size());
308 for (const auto& key_value : logic.subParameter) {
309 StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
310 }
312}
313
314
315void
316TrafficLight::addConstraint(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId, const int type, const int limit) {
317 tcpip::Storage content;
318 StoHelp::writeCompound(content, 5);
319 StoHelp::writeTypedString(content, tripId);
320 StoHelp::writeTypedString(content, foeSignal);
321 StoHelp::writeTypedString(content, foeId);
322 StoHelp::writeTypedInt(content, type);
323 StoHelp::writeTypedInt(content, limit);
324 Dom::set(libsumo::TL_CONSTRAINT_ADD, tlsID, &content);
325}
326
327
328std::vector<libsumo::TraCISignalConstraint>
329TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
330 std::vector<libsumo::TraCISignalConstraint> result;
331 tcpip::Storage content;
332 StoHelp::writeCompound(content, 3);
333 StoHelp::writeTypedString(content, tripId);
334 StoHelp::writeTypedString(content, foeSignal);
335 StoHelp::writeTypedString(content, foeId);
336 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
337 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
338 ret.readInt(); // components
339 // number of items
340 ret.readUnsignedByte();
341 const int n = ret.readInt();
342 for (int i = 0; i < n; ++i) {
350 c.mustWait = StoHelp::readTypedByte(ret) != 0;
351 c.active = StoHelp::readTypedByte(ret) != 0;
352 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
353 for (int j = 0; j < (int)paramItems.size(); j += 2) {
354 c.param[paramItems[j]] = paramItems[j + 1];
355 }
356 result.push_back(c);
357 }
358 return result;
359}
360
361
362void
363TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
364 tcpip::Storage content;
365 StoHelp::writeCompound(content, 3);
366 StoHelp::writeTypedString(content, tripId);
367 StoHelp::writeTypedString(content, foeSignal);
368 StoHelp::writeTypedString(content, foeId);
369 Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
370}
371
372void
373TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
375}
376
377std::string
378to_string(const std::vector<double>& value) {
379 std::ostringstream tmp;
380 for (double d : value) {
381 tmp << d << " ";
382 }
383 std::string tmp2 = tmp.str();
384 tmp2.pop_back();
385 return tmp2;
386}
387
388
389void
390TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
391 setParameter(tlsID, "NEMA.splits", to_string(splits));
392}
393
394void
395TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
396 setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
397}
398
399void
400TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
401 setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
402}
403
404void
405TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
406 setParameter(tlsID, "NEMA.offset", std::to_string(offset));
407}
408
409
411
412}
413
414
415/****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition Domain.h:77
#define TL(string)
Definition MsgHandler.h:315
C++ TraCI client API implementation.
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
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 writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
std::map< std::string, std::string > subParameter
Definition TraCIDefs.h:378
std::string programID
Definition TraCIDefs.h:374
std::vector< std::shared_ptr< libsumo::TraCIPhase > > phases
Definition TraCIDefs.h:377
std::vector< int > next
Definition TraCIDefs.h:354
std::string state
Definition TraCIDefs.h:352
std::string name
Definition TraCIDefs.h:355
static Connection & getActive()
Definition Connection.h:57
std::mutex & getMutex() const
Definition Connection.h:76
static void setDouble(int var, const std::string &id, double value)
Definition Domain.h:231
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 void set(int var, const std::string &id, tcpip::Storage *add)
Definition Domain.h:219
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:130
static void setInt(int var, const std::string &id, int value)
Definition Domain.h:224
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
virtual void writeInt(int)
Definition storage.cpp:321
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual void writeUnsignedByte(int)
Definition storage.cpp:165
StorageType::size_type size() const
Definition storage.h:119
virtual int readInt()
Definition storage.cpp:311
TRACI_CONST int VAR_NAME
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int TL_CONSTRAINT_REMOVE
TRACI_CONST int TL_BLOCKING_VEHICLES
TRACI_CONST int TL_CONSTRAINT_SWAP
TRACI_CONST int TL_PRIORITY_VEHICLES
TRACI_CONST int TL_CONTROLLED_LANES
TRACI_CONST int TL_SPENT_DURATION
TRACI_CONST int TL_COMPLETE_DEFINITION_RYG
TRACI_CONST int VAR_PERSON_NUMBER
TRACI_CONST int TL_CONSTRAINT_UPDATE
TRACI_CONST int TL_CONTROLLED_JUNCTIONS
TRACI_CONST int TL_CONTROLLED_LINKS
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int ID_COUNT
TRACI_CONST int TL_CONSTRAINT_BYFOE
TRACI_CONST int TL_CONSTRAINT
TRACI_CONST int TL_NEXT_SWITCH
TRACI_CONST int TL_PROGRAM
TRACI_CONST int TL_PHASE_DURATION
TRACI_CONST int TL_CONSTRAINT_ADD
TRACI_CONST int TL_PHASE_INDEX
TRACI_CONST int TL_CURRENT_PHASE
TRACI_CONST int TL_COMPLETE_PROGRAM_RYG
TRACI_CONST int TL_RED_YELLOW_GREEN_STATE
TRACI_CONST int TL_CURRENT_PROGRAM
TRACI_CONST int TL_RIVAL_VEHICLES
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
std::string to_string(const std::vector< double > &value)
std::string foeId
the tripId or vehicle id of the train that must pass first
Definition TraCIDefs.h:655
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition TraCIDefs.h:653
std::string foeSignal
the tlsID of the rail signla that the foe must pass first
Definition TraCIDefs.h:657
std::string signalId
the idea of the rail signal where this constraint is active
Definition TraCIDefs.h:651
std::map< std::string, std::string > param
additional parameters
Definition TraCIDefs.h:667
bool active
whether this constraint is active
Definition TraCIDefs.h:665
int type
the type of constraint (predecessor:0, insertionPredecessor:1)
Definition TraCIDefs.h:661
bool mustWait
whether tripId must still wait for foeId to pass foeSignal
Definition TraCIDefs.h:663
int limit
the number of trains that must be recorded at the foeSignal
Definition TraCIDefs.h:659