Eclipse SUMO - Simulation of Urban MObility
libsumo/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-2023 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
159TraCIColor
160VehicleType::getColor(const std::string& typeID) {
161 return Helper::makeTraCIColor(getVType(typeID)->getColor());
162}
163
164
165double
166VehicleType::getMinGapLat(const std::string& typeID) {
167 return getVType(typeID)->getMinGapLat();
168}
169
170
171double
172VehicleType::getMaxSpeedLat(const std::string& typeID) {
173 return getVType(typeID)->getMaxSpeedLat();
174}
175
176
177std::string
178VehicleType::getLateralAlignment(const std::string& typeID) {
179 if (getVType(typeID)->getPreferredLateralAlignment() != LatAlignmentDefinition::GIVEN) {
180 return toString(getVType(typeID)->getPreferredLateralAlignment());
181 } else {
182 return toString(getVType(typeID)->getPreferredLateralAlignmentOffset());
183 }
184}
185
186
187std::string
188VehicleType::getParameter(const std::string& typeID, const std::string& key) {
189 return getVType(typeID)->getParameter().getParameter(key, "");
190}
191
193
194int
195VehicleType::getPersonCapacity(const std::string& typeID) {
196 return getVType(typeID)->getPersonCapacity();
197}
198
199double
200VehicleType::getScale(const std::string& typeID) {
201 return getVType(typeID)->getParameter().scale;
202}
203
204double
205VehicleType::getBoardingDuration(const std::string& typeID) {
206 return STEPS2TIME(getVType(typeID)->getBoardingDuration(true));
207}
208
209void
210VehicleType::setLength(const std::string& typeID, double length) {
211 getVType(typeID)->setLength(length);
212}
213
214
215void
216VehicleType::setMaxSpeed(const std::string& typeID, double speed) {
217 getVType(typeID)->setMaxSpeed(speed);
218}
219
220
221void
222VehicleType::setActionStepLength(const std::string& typeID, double actionStepLength, bool resetActionOffset) {
223 getVType(typeID)->setActionStepLength(SUMOVehicleParserHelper::processActionStepLength(actionStepLength), resetActionOffset);
224}
225
226
227void
228VehicleType::setVehicleClass(const std::string& typeID, const std::string& clazz) {
229 getVType(typeID)->setVClass(getVehicleClassID(clazz));
230}
231
232
233void
234VehicleType::setSpeedFactor(const std::string& typeID, double factor) {
235 getVType(typeID)->setSpeedFactor(factor);
236}
237
238
239void
240VehicleType::setSpeedDeviation(const std::string& typeID, double deviation) {
241 getVType(typeID)->setSpeedDeviation(deviation);
242}
243
244
245void
246VehicleType::setEmissionClass(const std::string& typeID, const std::string& clazz) {
247 getVType(typeID)->setEmissionClass(PollutantsInterface::getClassByName(clazz));
248}
249
250
251void
252VehicleType::setShapeClass(const std::string& typeID, const std::string& shapeClass) {
253 getVType(typeID)->setShape(getVehicleShapeID(shapeClass));
254}
255
256
257void
258VehicleType::setWidth(const std::string& typeID, double width) {
259 getVType(typeID)->setWidth(width);
260}
261
262
263void
264VehicleType::setHeight(const std::string& typeID, double height) {
265 getVType(typeID)->setHeight(height);
266}
267
268
269void
270VehicleType::setMinGap(const std::string& typeID, double minGap) {
271 getVType(typeID)->setMinGap(minGap);
272}
273
274
275void
276VehicleType::setAccel(const std::string& typeID, double accel) {
277 getVType(typeID)->setAccel(accel);
278}
279
280
281void
282VehicleType::setDecel(const std::string& typeID, double decel) {
283 MSVehicleType* v = getVType(typeID);
284 v->setDecel(decel);
285 // automatically raise emergencyDecel to ensure it is at least as high as decel
286 if (decel > v->getCarFollowModel().getEmergencyDecel()) {
288 // notify user only if emergencyDecel was previously specified
289 WRITE_WARNINGF(TL("Automatically setting emergencyDecel to % for vType '%' to match decel."), toString(decel), typeID);
290 }
291 v->setEmergencyDecel(decel);
292 }
293}
294
295
296void
297VehicleType::setEmergencyDecel(const std::string& typeID, double decel) {
298 MSVehicleType* v = getVType(typeID);
299 v->setEmergencyDecel(decel);
300 if (decel < v->getCarFollowModel().getMaxDecel()) {
301 WRITE_WARNINGF(TL("New value of emergencyDecel (%) is lower than decel (%)"), toString(decel), toString(v->getCarFollowModel().getMaxDecel()));
302 }
303}
304
305
306void
307VehicleType::setApparentDecel(const std::string& typeID, double decel) {
308 getVType(typeID)->setApparentDecel(decel);
309}
310
311
312void
313VehicleType::setImperfection(const std::string& typeID, double imperfection) {
314 getVType(typeID)->setImperfection(imperfection);
315}
316
317
318void
319VehicleType::setTau(const std::string& typeID, double tau) {
320 getVType(typeID)->setTau(tau);
321}
322
323
324void
325VehicleType::setColor(const std::string& typeID, const TraCIColor& c) {
326 getVType(typeID)->setColor(Helper::makeRGBColor(c));
327}
328
329
330void
331VehicleType::setMinGapLat(const std::string& typeID, double minGapLat) {
332 getVType(typeID)->setMinGapLat(minGapLat);
333}
334
335
336void
337VehicleType::setMaxSpeedLat(const std::string& typeID, double speed) {
338 getVType(typeID)->setMaxSpeedLat(speed);
339}
340
341
342void
343VehicleType::setLateralAlignment(const std::string& typeID, const std::string& latAlignment) {
344 double lao;
346 if (SUMOVTypeParameter::parseLatAlignment(latAlignment, lao, lad)) {
347 getVType(typeID)->setPreferredLateralAlignment(lad, lao);
348 } else {
349 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)");
350 }
351}
352
353void
354VehicleType::setScale(const std::string& typeID, double value) {
355 getVType(typeID)->setScale(value);
356}
357
358void
359VehicleType::copy(const std::string& origTypeID, const std::string& newTypeID) {
360 getVType(origTypeID)->duplicateType(newTypeID, true);
361}
362
363
364void
365VehicleType::setParameter(const std::string& typeID, const std::string& name, const std::string& value) {
366 ((SUMOVTypeParameter&)getVType(typeID)->getParameter()).setParameter(name, value);
367}
368
369
371
372
374VehicleType::getVType(std::string id) {
376 if (t == nullptr) {
377 throw TraCIException("Vehicle type '" + id + "' is not known");
378 }
379 return t;
380}
381
382
383std::shared_ptr<VariableWrapper>
384VehicleType::makeWrapper() {
385 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
386}
387
388
389bool
390VehicleType::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
391 return handleVariableWithID(objID, objID, variable, wrapper, paramData);
392}
393
394
395bool
396VehicleType::handleVariableWithID(const std::string& objID, const std::string& typeID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
397 switch (variable) {
398 case TRACI_ID_LIST:
399 return wrapper->wrapStringList(objID, variable, getIDList());
400 case ID_COUNT:
401 return wrapper->wrapInt(objID, variable, getIDCount());
402 case VAR_LENGTH:
403 return wrapper->wrapDouble(objID, variable, getLength(typeID));
404 case VAR_HEIGHT:
405 return wrapper->wrapDouble(objID, variable, getHeight(typeID));
406 case VAR_MINGAP:
407 return wrapper->wrapDouble(objID, variable, getMinGap(typeID));
408 case VAR_MAXSPEED:
409 return wrapper->wrapDouble(objID, variable, getMaxSpeed(typeID));
410 case VAR_ACCEL:
411 return wrapper->wrapDouble(objID, variable, getAccel(typeID));
412 case VAR_DECEL:
413 return wrapper->wrapDouble(objID, variable, getDecel(typeID));
415 return wrapper->wrapDouble(objID, variable, getEmergencyDecel(typeID));
417 return wrapper->wrapDouble(objID, variable, getApparentDecel(typeID));
419 return wrapper->wrapDouble(objID, variable, getActionStepLength(typeID));
420 case VAR_IMPERFECTION:
421 return wrapper->wrapDouble(objID, variable, getImperfection(typeID));
422 case VAR_TAU:
423 return wrapper->wrapDouble(objID, variable, getTau(typeID));
424 case VAR_SPEED_FACTOR:
425 return wrapper->wrapDouble(objID, variable, getSpeedFactor(typeID));
427 return wrapper->wrapDouble(objID, variable, getSpeedDeviation(typeID));
428 case VAR_VEHICLECLASS:
429 return wrapper->wrapString(objID, variable, getVehicleClass(typeID));
431 return wrapper->wrapString(objID, variable, getEmissionClass(typeID));
432 case VAR_SHAPECLASS:
433 return wrapper->wrapString(objID, variable, getShapeClass(typeID));
434 case VAR_WIDTH:
435 return wrapper->wrapDouble(objID, variable, getWidth(typeID));
436 case VAR_COLOR:
437 return wrapper->wrapColor(objID, variable, getColor(typeID));
438 case VAR_MINGAP_LAT:
439 return wrapper->wrapDouble(objID, variable, getMinGapLat(typeID));
440 case VAR_MAXSPEED_LAT:
441 return wrapper->wrapDouble(objID, variable, getMaxSpeedLat(typeID));
442 case VAR_LATALIGNMENT:
443 return wrapper->wrapString(objID, variable, getLateralAlignment(typeID));
445 return wrapper->wrapInt(objID, variable, getPersonCapacity(typeID));
447 return wrapper->wrapDouble(objID, variable, getBoardingDuration(typeID));
448 case VAR_SCALE:
449 return wrapper->wrapDouble(objID, variable, getScale(typeID));
451 paramData->readUnsignedByte();
452 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
454 paramData->readUnsignedByte();
455 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
456 default:
457 return false;
458 }
459}
460
461}
462
463
464/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:268
#define TL(string)
Definition: MsgHandler.h:284
#define STEPS2TIME(x)
Definition: SUMOTime.h:54
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.
@ 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:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
double getEmergencyDecel() const
Get the vehicle type's maximal phisically possible deceleration [m/s^2].
Definition: MSCFModel.h:272
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition: MSCFModel.h:264
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
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.
Definition: MSVehicleType.h:63
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
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
Structure representing possible vehicle parameter.
SubParams cfParameter
Car-following parameter.
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...
C++ TraCI client API implementation.
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:360
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:371
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_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:338
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
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:337
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