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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <microsim/MSNet.h>
24 #include <microsim/MSVehicleType.h>
25 #include <libsumo/TraCIConstants.h>
28 #include "Helper.h"
29 #include "VehicleType.h"
30 
31 
32 namespace libsumo {
33 // ===========================================================================
34 // static member initializations
35 // ===========================================================================
36 SubscriptionResults VehicleType::mySubscriptionResults;
37 ContextSubscriptionResults VehicleType::myContextSubscriptionResults;
38 
39 
40 // ===========================================================================
41 // static member definitions
42 // ===========================================================================
43 std::vector<std::string>
44 VehicleType::getIDList() {
45  std::vector<std::string> ids;
47  return ids;
48 }
49 
50 
51 int
52 VehicleType::getIDCount() {
53  return (int)getIDList().size();
54 }
55 
56 
57 double
58 VehicleType::getLength(const std::string& typeID) {
59  return getVType(typeID)->getLength();
60 }
61 
62 
63 double
64 VehicleType::getMaxSpeed(const std::string& typeID) {
65  return getVType(typeID)->getMaxSpeed();
66 }
67 
68 
69 double
70 VehicleType::getActionStepLength(const std::string& typeID) {
71  return getVType(typeID)->getActionStepLengthSecs();
72 }
73 
74 
75 double
76 VehicleType::getSpeedFactor(const std::string& typeID) {
77  return getVType(typeID)->getSpeedFactor().getParameter()[0];
78 }
79 
80 
81 double
82 VehicleType::getSpeedDeviation(const std::string& typeID) {
83  return getVType(typeID)->getSpeedFactor().getParameter()[1];
84 }
85 
86 
87 double
88 VehicleType::getAccel(const std::string& typeID) {
89  return getVType(typeID)->getCarFollowModel().getMaxAccel();
90 }
91 
92 
93 double
94 VehicleType::getDecel(const std::string& typeID) {
95  return getVType(typeID)->getCarFollowModel().getMaxDecel();
96 }
97 
98 
99 double
100 VehicleType::getEmergencyDecel(const std::string& typeID) {
101  return getVType(typeID)->getCarFollowModel().getEmergencyDecel();
102 }
103 
104 
105 double
106 VehicleType::getApparentDecel(const std::string& typeID) {
107  return getVType(typeID)->getCarFollowModel().getApparentDecel();
108 }
109 
110 
111 double
112 VehicleType::getImperfection(const std::string& typeID) {
113  return getVType(typeID)->getCarFollowModel().getImperfection();
114 }
115 
116 
117 double
118 VehicleType::getTau(const std::string& typeID) {
119  return getVType(typeID)->getCarFollowModel().getHeadwayTime();
120 }
121 
122 
123 std::string
124 VehicleType::getVehicleClass(const std::string& typeID) {
125  return toString(getVType(typeID)->getVehicleClass());
126 }
127 
128 
129 std::string
130 VehicleType::getEmissionClass(const std::string& typeID) {
131  return PollutantsInterface::getName(getVType(typeID)->getEmissionClass());
132 }
133 
134 
135 std::string
136 VehicleType::getShapeClass(const std::string& typeID) {
137  return getVehicleShapeName(getVType(typeID)->getGuiShape());
138 }
139 
140 
141 double
142 VehicleType::getMinGap(const std::string& typeID) {
143  return getVType(typeID)->getMinGap();
144 }
145 
146 
147 double
148 VehicleType::getWidth(const std::string& typeID) {
149  return getVType(typeID)->getWidth();
150 }
151 
152 
153 double
154 VehicleType::getHeight(const std::string& typeID) {
155  return getVType(typeID)->getHeight();
156 }
157 
158 
159 TraCIColor
160 VehicleType::getColor(const std::string& typeID) {
161  return Helper::makeTraCIColor(getVType(typeID)->getColor());
162 }
163 
164 
165 double
166 VehicleType::getMinGapLat(const std::string& typeID) {
167  return getVType(typeID)->getMinGapLat();
168 }
169 
170 
171 double
172 VehicleType::getMaxSpeedLat(const std::string& typeID) {
173  return getVType(typeID)->getMaxSpeedLat();
174 }
175 
176 
177 std::string
178 VehicleType::getLateralAlignment(const std::string& typeID) {
179  return toString(getVType(typeID)->getPreferredLateralAlignment());
180 }
181 
182 
183 std::string
184 VehicleType::getParameter(const std::string& typeID, const std::string& key) {
185  return getVType(typeID)->getParameter().getParameter(key, "");
186 }
187 
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) {
233  getVType(typeID)->setEmissionClass(PollutantsInterface::getClassByName(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) {
312  getVType(typeID)->setColor(Helper::makeRGBColor(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) {
330  getVType(typeID)->setPreferredLateralAlignment(SUMOXMLDefinitions::LateralAlignments.get(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>
360 VehicleType::makeWrapper() {
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  return handleVariableWithID(objID, objID, variable, wrapper);
368 }
369 
370 
371 bool
372 VehicleType::handleVariableWithID(const std::string& objID, const std::string& typeID, const int variable, VariableWrapper* wrapper) {
373  switch (variable) {
374  case TRACI_ID_LIST:
375  return wrapper->wrapStringList(objID, variable, getIDList());
376  case ID_COUNT:
377  return wrapper->wrapInt(objID, variable, getIDCount());
378  case VAR_LENGTH:
379  return wrapper->wrapDouble(objID, variable, getLength(typeID));
380  case VAR_HEIGHT:
381  return wrapper->wrapDouble(objID, variable, getHeight(typeID));
382  case VAR_MINGAP:
383  return wrapper->wrapDouble(objID, variable, getMinGap(typeID));
384  case VAR_MAXSPEED:
385  return wrapper->wrapDouble(objID, variable, getMaxSpeed(typeID));
386  case VAR_ACCEL:
387  return wrapper->wrapDouble(objID, variable, getAccel(typeID));
388  case VAR_DECEL:
389  return wrapper->wrapDouble(objID, variable, getDecel(typeID));
390  case VAR_EMERGENCY_DECEL:
391  return wrapper->wrapDouble(objID, variable, getEmergencyDecel(typeID));
392  case VAR_APPARENT_DECEL:
393  return wrapper->wrapDouble(objID, variable, getApparentDecel(typeID));
395  return wrapper->wrapDouble(objID, variable, getActionStepLength(typeID));
396  case VAR_IMPERFECTION:
397  return wrapper->wrapDouble(objID, variable, getImperfection(typeID));
398  case VAR_TAU:
399  return wrapper->wrapDouble(objID, variable, getTau(typeID));
400  case VAR_SPEED_FACTOR:
401  return wrapper->wrapDouble(objID, variable, getSpeedFactor(typeID));
402  case VAR_SPEED_DEVIATION:
403  return wrapper->wrapDouble(objID, variable, getSpeedDeviation(typeID));
404  case VAR_VEHICLECLASS:
405  return wrapper->wrapString(objID, variable, getVehicleClass(typeID));
406  case VAR_EMISSIONCLASS:
407  return wrapper->wrapString(objID, variable, getEmissionClass(typeID));
408  case VAR_SHAPECLASS:
409  return wrapper->wrapString(objID, variable, getShapeClass(typeID));
410  case VAR_WIDTH:
411  return wrapper->wrapDouble(objID, variable, getWidth(typeID));
412  case VAR_COLOR:
413  return wrapper->wrapColor(objID, variable, getColor(typeID));
414  case VAR_MINGAP_LAT:
415  return wrapper->wrapDouble(objID, variable, getMinGapLat(typeID));
416  case VAR_MAXSPEED_LAT:
417  return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(typeID));
418  case VAR_LATALIGNMENT:
419  return wrapper->wrapString(objID, variable, getLateralAlignment(typeID));
420  case VAR_PERSON_CAPACITY:
421  return wrapper->wrapInt(objID, variable, getPersonCapacity(typeID));
422  default:
423  return false;
424  }
425 }
426 
427 }
428 
429 
430 /****************************************************************************/
TRACI_CONST int VAR_APPARENT_DECEL
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:245
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_MAXSPEED_LAT
TRACI_CONST int VAR_ACTIONSTEPLENGTH
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:370
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:102
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
Structure representing possible vehicle parameter.
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.
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_VEHICLECLASS
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:171
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:246
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int VAR_TAU
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
The car-following model and parameter.
Definition: MSVehicleType.h:62
std::string getVehicleShapeName(SUMOVehicleShape id)
Returns the class name of the shape class given by its id.
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_ACCEL
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:381
TRACI_CONST int TRACI_ID_LIST
C++ TraCI client API implementation.
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:44
double getEmergencyDecel() const
Get the vehicle type&#39;s maximal phisically possible deceleration [m/s^2].
Definition: MSCFModel.h:224
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:348
TRACI_CONST int VAR_LENGTH
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:216
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:55
const SUMOVTypeParameter & getParameter() const
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
TRACI_CONST int VAR_EMERGENCY_DECEL
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
TRACI_CONST int VAR_SPEED_FACTOR
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.
TRACI_CONST int VAR_PERSON_CAPACITY
SubParams cfParameter
Car-following parameter.
TRACI_CONST int VAR_SHAPECLASS
SUMOVehicleShape getVehicleShapeID(const std::string &name)
Returns the class id of the shape class given by its name.
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_DECEL