Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libtraci/TrafficLight.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 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
/****************************************************************************/
14
/// @file TrafficLight.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Mario Krumnow
17
/// @author Jakob Erdmann
18
/// @author Michael Behrisch
19
/// @date 30.05.2012
20
///
21
// C++ TraCI client API implementation
22
/****************************************************************************/
23
#include <config.h>
24
25
#define LIBTRACI 1
26
#include <libsumo/StorageHelper.h>
27
#include <libsumo/TraCIConstants.h>
28
#include <libsumo/TrafficLight.h>
29
#include "Domain.h"
30
31
32
namespace libtraci {
33
34
typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
35
36
// ===========================================================================
37
// static member definitions
38
// ===========================================================================
39
std::vector<std::string>
40
TrafficLight::getIDList() {
41
return Dom::getStringVector(libsumo::TRACI_ID_LIST, "");
42
}
43
44
45
int
46
TrafficLight::getIDCount() {
47
return Dom::getInt(libsumo::ID_COUNT, "");
48
}
49
50
51
std::string
52
TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
53
return Dom::getString(libsumo::TL_RED_YELLOW_GREEN_STATE, tlsID);
54
}
55
56
57
std::vector<libsumo::TraCILogic>
58
TrafficLight::getAllProgramLogics(const std::string& tlsID) {
59
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
60
tcpip::Storage& ret = Dom::get(libsumo::TL_COMPLETE_DEFINITION_RYG, tlsID);
61
std::vector<libsumo::TraCILogic> result;
62
int numLogics = ret.readInt();
63
while (numLogics-- > 0) {
64
libsumo::TraCILogic logic;
65
StoHelp::readLogic(ret, logic);
66
result.emplace_back(logic);
67
}
68
return result;
69
}
70
71
72
std::vector<std::string>
73
TrafficLight::getControlledJunctions(const std::string& tlsID) {
74
return Dom::getStringVector(libsumo::TL_CONTROLLED_JUNCTIONS, tlsID);
75
}
76
77
78
std::vector<std::string>
79
TrafficLight::getControlledLanes(const std::string& tlsID) {
80
return Dom::getStringVector(libsumo::TL_CONTROLLED_LANES, tlsID);
81
}
82
83
84
std::vector<std::vector<libsumo::TraCILink> >
85
TrafficLight::getControlledLinks(const std::string& tlsID) {
86
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
87
tcpip::Storage& ret = Dom::get(libsumo::TL_CONTROLLED_LINKS, tlsID);
88
std::vector< std::vector<libsumo::TraCILink> > result;
89
ret.readInt();
90
StoHelp::readLinkVectorVector(ret, result);
91
return result;
92
}
93
94
95
std::string
96
TrafficLight::getProgram(const std::string& tlsID) {
97
return Dom::getString(libsumo::TL_CURRENT_PROGRAM, tlsID);
98
}
99
100
101
int
102
TrafficLight::getPhase(const std::string& tlsID) {
103
return Dom::getInt(libsumo::TL_CURRENT_PHASE, tlsID);
104
}
105
106
107
std::string
108
TrafficLight::getPhaseName(const std::string& tlsID) {
109
return Dom::getString(libsumo::VAR_NAME, tlsID);
110
}
111
112
113
double
114
TrafficLight::getPhaseDuration(const std::string& tlsID) {
115
return Dom::getDouble(libsumo::TL_PHASE_DURATION, tlsID);
116
}
117
118
119
double
120
TrafficLight::getNextSwitch(const std::string& tlsID) {
121
return Dom::getDouble(libsumo::TL_NEXT_SWITCH, tlsID);
122
}
123
124
125
double
126
TrafficLight::getSpentDuration(const std::string& tlsID) {
127
return Dom::getDouble(libsumo::TL_SPENT_DURATION, tlsID);
128
}
129
130
131
int
132
TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
133
tcpip::Storage content;
134
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
135
content.writeInt(index);
136
return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
137
}
138
139
std::vector<std::string>
140
TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
141
tcpip::Storage content;
142
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
143
content.writeInt(linkIndex);
144
return Dom::getStringVector(libsumo::TL_BLOCKING_VEHICLES, tlsID, &content);
145
}
146
147
std::vector<std::string>
148
TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
149
tcpip::Storage content;
150
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
151
content.writeInt(linkIndex);
152
return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
153
}
154
155
std::vector<std::string>
156
TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
157
tcpip::Storage content;
158
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
159
content.writeInt(linkIndex);
160
return Dom::getStringVector(libsumo::TL_PRIORITY_VEHICLES, tlsID, &content);
161
}
162
163
std::vector<libsumo::TraCISignalConstraint>
164
TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
165
std::vector<libsumo::TraCISignalConstraint> result;
166
tcpip::Storage content;
167
StoHelp::writeTypedString(content, tripId);
168
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
169
tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
170
ret.readInt(); // components
171
StoHelp::readConstraintVector(ret, result);
172
return result;
173
}
174
175
std::vector<libsumo::TraCISignalConstraint>
176
TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
177
std::vector<libsumo::TraCISignalConstraint> result;
178
tcpip::Storage content;
179
StoHelp::writeTypedString(content, foeId);
180
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
181
tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
182
ret.readInt(); // components
183
StoHelp::readConstraintVector(ret, result);
184
return result;
185
}
186
187
LIBTRACI_PARAMETER_IMPLEMENTATION(TrafficLight, TL)
188
189
void
190
TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
191
Dom::setString(libsumo::TL_RED_YELLOW_GREEN_STATE, tlsID, state);
192
}
193
194
195
void
196
TrafficLight::setPhase(const std::string& tlsID, const int index) {
197
Dom::setInt(libsumo::TL_PHASE_INDEX, tlsID, index);
198
}
199
200
201
void
202
TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
203
Dom::setString(libsumo::VAR_NAME, tlsID, name);
204
}
205
206
207
void
208
TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
209
Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
210
}
211
212
213
void
214
TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
215
Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
216
}
217
218
219
void
220
TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
221
tcpip::Storage content;
222
StoHelp::writeCompound(content, 5);
223
StoHelp::writeTypedString(content, logic.programID);
224
StoHelp::writeTypedInt(content, logic.type);
225
StoHelp::writeTypedInt(content, logic.currentPhaseIndex);
226
StoHelp::writeCompound(content, (int)logic.phases.size());
227
for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
228
StoHelp::writeCompound(content, 6);
229
StoHelp::writeTypedDouble(content, phase->duration);
230
StoHelp::writeTypedString(content, phase->state);
231
StoHelp::writeTypedDouble(content, phase->minDur);
232
StoHelp::writeTypedDouble(content, phase->maxDur);
233
StoHelp::writeCompound(content, (int)phase->next.size());
234
for (int n : phase->next) {
235
StoHelp::writeTypedInt(content, n);
236
}
237
StoHelp::writeTypedString(content, phase->name);
238
}
239
StoHelp::writeCompound(content, (int)logic.subParameter.size());
240
for (const auto& key_value : logic.subParameter) {
241
StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
242
}
243
Dom::set(libsumo::TL_COMPLETE_PROGRAM_RYG, tlsID, &content);
244
}
245
246
247
void
248
TrafficLight::addConstraint(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId, const int type, const int limit) {
249
tcpip::Storage content;
250
StoHelp::writeCompound(content, 5);
251
StoHelp::writeTypedString(content, tripId);
252
StoHelp::writeTypedString(content, foeSignal);
253
StoHelp::writeTypedString(content, foeId);
254
StoHelp::writeTypedInt(content, type);
255
StoHelp::writeTypedInt(content, limit);
256
Dom::set(libsumo::TL_CONSTRAINT_ADD, tlsID, &content);
257
}
258
259
260
std::vector<libsumo::TraCISignalConstraint>
261
TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
262
std::vector<libsumo::TraCISignalConstraint> result;
263
tcpip::Storage content;
264
StoHelp::writeCompound(content, 3);
265
StoHelp::writeTypedString(content, tripId);
266
StoHelp::writeTypedString(content, foeSignal);
267
StoHelp::writeTypedString(content, foeId);
268
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
269
tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
270
ret.readInt(); // components
271
// number of items
272
ret.readUnsignedByte();
273
const int n = ret.readInt();
274
for (int i = 0; i < n; ++i) {
275
libsumo::TraCISignalConstraint c;
276
c.signalId = StoHelp::readTypedString(ret);
277
c.tripId = StoHelp::readTypedString(ret);
278
c.foeId = StoHelp::readTypedString(ret);
279
c.foeSignal = StoHelp::readTypedString(ret);
280
c.limit = StoHelp::readTypedInt(ret);
281
c.type = StoHelp::readTypedInt(ret);
282
c.mustWait = StoHelp::readTypedByte(ret) != 0;
283
c.active = StoHelp::readTypedByte(ret) != 0;
284
const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
285
for (int j = 0; j < (int)paramItems.size(); j += 2) {
286
c.param[paramItems[j]] = paramItems[j + 1];
287
}
288
result.push_back(c);
289
}
290
return result;
291
}
292
293
294
void
295
TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
296
tcpip::Storage content;
297
StoHelp::writeCompound(content, 3);
298
StoHelp::writeTypedString(content, tripId);
299
StoHelp::writeTypedString(content, foeSignal);
300
StoHelp::writeTypedString(content, foeId);
301
Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
302
}
303
304
void
305
TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
306
Dom::setString(libsumo::TL_CONSTRAINT_UPDATE, vehID, tripId);
307
}
308
309
std::string
310
to_string(const std::vector<double>& value) {
311
std::ostringstream tmp;
312
for (double d : value) {
313
tmp << d << " ";
314
}
315
std::string tmp2 = tmp.str();
316
tmp2.pop_back();
317
return tmp2;
318
}
319
320
321
void
322
TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
323
setParameter(tlsID, "NEMA.splits", to_string(splits));
324
}
325
326
void
327
TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
328
setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
329
}
330
331
void
332
TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
333
setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
334
}
335
336
void
337
TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
338
setParameter(tlsID, "NEMA.offset", std::to_string(offset));
339
}
340
341
342
LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(TrafficLight, TL)
343
344
}
345
346
347
/****************************************************************************/
348
349