SUMO - Simulation of Urban MObility
VehicleType.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // C++ TraCI client API implementation
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <microsim/MSNet.h>
26 #include <microsim/MSVehicleType.h>
30 #include "Helper.h"
31 #include "VehicleType.h"
32 
33 
34 namespace libsumo {
35 // ===========================================================================
36 // static member initializations
37 // ===========================================================================
40 
41 
42 // ===========================================================================
43 // static member definitions
44 // ===========================================================================
45 std::vector<std::string>
47  std::vector<std::string> ids;
49  return ids;
50 }
51 
52 
53 int
55  return (int)getIDList().size();
56 }
57 
58 
59 double
60 VehicleType::getLength(const std::string& typeID) {
61  return getVType(typeID)->getLength();
62 }
63 
64 
65 double
66 VehicleType::getMaxSpeed(const std::string& typeID) {
67  return getVType(typeID)->getMaxSpeed();
68 }
69 
70 
71 double
72 VehicleType::getActionStepLength(const std::string& typeID) {
73  return getVType(typeID)->getActionStepLengthSecs();
74 }
75 
76 
77 double
78 VehicleType::getSpeedFactor(const std::string& typeID) {
79  return getVType(typeID)->getSpeedFactor().getParameter()[0];
80 }
81 
82 
83 double
84 VehicleType::getSpeedDeviation(const std::string& typeID) {
85  return getVType(typeID)->getSpeedFactor().getParameter()[1];
86 }
87 
88 
89 double
90 VehicleType::getAccel(const std::string& typeID) {
91  return getVType(typeID)->getCarFollowModel().getMaxAccel();
92 }
93 
94 
95 double
96 VehicleType::getDecel(const std::string& typeID) {
97  return getVType(typeID)->getCarFollowModel().getMaxDecel();
98 }
99 
100 
101 double
102 VehicleType::getEmergencyDecel(const std::string& typeID) {
103  return getVType(typeID)->getCarFollowModel().getEmergencyDecel();
104 }
105 
106 
107 double
108 VehicleType::getApparentDecel(const std::string& typeID) {
109  return getVType(typeID)->getCarFollowModel().getApparentDecel();
110 }
111 
112 
113 double
114 VehicleType::getImperfection(const std::string& typeID) {
115  return getVType(typeID)->getCarFollowModel().getImperfection();
116 }
117 
118 
119 double
120 VehicleType::getTau(const std::string& typeID) {
121  return getVType(typeID)->getCarFollowModel().getHeadwayTime();
122 }
123 
124 
125 std::string
126 VehicleType::getVehicleClass(const std::string& typeID) {
127  return toString(getVType(typeID)->getVehicleClass());
128 }
129 
130 
131 std::string
132 VehicleType::getEmissionClass(const std::string& typeID) {
133  return PollutantsInterface::getName(getVType(typeID)->getEmissionClass());
134 }
135 
136 
137 std::string
138 VehicleType::getShapeClass(const std::string& typeID) {
139  return getVehicleShapeName(getVType(typeID)->getGuiShape());
140 }
141 
142 
143 double
144 VehicleType::getMinGap(const std::string& typeID) {
145  return getVType(typeID)->getMinGap();
146 }
147 
148 
149 double
150 VehicleType::getWidth(const std::string& typeID) {
151  return getVType(typeID)->getWidth();
152 }
153 
154 
155 double
156 VehicleType::getHeight(const std::string& typeID) {
157  return getVType(typeID)->getHeight();
158 }
159 
160 
162 VehicleType::getColor(const std::string& typeID) {
163  return Helper::makeTraCIColor(getVType(typeID)->getColor());
164 }
165 
166 
167 double
168 VehicleType::getMinGapLat(const std::string& typeID) {
169  return getVType(typeID)->getMinGapLat();
170 }
171 
172 
173 double
174 VehicleType::getMaxSpeedLat(const std::string& typeID) {
175  return getVType(typeID)->getMaxSpeedLat();
176 }
177 
178 
179 std::string
180 VehicleType::getLateralAlignment(const std::string& typeID) {
181  return toString(getVType(typeID)->getPreferredLateralAlignment());
182 }
183 
184 
185 std::string
186 VehicleType::getParameter(const std::string& typeID, const std::string& key) {
187  return getVType(typeID)->getParameter().getParameter(key, "");
188 }
189 
190 
191 void
192 VehicleType::setLength(const std::string& typeID, double length) {
193  getVType(typeID)->setLength(length);
194 }
195 
196 
197 void
198 VehicleType::setMaxSpeed(const std::string& typeID, double speed) {
199  getVType(typeID)->setMaxSpeed(speed);
200 }
201 
202 
203 void
204 VehicleType::setActionStepLength(const std::string& typeID, double actionStepLength, bool resetActionOffset) {
205  getVType(typeID)->setActionStepLength(SUMOVehicleParserHelper::processActionStepLength(actionStepLength), resetActionOffset);
206 }
207 
208 
209 void
210 VehicleType::setVehicleClass(const std::string& typeID, const std::string& clazz) {
211  getVType(typeID)->setVClass(getVehicleClassID(clazz));
212 }
213 
214 
215 void
216 VehicleType::setSpeedFactor(const std::string& typeID, double factor) {
217  getVType(typeID)->setSpeedFactor(factor);
218 }
219 
220 
221 void
222 VehicleType::setSpeedDeviation(const std::string& typeID, double deviation) {
223  getVType(typeID)->setSpeedDeviation(deviation);
224 }
225 
226 
227 void
228 VehicleType::setEmissionClass(const std::string& typeID, const std::string& clazz) {
230 }
231 
232 
233 void
234 VehicleType::setShapeClass(const std::string& typeID, const std::string& shapeClass) {
235  getVType(typeID)->setShape(getVehicleShapeID(shapeClass));
236 }
237 
238 
239 void
240 VehicleType::setWidth(const std::string& typeID, double width) {
241  getVType(typeID)->setWidth(width);
242 }
243 
244 
245 void
246 VehicleType::setHeight(const std::string& typeID, double height) {
247  getVType(typeID)->setHeight(height);
248 }
249 
250 
251 void
252 VehicleType::setMinGap(const std::string& typeID, double minGap) {
253  getVType(typeID)->setMinGap(minGap);
254 }
255 
256 
257 void
258 VehicleType::setAccel(const std::string& typeID, double accel) {
259  getVType(typeID)->setAccel(accel);
260 }
261 
262 
263 void
264 VehicleType::setDecel(const std::string& typeID, double decel) {
265  MSVehicleType* v = getVType(typeID);
266  v->setDecel(decel);
267  // automatically raise emergencyDecel to ensure it is at least as high as decel
268  if (decel > v->getCarFollowModel().getEmergencyDecel()) {
269  if (v->getParameter().cfParameter.count(SUMO_ATTR_EMERGENCYDECEL) > 0) {
270  // notify user only if emergencyDecel was previously specified
271  WRITE_WARNING("Automatically setting emergencyDecel to " + toString(decel) + " for vType '" + typeID + "' to match decel.");
272  }
273  v->setEmergencyDecel(decel);
274  }
275 }
276 
277 
278 void
279 VehicleType::setEmergencyDecel(const std::string& typeID, double decel) {
280  MSVehicleType* v = getVType(typeID);
281  v->setEmergencyDecel(decel);
282  if (decel < v->getCarFollowModel().getMaxDecel()) {
283  WRITE_WARNING("New value of emergencyDecel (" + toString(decel) + ") is lower than decel (" + toString(v->getCarFollowModel().getMaxDecel()) + ")");
284  }
285 }
286 
287 
288 void
289 VehicleType::setApparentDecel(const std::string& typeID, double decel) {
290  getVType(typeID)->setApparentDecel(decel);
291 }
292 
293 
294 void
295 VehicleType::setImperfection(const std::string& typeID, double imperfection) {
296  getVType(typeID)->setImperfection(imperfection);
297 }
298 
299 
300 void
301 VehicleType::setTau(const std::string& typeID, double tau) {
302  getVType(typeID)->setTau(tau);
303 }
304 
305 
306 void
307 VehicleType::setColor(const std::string& typeID, const TraCIColor& c) {
309 }
310 
311 
312 void
313 VehicleType::setMinGapLat(const std::string& typeID, double minGapLat) {
314  getVType(typeID)->setMinGapLat(minGapLat);
315 }
316 
317 
318 void
319 VehicleType::setMaxSpeedLat(const std::string& typeID, double speed) {
320  getVType(typeID)->setMaxSpeedLat(speed);
321 }
322 
323 
324 void
325 VehicleType::setLateralAlignment(const std::string& typeID, const std::string& latAlignment) {
327 }
328 
329 
330 void
331 VehicleType::copy(const std::string& origTypeID, const std::string& newTypeID) {
332  getVType(origTypeID)->duplicateType(newTypeID, true);
333 }
334 
335 
336 void
337 VehicleType::setParameter(const std::string& typeID, const std::string& name, const std::string& value) {
338  ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).setParameter(name, value);
339 }
340 
341 
343 
344 
346 VehicleType::getVType(std::string id) {
348  if (t == nullptr) {
349  throw TraCIException("Vehicle type '" + id + "' is not known");
350  }
351  return t;
352 }
353 
354 
355 std::shared_ptr<VariableWrapper>
357  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
358 }
359 
360 
361 bool
362 VehicleType::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
363  switch (variable) {
364  case TRACI_ID_LIST:
365  return wrapper->wrapStringList(objID, variable, getIDList());
366  case ID_COUNT:
367  return wrapper->wrapInt(objID, variable, getIDCount());
368  case VAR_LENGTH:
369  return wrapper->wrapDouble(objID, variable, getLength(objID));
370  case VAR_HEIGHT:
371  return wrapper->wrapDouble(objID, variable, getHeight(objID));
372  case VAR_MINGAP:
373  return wrapper->wrapDouble(objID, variable, getMinGap(objID));
374  case VAR_MAXSPEED:
375  return wrapper->wrapDouble(objID, variable, getMaxSpeed(objID));
376  case VAR_ACCEL:
377  return wrapper->wrapDouble(objID, variable, getAccel(objID));
378  case VAR_DECEL:
379  return wrapper->wrapDouble(objID, variable, getDecel(objID));
380  case VAR_EMERGENCY_DECEL:
381  return wrapper->wrapDouble(objID, variable, getEmergencyDecel(objID));
382  case VAR_APPARENT_DECEL:
383  return wrapper->wrapDouble(objID, variable, getApparentDecel(objID));
385  return wrapper->wrapDouble(objID, variable, getActionStepLength(objID));
386  case VAR_IMPERFECTION:
387  return wrapper->wrapDouble(objID, variable, getImperfection(objID));
388  case VAR_TAU:
389  return wrapper->wrapDouble(objID, variable, getTau(objID));
390  case VAR_SPEED_FACTOR:
391  return wrapper->wrapDouble(objID, variable, getSpeedFactor(objID));
392  case VAR_SPEED_DEVIATION:
393  return wrapper->wrapDouble(objID, variable, getSpeedDeviation(objID));
394  case VAR_VEHICLECLASS:
395  return wrapper->wrapString(objID, variable, getVehicleClass(objID));
396  case VAR_EMISSIONCLASS:
397  return wrapper->wrapString(objID, variable, getEmissionClass(objID));
398  case VAR_SHAPECLASS:
399  return wrapper->wrapString(objID, variable, getShapeClass(objID));
400  case VAR_WIDTH:
401  return wrapper->wrapDouble(objID, variable, getWidth(objID));
402  case VAR_COLOR:
403  return wrapper->wrapColor(objID, variable, getColor(objID));
404  case VAR_MINGAP_LAT:
405  return wrapper->wrapDouble(objID, variable, getMinGapLat(objID));
406  case VAR_MAXSPEED_LAT:
407  return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(objID));
408  case VAR_LATALIGNMENT:
409  return wrapper->wrapString(objID, variable, getLateralAlignment(objID));
410  default:
411  return false;
412  }
413 }
414 
415 
416 }
417 
418 
419 /****************************************************************************/
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
double getApparentDecel() const
Get the vehicle type&#39;s apparent deceleration [m/s^2] (the one regarded by its followers.
Definition: MSCFModel.h:234
void setMinGap(const double &minGap)
Set a new value for this type&#39;s minimum gap.
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
#define VAR_EMISSIONCLASS
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
#define VAR_LENGTH
#define VAR_LATALIGNMENT
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:230
void setTau(double tau)
Set a new value for this type&#39;s headway.
void setShape(SUMOVehicleShape shape)
Set a new value for this type&#39;s shape.
#define VAR_TAU
Structure representing possible vehicle parameter.
virtual double getImperfection() const
Get the driver&#39;s imperfection.
Definition: MSCFModel.h:251
std::vector< double > & getParameter()
Returns the parameters of this distribution.
void setDecel(double decel)
Set a new value for this type&#39;s deceleration.
#define VAR_HEIGHT
static ContextSubscriptionResults myContextSubscriptionResults
Definition: VehicleType.h:114
static void setSpeedDeviation(const std::string &typeID, double deviation)
#define VAR_VEHICLECLASS
static LIBSUMO_VEHICLE_TYPE_GETTER std::string getParameter(const std::string &typeID, const std::string &key)
#define VAR_SPEED_FACTOR
#define VAR_COLOR
static SUMOTime processActionStepLength(double given)
Checks and converts given value for the action step length from seconds to miliseconds assuring it be...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
static SubscriptionResults mySubscriptionResults
Definition: VehicleType.h:113
double getMinGapLat() const
Get the minimum lateral gap that vehicles of this type maintain.
void setApparentDecel(double apparentDecel)
Set a new value for this type&#39;s apparent deceleration.
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
void setLength(const double &length)
Set a new value for this type&#39;s length.
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
void setMaxSpeed(const double &maxSpeed)
Set a new value for this type&#39;s maximum speed.
static int getIDCount()
Definition: VehicleType.cpp:54
void setImperfection(double imperfection)
Set a new value for this type&#39;s imperfection.
#define VAR_SPEED_DEVIATION
MSVehicleType * duplicateType(const std::string &id, bool persistent) const
Duplicates the microsim vehicle type giving the newly created type the given id.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
The car-following model and parameter.
Definition: MSVehicleType.h:66
std::string getVehicleShapeName(SUMOVehicleShape id)
Returns the class name of the shape class given by its id.
static LIBSUMO_SUBSCRIPTION_API std::shared_ptr< VariableWrapper > makeWrapper()
double getMaxAccel() const
Get the vehicle type&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:210
void setMinGapLat(const double &minGapLat)
Set a new value for this type&#39;s minimum lataral gap.
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:241
#define VAR_SHAPECLASS
static MSVehicleType * getVType(std::string id)
#define TRACI_ID_LIST
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
#define VAR_ACCEL
void setAccel(double accel)
Set a new value for this type&#39;s acceleration.
#define VAR_ACTIONSTEPLENGTH
double getEmergencyDecel() const
Get the vehicle type&#39;s maximal phisically possible deceleration [m/s^2].
Definition: MSCFModel.h:226
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:316
void setHeight(const double &height)
Set a new value for this type&#39;s height.
#define VAR_EMERGENCY_DECEL
static LIBSUMO_VEHICLE_TYPE_SETTER void copy(const std::string &origTypeID, const std::string &newTypeID)
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
#define VAR_IMPERFECTION
void setEmissionClass(SUMOEmissionClass eclass)
Set a new value for this type&#39;s emission class.
double getMinGap() const
Get the free space in front of vehicles of this class.
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
double getMaxDecel() const
Get the vehicle type&#39;s maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:218
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:52
void setSpeedDeviation(const double &dev)
Set a new value for this type&#39;s speed deviation.
void setSpeedFactor(const double &factor)
Set a new value for this type&#39;s speed factor.
const SUMOVTypeParameter & getParameter() const
Definition: Edge.cpp:30
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector...
void setActionStepLength(const SUMOTime actionStepLength, bool resetActionOffset)
Set a new value for this type&#39;s action step length.
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=0)
Returns the named vehicle type or a sample from the named distribution.
double getMaxSpeedLat() const
Get vehicle&#39;s maximum lateral speed [m/s].
double getWidth() const
Get the width which vehicles of this class shall have when being drawn.
double getHeight() const
Get the height which vehicles of this class shall have when being drawn.
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
void setEmergencyDecel(double emergencyDecel)
Set a new value for this type&#39;s emergency deceleration.
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
virtual bool wrapColor(const std::string &objID, const int variable, const TraCIColor &value)=0
virtual double getHeadwayTime() const
Get the driver&#39;s desired headway [s].
Definition: MSCFModel.h:259
SubParams cfParameter
Car-following parameter.
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
void setWidth(const double &width)
Set a new value for this type&#39;s width.
double getActionStepLengthSecs() const
Returns this type&#39;s default action step length in seconds.
double getLength() const
Get vehicle&#39;s length [m].
void setVClass(SUMOVehicleClass vclass)
Set a new value for this type&#39;s vehicle class.
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.
#define VAR_APPARENT_DECEL
#define VAR_MAXSPEED
#define VAR_MINGAP_LAT
#define VAR_DECEL
#define ID_COUNT
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
void setMaxSpeedLat(const double &maxSpeedLat)
Set a new value for this type&#39;s maximum lateral speed.
void setPreferredLateralAlignment(LateralAlignment latAlignment)
Set vehicle&#39;s preferred lateral alignment.
#define VAR_MINGAP
const Distribution_Parameterized & getSpeedFactor() const
Returns this type&#39;s speed factor.
#define VAR_MAXSPEED_LAT
#define VAR_WIDTH
static std::vector< std::string > getIDList()
Definition: VehicleType.cpp:46