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 
#define ENGINE_TAG_GEAR_RATIO
#define ENGINE_TAG_DRAG_CAIR
#define ENGINE_TAG_DRAG
void loadWheelsData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
struct GearShiftingRules shiftingRule
#define ENGINE_TAG_GEAR
std::string parseStringAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
void loadBrakesData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
double parsePolynomialCoefficient(int index, const XERCES_CPP_NAMESPACE::Attributes &attrs)
void loadDifferentialData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define TAG_VEHICLE
void raiseUnknownTagError(std::string tag)
#define ENGINE_TAG_SHIFTING_DELTARPM
double tiresFrictionCoefficient
#define ENGINE_TAG_WHEELS
#define ENGINE_TAG_DRAG_SECTION
#define ENGINE_TAG_BRAKES
double parseDoubleAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_VEHICLE_ID
std::string transcode(const XMLCh *const qname)
#define MAX_POLY_DEGREE
#define ENGINE_TAG_ENGINE_TYPE
void loadEngineModelData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_GEAR_DIFFERENTIAL
#define ENGINE_TAG_MASS
#define ENGINE_TAG_ENGINE_MINRPM
void loadDragData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
EngineParameters engineParameters
#define ENGINE_TAG_ENGINE_CYLINDERS
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
#define ENGINE_TAG_ENGINE_EFFICIENCY
#define ENGINE_TAG_MASS_MASS
#define ENGINE_TAG_ENGINE
#define ENGINE_TAG_ENGINE_TAU_EX
void loadMassData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
#define TAG_GEARS
std::vector< double > gearRatios
#define ENGINE_TAG_ENGINE_POWER
const EngineParameters & getEngineParameters()
#define ENGINE_TAG_SHIFTING_RPM
void raiseMissingAttributeError(std::string tag, std::string attribute)
#define ENGINE_TAG_WHEELS_CR2
#define TAG_ENGINE
#define TAG_VEHICLES
#define ENGINE_TAG_VEHICLES
struct PolynomialEngineModelRpmToHp engineMapping
#define ENGINE_TAG_BRAKES_TAU
void loadGearData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_MASS_FACTOR
#define ENGINE_TAG_GEARS
#define ENGINE_TAG_WHEELS_DIAMETER
VehicleEngineHandler(const std::string &toLoad)
#define ENGINE_TAG_WHEELS_CR1
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
#define ENGINE_TAG_SHIFTING
std::string getAttributeValue(const char *attributeName, const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_ENGINE_MAXRPM
int parseIntAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_ENGINE_TAU_BURN
void loadEngineData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
virtual ~VehicleEngineHandler()
Destructor.
void loadShiftingData(const XERCES_CPP_NAMESPACE::Attributes &attrs)
int existsAttribute(std::string tag, const char *attribute, const XERCES_CPP_NAMESPACE::Attributes &attrs)
#define ENGINE_TAG_VEHICLE
#define ENGINE_TAG_WHEELS_FRICTION
#define ENGINE_TAG_GEAR_N