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>
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  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
63  }
64  // begin response building
65  tcpip::Storage tempMsg;
66  // response-code, variableID, objectID
68  tempMsg.writeUnsignedByte(variable);
69  tempMsg.writeString(id);
70  // process request
71  if (variable == ID_LIST) {
72  std::vector<std::string> ids;
75  tempMsg.writeStringList(ids);
76  } else if (variable == ID_COUNT) {
77  std::vector<std::string> ids;
80  tempMsg.writeInt((int) ids.size());
81  } else {
83  if (v == 0) {
84  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
85  }
86  getVariable(variable, *v, tempMsg);
87  }
88  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
89  server.writeResponseWithLength(outputStorage, tempMsg);
90  return true;
91 }
92 
93 bool
94 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
95  switch (variable) {
96  case VAR_LENGTH:
98  tempMsg.writeDouble(v.getLength());
99  break;
100  case VAR_MINGAP:
102  tempMsg.writeDouble(v.getMinGap());
103  break;
104  case VAR_MAXSPEED:
106  tempMsg.writeDouble(v.getMaxSpeed());
107  break;
108  case VAR_ACCEL:
111  break;
112  case VAR_DECEL:
115  break;
116  case VAR_IMPERFECTION:
119  break;
120  case VAR_TAU:
123  break;
124  case VAR_SPEED_FACTOR:
126  tempMsg.writeDouble(v.getSpeedFactor());
127  break;
128  case VAR_SPEED_DEVIATION:
130  tempMsg.writeDouble(v.getSpeedDeviation());
131  break;
132  case VAR_VEHICLECLASS:
134  tempMsg.writeString(toString(v.getVehicleClass()));
135  break;
136  case VAR_EMISSIONCLASS:
139  break;
140  case VAR_SHAPECLASS:
143  break;
144  case VAR_WIDTH:
146  tempMsg.writeDouble(v.getWidth());
147  break;
148  case VAR_COLOR:
149  tempMsg.writeUnsignedByte(TYPE_COLOR);
150  tempMsg.writeUnsignedByte(v.getColor().red());
151  tempMsg.writeUnsignedByte(v.getColor().green());
152  tempMsg.writeUnsignedByte(v.getColor().blue());
153  tempMsg.writeUnsignedByte(v.getColor().alpha());
154  break;
155  default:
156  break;
157  }
158  return true;
159 }
160 
161 bool
163  tcpip::Storage& outputStorage) {
164  std::string warning = ""; // additional description for response
165  // variable
166  int variable = inputStorage.readUnsignedByte();
167  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
168  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
169  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
170  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
171  && variable != VAR_TAU && variable != VAR_COLOR
172  ) {
173  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable specified", outputStorage);
174  }
175  // id
176  std::string id = inputStorage.readString();
178  if (v == 0) {
179  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
180  }
181  // process
182  try {
183  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
184  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
185  return true;
186  }
187  } catch (ProcessError& e) {
188  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
189  }
190  return false;
191 }
192 
193 
194 bool
195 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
196  MSVehicleType& v, TraCIServer& server,
197  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
198  switch (variable) {
199  case VAR_LENGTH: {
200  double value = 0;
201  if (!server.readTypeCheckingDouble(inputStorage, value)) {
202  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
203  }
204  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
205  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
206  }
207  v.setLength(value);
208  }
209  break;
210  case VAR_MAXSPEED: {
211  double value = 0;
212  if (!server.readTypeCheckingDouble(inputStorage, value)) {
213  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
214  }
215  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
216  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
217  }
218  v.setMaxSpeed(value);
219  }
220  break;
221  case VAR_VEHICLECLASS: {
222  std::string vclass;
223  if (!server.readTypeCheckingString(inputStorage, vclass)) {
224  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
225  }
226  v.setVClass(getVehicleClassID(vclass));
227  }
228  break;
229  case VAR_SPEED_FACTOR: {
230  double value = 0;
231  if (!server.readTypeCheckingDouble(inputStorage, value)) {
232  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
233  }
234  v.setSpeedFactor(value);
235  }
236  break;
237  case VAR_SPEED_DEVIATION: {
238  double value = 0;
239  if (!server.readTypeCheckingDouble(inputStorage, value)) {
240  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
241  }
242  v.setSpeedDeviation(value);
243  }
244  break;
245  case VAR_EMISSIONCLASS: {
246  std::string eclass;
247  if (!server.readTypeCheckingString(inputStorage, eclass)) {
248  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
249  }
250  try {
252  } catch (InvalidArgument e) {
253  return server.writeErrorStatusCmd(cmd, "Unknown emission class '" + eclass + "'.", outputStorage);
254  }
255  }
256  break;
257  case VAR_WIDTH: {
258  double value = 0;
259  if (!server.readTypeCheckingDouble(inputStorage, value)) {
260  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
261  }
262  v.setWidth(value);
263  }
264  break;
265  case VAR_MINGAP: {
266  double value = 0;
267  if (!server.readTypeCheckingDouble(inputStorage, value)) {
268  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
269  }
270  v.setMinGap(value);
271  }
272  break;
273  case VAR_SHAPECLASS: {
274  std::string sclass;
275  if (!server.readTypeCheckingString(inputStorage, sclass)) {
276  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
277  }
278  v.setShape(getVehicleShapeID(sclass));
279  }
280  break;
281  case VAR_ACCEL: {
282  double value = 0;
283  if (!server.readTypeCheckingDouble(inputStorage, value)) {
284  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
285  }
286  v.getCarFollowModel().setMaxAccel(value);
287  }
288  break;
289  case VAR_DECEL: {
290  double value = 0;
291  if (!server.readTypeCheckingDouble(inputStorage, value)) {
292  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
293  }
294  v.getCarFollowModel().setMaxDecel(value);
295  }
296  break;
297  case VAR_IMPERFECTION: {
298  double value = 0;
299  if (!server.readTypeCheckingDouble(inputStorage, value)) {
300  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
301  }
303  }
304  break;
305  case VAR_TAU: {
306  double value = 0;
307  if (!server.readTypeCheckingDouble(inputStorage, value)) {
308  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
309  }
311  }
312  break;
313  case VAR_COLOR: {
314  RGBColor col;
315  if (!server.readTypeCheckingColor(inputStorage, col)) {
316  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
317  }
318  v.setColor(col);
319  }
320  break;
321  default:
322  break;
323  }
324  return true;
325 }
326 
327 #endif
328 
329 
330 /****************************************************************************/
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:176
#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 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: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)
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:269
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:165
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
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
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
#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