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