Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
23// APIs for getting/setting vehicle type values via TraCI
24/****************************************************************************/
25#include <config.h>
26
27#include <limits>
29#include <microsim/MSNet.h>
33#include <libsumo/VehicleType.h>
35
36
37// ===========================================================================
38// method definitions
39// ===========================================================================
40bool
42 tcpip::Storage& outputStorage) {
43 std::string warning = ""; // additional description for response
44 // variable
45 int variable = inputStorage.readUnsignedByte();
46 if (variable != libsumo::VAR_LENGTH && variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_VEHICLECLASS
48 && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MINGAP && variable != libsumo::VAR_SHAPECLASS
49 && variable != libsumo::VAR_ACCEL && variable != libsumo::VAR_IMPERFECTION
50 && variable != libsumo::VAR_DECEL && variable != libsumo::VAR_EMERGENCY_DECEL && variable != libsumo::VAR_APPARENT_DECEL
51 && variable != libsumo::VAR_TAU && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_ACTIONSTEPLENGTH
52 && variable != libsumo::VAR_SCALE
53 && variable != libsumo::VAR_HEIGHT
54 && variable != libsumo::VAR_MASS
55 && variable != libsumo::VAR_MINGAP_LAT
56 && variable != libsumo::VAR_MAXSPEED_LAT
57 && variable != libsumo::VAR_LATALIGNMENT
59 && variable != libsumo::VAR_IMPATIENCE
60 && variable != libsumo::VAR_PARAMETER
61 && variable != libsumo::COPY
62 ) {
64 "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
65 + " specified", outputStorage);
66 }
67 // id
68 std::string id = inputStorage.readString();
69// MSVehicleType* v = libsumo::VehicleType::getVType(id);
70// if (v == 0) {
71// return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
72// outputStorage);
73// }
74 // process
75 try {
76 if (setVariable(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
78 return true;
79 }
80 } catch (ProcessError& e) {
81 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
82 } catch (libsumo::TraCIException& e) {
83 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
84 }
85 return false;
86}
87
88
89bool
90TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
91 const std::string& id, TraCIServer& server,
92 tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
93 switch (variable) {
95 const double value = StoHelp::readTypedDouble(inputStorage, "Setting length requires a double.");
96 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
97 return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
98 }
99 libsumo::VehicleType::setLength(id, value);
100 break;
101 }
102 case libsumo::VAR_HEIGHT: {
103 const double value = StoHelp::readTypedDouble(inputStorage, "Setting height requires a double.");
104 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
105 return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
106 }
107 libsumo::VehicleType::setHeight(id, value);
108 break;
109 }
110 case libsumo::VAR_MASS: {
111 const double value = StoHelp::readTypedDouble(inputStorage, "Setting mass requires a double.");
112 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
113 return server.writeErrorStatusCmd(cmd, "Invalid mass.", outputStorage);
114 }
115 libsumo::VehicleType::setMass(id, value);
116 break;
117 }
119 const double value = StoHelp::readTypedDouble(inputStorage, "Setting maximum speed requires a double.");
120 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
121 return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
122 }
123 libsumo::VehicleType::setMaxSpeed(id, value);
124 break;
125 }
127 libsumo::VehicleType::setVehicleClass(id, StoHelp::readTypedString(inputStorage, "Setting vehicle class requires a string."));
128 break;
129 }
131 double value = StoHelp::readTypedDouble(inputStorage, "Setting speed factor requires a double.");
132 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
133 return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
134 }
135 libsumo::VehicleType::setSpeedFactor(id, value);
136 break;
137 }
139 const double value = StoHelp::readTypedDouble(inputStorage, "Setting speed deviation requires a double.");
140 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
141 return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
142 }
143 libsumo::VehicleType::setSpeedDeviation(id, value);
144 break;
145 }
147 libsumo::VehicleType::setEmissionClass(id, StoHelp::readTypedString(inputStorage, "Setting emission class requires a string."));
148 break;
149 }
150 case libsumo::VAR_WIDTH: {
151 const double value = StoHelp::readTypedDouble(inputStorage, "Setting width requires a double.");
152 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
153 return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
154 }
155 libsumo::VehicleType::setWidth(id, value);
156 break;
157 }
158 case libsumo::VAR_MINGAP: {
159 const double value = StoHelp::readTypedDouble(inputStorage, "Setting minimum gap requires a double.");
160 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
161 return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
162 }
163 libsumo::VehicleType::setMinGap(id, value);
164 break;
165 }
167 const double value = StoHelp::readTypedDouble(inputStorage, "Setting minimum lateral gap requires a double.");
168 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
169 return server.writeErrorStatusCmd(cmd, "Invalid minimum lateral gap.", outputStorage);
170 }
171 libsumo::VehicleType::setMinGapLat(id, value);
172 break;
173 }
175 const double value = StoHelp::readTypedDouble(inputStorage, "Setting maximum lateral speed requires a double.");
176 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
177 return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
178 }
179 libsumo::VehicleType::setMaxSpeedLat(id, value);
180 break;
181 }
183 libsumo::VehicleType::setLateralAlignment(id, StoHelp::readTypedString(inputStorage, "Setting preferred lateral alignment requires a string."));
184 break;
185 }
187 libsumo::VehicleType::setShapeClass(id, StoHelp::readTypedString(inputStorage, "Setting vehicle shape requires a string."));
188 break;
189 }
190 case libsumo::VAR_ACCEL: {
191 const double value = StoHelp::readTypedDouble(inputStorage, "Setting acceleration requires a double.");
192 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
193 return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
194 }
195 libsumo::VehicleType::setAccel(id, value);
196 break;
197 }
198 case libsumo::VAR_DECEL: {
199 const double value = StoHelp::readTypedDouble(inputStorage, "Setting deceleration requires a double.");
200 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
201 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
202 }
203 libsumo::VehicleType::setDecel(id, value);
204 break;
205 }
207 const double value = StoHelp::readTypedDouble(inputStorage, "Setting emergency deceleration requires a double.");
208 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
209 return server.writeErrorStatusCmd(cmd, "Invalid emergency deceleration.", outputStorage);
210 }
211 libsumo::VehicleType::setEmergencyDecel(id, value);
212 break;
213 }
215 const double value = StoHelp::readTypedDouble(inputStorage, "Setting apparent deceleration requires a double.");
216 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
217 return server.writeErrorStatusCmd(cmd, "Invalid apparent deceleration.", outputStorage);
218 }
219 libsumo::VehicleType::setApparentDecel(id, value);
220 break;
221 }
222 case libsumo::VAR_SCALE: {
223 const double value = StoHelp::readTypedDouble(inputStorage, "Traffic scale requires a double.");
224 if (value < 0.0) {
225 return server.writeErrorStatusCmd(cmd, "Invalid traffic scale.", outputStorage);
226 }
227 libsumo::VehicleType::setScale(id, value);
228 break;
229 }
231 const double value = StoHelp::readTypedDouble(inputStorage, "Setting action step length requires a double.");
232 if (fabs(value) == std::numeric_limits<double>::infinity()) {
233 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Invalid action step length.", outputStorage);
234 }
235 bool resetActionOffset = value >= 0.0;
236 libsumo::VehicleType::setActionStepLength(id, fabs(value), resetActionOffset);
237 break;
238 }
240 const double value = StoHelp::readTypedDouble(inputStorage, "Setting driver imperfection requires a double.");
241 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
242 return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
243 }
244 libsumo::VehicleType::setImperfection(id, value);
245 break;
246 }
247 case libsumo::VAR_TAU: {
248 const double value = StoHelp::readTypedDouble(inputStorage, "Setting headway time requires a double.");
249 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
250 return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
251 }
252 libsumo::VehicleType::setTau(id, value);
253 break;
254 }
256 libsumo::VehicleType::setImpatience(id, StoHelp::readTypedDouble(inputStorage, "Setting impatience requires a double."));
257 break;
258 }
260 libsumo::VehicleType::setBoardingDuration(id, StoHelp::readTypedDouble(inputStorage, "Setting boarding duration requires a double."));
261 break;
262 }
263 case libsumo::VAR_COLOR: {
264 libsumo::VehicleType::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
265 break;
266 }
267 case libsumo::COPY: {
268 libsumo::VehicleType::copy(id, StoHelp::readTypedString(inputStorage, "Copying a vehicle type requires a string."));
269 break;
270 }
272 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
273 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
274 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
275 libsumo::VehicleType::setParameter(id, name, value);
276 break;
277 }
278 default:
279 break;
280 }
281 return true;
282}
283
284
285/****************************************************************************/
const std::string invalid_return< std::string >::value
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:81
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State).
static bool setVariable(const int cmd, const int variable, const std::string &id, TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value for the given type.
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.
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::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIColor readTypedColor(tcpip::Storage &ret, const std::string &error="")
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_VEHICLECLASS
TRACI_CONST int VAR_IMPATIENCE
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_SCALE
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_SHAPECLASS
TRACI_CONST int VAR_ACTIONSTEPLENGTH
TRACI_CONST int VAR_SPEED_FACTOR
TRACI_CONST int VAR_TAU
TRACI_CONST int VAR_BOARDING_DURATION
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int COPY
TRACI_CONST int VAR_MASS
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_MAXSPEED_LAT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int CMD_SET_VEHICLETYPE_VARIABLE
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_APPARENT_DECEL
TRACI_CONST int VAR_DECEL
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_EMERGENCY_DECEL
TRACI_CONST int RTYPE_OK
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_ACCEL
TRACI_CONST int VAR_SPEED_DEVIATION