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-2023 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
156int
157TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
158 tcpip::Storage content;
160 content.writeInt(index);
161 return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
162}
163
164std::vector<std::string>
165TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
166 tcpip::Storage content;
168 content.writeInt(linkIndex);
170}
171
172std::vector<std::string>
173TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
174 tcpip::Storage content;
176 content.writeInt(linkIndex);
177 return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
178}
179
180std::vector<std::string>
181TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
182 tcpip::Storage content;
184 content.writeInt(linkIndex);
186}
187
188std::vector<libsumo::TraCISignalConstraint>
189TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
190 std::vector<libsumo::TraCISignalConstraint> result;
191 tcpip::Storage content;
192 StoHelp::writeTypedString(content, tripId);
193 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
194 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
195 ret.readInt(); // components
196 // number of items
197 ret.readUnsignedByte();
198 const int n = ret.readInt();
199 for (int i = 0; i < n; ++i) {
207 c.mustWait = StoHelp::readTypedByte(ret) != 0;
208 c.active = StoHelp::readTypedByte(ret) != 0;
209 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
210 for (int j = 0; j < (int)paramItems.size(); j += 2) {
211 c.param[paramItems[j]] = paramItems[j + 1];
212 }
213 result.push_back(c);
214 }
215 return result;
216}
217
218std::vector<libsumo::TraCISignalConstraint>
219TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
220 std::vector<libsumo::TraCISignalConstraint> result;
221 tcpip::Storage content;
222 StoHelp::writeTypedString(content, foeId);
223 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
224 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
225 ret.readInt(); // components
226 // number of items
227 ret.readUnsignedByte();
228 const int n = ret.readInt();
229 for (int i = 0; i < n; ++i) {
237 c.mustWait = StoHelp::readTypedByte(ret) != 0;
238 c.active = StoHelp::readTypedByte(ret) != 0;
239 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
240 for (int j = 0; j < (int)paramItems.size(); j += 2) {
241 c.param[paramItems[j]] = paramItems[j + 1];
242 }
243 result.push_back(c);
244 }
245 return result;
246}
247
249
250void
251TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
253}
254
255
256void
257TrafficLight::setPhase(const std::string& tlsID, const int index) {
259}
260
261
262void
263TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
264 Dom::setString(libsumo::VAR_NAME, tlsID, name);
265}
266
267
268void
269TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
270 Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
271}
272
273
274void
275TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
276 Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
277}
278
279
280void
281TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
282 tcpip::Storage content;
283 StoHelp::writeCompound(content, 5);
284 StoHelp::writeTypedString(content, logic.programID);
285 StoHelp::writeTypedInt(content, logic.type);
287 StoHelp::writeCompound(content, (int)logic.phases.size());
288 for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
289 StoHelp::writeCompound(content, 6);
290 StoHelp::writeTypedDouble(content, phase->duration);
291 StoHelp::writeTypedString(content, phase->state);
292 StoHelp::writeTypedDouble(content, phase->minDur);
293 StoHelp::writeTypedDouble(content, phase->maxDur);
294 StoHelp::writeCompound(content, (int)phase->next.size());
295 for (int n : phase->next) {
296 StoHelp::writeTypedInt(content, n);
297 }
298 StoHelp::writeTypedString(content, phase->name);
299 }
300 StoHelp::writeCompound(content, (int)logic.subParameter.size());
301 for (const auto& key_value : logic.subParameter) {
302 StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
303 }
305}
306
307
308std::vector<libsumo::TraCISignalConstraint>
309TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
310 std::vector<libsumo::TraCISignalConstraint> result;
311 tcpip::Storage content;
313 content.writeInt(3);
314 StoHelp::writeTypedString(content, tripId);
315 StoHelp::writeTypedString(content, foeSignal);
316 StoHelp::writeTypedString(content, foeId);
317 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
318 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
319 ret.readInt(); // components
320 // number of items
321 ret.readUnsignedByte();
322 const int n = ret.readInt();
323 for (int i = 0; i < n; ++i) {
331 c.mustWait = StoHelp::readTypedByte(ret) != 0;
332 c.active = StoHelp::readTypedByte(ret) != 0;
333 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
334 for (int j = 0; j < (int)paramItems.size(); j += 2) {
335 c.param[paramItems[j]] = paramItems[j + 1];
336 }
337 result.push_back(c);
338 }
339 return result;
340}
341
342
343void
344TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
345 tcpip::Storage content;
347 content.writeInt(3);
348 StoHelp::writeTypedString(content, tripId);
349 StoHelp::writeTypedString(content, foeSignal);
350 StoHelp::writeTypedString(content, foeId);
351 Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
352}
353
354void
355TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
357}
358
359std::string
360to_string(const std::vector<double>& value) {
361 std::ostringstream tmp;
362 for (double d : value) {
363 tmp << d << " ";
364 }
365 std::string tmp2 = tmp.str();
366 tmp2.pop_back();
367 return tmp2;
368}
369
370
371void
372TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
373 setParameter(tlsID, "NEMA.splits", to_string(splits));
374}
375
376void
377TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
378 setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
379}
380
381void
382TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
383 setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
384}
385
386void
387TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
388 setParameter(tlsID, "NEMA.offset", std::to_string(offset));
389}
390
391
393
394}
395
396
397/****************************************************************************/
#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:287
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 void writeByte(int)
Definition storage.cpp:140
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 TYPE_COMPOUND
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_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