Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_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) 2009-2026 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// APIs for getting/setting traffic light values via TraCI
22/****************************************************************************/
23#include <config.h>
24
25#include <microsim/MSLane.h>
26#include <microsim/MSEdge.h>
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
38bool
40 tcpip::Storage& outputStorage) {
41 std::string warning = ""; // additional description for response
42 // variable
43 const int variable = inputStorage.readUnsignedByte();
44 if (variable != libsumo::TL_PHASE_INDEX && variable != libsumo::TL_PROGRAM && variable != libsumo::TL_PHASE_DURATION
46 && variable != libsumo::VAR_NAME
49 && variable != libsumo::TL_CONSTRAINT_ADD
50 && variable != libsumo::VAR_PARAMETER) {
51 return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "Change TLS State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
52 }
53 const std::string id = inputStorage.readString();
54 try {
55 switch (variable) {
57 libsumo::TrafficLight::setPhase(id, StoHelp::readTypedInt(inputStorage, "The phase index must be given as an integer."));
58 }
59 break;
61 libsumo::TrafficLight::setPhaseName(id, StoHelp::readTypedString(inputStorage, "The phase name must be given as a string."));
62 break;
64 libsumo::TrafficLight::setProgram(id, StoHelp::readTypedString(inputStorage, "The program must be given as a string."));
65 break;
67 libsumo::TrafficLight::setPhaseDuration(id, StoHelp::readTypedDouble(inputStorage, "The phase duration must be given as a double."));
68 break;
70 libsumo::TrafficLight::setRedYellowGreenState(id, StoHelp::readTypedString(inputStorage, "The phase must be given as a string."));
71 break;
73 StoHelp::readCompound(inputStorage, -1, "A compound object is needed for setting a new program.");
75 logic.programID = StoHelp::readTypedString(inputStorage, "set program: 1. parameter (programID) must be a string.");
76 logic.type = StoHelp::readTypedInt(inputStorage, "set program: 2. parameter (type) must be an int.");
77 logic.currentPhaseIndex = StoHelp::readTypedInt(inputStorage, "set program: 3. parameter (index) must be an int.");
78
79 const int numPhases = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for the phases.");
80 for (int j = 0; j < numPhases; ++j) {
81 const int items = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for every phase.");
82 if (items != 6 && items != 5) {
83 return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A phase compound object requires 5 or 6 items.", outputStorage);
84 }
85 const double duration = StoHelp::readTypedDouble(inputStorage, "set program: 4.1. parameter (duration) must be a double.");
86 const std::string state = StoHelp::readTypedString(inputStorage, "set program: 4.2. parameter (phase) must be a string.");
87 const double minDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.3. parameter (min duration) must be a double.");
88 const double maxDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.4. parameter (max duration) must be a double.");
89 const int numNext = StoHelp::readCompound(inputStorage, -1, "set program 4.5 parameter (next) must be a compound (list of ints).");
90 std::vector<int> next;
91 for (int k = 0; k < numNext; ++k) {
92 next.push_back(StoHelp::readTypedInt(inputStorage, "set program: 4.5. parameter (next) must be a list of int."));
93 }
94 std::string name;
95 if (items == 6) {
96 name = StoHelp::readTypedString(inputStorage, "set program: 4.6. parameter (name) must be a string.");
97 }
98 logic.phases.emplace_back(new libsumo::TraCIPhase(duration, state, minDuration, maxDuration, next, name));
99 }
100 const int numParams = StoHelp::readCompound(inputStorage, -1, "set program: 5. parameter (subparams) must be a compound object.");
101 for (int j = 0; j < numParams; ++j) {
102 const std::vector<std::string> par = StoHelp::readTypedStringList(inputStorage);
103 logic.subParameter[par[0]] = par[1];
104 }
105 libsumo::TrafficLight::setCompleteRedYellowGreenDefinition(id, logic);
106 }
107 break;
109 StoHelp::readCompound(inputStorage, 3, "A compound object of size 3 is needed for removing constraints.");
110 const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
111 const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foeSignal id must be given as a string.");
112 const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
113 libsumo::TrafficLight::removeConstraints(id, tripId, foeSignal, foeId);
114 }
115 break;
117 libsumo::TrafficLight::updateConstraints(id, StoHelp::readTypedString(inputStorage, "The tripId index must be given as a string."));
118 break;
120 StoHelp::readCompound(inputStorage, 5, "A compound object of size 5 is needed for adding constraints.");
121 const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
122 const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foe signal must be given as a string.");
123 const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
124 const int type = StoHelp::readTypedInt(inputStorage, "The type must be an int.");
125 const int limit = StoHelp::readTypedInt(inputStorage, "The limit must be an int.");
126 libsumo::TrafficLight::addConstraint(id, tripId, foeSignal, foeId, type, limit);
127 }
128 break;
130 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
131 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
132 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
133 libsumo::TrafficLight::setParameter(id, name, value);
134 }
135 break;
136 default:
137 break;
138 }
139 } catch (libsumo::TraCIException& e) {
140 return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, e.what(), outputStorage);
141 }
142 server.writeStatusCmd(libsumo::CMD_SET_TL_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
143 return true;
144}
145
146
147void
157 std::vector<std::string> paramItems;
158 for (auto item : c.param) {
159 paramItems.push_back(item.first);
160 paramItems.push_back(item.second);
161 }
163}
164
165
166/****************************************************************************/
const std::string invalid_return< std::string >::value
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:81
static void writeConstraint(TraCIServer &server, const libsumo::TraCISignalConstraint &c)
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc2: Change Traffic Lights State).
TraCI server used to control sumo by a remote TraCI client.
Definition TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
tcpip::Storage & getWrapperStorage()
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
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 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 writeTypedByte(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:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
TRACI_CONST int VAR_NAME
TRACI_CONST int TL_CONSTRAINT_REMOVE
TRACI_CONST int TL_CONSTRAINT_UPDATE
TRACI_CONST int VAR_PARAMETER
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_COMPLETE_PROGRAM_RYG
TRACI_CONST int CMD_SET_TL_VARIABLE
TRACI_CONST int TL_RED_YELLOW_GREEN_STATE
TRACI_CONST int RTYPE_OK
std::map< std::string, std::string > subParameter
Definition TraCIDefs.h:425
std::string programID
Definition TraCIDefs.h:421
std::vector< std::shared_ptr< libsumo::TraCIPhase > > phases
Definition TraCIDefs.h:424
std::string foeId
the tripId or vehicle id of the train that must pass first
Definition TraCIDefs.h:873
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition TraCIDefs.h:871
std::string foeSignal
the tlsID of the rail signla that the foe must pass first
Definition TraCIDefs.h:875
std::string signalId
the idea of the rail signal where this constraint is active
Definition TraCIDefs.h:869
std::map< std::string, std::string > param
additional parameters
Definition TraCIDefs.h:885
bool active
whether this constraint is active
Definition TraCIDefs.h:883
int type
the type of constraint (predecessor:0, insertionPredecessor:1)
Definition TraCIDefs.h:879
bool mustWait
whether tripId must still wait for foeId to pass foeSignal
Definition TraCIDefs.h:881
int limit
the number of trains that must be recorded at the foeSignal
Definition TraCIDefs.h:877