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