SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
13 // Copyright (C) 2001-2014 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>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSVehicleType.h>
39 #include "TraCIConstants.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 bool
52  tcpip::Storage& outputStorage) {
53  // variable & id
54  int variable = inputStorage.readUnsignedByte();
55  std::string id = inputStorage.readString();
56  // check variable
57  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
58  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
59  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
60  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
61  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
62  }
63  // begin response building
64  tcpip::Storage tempMsg;
65  // response-code, variableID, objectID
67  tempMsg.writeUnsignedByte(variable);
68  tempMsg.writeString(id);
69  // process request
70  if (variable == ID_LIST) {
71  std::vector<std::string> ids;
74  tempMsg.writeStringList(ids);
75  } else if (variable == ID_COUNT) {
76  std::vector<std::string> ids;
79  tempMsg.writeInt((int) ids.size());
80  } else {
82  if (v == 0) {
83  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
84  }
85  getVariable(variable, *v, tempMsg);
86  }
87  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
88  server.writeResponseWithLength(outputStorage, tempMsg);
89  return true;
90 }
91 
92 bool
93 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
94  switch (variable) {
95  case VAR_LENGTH:
97  tempMsg.writeDouble(v.getLength());
98  break;
99  case VAR_MINGAP:
101  tempMsg.writeDouble(v.getMinGap());
102  break;
103  case VAR_MAXSPEED:
105  tempMsg.writeDouble(v.getMaxSpeed());
106  break;
107  case VAR_ACCEL:
110  break;
111  case VAR_DECEL:
114  break;
115  case VAR_IMPERFECTION:
118  break;
119  case VAR_TAU:
122  break;
123  case VAR_SPEED_FACTOR:
125  tempMsg.writeDouble(v.getSpeedFactor());
126  break;
127  case VAR_SPEED_DEVIATION:
129  tempMsg.writeDouble(v.getSpeedDeviation());
130  break;
131  case VAR_VEHICLECLASS:
133  tempMsg.writeString(toString(v.getVehicleClass()));
134  break;
135  case VAR_EMISSIONCLASS:
138  break;
139  case VAR_SHAPECLASS:
142  break;
143  case VAR_WIDTH:
145  tempMsg.writeDouble(v.getWidth());
146  break;
147  case VAR_COLOR:
148  tempMsg.writeUnsignedByte(TYPE_COLOR);
149  tempMsg.writeUnsignedByte(v.getColor().red());
150  tempMsg.writeUnsignedByte(v.getColor().green());
151  tempMsg.writeUnsignedByte(v.getColor().blue());
152  tempMsg.writeUnsignedByte(v.getColor().alpha());
153  break;
154  default:
155  break;
156  }
157  return true;
158 }
159 
160 bool
162  tcpip::Storage& outputStorage) {
163  std::string warning = ""; // additional description for response
164  // variable
165  int variable = inputStorage.readUnsignedByte();
166  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
167  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
168  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
169  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
170  && variable != VAR_TAU && variable != VAR_COLOR
171  ) {
172  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable specified", outputStorage);
173  }
174  // id
175  std::string id = inputStorage.readString();
177  if (v == 0) {
178  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
179  }
180  // process
181  try {
182  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
183  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
184  return true;
185  }
186  } catch (ProcessError& e) {
187  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
188  }
189  return false;
190 }
191 
192 
193 bool
194 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
195  MSVehicleType& v, TraCIServer& server,
196  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
197  switch (variable) {
198  case VAR_LENGTH: {
199  double value = 0;
200  if (!server.readTypeCheckingDouble(inputStorage, value)) {
201  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
202  }
203  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
204  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
205  }
206  v.setLength(value);
207  }
208  break;
209  case VAR_MAXSPEED: {
210  double value = 0;
211  if (!server.readTypeCheckingDouble(inputStorage, value)) {
212  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
213  }
214  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
215  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
216  }
217  v.setMaxSpeed(value);
218  }
219  break;
220  case VAR_VEHICLECLASS: {
221  std::string vclass;
222  if (!server.readTypeCheckingString(inputStorage, vclass)) {
223  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
224  }
225  v.setVClass(getVehicleClassID(vclass));
226  }
227  break;
228  case VAR_SPEED_FACTOR: {
229  double value = 0;
230  if (!server.readTypeCheckingDouble(inputStorage, value)) {
231  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
232  }
233  v.setSpeedFactor(value);
234  }
235  break;
236  case VAR_SPEED_DEVIATION: {
237  double value = 0;
238  if (!server.readTypeCheckingDouble(inputStorage, value)) {
239  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
240  }
241  v.setSpeedDeviation(value);
242  }
243  break;
244  case VAR_EMISSIONCLASS: {
245  std::string eclass;
246  if (!server.readTypeCheckingString(inputStorage, eclass)) {
247  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
248  }
250  }
251  break;
252  case VAR_WIDTH: {
253  double value = 0;
254  if (!server.readTypeCheckingDouble(inputStorage, value)) {
255  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
256  }
257  v.setWidth(value);
258  }
259  break;
260  case VAR_MINGAP: {
261  double value = 0;
262  if (!server.readTypeCheckingDouble(inputStorage, value)) {
263  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
264  }
265  v.setMinGap(value);
266  }
267  break;
268  case VAR_SHAPECLASS: {
269  std::string sclass;
270  if (!server.readTypeCheckingString(inputStorage, sclass)) {
271  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
272  }
273  v.setShape(getVehicleShapeID(sclass));
274  }
275  break;
276  case VAR_ACCEL: {
277  double value = 0;
278  if (!server.readTypeCheckingDouble(inputStorage, value)) {
279  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
280  }
281  v.getCarFollowModel().setMaxAccel(value);
282  }
283  break;
284  case VAR_DECEL: {
285  double value = 0;
286  if (!server.readTypeCheckingDouble(inputStorage, value)) {
287  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
288  }
289  v.getCarFollowModel().setMaxDecel(value);
290  }
291  break;
292  case VAR_IMPERFECTION: {
293  double value = 0;
294  if (!server.readTypeCheckingDouble(inputStorage, value)) {
295  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
296  }
298  }
299  break;
300  case VAR_TAU: {
301  double value = 0;
302  if (!server.readTypeCheckingDouble(inputStorage, value)) {
303  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
304  }
306  }
307  break;
308  case VAR_COLOR: {
309  RGBColor col;
310  if (!server.readTypeCheckingColor(inputStorage, col)) {
311  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
312  }
313  v.setColor(col);
314  }
315  break;
316  default:
317  break;
318  }
319  return true;
320 }
321 
322 #endif
323 
324 
325 /****************************************************************************/
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.
SUMOEmissionClass getVehicleEmissionTypeID(const std::string &name)
Returns the class id of the emission class given by its name.
virtual SUMOReal getImperfection() const
Get the driver's imperfection.
Definition: MSCFModel.h:176
#define VAR_EMISSIONCLASS
SUMOReal getMaxSpeed() const
Get vehicle's maximum speed [m/s].
std::string getVehicleEmissionTypeName(SUMOEmissionClass id)
Returns the class name of the emission class given by its id.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type's vehicle class.
#define VAR_LENGTH
#define RESPONSE_GET_VEHICLETYPE_VARIABLE
virtual void setMaxDecel(SUMOReal decel)
Sets a new value for maximum deceleration [m/s^2].
Definition: MSCFModel.h:265
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
void setSpeedFactor(const SUMOReal &factor)
Set a new value for this type's speed factor.
#define RTYPE_OK
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.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID)
Returns the named vehicle type or a sample from the named distribution.
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:154
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)
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:257
#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.
#define VAR_ACCEL
SUMOReal getMinGap() const
Get the free space in front of vehicles of this class.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:263
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.
SUMOReal getMaxDecel() const
Get the vehicle type's maximum deceleration [m/s^2].
Definition: MSCFModel.h:165
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
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:184
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:157
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:273
#define TYPE_DOUBLE
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 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 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:281
#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