Eclipse 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-2019 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>
27 #include <libsumo/TraCIConstants.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 int
191 VehicleType::getPersonCapacity(const std::string& typeID) {
192  return getVType(typeID)->getPersonCapacity();
193 }
194 
195 void
196 VehicleType::setLength(const std::string& typeID, double length) {
197  getVType(typeID)->setLength(length);
198 }
199 
200 
201 void
202 VehicleType::setMaxSpeed(const std::string& typeID, double speed) {
203  getVType(typeID)->setMaxSpeed(speed);
204 }
205 
206 
207 void
208 VehicleType::setActionStepLength(const std::string& typeID, double actionStepLength, bool resetActionOffset) {
209  getVType(typeID)->setActionStepLength(SUMOVehicleParserHelper::processActionStepLength(actionStepLength), resetActionOffset);
210 }
211 
212 
213 void
214 VehicleType::setVehicleClass(const std::string& typeID, const std::string& clazz) {
215  getVType(typeID)->setVClass(getVehicleClassID(clazz));
216 }
217 
218 
219 void
220 VehicleType::setSpeedFactor(const std::string& typeID, double factor) {
221  getVType(typeID)->setSpeedFactor(factor);
222 }
223 
224 
225 void
226 VehicleType::setSpeedDeviation(const std::string& typeID, double deviation) {
227  getVType(typeID)->setSpeedDeviation(deviation);
228 }
229 
230 
231 void
232 VehicleType::setEmissionClass(const std::string& typeID, const std::string& clazz) {
234 }
235 
236 
237 void
238 VehicleType::setShapeClass(const std::string& typeID, const std::string& shapeClass) {
239  getVType(typeID)->setShape(getVehicleShapeID(shapeClass));
240 }
241 
242 
243 void
244 VehicleType::setWidth(const std::string& typeID, double width) {
245  getVType(typeID)->setWidth(width);
246 }
247 
248 
249 void
250 VehicleType::setHeight(const std::string& typeID, double height) {
251  getVType(typeID)->setHeight(height);
252 }
253 
254 
255 void
256 VehicleType::setMinGap(const std::string& typeID, double minGap) {
257  getVType(typeID)->setMinGap(minGap);
258 }
259 
260 
261 void
262 VehicleType::setAccel(const std::string& typeID, double accel) {
263  getVType(typeID)->setAccel(accel);
264 }
265 
266 
267 void
268 VehicleType::setDecel(const std::string& typeID, double decel) {
269  MSVehicleType* v = getVType(typeID);
270  v->setDecel(decel);
271  // automatically raise emergencyDecel to ensure it is at least as high as decel
272  if (decel > v->getCarFollowModel().getEmergencyDecel()) {
273  if (v->getParameter().cfParameter.count(SUMO_ATTR_EMERGENCYDECEL) > 0) {
274  // notify user only if emergencyDecel was previously specified
275  WRITE_WARNING("Automatically setting emergencyDecel to " + toString(decel) + " for vType '" + typeID + "' to match decel.");
276  }
277  v->setEmergencyDecel(decel);
278  }
279 }
280 
281 
282 void
283 VehicleType::setEmergencyDecel(const std::string& typeID, double decel) {
284  MSVehicleType* v = getVType(typeID);
285  v->setEmergencyDecel(decel);
286  if (decel < v->getCarFollowModel().getMaxDecel()) {
287  WRITE_WARNING("New value of emergencyDecel (" + toString(decel) + ") is lower than decel (" + toString(v->getCarFollowModel().getMaxDecel()) + ")");
288  }
289 }
290 
291 
292 void
293 VehicleType::setApparentDecel(const std::string& typeID, double decel) {
294  getVType(typeID)->setApparentDecel(decel);
295 }
296 
297 
298 void
299 VehicleType::setImperfection(const std::string& typeID, double imperfection) {
300  getVType(typeID)->setImperfection(imperfection);
301 }
302 
303 
304 void
305 VehicleType::setTau(const std::string& typeID, double tau) {
306  getVType(typeID)->setTau(tau);
307 }
308 
309 
310 void
311 VehicleType::setColor(const std::string& typeID, const TraCIColor& c) {
313 }
314 
315 
316 void
317 VehicleType::setMinGapLat(const std::string& typeID, double minGapLat) {
318  getVType(typeID)->setMinGapLat(minGapLat);
319 }
320 
321 
322 void
323 VehicleType::setMaxSpeedLat(const std::string& typeID, double speed) {
324  getVType(typeID)->setMaxSpeedLat(speed);
325 }
326 
327 
328 void
329 VehicleType::setLateralAlignment(const std::string& typeID, const std::string& latAlignment) {
331 }
332 
333 
334 void
335 VehicleType::copy(const std::string& origTypeID, const std::string& newTypeID) {
336  getVType(origTypeID)->duplicateType(newTypeID, true);
337 }
338 
339 
340 void
341 VehicleType::setParameter(const std::string& typeID, const std::string& name, const std::string& value) {
342  ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).setParameter(name, value);
343 }
344 
345 
347 
348 
350 VehicleType::getVType(std::string id) {
352  if (t == nullptr) {
353  throw TraCIException("Vehicle type '" + id + "' is not known");
354  }
355  return t;
356 }
357 
358 
359 std::shared_ptr<VariableWrapper>
361  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
362 }
363 
364 
365 bool
366 VehicleType::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
367  switch (variable) {
368  case TRACI_ID_LIST:
369  return wrapper->wrapStringList(objID, variable, getIDList());
370  case ID_COUNT:
371  return wrapper->wrapInt(objID, variable, getIDCount());
372  case VAR_LENGTH:
373  return wrapper->wrapDouble(objID, variable, getLength(objID));
374  case VAR_HEIGHT:
375  return wrapper->wrapDouble(objID, variable, getHeight(objID));
376  case VAR_MINGAP:
377  return wrapper->wrapDouble(objID, variable, getMinGap(objID));
378  case VAR_MAXSPEED:
379  return wrapper->wrapDouble(objID, variable, getMaxSpeed(objID));
380  case VAR_ACCEL:
381  return wrapper->wrapDouble(objID, variable, getAccel(objID));
382  case VAR_DECEL:
383  return wrapper->wrapDouble(objID, variable, getDecel(objID));
384  case VAR_EMERGENCY_DECEL:
385  return wrapper->wrapDouble(objID, variable, getEmergencyDecel(objID));
386  case VAR_APPARENT_DECEL:
387  return wrapper->wrapDouble(objID, variable, getApparentDecel(objID));
389  return wrapper->wrapDouble(objID, variable, getActionStepLength(objID));
390  case VAR_IMPERFECTION:
391  return wrapper->wrapDouble(objID, variable, getImperfection(objID));
392  case VAR_TAU:
393  return wrapper->wrapDouble(objID, variable, getTau(objID));
394  case VAR_SPEED_FACTOR:
395  return wrapper->wrapDouble(objID, variable, getSpeedFactor(objID));
396  case VAR_SPEED_DEVIATION:
397  return wrapper->wrapDouble(objID, variable, getSpeedDeviation(objID));
398  case VAR_VEHICLECLASS:
399  return wrapper->wrapString(objID, variable, getVehicleClass(objID));
400  case VAR_EMISSIONCLASS:
401  return wrapper->wrapString(objID, variable, getEmissionClass(objID));
402  case VAR_SHAPECLASS:
403  return wrapper->wrapString(objID, variable, getShapeClass(objID));
404  case VAR_WIDTH:
405  return wrapper->wrapDouble(objID, variable, getWidth(objID));
406  case VAR_COLOR:
407  return wrapper->wrapColor(objID, variable, getColor(objID));
408  case VAR_MINGAP_LAT:
409  return wrapper->wrapDouble(objID, variable, getMinGapLat(objID));
410  case VAR_MAXSPEED_LAT:
411  return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(objID));
412  case VAR_LATALIGNMENT:
413  return wrapper->wrapString(objID, variable, getLateralAlignment(objID));
414  case VAR_PERSON_CAPACITY:
415  return wrapper->wrapInt(objID, variable, getPersonCapacity(objID));
416  default:
417  return false;
418  }
419 }
420 
421 
422 }
423 
424 
425 /****************************************************************************/
TRACI_CONST int VAR_APPARENT_DECEL
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:204
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_MAXSPEED_LAT
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
TRACI_CONST int VAR_ACTIONSTEPLENGTH
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:247
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
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.
Structure representing possible vehicle parameter.
virtual double getImperfection() const
Get the driver&#39;s imperfection.
Definition: MSCFModel.h:251
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=nullptr)
Returns the named vehicle type or a sample from the named distribution.
void setDecel(double decel)
Set a new value for this type&#39;s deceleration.
static ContextSubscriptionResults myContextSubscriptionResults
Definition: VehicleType.h:115
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_VEHICLECLASS
static void setSpeedDeviation(const std::string &typeID, double deviation)
static LIBSUMO_VEHICLE_TYPE_GETTER std::string getParameter(const std::string &typeID, const std::string &key)
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:168
std::vector< double > & getParameter()
Returns the parameters of this distribution.
static SubscriptionResults mySubscriptionResults
Definition: VehicleType.h:114
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:205
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
TRACI_CONST int VAR_MAXSPEED
void setMaxSpeed(const double &maxSpeed)
Set a new value for this type&#39;s maximum speed.
static int getIDCount()
Definition: VehicleType.cpp:54
TRACI_CONST int VAR_TAU
void setImperfection(double imperfection)
Set a new value for this type&#39;s imperfection.
int getPersonCapacity() const
Get this vehicle type&#39;s person capacity.
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:239
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.
TRACI_CONST int VAR_LATALIGNMENT
static LIBSUMO_SUBSCRIPTION_API std::shared_ptr< VariableWrapper > makeWrapper()
TRACI_CONST int VAR_ACCEL
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:258
TRACI_CONST int TRACI_ID_LIST
static MSVehicleType * getVType(std::string id)
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
TRACI_CONST int ID_COUNT
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
void setAccel(double accel)
Set a new value for this type&#39;s acceleration.
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:337
void setHeight(const double &height)
Set a new value for this type&#39;s height.
static LIBSUMO_VEHICLE_TYPE_SETTER void copy(const std::string &origTypeID, const std::string &newTypeID)
TRACI_CONST int VAR_LENGTH
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
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:51
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...
TRACI_CONST int VAR_SPEED_DEVIATION
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
TRACI_CONST int VAR_EMERGENCY_DECEL
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)
TRACI_CONST int VAR_SPEED_FACTOR
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
TRACI_CONST int VAR_PERSON_CAPACITY
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.
TRACI_CONST int VAR_SHAPECLASS
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.
TRACI_CONST int VAR_HEIGHT
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
TRACI_CONST int VAR_IMPERFECTION
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.
TRACI_CONST int VAR_DECEL
const Distribution_Parameterized & getSpeedFactor() const
Returns this type&#39;s speed factor.
static std::vector< std::string > getIDList()
Definition: VehicleType.cpp:46