Eclipse SUMO - Simulation of Urban MObility
VehicleEngineHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
16 
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
24 #include "VehicleEngineHandler.h"
25 
26 
27 // ===========================================================================
28 // class definitions
29 // ===========================================================================
31  : currentTag(TAG_VEHICLES), skip(false), currentGear(1) {
32  vehicleToLoad = toLoad;
33 }
34 
35 
37 
38 std::string transcode(const XMLCh* const qname) {
39  return std::string(XERCES_CPP_NAMESPACE::XMLString::transcode(qname));
40 }
41 
42 const XMLCh* transcode(const char* name) {
44 }
45 
46 std::string getAttributeValue(const char* attributeName, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
47  return transcode(attrs.getValue(transcode(attributeName)));
48 }
49 
50 void
51 VehicleEngineHandler::startElement(const XMLCh* const /*uri*/,
52  const XMLCh* const /*localname*/,
53  const XMLCh* const qname,
54  const XERCES_CPP_NAMESPACE::Attributes& attrs) {
55  std::string tag = XERCES_CPP_NAMESPACE::XMLString::transcode(qname);
56 
57  switch (currentTag) {
58 
59  case TAG_VEHICLES:
60 
61  //we are already inside the root. just ignore this
62  if (tag == ENGINE_TAG_VEHICLES) {
63  break;
64  }
65 
66  //this is a new vehicle definition. is this the one we should load?
67  if (tag == ENGINE_TAG_VEHICLE) {
69  skip = true;
70  } else {
72  }
74  }
75 
76  break;
77 
78  case TAG_VEHICLE:
79 
80  //we are not interested in this vehicle
81  if (skip) {
82  break;
83  }
84 
85  //definition of gear ratios
86  if (tag == ENGINE_TAG_GEARS) {
88  }
89  //definition of masses
90  else if (tag == ENGINE_TAG_MASS) {
91  loadMassData(attrs);
92  }
93  //definition of air drag
94  else if (tag == ENGINE_TAG_DRAG) {
95  loadDragData(attrs);
96  }
97  //definition of wheels
98  else if (tag == ENGINE_TAG_WHEELS) {
99  loadWheelsData(attrs);
100  }
101  //definition of engine
102  else if (tag == ENGINE_TAG_ENGINE) {
103  loadEngineData(attrs);
105  }
106  //definition of the shifting rule
107  else if (tag == ENGINE_TAG_SHIFTING) {
108  loadShiftingData(attrs);
109  }
110  //definition of brakes
111  else if (tag == ENGINE_TAG_BRAKES) {
112  loadBrakesData(attrs);
113  } else {
115  }
116 
117  break;
118 
119  case TAG_GEARS:
120 
121  if (skip) {
122  break;
123  }
124 
125  if (tag == ENGINE_TAG_GEAR) {
126  //definition of the ratio for a single gear
127  loadGearData(attrs);
128  } else if (tag == ENGINE_TAG_GEAR_DIFFERENTIAL) {
129  //definition of the ratio for the final drive
130  loadDifferentialData(attrs);
131  } else {
133  }
134 
135  break;
136 
137  case TAG_ENGINE:
138 
139  if (skip) {
140  break;
141  }
142 
143  if (tag == ENGINE_TAG_ENGINE_POWER) {
144  loadEngineModelData(attrs);
145  } else {
147  }
148 
149  break;
150 
151  default:
152 
153  break;
154 
155  }
156 
157 }
158 
159 
160 void
161 VehicleEngineHandler::endElement(const XMLCh* const /*uri*/,
162  const XMLCh* const /*localname*/,
163  const XMLCh* const qname) {
164  std::string tag = XERCES_CPP_NAMESPACE::XMLString::transcode(qname);
165 
166  switch (currentTag) {
167 
168  case TAG_VEHICLES:
169  break;
170 
171  case TAG_VEHICLE:
172  if (tag == ENGINE_TAG_VEHICLE) {
173  skip = false;
175  }
176  break;
177 
178  case TAG_GEARS:
179  if (tag == ENGINE_TAG_GEARS) {
181  currentGear = 0;
182 
183  delete [] engineParameters.gearRatios;
184  engineParameters.gearRatios = new double[gearRatios.size()];
185  for (int i = 0; i < (int)gearRatios.size(); i++) {
187  }
188  engineParameters.nGears = (int)gearRatios.size();
189  }
190 
191  break;
192 
193  case TAG_ENGINE:
194  if (tag == ENGINE_TAG_ENGINE) {
196  }
197  break;
198 
199  default:
200 
201  break;
202 
203  }
204 
205 }
206 
207 
208 
209 void
212 }
213 
215  return engineParameters;
216 }
217 
218 
219 
220 void
221 VehicleEngineHandler::loadMassData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
224 }
225 
226 
227 void
228 VehicleEngineHandler::loadDragData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
231 }
232 
233 
234 void
235 VehicleEngineHandler::loadWheelsData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
240 }
241 
242 
243 void
244 VehicleEngineHandler::loadEngineData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
253  } else {
255  }
257  std::string mapType = parseStringAttribute(ENGINE_TAG_ENGINE, ENGINE_TAG_ENGINE_TYPE, attrs);
258  if (mapType != "poly") {
259  throw ProcessError("Invalid engine map type. Only \"poly\" is supported for now");
260  }
261 }
262 
263 
264 void
265 VehicleEngineHandler::loadGearData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
266 
268  if (number != currentGear) {
269  //fatal
270  std::stringstream ss;
271  ss << "Invalid gear number " << number << ". Please check that gears are inserted in order";
272  throw ProcessError(ss.str());
273  }
275  currentGear++;
276 
277 }
278 
279 
280 void
281 VehicleEngineHandler::loadDifferentialData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
283 }
284 
285 
286 void
287 VehicleEngineHandler::loadEngineModelData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
288  //check that the degree is within the maximum supported
289  if (attrs.getLength() > MAX_POLY_DEGREE) {
290  std::stringstream ss;
291  ss << "Maximum degree for the engine polynomial is " << MAX_POLY_DEGREE << ". Please check your model's data";
292  throw ProcessError(ss.str());
293  }
294  //parse all polynomial coefficients
295  for (int i = 0; i < (int)attrs.getLength(); i++) {
297  }
298  //save the actual degree
299  engineParameters.engineMapping.degree = (int)attrs.getLength();
300 }
301 
302 
303 void
304 VehicleEngineHandler::loadShiftingData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
307 }
308 
309 
310 void
311 VehicleEngineHandler::loadBrakesData(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
313 }
314 
315 int VehicleEngineHandler::existsAttribute(std::string /*tag*/, const char* attribute, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
316  return attrs.getIndex(transcode(attribute));
317 }
318 std::string VehicleEngineHandler::parseStringAttribute(std::string tag, const char* attribute, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
319  int attributeIndex;
320  std::string strValue;
321  attributeIndex = existsAttribute(tag, attribute, attrs);
322  if (attributeIndex == -1) {
323  //raise will stop execution
324  raiseMissingAttributeError(tag, attribute);
325  }
326  return transcode(attrs.getValue(attributeIndex));
327 }
328 int VehicleEngineHandler::parseIntAttribute(std::string tag, const char* attribute, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
329  return StringUtils::toInt(parseStringAttribute(tag, attribute, attrs));
330 }
331 double VehicleEngineHandler::parseDoubleAttribute(std::string tag, const char* attribute, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
332  return StringUtils::toDouble(parseStringAttribute(tag, attribute, attrs));
333 }
334 double VehicleEngineHandler::parsePolynomialCoefficient(int index, const XERCES_CPP_NAMESPACE::Attributes& attrs) {
335  std::stringstream ss;
336  ss << "x" << index;
337  return parseDoubleAttribute(ENGINE_TAG_ENGINE_POWER, ss.str().c_str(), attrs);
338 }
339 
340 
341 void
342 VehicleEngineHandler::raiseMissingAttributeError(std::string tag, std::string attribute) {
343  std::stringstream ss;
344  ss << "Missing attribute \"" << attribute << "\" for tag " << tag;
345  throw ProcessError(ss.str());
346 }
347 
348 
349 void
351  std::stringstream ss;
352  ss << "I don't know what to do with this tag: " << tag;
353  throw ProcessError(ss.str());
354 }
355 
356 
357 /****************************************************************************/
358 
getAttributeValue
std::string getAttributeValue(const char *attributeName, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:46
VehicleEngineHandler::existsAttribute
int existsAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:315
EngineParameters::shiftingRule
struct GearShiftingRules shiftingRule
Definition: EngineParameters.h:90
ENGINE_TAG_GEAR_RATIO
#define ENGINE_TAG_GEAR_RATIO
Definition: VehicleEngineHandler.h:38
VehicleEngineHandler::endDocument
void endDocument()
Definition: VehicleEngineHandler.cpp:210
ENGINE_TAG_VEHICLE_ID
#define ENGINE_TAG_VEHICLE_ID
Definition: VehicleEngineHandler.h:33
VehicleEngineHandler::currentTag
int currentTag
Definition: VehicleEngineHandler.h:193
EngineParameters::cr2
double cr2
Definition: EngineParameters.h:76
ENGINE_TAG_ENGINE_TYPE
#define ENGINE_TAG_ENGINE_TYPE
Definition: VehicleEngineHandler.h:52
VehicleEngineHandler::currentGear
int currentGear
Definition: VehicleEngineHandler.h:199
EngineParameters::gearRatios
double * gearRatios
Definition: EngineParameters.h:64
VehicleEngineHandler::loadWheelsData
void loadWheelsData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:235
VehicleEngineHandler.h
VehicleEngineHandler::parseStringAttribute
std::string parseStringAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:318
VehicleEngineHandler::loadBrakesData
void loadBrakesData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:311
ENGINE_TAG_WHEELS_CR2
#define ENGINE_TAG_WHEELS_CR2
Definition: VehicleEngineHandler.h:47
EngineParameters::differentialRatio
double differentialRatio
Definition: EngineParameters.h:66
ENGINE_TAG_SHIFTING_DELTARPM
#define ENGINE_TAG_SHIFTING_DELTARPM
Definition: VehicleEngineHandler.h:67
ENGINE_TAG_DRAG
#define ENGINE_TAG_DRAG
Definition: VehicleEngineHandler.h:48
EngineParameters::GearShiftingRules::rpm
double rpm
Definition: EngineParameters.h:55
EngineParameters::engineEfficiency
double engineEfficiency
Definition: EngineParameters.h:92
EngineParameters::id
std::string id
Definition: EngineParameters.h:60
VehicleEngineHandler::loadDifferentialData
void loadDifferentialData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:281
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:313
ENGINE_TAG_MASS_FACTOR
#define ENGINE_TAG_MASS_FACTOR
Definition: VehicleEngineHandler.h:42
EngineParameters::wheelDiameter_m
double wheelDiameter_m
Definition: EngineParameters.h:68
VehicleEngineHandler::parsePolynomialCoefficient
double parsePolynomialCoefficient(int index, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:334
VehicleEngineHandler::parseDoubleAttribute
double parseDoubleAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:331
ENGINE_TAG_VEHICLE
#define ENGINE_TAG_VEHICLE
Definition: VehicleEngineHandler.h:32
EngineParameters::mass_kg
double mass_kg
Definition: EngineParameters.h:70
TAG_VEHICLES
#define TAG_VEHICLES
Definition: VehicleEngineHandler.h:71
EngineParameters::minRpm
double minRpm
Definition: EngineParameters.h:100
ENGINE_TAG_GEAR
#define ENGINE_TAG_GEAR
Definition: VehicleEngineHandler.h:36
VehicleEngineHandler::raiseUnknownTagError
void raiseUnknownTagError(std::string tag)
Definition: VehicleEngineHandler.cpp:350
ENGINE_TAG_BRAKES_TAU
#define ENGINE_TAG_BRAKES_TAU
Definition: VehicleEngineHandler.h:69
TAG_ENGINE
#define TAG_ENGINE
Definition: VehicleEngineHandler.h:74
ENGINE_TAG_ENGINE_MINRPM
#define ENGINE_TAG_ENGINE_MINRPM
Definition: VehicleEngineHandler.h:55
VehicleEngineHandler::loadEngineModelData
void loadEngineModelData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:287
ENGINE_TAG_WHEELS_CR1
#define ENGINE_TAG_WHEELS_CR1
Definition: VehicleEngineHandler.h:46
EngineParameters::tauBurn_s
double tauBurn_s
Definition: EngineParameters.h:104
EngineParameters::tiresFrictionCoefficient
double tiresFrictionCoefficient
Definition: EngineParameters.h:84
ENGINE_TAG_WHEELS_DIAMETER
#define ENGINE_TAG_WHEELS_DIAMETER
Definition: VehicleEngineHandler.h:44
VehicleEngineHandler::engineParameters
EngineParameters engineParameters
Definition: VehicleEngineHandler.h:201
VehicleEngineHandler::loadDragData
void loadDragData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:228
EngineParameters::cAir
double cAir
Definition: EngineParameters.h:72
ENGINE_TAG_WHEELS_FRICTION
#define ENGINE_TAG_WHEELS_FRICTION
Definition: VehicleEngineHandler.h:45
ENGINE_TAG_ENGINE_CYLINDERS
#define ENGINE_TAG_ENGINE_CYLINDERS
Definition: VehicleEngineHandler.h:54
ENGINE_TAG_MASS
#define ENGINE_TAG_MASS
Definition: VehicleEngineHandler.h:40
EngineParameters::massFactor
double massFactor
Definition: EngineParameters.h:94
EngineParameters::tauEx_s
double tauEx_s
Definition: EngineParameters.h:104
EngineParameters::cylinders
int cylinders
Definition: EngineParameters.h:96
ENGINE_TAG_MASS_MASS
#define ENGINE_TAG_MASS_MASS
Definition: VehicleEngineHandler.h:41
VehicleEngineHandler::vehicleToLoad
std::string vehicleToLoad
Definition: VehicleEngineHandler.h:195
ENGINE_TAG_SHIFTING_RPM
#define ENGINE_TAG_SHIFTING_RPM
Definition: VehicleEngineHandler.h:66
EngineParameters::PolynomialEngineModelRpmToHp::x
double x[MAX_POLY_DEGREE]
Definition: EngineParameters.h:44
ProcessError
Definition: UtilExceptions.h:40
VehicleEngineHandler::loadMassData
void loadMassData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:221
TAG_GEARS
#define TAG_GEARS
Definition: VehicleEngineHandler.h:73
ENGINE_TAG_ENGINE_TAU_EX
#define ENGINE_TAG_ENGINE_TAU_EX
Definition: VehicleEngineHandler.h:57
VehicleEngineHandler::getEngineParameters
const EngineParameters & getEngineParameters()
Definition: VehicleEngineHandler.cpp:214
EngineParameters::nGears
int nGears
Definition: EngineParameters.h:62
EngineParameters::PolynomialEngineModelRpmToHp::degree
int degree
Definition: EngineParameters.h:43
EngineParameters::maxRpm
double maxRpm
Definition: EngineParameters.h:100
ENGINE_TAG_ENGINE_MAXRPM
#define ENGINE_TAG_ENGINE_MAXRPM
Definition: VehicleEngineHandler.h:56
ENGINE_TAG_DRAG_CAIR
#define ENGINE_TAG_DRAG_CAIR
Definition: VehicleEngineHandler.h:49
VehicleEngineHandler::raiseMissingAttributeError
void raiseMissingAttributeError(std::string tag, std::string attribute)
Definition: VehicleEngineHandler.cpp:342
ENGINE_TAG_ENGINE_POWER
#define ENGINE_TAG_ENGINE_POWER
Definition: VehicleEngineHandler.h:59
ENGINE_TAG_WHEELS
#define ENGINE_TAG_WHEELS
Definition: VehicleEngineHandler.h:43
VehicleEngineHandler::gearRatios
std::vector< double > gearRatios
Definition: VehicleEngineHandler.h:203
EngineParameters
Definition: EngineParameters.h:32
StringUtils.h
ENGINE_TAG_GEARS
#define ENGINE_TAG_GEARS
Definition: VehicleEngineHandler.h:35
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:246
EngineParameters::fixedTauBurn
bool fixedTauBurn
Definition: EngineParameters.h:106
VehicleEngineHandler::loadGearData
void loadGearData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:265
ENGINE_TAG_VEHICLES
#define ENGINE_TAG_VEHICLES
Definition: VehicleEngineHandler.h:31
transcode
std::string transcode(const XMLCh *const qname)
Definition: VehicleEngineHandler.cpp:38
EngineParameters::computeCoefficients
void computeCoefficients()
Definition: EngineParameters.cpp:110
VehicleEngineHandler::VehicleEngineHandler
VehicleEngineHandler(const std::string &toLoad)
Definition: VehicleEngineHandler.cpp:30
ENGINE_TAG_SHIFTING
#define ENGINE_TAG_SHIFTING
Definition: VehicleEngineHandler.h:65
TAG_VEHICLE
#define TAG_VEHICLE
Definition: VehicleEngineHandler.h:72
ENGINE_TAG_ENGINE_TAU_BURN
#define ENGINE_TAG_ENGINE_TAU_BURN
Definition: VehicleEngineHandler.h:58
ENGINE_TAG_ENGINE
#define ENGINE_TAG_ENGINE
Definition: VehicleEngineHandler.h:51
ENGINE_TAG_BRAKES
#define ENGINE_TAG_BRAKES
Definition: VehicleEngineHandler.h:68
config.h
EngineParameters::a_m2
double a_m2
Definition: EngineParameters.h:72
VehicleEngineHandler::parseIntAttribute
int parseIntAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:328
VehicleEngineHandler::skip
bool skip
Definition: VehicleEngineHandler.h:197
EngineParameters::cr1
double cr1
Definition: EngineParameters.h:76
EngineParameters::GearShiftingRules::deltaRpm
double deltaRpm
Definition: EngineParameters.h:56
ENGINE_TAG_DRAG_SECTION
#define ENGINE_TAG_DRAG_SECTION
Definition: VehicleEngineHandler.h:50
VehicleEngineHandler::endElement
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
Definition: VehicleEngineHandler.cpp:161
EngineParameters::brakesTau_s
double brakesTau_s
Definition: EngineParameters.h:102
MAX_POLY_DEGREE
#define MAX_POLY_DEGREE
Definition: EngineParameters.h:30
EngineParameters::engineMapping
struct PolynomialEngineModelRpmToHp engineMapping
Definition: EngineParameters.h:88
VehicleEngineHandler::startElement
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:51
ENGINE_TAG_ENGINE_EFFICIENCY
#define ENGINE_TAG_ENGINE_EFFICIENCY
Definition: VehicleEngineHandler.h:53
ENGINE_TAG_GEAR_DIFFERENTIAL
#define ENGINE_TAG_GEAR_DIFFERENTIAL
Definition: VehicleEngineHandler.h:39
VehicleEngineHandler::loadEngineData
void loadEngineData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:244
VehicleEngineHandler::~VehicleEngineHandler
virtual ~VehicleEngineHandler()
Destructor.
Definition: VehicleEngineHandler.cpp:36
VehicleEngineHandler::loadShiftingData
void loadShiftingData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
Definition: VehicleEngineHandler.cpp:304
ENGINE_TAG_GEAR_N
#define ENGINE_TAG_GEAR_N
Definition: VehicleEngineHandler.h:37