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-2013 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 // used namespaces
49 // ===========================================================================
50 using namespace traci;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 bool
58  tcpip::Storage& outputStorage) {
59  // variable & id
60  int variable = inputStorage.readUnsignedByte();
61  std::string id = inputStorage.readString();
62  // check variable
63  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
64  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
65  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
66  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
67  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
68  }
69  // begin response building
70  tcpip::Storage tempMsg;
71  // response-code, variableID, objectID
73  tempMsg.writeUnsignedByte(variable);
74  tempMsg.writeString(id);
75  // process request
76  if (variable == ID_LIST) {
77  std::vector<std::string> ids;
80  tempMsg.writeStringList(ids);
81  } else if (variable == ID_COUNT) {
82  std::vector<std::string> ids;
85  tempMsg.writeInt((int) ids.size());
86  } else {
88  if (v == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
90  }
91  getVariable(variable, *v, tempMsg);
92  }
93  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
94  server.writeResponseWithLength(outputStorage, tempMsg);
95  return true;
96 }
97 
98 bool
99 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
100  switch (variable) {
101  case VAR_LENGTH:
103  tempMsg.writeDouble(v.getLength());
104  break;
105  case VAR_MINGAP:
107  tempMsg.writeDouble(v.getMinGap());
108  break;
109  case VAR_MAXSPEED:
111  tempMsg.writeDouble(v.getMaxSpeed());
112  break;
113  case VAR_ACCEL:
116  break;
117  case VAR_DECEL:
120  break;
121  case VAR_IMPERFECTION:
124  break;
125  case VAR_TAU:
128  break;
129  case VAR_SPEED_FACTOR:
131  tempMsg.writeDouble(v.getSpeedFactor());
132  break;
133  case VAR_SPEED_DEVIATION:
135  tempMsg.writeDouble(v.getSpeedDeviation());
136  break;
137  case VAR_VEHICLECLASS:
139  tempMsg.writeString(toString(v.getVehicleClass()));
140  break;
141  case VAR_EMISSIONCLASS:
144  break;
145  case VAR_SHAPECLASS:
148  break;
149  case VAR_WIDTH:
151  tempMsg.writeDouble(v.getWidth());
152  break;
153  case VAR_COLOR:
154  tempMsg.writeUnsignedByte(TYPE_COLOR);
155  tempMsg.writeUnsignedByte(v.getColor().red());
156  tempMsg.writeUnsignedByte(v.getColor().green());
157  tempMsg.writeUnsignedByte(v.getColor().blue());
158  tempMsg.writeUnsignedByte(v.getColor().alpha());
159  break;
160  default:
161  break;
162  }
163  return true;
164 }
165 
166 bool
168  tcpip::Storage& outputStorage) {
169  std::string warning = ""; // additional description for response
170  // variable
171  int variable = inputStorage.readUnsignedByte();
172  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
173  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
174  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
175  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
176  && variable != VAR_TAU && variable != VAR_COLOR
177  ) {
178  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable specified", outputStorage);
179  }
180  // id
181  std::string id = inputStorage.readString();
183  if (v == 0) {
184  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
185  }
186  // process
187  try {
188  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
189  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
190  return true;
191  }
192  } catch (ProcessError& e) {
193  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
194  }
195  return false;
196 }
197 
198 
199 bool
200 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
201  MSVehicleType& v, traci::TraCIServer& server,
202  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
203  switch (variable) {
204  case VAR_LENGTH: {
205  double value = 0;
206  if (!server.readTypeCheckingDouble(inputStorage, value)) {
207  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
208  }
209  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
210  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
211  }
212  v.setLength(value);
213  }
214  break;
215  case VAR_MAXSPEED: {
216  double value = 0;
217  if (!server.readTypeCheckingDouble(inputStorage, value)) {
218  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
219  }
220  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
221  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
222  }
223  v.setMaxSpeed(value);
224  }
225  break;
226  case VAR_VEHICLECLASS: {
227  std::string vclass;
228  if (!server.readTypeCheckingString(inputStorage, vclass)) {
229  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
230  }
231  v.setVClass(getVehicleClassID(vclass));
232  }
233  break;
234  case VAR_SPEED_FACTOR: {
235  double value = 0;
236  if (!server.readTypeCheckingDouble(inputStorage, value)) {
237  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
238  }
239  v.setSpeedFactor(value);
240  }
241  break;
242  case VAR_SPEED_DEVIATION: {
243  double value = 0;
244  if (!server.readTypeCheckingDouble(inputStorage, value)) {
245  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
246  }
247  v.setSpeedDeviation(value);
248  }
249  break;
250  case VAR_EMISSIONCLASS: {
251  std::string eclass;
252  if (!server.readTypeCheckingString(inputStorage, eclass)) {
253  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
254  }
256  }
257  break;
258  case VAR_WIDTH: {
259  double value = 0;
260  if (!server.readTypeCheckingDouble(inputStorage, value)) {
261  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
262  }
263  v.setWidth(value);
264  }
265  break;
266  case VAR_MINGAP: {
267  double value = 0;
268  if (!server.readTypeCheckingDouble(inputStorage, value)) {
269  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
270  }
271  v.setMinGap(value);
272  }
273  break;
274  case VAR_SHAPECLASS: {
275  std::string sclass;
276  if (!server.readTypeCheckingString(inputStorage, sclass)) {
277  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
278  }
279  v.setShape(getVehicleShapeID(sclass));
280  }
281  break;
282  case VAR_ACCEL: {
283  double value = 0;
284  if (!server.readTypeCheckingDouble(inputStorage, value)) {
285  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
286  }
287  v.getCarFollowModel().setMaxAccel(value);
288  }
289  break;
290  case VAR_DECEL: {
291  double value = 0;
292  if (!server.readTypeCheckingDouble(inputStorage, value)) {
293  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
294  }
295  v.getCarFollowModel().setMaxDecel(value);
296  }
297  break;
298  case VAR_IMPERFECTION: {
299  double value = 0;
300  if (!server.readTypeCheckingDouble(inputStorage, value)) {
301  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
302  }
304  }
305  break;
306  case VAR_TAU: {
307  double value = 0;
308  if (!server.readTypeCheckingDouble(inputStorage, value)) {
309  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
310  }
312  }
313  break;
314  case VAR_COLOR: {
315  RGBColor col;
316  if (!server.readTypeCheckingColor(inputStorage, col)) {
317  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
318  }
319  v.setColor(col);
320  }
321  break;
322  default:
323  break;
324  }
325  return true;
326 }
327 
328 #endif
329 
330 
331 /****************************************************************************/
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
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&#39;s imperfection.
Definition: MSCFModel.h:176
#define VAR_EMISSIONCLASS
SUMOReal getMaxSpeed() const
Get vehicle&#39;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&#39;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:258
void setShape(SUMOVehicleShape shape)
Set a new value for this type&#39;s shape.
#define VAR_TAU
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.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID)
Returns the named vehicle type or a sample from the named distribution.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
SUMOReal getLength() const
Get vehicle&#39;s length [m].
#define VAR_VEHICLECLASS
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_SPEED_FACTOR
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_COLOR
#define TYPE_STRINGLIST
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)
virtual void setMaxAccel(SUMOReal accel)
Sets a new value for maximum acceleration [m/s^2].
Definition: MSCFModel.h:250
#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
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
#define CMD_GET_VEHICLETYPE_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
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.
static bool setVariable(const int cmd, const int variable, MSVehicleType &v, traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value for the given type.
#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:248
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
#define CMD_SET_VEHICLETYPE_VARIABLE
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
virtual void writeStringList(const std::vector< std::string > &s)
#define VAR_IMPERFECTION
virtual std::string readString()
void setEmissionClass(SUMOEmissionClass eclass)
Set a new value for this type&#39;s emission class.
SUMOReal getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:165
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
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:184
SUMOVehicleShape getGuiShape() const
Get this vehicle type&#39;s shape.
SUMOReal getMaxAccel() const
Get the vehicle type&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:157
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.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
virtual void setImperfection(SUMOReal imperfection)
Sets a new value for driver imperfection.
Definition: MSCFModel.h:266
#define TYPE_DOUBLE
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State)
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
#define VAR_MAXSPEED
#define VAR_DECEL
#define ID_COUNT
SUMOEmissionClass getEmissionClass() const
Get this vehicle type&#39;s emission class.
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
virtual void setHeadwayTime(SUMOReal headwayTime)
Sets a new value for driver reaction time [s].
Definition: MSCFModel.h:274
#define TYPE_INTEGER
#define VAR_MINGAP
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define VAR_WIDTH