Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/VehicleType.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2025 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>
28#include "Helper.h"
29#include "VehicleType.h"
30
31
32namespace libsumo {
33// ===========================================================================
34// static member initializations
35// ===========================================================================
36SubscriptionResults VehicleType::mySubscriptionResults;
37ContextSubscriptionResults VehicleType::myContextSubscriptionResults;
38
39
40// ===========================================================================
41// static member definitions
42// ===========================================================================
43std::vector<std::string>
44VehicleType::getIDList() {
45 std::vector<std::string> ids;
47 return ids;
48}
49
50
51int
52VehicleType::getIDCount() {
53 return (int)getIDList().size();
54}
55
56
57double
58VehicleType::getLength(const std::string& typeID) {
59 return getVType(typeID)->getLength();
60}
61
62
63double
64VehicleType::getMaxSpeed(const std::string& typeID) {
65 return getVType(typeID)->getMaxSpeed();
66}
67
68
69double
70VehicleType::getActionStepLength(const std::string& typeID) {
71 return getVType(typeID)->getActionStepLengthSecs();
72}
73
74
75double
76VehicleType::getSpeedFactor(const std::string& typeID) {
77 return getVType(typeID)->getSpeedFactor().getParameter(0);
78}
79
80
81double
82VehicleType::getSpeedDeviation(const std::string& typeID) {
83 return getVType(typeID)->getSpeedFactor().getParameter(1);
84}
85
86
87double
88VehicleType::getAccel(const std::string& typeID) {
89 return getVType(typeID)->getCarFollowModel().getMaxAccel();
90}
91
92
93double
94VehicleType::getDecel(const std::string& typeID) {
95 return getVType(typeID)->getCarFollowModel().getMaxDecel();
96}
97
98
99double
100VehicleType::getEmergencyDecel(const std::string& typeID) {
101 return getVType(typeID)->getCarFollowModel().getEmergencyDecel();
102}
103
104
105double
106VehicleType::getApparentDecel(const std::string& typeID) {
107 return getVType(typeID)->getCarFollowModel().getApparentDecel();
108}
109
110
111double
112VehicleType::getImperfection(const std::string& typeID) {
113 return getVType(typeID)->getCarFollowModel().getImperfection();
114}
115
116
117double
118VehicleType::getTau(const std::string& typeID) {
119 return getVType(typeID)->getCarFollowModel().getHeadwayTime();
120}
121
122
123std::string
124VehicleType::getVehicleClass(const std::string& typeID) {
125 return toString(getVType(typeID)->getVehicleClass());
126}
127
128
129std::string
130VehicleType::getEmissionClass(const std::string& typeID) {
131 return PollutantsInterface::getName(getVType(typeID)->getEmissionClass());
132}
133
134
135std::string
136VehicleType::getShapeClass(const std::string& typeID) {
137 return getVehicleShapeName(getVType(typeID)->getGuiShape());
138}
139
140
141double
142VehicleType::getMinGap(const std::string& typeID) {
143 return getVType(typeID)->getMinGap();
144}
145
146
147double
148VehicleType::getWidth(const std::string& typeID) {
149 return getVType(typeID)->getWidth();
150}
151
152
153double
154VehicleType::getHeight(const std::string& typeID) {
155 return getVType(typeID)->getHeight();
156}
157
158
159double
160VehicleType::getMass(const std::string& typeID) {
161 return getVType(typeID)->getMass();
162}
163
164
166VehicleType::getColor(const std::string& typeID) {
167 return Helper::makeTraCIColor(getVType(typeID)->getColor());
168}
169
170
171double
172VehicleType::getMinGapLat(const std::string& typeID) {
173 return getVType(typeID)->getMinGapLat();
174}
175
176
177double
178VehicleType::getMaxSpeedLat(const std::string& typeID) {
179 return getVType(typeID)->getMaxSpeedLat();
180}
181
182
183std::string
184VehicleType::getLateralAlignment(const std::string& typeID) {
185 if (getVType(typeID)->getPreferredLateralAlignment() != LatAlignmentDefinition::GIVEN) {
186 return toString(getVType(typeID)->getPreferredLateralAlignment());
187 } else {
188 return toString(getVType(typeID)->getPreferredLateralAlignmentOffset());
189 }
190}
191
192
193std::string
194VehicleType::getParameter(const std::string& typeID, const std::string& key) {
195 if (StringUtils::startsWith(key, "junctionModel.")) {
196 const std::string attrName = key.substr(14);
197 if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
198 throw TraCIException("Invalid junctionModel parameter '" + key + "' for type '" + typeID + "'");
199 }
202 throw TraCIException("Invalid junctionModel parameter '" + key + "' for type '" + typeID + "'");
203 }
204 if (getVType(typeID)->getParameter().jmParameter.count(attr) != 0) {
205 return getVType(typeID)->getParameter().jmParameter.find(attr)->second;
206 } else {
207 return "";
208 }
209 } else {
210 return getVType(typeID)->getParameter().getParameter(key, "");
211 }
212}
213
215
216int
217VehicleType::getPersonCapacity(const std::string& typeID) {
218 return getVType(typeID)->getPersonCapacity();
219}
220
221double
222VehicleType::getScale(const std::string& typeID) {
223 return getVType(typeID)->getParameter().scale;
224}
225
226double
227VehicleType::getBoardingDuration(const std::string& typeID) {
228 return STEPS2TIME(getVType(typeID)->getBoardingDuration(true));
229}
230
231double
232VehicleType::getImpatience(const std::string& typeID) {
233 return getVType(typeID)->getImpatience();
234}
235
236void
237VehicleType::setLength(const std::string& typeID, double length) {
238 getVType(typeID)->setLength(length);
239}
240
241
242void
243VehicleType::setMaxSpeed(const std::string& typeID, double speed) {
244 getVType(typeID)->setMaxSpeed(speed);
245}
246
247
248void
249VehicleType::setActionStepLength(const std::string& typeID, double actionStepLength, bool resetActionOffset) {
250 getVType(typeID)->setActionStepLength(SUMOVehicleParserHelper::processActionStepLength(actionStepLength), resetActionOffset);
251}
252
253
254void
255VehicleType::setBoardingDuration(const std::string& typeID, double boardingDuration) {
256 try {
257 checkTimeBounds(boardingDuration);
258 } catch (ProcessError&) {
259 throw TraCIException("BoardingDuration parameter exceeds the time value range.");
260 }
261 getVType(typeID)->setBoardingDuration(TIME2STEPS(boardingDuration), true);
262}
263
264
265void
266VehicleType::setImpatience(const std::string& typeID, double impatience) {
267 getVType(typeID)->setImpatience(impatience);
268}
269
270
271void
272VehicleType::setVehicleClass(const std::string& typeID, const std::string& clazz) {
273 getVType(typeID)->setVClass(getVehicleClassID(clazz));
274}
275
276
277void
278VehicleType::setSpeedFactor(const std::string& typeID, double factor) {
279 getVType(typeID)->setSpeedFactor(factor);
280}
281
282
283void
284VehicleType::setSpeedDeviation(const std::string& typeID, double deviation) {
285 getVType(typeID)->setSpeedDeviation(deviation);
286}
287
288
289void
290VehicleType::setEmissionClass(const std::string& typeID, const std::string& clazz) {
291 getVType(typeID)->setEmissionClass(PollutantsInterface::getClassByName(clazz));
292}
293
294
295void
296VehicleType::setShapeClass(const std::string& typeID, const std::string& shapeClass) {
297 getVType(typeID)->setShape(getVehicleShapeID(shapeClass));
298}
299
300
301void
302VehicleType::setWidth(const std::string& typeID, double width) {
303 getVType(typeID)->setWidth(width);
304}
305
306
307void
308VehicleType::setHeight(const std::string& typeID, double height) {
309 getVType(typeID)->setHeight(height);
310}
311
312
313void
314VehicleType::setMass(const std::string& typeID, double mass) {
315 getVType(typeID)->setMass(mass);
316}
317
318
319void
320VehicleType::setMinGap(const std::string& typeID, double minGap) {
321 getVType(typeID)->setMinGap(minGap);
322}
323
324
325void
326VehicleType::setAccel(const std::string& typeID, double accel) {
327 getVType(typeID)->setAccel(accel);
328}
329
330
331void
332VehicleType::setDecel(const std::string& typeID, double decel) {
333 MSVehicleType* v = getVType(typeID);
334 v->setDecel(decel);
335 // automatically raise emergencyDecel to ensure it is at least as high as decel
336 if (decel > v->getCarFollowModel().getEmergencyDecel()) {
338 // notify user only if emergencyDecel was previously specified
339 WRITE_WARNINGF(TL("Automatically setting emergencyDecel to % for vType '%' to match decel."), toString(decel), typeID);
340 }
341 v->setEmergencyDecel(decel);
342 }
343}
344
345
346void
347VehicleType::setEmergencyDecel(const std::string& typeID, double decel) {
348 MSVehicleType* v = getVType(typeID);
349 v->setEmergencyDecel(decel);
350 if (decel < v->getCarFollowModel().getMaxDecel()) {
351 WRITE_WARNINGF(TL("New value of emergencyDecel (%) is lower than decel (%)"), toString(decel), toString(v->getCarFollowModel().getMaxDecel()));
352 }
353}
354
355
356void
357VehicleType::setApparentDecel(const std::string& typeID, double decel) {
358 getVType(typeID)->setApparentDecel(decel);
359}
360
361
362void
363VehicleType::setImperfection(const std::string& typeID, double imperfection) {
364 getVType(typeID)->setImperfection(imperfection);
365}
366
367
368void
369VehicleType::setTau(const std::string& typeID, double tau) {
370 getVType(typeID)->setTau(tau);
371}
372
373
374void
375VehicleType::setColor(const std::string& typeID, const TraCIColor& c) {
376 getVType(typeID)->setColor(Helper::makeRGBColor(c));
377}
378
379
380void
381VehicleType::setMinGapLat(const std::string& typeID, double minGapLat) {
382 getVType(typeID)->setMinGapLat(minGapLat);
383}
384
385
386void
387VehicleType::setMaxSpeedLat(const std::string& typeID, double speed) {
388 getVType(typeID)->setMaxSpeedLat(speed);
389}
390
391
392void
393VehicleType::setLateralAlignment(const std::string& typeID, const std::string& latAlignment) {
394 double lao;
396 if (SUMOVTypeParameter::parseLatAlignment(latAlignment, lao, lad)) {
397 getVType(typeID)->setPreferredLateralAlignment(lad, lao);
398 } else {
399 throw TraCIException("Unknown value '" + latAlignment + "' when setting latAlignment for vType '" + typeID + "';\n must be one of (\"right\", \"center\", \"arbitrary\", \"nice\", \"compact\", \"left\" or a float)");
400 }
401}
402
403void
404VehicleType::setScale(const std::string& typeID, double value) {
405 getVType(typeID)->setScale(value);
406}
407
408void
409VehicleType::copy(const std::string& origTypeID, const std::string& newTypeID) {
410 getVType(origTypeID)->duplicateType(newTypeID, true);
411}
412
413
414void
415VehicleType::setParameter(const std::string& typeID, const std::string& name, const std::string& value) {
416 if (StringUtils::startsWith(name, "junctionModel.")) {
417 const std::string attrName = name.substr(14);
418 if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
419 throw TraCIException("Invalid junctionModel parameter '" + name + "' for type '" + typeID + "'");
420 }
422 if (SUMOVTypeParameter::AllowedJMAttrs.count(attr) == 0) {
423 throw TraCIException("Invalid junctionModel parameter '" + name + "' for type '" + typeID + "'");
424 }
425 try {
426 StringUtils::toDouble(value); // check number format
427 ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).jmParameter[attr] = value;
428 } catch (NumberFormatException&) {
429 throw TraCIException("Invalid junctionModel parameter value '" + value + "' for type '" + typeID + " (should be numeric)'");
430 }
431 } else {
432 ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).setParameter(name, value);
433 }
434}
435
436
437LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(VehicleType, VEHICLETYPE)
438
439
440MSVehicleType*
441VehicleType::getVType(std::string id) {
443 if (t == nullptr) {
444 throw TraCIException("Vehicle type '" + id + "' is not known");
445 }
446 return t;
447}
448
449
450std::shared_ptr<VariableWrapper>
452 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
453}
454
455
456bool
457VehicleType::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
458 return handleVariableWithID(objID, objID, variable, wrapper, paramData);
459}
460
461
462bool
463VehicleType::handleVariableWithID(const std::string& objID, const std::string& typeID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
464 switch (variable) {
465 case TRACI_ID_LIST:
466 return wrapper->wrapStringList(objID, variable, getIDList());
467 case ID_COUNT:
468 return wrapper->wrapInt(objID, variable, getIDCount());
469 case VAR_LENGTH:
470 return wrapper->wrapDouble(objID, variable, getLength(typeID));
471 case VAR_HEIGHT:
472 return wrapper->wrapDouble(objID, variable, getHeight(typeID));
473 case VAR_MASS:
474 return wrapper->wrapDouble(objID, variable, getMass(typeID));
475 case VAR_MINGAP:
476 return wrapper->wrapDouble(objID, variable, getMinGap(typeID));
477 case VAR_MAXSPEED:
478 return wrapper->wrapDouble(objID, variable, getMaxSpeed(typeID));
479 case VAR_ACCEL:
480 return wrapper->wrapDouble(objID, variable, getAccel(typeID));
481 case VAR_DECEL:
482 return wrapper->wrapDouble(objID, variable, getDecel(typeID));
484 return wrapper->wrapDouble(objID, variable, getEmergencyDecel(typeID));
486 return wrapper->wrapDouble(objID, variable, getApparentDecel(typeID));
488 return wrapper->wrapDouble(objID, variable, getActionStepLength(typeID));
489 case VAR_IMPERFECTION:
490 return wrapper->wrapDouble(objID, variable, getImperfection(typeID));
491 case VAR_TAU:
492 return wrapper->wrapDouble(objID, variable, getTau(typeID));
493 case VAR_SPEED_FACTOR:
494 return wrapper->wrapDouble(objID, variable, getSpeedFactor(typeID));
496 return wrapper->wrapDouble(objID, variable, getSpeedDeviation(typeID));
497 case VAR_VEHICLECLASS:
498 return wrapper->wrapString(objID, variable, getVehicleClass(typeID));
500 return wrapper->wrapString(objID, variable, getEmissionClass(typeID));
501 case VAR_SHAPECLASS:
502 return wrapper->wrapString(objID, variable, getShapeClass(typeID));
503 case VAR_WIDTH:
504 return wrapper->wrapDouble(objID, variable, getWidth(typeID));
505 case VAR_COLOR:
506 return wrapper->wrapColor(objID, variable, getColor(typeID));
507 case VAR_MINGAP_LAT:
508 return wrapper->wrapDouble(objID, variable, getMinGapLat(typeID));
509 case VAR_MAXSPEED_LAT:
510 return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(typeID));
511 case VAR_LATALIGNMENT:
512 return wrapper->wrapString(objID, variable, getLateralAlignment(typeID));
514 return wrapper->wrapInt(objID, variable, getPersonCapacity(typeID));
516 return wrapper->wrapDouble(objID, variable, getBoardingDuration(typeID));
517 case VAR_IMPATIENCE:
518 return wrapper->wrapDouble(objID, variable, getImpatience(typeID));
519 case VAR_SCALE:
520 return wrapper->wrapDouble(objID, variable, getScale(typeID));
522 paramData->readUnsignedByte();
523 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
525 paramData->readUnsignedByte();
526 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
527 default:
528 return false;
529 }
530}
531
532}
533
534
535/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:304
const std::string invalid_return< std::string >::value
void checkTimeBounds(const double time)
check the valid SUMOTime range of double input and throw an error if out of bounds
Definition SUMOTime.cpp:173
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define TIME2STEPS(x)
Definition SUMOTime.h:57
LatAlignmentDefinition
Possible ways to choose the lateral alignment, i.e., how vehicles align themselves within their lane.
@ GIVEN
The alignment as offset is given.
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
SUMOVehicleShape getVehicleShapeID(const std::string &name)
Returns the class id of the shape class given by its name.
std::string getVehicleShapeName(SUMOVehicleShape id)
Returns the class name of the shape class given by its id.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_EMERGENCYDECEL
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
static void setSpeedDeviation(const std::string &typeID, double deviation)
static void setScale(const std::string &typeID, double value)
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper, tcpip::Storage *paramData)
static std::shared_ptr< VariableWrapper > makeWrapper()
static double getScale(const std::string &typeID)
static LIBSUMO_SUBSCRIPTION_API MSVehicleType * getVType(std::string id)
static SubscriptionResults mySubscriptionResults
LIBSUMO_VEHICLE_TYPE_GETTER LIBSUMO_ID_PARAMETER_API static LIBSUMO_VEHICLE_TYPE_SETTER void copy(const std::string &origTypeID, const std::string &newTypeID)
static ContextSubscriptionResults myContextSubscriptionResults
static bool handleVariableWithID(const std::string &objID, const std::string &typeID, const int variable, VariableWrapper *wrapper, tcpip::Storage *paramData)
double getEmergencyDecel() const
Get the vehicle type's maximal physically possible deceleration [m/s^2].
Definition MSCFModel.h:277
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition MSCFModel.h:269
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:392
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, SumoRNG *rng=nullptr, bool readOnly=false)
Returns the named vehicle type or a sample from the named distribution.
void insertVTypeIDs(std::vector< std::string > &into) const
Inserts ids of all known vehicle types and vehicle type distributions to the given vector.
The car-following model and parameter.
void setEmergencyDecel(double emergencyDecel)
Set a new value for this type's emergency deceleration.
void setDecel(double decel)
Set a new value for this type's deceleration.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version).
const SUMOVTypeParameter & getParameter() const
SubParams cfParameter
Car-following parameter.
static std::set< SumoXMLAttr > AllowedJMAttrs
allowed attrs for the junction model
static bool parseLatAlignment(const std::string &val, double &lao, LatAlignmentDefinition &lad)
Parses and validates a given latAlignment value.
static SUMOTime processActionStepLength(double given)
Checks and converts given value for the action step length from seconds to miliseconds assuring it be...
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition Helper.cpp:376
static RGBColor makeRGBColor(const TraCIColor &color)
Definition Helper.cpp:387
An error which allows to continue.
Definition TraCIDefs.h:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
TRACI_CONST int VAR_VEHICLECLASS
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_IMPATIENCE
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_SCALE
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_SHAPECLASS
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:379
TRACI_CONST int VAR_ACTIONSTEPLENGTH
TRACI_CONST int VAR_SPEED_FACTOR
TRACI_CONST int VAR_TAU
TRACI_CONST int VAR_BOARDING_DURATION
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_PERSON_CAPACITY
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int VAR_MASS
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:378
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_MAXSPEED_LAT
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_APPARENT_DECEL
TRACI_CONST int VAR_DECEL
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_EMERGENCY_DECEL
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_ACCEL
TRACI_CONST int VAR_SPEED_DEVIATION
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition json.hpp:4451
size_type count(const Key &key) const
Definition json.hpp:17179