SUMO - Simulation of Urban MObility
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting vehicle type values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifndef NO_TRACI
35 
36 #include <limits>
38 #include <microsim/MSNet.h>
39 #include <microsim/MSVehicleType.h>
40 #include "TraCIConstants.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 bool
53  tcpip::Storage& outputStorage) {
54  // variable & id
55  int variable = inputStorage.readUnsignedByte();
56  std::string id = inputStorage.readString();
57  // check variable
58  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
59  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
60  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
61  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT
62  && variable != VAR_PARAMETER) {
63  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
64  }
65  // begin response building
66  tcpip::Storage tempMsg;
67  // response-code, variableID, objectID
69  tempMsg.writeUnsignedByte(variable);
70  tempMsg.writeString(id);
71  // process request
72  if (variable == ID_LIST) {
73  std::vector<std::string> ids;
76  tempMsg.writeStringList(ids);
77  } else if (variable == ID_COUNT) {
78  std::vector<std::string> ids;
81  tempMsg.writeInt((int) ids.size());
82  } else {
84  if (v == 0) {
85  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
86  }
87  switch (variable) {
88  case VAR_PARAMETER: {
89  std::string paramName = "";
90  if (!server.readTypeCheckingString(inputStorage, paramName)) {
91  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
92  }
94  tempMsg.writeString(v->getParameter().getParameter(paramName, ""));
95  }
96  break;
97  default:
98  getVariable(variable, *v, tempMsg);
99  break;
100  }
101  }
102  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
103  server.writeResponseWithLength(outputStorage, tempMsg);
104  return true;
105 }
106 
107 bool
109  switch (variable) {
110  case VAR_LENGTH:
112  tempMsg.writeDouble(v.getLength());
113  break;
114  case VAR_MINGAP:
116  tempMsg.writeDouble(v.getMinGap());
117  break;
118  case VAR_MAXSPEED:
120  tempMsg.writeDouble(v.getMaxSpeed());
121  break;
122  case VAR_ACCEL:
125  break;
126  case VAR_DECEL:
129  break;
130  case VAR_IMPERFECTION:
133  break;
134  case VAR_TAU:
137  break;
138  case VAR_SPEED_FACTOR:
140  tempMsg.writeDouble(v.getSpeedFactor());
141  break;
142  case VAR_SPEED_DEVIATION:
144  tempMsg.writeDouble(v.getSpeedDeviation());
145  break;
146  case VAR_VEHICLECLASS:
148  tempMsg.writeString(toString(v.getVehicleClass()));
149  break;
150  case VAR_EMISSIONCLASS:
153  break;
154  case VAR_SHAPECLASS:
157  break;
158  case VAR_WIDTH:
160  tempMsg.writeDouble(v.getWidth());
161  break;
162  case VAR_COLOR:
163  tempMsg.writeUnsignedByte(TYPE_COLOR);
164  tempMsg.writeUnsignedByte(v.getColor().red());
165  tempMsg.writeUnsignedByte(v.getColor().green());
166  tempMsg.writeUnsignedByte(v.getColor().blue());
167  tempMsg.writeUnsignedByte(v.getColor().alpha());
168  break;
169  default:
170  break;
171  }
172  return true;
173 }
174 
175 bool
177  tcpip::Storage& outputStorage) {
178  std::string warning = ""; // additional description for response
179  // variable
180  int variable = inputStorage.readUnsignedByte();
181  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
182  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
183  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
184  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
185  && variable != VAR_TAU && variable != VAR_COLOR && variable != VAR_PARAMETER
186  ) {
187  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
188  }
189  // id
190  std::string id = inputStorage.readString();
192  if (v == 0) {
193  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
194  }
195  // process
196  try {
197  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
198  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
199  return true;
200  }
201  } catch (ProcessError& e) {
202  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
203  }
204  return false;
205 }
206 
207 
208 bool
209 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
210  MSVehicleType& v, TraCIServer& server,
211  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
212  switch (variable) {
213  case VAR_LENGTH: {
214  double value = 0;
215  if (!server.readTypeCheckingDouble(inputStorage, value)) {
216  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
217  }
218  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
219  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
220  }
221  v.setLength(value);
222  }
223  break;
224  case VAR_MAXSPEED: {
225  double value = 0;
226  if (!server.readTypeCheckingDouble(inputStorage, value)) {
227  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
228  }
229  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
230  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
231  }
232  v.setMaxSpeed(value);
233  }
234  break;
235  case VAR_VEHICLECLASS: {
236  std::string vclass;
237  if (!server.readTypeCheckingString(inputStorage, vclass)) {
238  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
239  }
240  try {
241  v.setVClass(getVehicleClassID(vclass));
242  } catch (InvalidArgument e) {
243  return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
244  }
245  }
246  break;
247  case VAR_SPEED_FACTOR: {
248  double value = 0;
249  if (!server.readTypeCheckingDouble(inputStorage, value)) {
250  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
251  }
252  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
253  return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
254  }
255  v.setSpeedFactor(value);
256  }
257  break;
258  case VAR_SPEED_DEVIATION: {
259  double value = 0;
260  if (!server.readTypeCheckingDouble(inputStorage, value)) {
261  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
262  }
263  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
264  return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
265  }
266  v.setSpeedDeviation(value);
267  }
268  break;
269  case VAR_EMISSIONCLASS: {
270  std::string eclass;
271  if (!server.readTypeCheckingString(inputStorage, eclass)) {
272  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
273  }
274  try {
276  } catch (InvalidArgument e) {
277  return server.writeErrorStatusCmd(cmd, "Unknown emission class '" + eclass + "'.", outputStorage);
278  }
279  }
280  break;
281  case VAR_WIDTH: {
282  double value = 0;
283  if (!server.readTypeCheckingDouble(inputStorage, value)) {
284  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
285  }
286  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
287  return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
288  }
289  v.setWidth(value);
290  }
291  break;
292  case VAR_MINGAP: {
293  double value = 0;
294  if (!server.readTypeCheckingDouble(inputStorage, value)) {
295  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
296  }
297  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
298  return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
299  }
300  v.setMinGap(value);
301  }
302  break;
303  case VAR_SHAPECLASS: {
304  std::string sclass;
305  if (!server.readTypeCheckingString(inputStorage, sclass)) {
306  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
307  }
308  try {
309  v.setShape(getVehicleShapeID(sclass));
310  } catch (InvalidArgument e) {
311  return server.writeErrorStatusCmd(cmd, "Unknown vehicle shape " + sclass + "'.", outputStorage);
312  }
313  }
314  break;
315  case VAR_ACCEL: {
316  double value = 0;
317  if (!server.readTypeCheckingDouble(inputStorage, value)) {
318  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
319  }
320  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
321  return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
322  }
323  v.getCarFollowModel().setMaxAccel(value);
324  }
325  break;
326  case VAR_DECEL: {
327  double value = 0;
328  if (!server.readTypeCheckingDouble(inputStorage, value)) {
329  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
330  }
331  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
332  return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
333  }
334  v.getCarFollowModel().setMaxDecel(value);
335  }
336  break;
337  case VAR_IMPERFECTION: {
338  double value = 0;
339  if (!server.readTypeCheckingDouble(inputStorage, value)) {
340  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
341  }
342  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
343  return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
344  }
346  }
347  break;
348  case VAR_TAU: {
349  double value = 0;
350  if (!server.readTypeCheckingDouble(inputStorage, value)) {
351  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
352  }
353  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
354  return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
355  }
357  }
358  break;
359  case VAR_COLOR: {
360  RGBColor col;
361  if (!server.readTypeCheckingColor(inputStorage, col)) {
362  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
363  }
364  v.setColor(col);
365  }
366  break;
367  case VAR_PARAMETER: {
368  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
369  return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.", outputStorage);
370  }
371  //readt itemNo
372  inputStorage.readInt();
373  std::string name;
374  if (!server.readTypeCheckingString(inputStorage, name)) {
375  return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.", outputStorage);
376  }
377  std::string value;
378  if (!server.readTypeCheckingString(inputStorage, value)) {
379  return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.", outputStorage);
380  }
381  ((SUMOVTypeParameter&) v.getParameter()).addParameter(name, value);
382  }
383  break;
384  default:
385  break;
386  }
387  return true;
388 }
389 
390 #endif
391 
392 
393 /****************************************************************************/
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
static bool setVariable(const int cmd, const int variable, MSVehicleType &v, TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value for the given type.
virtual SUMOReal getImperfection() const
Get the driver&#39;s imperfection.
Definition: MSCFModel.h:197
#define VAR_EMISSIONCLASS
SUMOReal getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
#define VAR_LENGTH
#define TYPE_COMPOUND
#define RESPONSE_GET_VEHICLETYPE_VARIABLE
virtual void setMaxDecel(SUMOReal decel)
Sets a new value for maximum deceleration [m/s^2].
Definition: MSCFModel.h:305
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
void setShape(SUMOVehicleShape shape)
Set a new value for this type&#39;s shape.
#define VAR_TAU
Structure representing possible vehicle parameter.
void setSpeedFactor(const SUMOReal &factor)
Set a new value for this type&#39;s speed factor.
#define RTYPE_OK
void setLength(const SUMOReal &length)
Set a new value for this type&#39;s length.
static bool getVariable(const int variable, const MSVehicleType &v, tcpip::Storage &tempMsg)
Processes a value request for the given type.
SUMOReal getLength() const
Get vehicle&#39;s length [m].
#define VAR_VEHICLECLASS
#define VAR_SPEED_FACTOR
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void setWidth(const SUMOReal &width)
Set a new value for this type&#39;s width.
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector...
virtual void writeUnsignedByte(int)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
virtual void setMaxAccel(SUMOReal accel)
Sets a new value for maximum acceleration [m/s^2].
Definition: MSCFModel.h:297
#define VAR_SPEED_DEVIATION
virtual void writeInt(int)
The car-following model and parameter.
Definition: MSVehicleType.h:74
#define TYPE_STRING
virtual int readUnsignedByte()
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
std::string getVehicleShapeName(SUMOVehicleShape id)
Returns the class name of the shape class given by its id.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
#define CMD_GET_VEHICLETYPE_VARIABLE
void setSpeedDeviation(const SUMOReal &dev)
Set a new value for this type&#39;s speed deviation.
#define VAR_SHAPECLASS
SUMOReal getSpeedDeviation() const
Returns this type&#39;s speed deviation.
const SUMOVTypeParameter & getParameter() const
#define VAR_ACCEL
virtual int readInt()
SUMOReal getMinGap() const
Get the free space in front of vehicles of this class.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:308
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
#define CMD_SET_VEHICLETYPE_VARIABLE
virtual void writeStringList(const std::vector< std::string > &s)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
#define VAR_IMPERFECTION
virtual std::string readString()
void setEmissionClass(SUMOEmissionClass eclass)
Set a new value for this type&#39;s emission class.
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
SUMOReal getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:186
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
void setMinGap(const SUMOReal &minGap)
Set a new value for this type&#39;s minimum gap.
virtual SUMOReal getHeadwayTime() const
Get the driver&#39;s reaction time [s].
Definition: MSCFModel.h:205
SUMOVehicleShape getGuiShape() const
Get this vehicle type&#39;s shape.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
SUMOReal getMaxAccel() const
Get the vehicle type&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:178
SUMOReal getSpeedFactor() const
Returns this type&#39;s speed factor.
SUMOReal getWidth() const
Get the width which vehicles of this class shall have when being drawn.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State)
virtual void writeString(const std::string &s)
virtual void setImperfection(SUMOReal imperfection)
Sets a new value for driver imperfection.
Definition: MSCFModel.h:313
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:64
const RGBColor & getColor() const
Returns this type&#39;s color.
virtual void writeDouble(double)
void setVClass(SUMOVehicleClass vclass)
Set a new value for this type&#39;s vehicle class.
void setMaxSpeed(const SUMOReal &maxSpeed)
Set a new value for this type&#39;s maximum speed.
void setColor(const RGBColor &color)
Set a new value for this type&#39;s color.
SUMOVehicleShape getVehicleShapeID(const std::string &name)
Returns the class id of the shape class given by its name.
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_MAXSPEED
#define VAR_DECEL
#define VAR_PARAMETER
#define ID_COUNT
SUMOEmissionClass getEmissionClass() const
Get this vehicle type&#39;s emission class.
virtual void setHeadwayTime(SUMOReal headwayTime)
Sets a new value for driver reaction time [s].
Definition: MSCFModel.h:321
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, MTRand *rng=0)
Returns the named vehicle type or a sample from the named distribution.
#define TYPE_INTEGER
#define VAR_MINGAP
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
#define VAR_WIDTH