SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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_HEIGHT
63  && variable != VAR_PARAMETER) {
64  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
65  }
66  // begin response building
67  tcpip::Storage tempMsg;
68  // response-code, variableID, objectID
70  tempMsg.writeUnsignedByte(variable);
71  tempMsg.writeString(id);
72  // process request
73  if (variable == ID_LIST) {
74  std::vector<std::string> ids;
77  tempMsg.writeStringList(ids);
78  } else if (variable == ID_COUNT) {
79  std::vector<std::string> ids;
82  tempMsg.writeInt((int) ids.size());
83  } else {
85  if (v == 0) {
86  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
87  }
88  switch (variable) {
89  case VAR_PARAMETER: {
90  std::string paramName = "";
91  if (!server.readTypeCheckingString(inputStorage, paramName)) {
92  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
93  }
95  tempMsg.writeString(v->getParameter().getParameter(paramName, ""));
96  }
97  break;
98  default:
99  getVariable(variable, *v, tempMsg);
100  break;
101  }
102  }
103  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
104  server.writeResponseWithLength(outputStorage, tempMsg);
105  return true;
106 }
107 
108 bool
110  switch (variable) {
111  case VAR_LENGTH:
113  tempMsg.writeDouble(v.getLength());
114  break;
115  case VAR_HEIGHT:
117  tempMsg.writeDouble(v.getHeight());
118  break;
119  case VAR_MINGAP:
121  tempMsg.writeDouble(v.getMinGap());
122  break;
123  case VAR_MAXSPEED:
125  tempMsg.writeDouble(v.getMaxSpeed());
126  break;
127  case VAR_ACCEL:
130  break;
131  case VAR_DECEL:
134  break;
135  case VAR_IMPERFECTION:
138  break;
139  case VAR_TAU:
142  break;
143  case VAR_SPEED_FACTOR:
145  tempMsg.writeDouble(v.getSpeedFactor());
146  break;
147  case VAR_SPEED_DEVIATION:
149  tempMsg.writeDouble(v.getSpeedDeviation());
150  break;
151  case VAR_VEHICLECLASS:
153  tempMsg.writeString(toString(v.getVehicleClass()));
154  break;
155  case VAR_EMISSIONCLASS:
158  break;
159  case VAR_SHAPECLASS:
162  break;
163  case VAR_WIDTH:
165  tempMsg.writeDouble(v.getWidth());
166  break;
167  case VAR_COLOR:
168  tempMsg.writeUnsignedByte(TYPE_COLOR);
169  tempMsg.writeUnsignedByte(v.getColor().red());
170  tempMsg.writeUnsignedByte(v.getColor().green());
171  tempMsg.writeUnsignedByte(v.getColor().blue());
172  tempMsg.writeUnsignedByte(v.getColor().alpha());
173  break;
174  default:
175  break;
176  }
177  return true;
178 }
179 
180 bool
182  tcpip::Storage& outputStorage) {
183  std::string warning = ""; // additional description for response
184  // variable
185  int variable = inputStorage.readUnsignedByte();
186  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
187  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
188  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
189  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
190  && variable != VAR_TAU && variable != VAR_COLOR
191  && variable != VAR_HEIGHT
192  && variable != VAR_PARAMETER
193  ) {
194  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
195  }
196  // id
197  std::string id = inputStorage.readString();
199  if (v == 0) {
200  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
201  }
202  // process
203  try {
204  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
205  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
206  return true;
207  }
208  } catch (ProcessError& e) {
209  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
210  }
211  return false;
212 }
213 
214 
215 bool
216 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
217  MSVehicleType& v, TraCIServer& server,
218  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
219  switch (variable) {
220  case VAR_LENGTH: {
221  double value = 0;
222  if (!server.readTypeCheckingDouble(inputStorage, value)) {
223  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
224  }
225  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
226  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
227  }
228  v.setLength(value);
229  }
230  break;
231  case VAR_HEIGHT: {
232  double value = 0;
233  if (!server.readTypeCheckingDouble(inputStorage, value)) {
234  return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
235  }
236  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
237  return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
238  }
239  v.setHeight(value);
240  }
241  break;
242  case VAR_MAXSPEED: {
243  double value = 0;
244  if (!server.readTypeCheckingDouble(inputStorage, value)) {
245  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
246  }
247  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
248  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
249  }
250  v.setMaxSpeed(value);
251  }
252  break;
253  case VAR_VEHICLECLASS: {
254  std::string vclass;
255  if (!server.readTypeCheckingString(inputStorage, vclass)) {
256  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
257  }
258  try {
259  v.setVClass(getVehicleClassID(vclass));
260  } catch (InvalidArgument e) {
261  return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
262  }
263  }
264  break;
265  case VAR_SPEED_FACTOR: {
266  double value = 0;
267  if (!server.readTypeCheckingDouble(inputStorage, value)) {
268  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
269  }
270  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
271  return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
272  }
273  v.setSpeedFactor(value);
274  }
275  break;
276  case VAR_SPEED_DEVIATION: {
277  double value = 0;
278  if (!server.readTypeCheckingDouble(inputStorage, value)) {
279  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
280  }
281  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
282  return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
283  }
284  v.setSpeedDeviation(value);
285  }
286  break;
287  case VAR_EMISSIONCLASS: {
288  std::string eclass;
289  if (!server.readTypeCheckingString(inputStorage, eclass)) {
290  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
291  }
292  try {
294  } catch (InvalidArgument e) {
295  return server.writeErrorStatusCmd(cmd, "Unknown emission class '" + eclass + "'.", outputStorage);
296  }
297  }
298  break;
299  case VAR_WIDTH: {
300  double value = 0;
301  if (!server.readTypeCheckingDouble(inputStorage, value)) {
302  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
303  }
304  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
305  return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
306  }
307  v.setWidth(value);
308  }
309  break;
310  case VAR_MINGAP: {
311  double value = 0;
312  if (!server.readTypeCheckingDouble(inputStorage, value)) {
313  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
314  }
315  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
316  return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
317  }
318  v.setMinGap(value);
319  }
320  break;
321  case VAR_SHAPECLASS: {
322  std::string sclass;
323  if (!server.readTypeCheckingString(inputStorage, sclass)) {
324  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
325  }
326  try {
327  v.setShape(getVehicleShapeID(sclass));
328  } catch (InvalidArgument e) {
329  return server.writeErrorStatusCmd(cmd, "Unknown vehicle shape " + sclass + "'.", outputStorage);
330  }
331  }
332  break;
333  case VAR_ACCEL: {
334  double value = 0;
335  if (!server.readTypeCheckingDouble(inputStorage, value)) {
336  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
337  }
338  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
339  return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
340  }
341  v.getCarFollowModel().setMaxAccel(value);
342  }
343  break;
344  case VAR_DECEL: {
345  double value = 0;
346  if (!server.readTypeCheckingDouble(inputStorage, value)) {
347  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
348  }
349  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
350  return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
351  }
352  v.getCarFollowModel().setMaxDecel(value);
353  }
354  break;
355  case VAR_IMPERFECTION: {
356  double value = 0;
357  if (!server.readTypeCheckingDouble(inputStorage, value)) {
358  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
359  }
360  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
361  return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
362  }
364  }
365  break;
366  case VAR_TAU: {
367  double value = 0;
368  if (!server.readTypeCheckingDouble(inputStorage, value)) {
369  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
370  }
371  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
372  return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
373  }
375  }
376  break;
377  case VAR_COLOR: {
378  RGBColor col;
379  if (!server.readTypeCheckingColor(inputStorage, col)) {
380  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
381  }
382  v.setColor(col);
383  }
384  break;
385  case VAR_PARAMETER: {
386  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
387  return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.", outputStorage);
388  }
389  //readt itemNo
390  inputStorage.readInt();
391  std::string name;
392  if (!server.readTypeCheckingString(inputStorage, name)) {
393  return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.", outputStorage);
394  }
395  std::string value;
396  if (!server.readTypeCheckingString(inputStorage, value)) {
397  return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.", outputStorage);
398  }
399  ((SUMOVTypeParameter&) v.getParameter()).addParameter(name, value);
400  }
401  break;
402  default:
403  break;
404  }
405  return true;
406 }
407 
408 #endif
409 
410 
411 /****************************************************************************/
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's imperfection.
Definition: MSCFModel.h:212
#define VAR_EMISSIONCLASS
SUMOReal getMaxSpeed() const
Get vehicle's maximum speed [m/s].
SUMOVehicleClass getVehicleClass() const
Get this vehicle type'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:399
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's shape.
#define VAR_TAU
Structure representing possible vehicle parameter.
void setSpeedFactor(const SUMOReal &factor)
Set a new value for this type's speed factor.
#define RTYPE_OK
#define VAR_HEIGHT
void setLength(const SUMOReal &length)
Set a new value for this type'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'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:159
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'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)
SUMOReal getHeight() const
Get the height which vehicles of this class shall have when being drawn.
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:391
#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'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's speed deviation.
#define VAR_SHAPECLASS
SUMOReal getSpeedDeviation() const
Returns this type'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:307
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'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's maximum deceleration [m/s^2].
Definition: MSCFModel.h:201
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
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's minimum gap.
virtual SUMOReal getHeadwayTime() const
Get the driver's reaction time [s].
Definition: MSCFModel.h:220
SUMOVehicleShape getGuiShape() const
Get this vehicle type's shape.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
SUMOReal getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition: MSCFModel.h:193
SUMOReal getSpeedFactor() const
Returns this type'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:407
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:65
const RGBColor & getColor() const
Returns this type's color.
virtual void writeDouble(double)
void setVClass(SUMOVehicleClass vclass)
Set a new value for this type's vehicle class.
void setHeight(const SUMOReal &height)
Set a new value for this type's height.
void setMaxSpeed(const SUMOReal &maxSpeed)
Set a new value for this type's maximum speed.
void setColor(const RGBColor &color)
Set a new value for this type'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's emission class.
virtual void setHeadwayTime(SUMOReal headwayTime)
Sets a new value for driver reaction time [s].
Definition: MSCFModel.h:415
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