LCOV - code coverage report
Current view: top level - src/microsim/engine - VehicleEngineHandler.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 87 0
Test Date: 2024-10-17 15:44:51 Functions: 0.0 % 5 0

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 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              : /****************************************************************************/
      14              : /// @file    VehicleEngineHandler.cpp
      15              : /// @author  Michele Segata
      16              : /// @date    4 Feb 2015
      17              : ///
      18              : /****************************************************************************/
      19              : #include <config.h>
      20              : 
      21              : #include <utils/common/MsgHandler.h>
      22              : #include "VehicleEngineHandler.h"
      23              : 
      24              : 
      25              : // ===========================================================================
      26              : // static variables
      27              : // ===========================================================================
      28              : SequentialStringBijection::Entry VehicleEngineHandler::engineTags[] = {
      29              :     { "vehicles",     VehicleEngineHandler::ENGINE_TAG_VEHICLES },
      30              :     { "vehicle",      VehicleEngineHandler::ENGINE_TAG_VEHICLE },
      31              :     { "gears",        VehicleEngineHandler::ENGINE_TAG_GEARS                  },
      32              :     { "gear",         VehicleEngineHandler::ENGINE_TAG_GEAR                   },
      33              :     { "differential", VehicleEngineHandler::ENGINE_TAG_GEAR_DIFFERENTIAL      },
      34              :     { "mass",         VehicleEngineHandler::ENGINE_TAG_MASS                   },
      35              :     { "wheels",       VehicleEngineHandler::ENGINE_TAG_WHEELS                 },
      36              :     { "drag",         VehicleEngineHandler::ENGINE_TAG_DRAG                   },
      37              :     { "engine",       VehicleEngineHandler::ENGINE_TAG_ENGINE                 },
      38              :     { "power",        VehicleEngineHandler::ENGINE_TAG_ENGINE_POWER           },
      39              :     { "shifting",     VehicleEngineHandler::ENGINE_TAG_SHIFTING               },
      40              :     { "brakes",       VehicleEngineHandler::ENGINE_TAG_BRAKES                 },
      41              : 
      42              :     { "",             VehicleEngineHandler::ENGINE_TAG_NOTHING }
      43              : };
      44              : 
      45              : 
      46              : SequentialStringBijection::Entry VehicleEngineHandler::engineAttrs[] = {
      47              :     { "id",           VehicleEngineHandler::ENGINE_ATTR_ID},
      48              :     { "n",            VehicleEngineHandler::ENGINE_ATTR_GEAR_N                 },
      49              :     { "ratio",        VehicleEngineHandler::ENGINE_ATTR_GEAR_RATIO             },
      50              :     { "mass",         VehicleEngineHandler::ENGINE_ATTR_MASS                   },
      51              :     { "massFactor",   VehicleEngineHandler::ENGINE_ATTR_MASS_FACTOR            },
      52              :     { "diameter",     VehicleEngineHandler::ENGINE_ATTR_WHEELS_DIAMETER        },
      53              :     { "friction",     VehicleEngineHandler::ENGINE_ATTR_WHEELS_FRICTION        },
      54              :     { "cr1",          VehicleEngineHandler::ENGINE_ATTR_WHEELS_CR1             },
      55              :     { "cr2",          VehicleEngineHandler::ENGINE_ATTR_WHEELS_CR2             },
      56              :     { "cAir",         VehicleEngineHandler::ENGINE_ATTR_DRAG_CAIR              },
      57              :     { "section",      VehicleEngineHandler::ENGINE_ATTR_DRAG_SECTION           },
      58              :     { "type",         VehicleEngineHandler::ENGINE_ATTR_ENGINE_TYPE            },
      59              :     { "efficiency",   VehicleEngineHandler::ENGINE_ATTR_ENGINE_EFFICIENCY      },
      60              :     { "cylinders",    VehicleEngineHandler::ENGINE_ATTR_ENGINE_CYLINDERS       },
      61              :     { "minRpm",       VehicleEngineHandler::ENGINE_ATTR_ENGINE_MINRPM          },
      62              :     { "maxRpm",       VehicleEngineHandler::ENGINE_ATTR_ENGINE_MAXRPM          },
      63              :     { "tauEx",        VehicleEngineHandler::ENGINE_ATTR_ENGINE_TAU_EX          },
      64              :     { "tauBurn",      VehicleEngineHandler::ENGINE_ATTR_ENGINE_TAU_BURN        },
      65              :     { "rpm",          VehicleEngineHandler::ENGINE_ATTR_ENGINE_POWER_RPM       },
      66              :     { "hp",           VehicleEngineHandler::ENGINE_ATTR_ENGINE_POWER_HP        },
      67              :     { "kw",           VehicleEngineHandler::ENGINE_ATTR_ENGINE_POWER_KW        },
      68              :     { "slope",        VehicleEngineHandler::ENGINE_ATTR_ENGINE_POWER_SLOPE     },
      69              :     { "intercept",    VehicleEngineHandler::ENGINE_ATTR_ENGINE_POWER_INTERCEPT },
      70              :     { "rpm",          VehicleEngineHandler::ENGINE_ATTR_SHIFTING_RPM           },
      71              :     { "deltaRpm",     VehicleEngineHandler::ENGINE_ATTR_SHIFTING_DELTARPM      },
      72              :     { "tau",          VehicleEngineHandler::ENGINE_ATTR_BRAKES_TAU             },
      73              : 
      74              :     { "",             VehicleEngineHandler::ENGINE_ATTR_NOTHING }
      75              : };
      76              : 
      77              : 
      78              : // ===========================================================================
      79              : // method definitions
      80              : // ===========================================================================
      81            0 : VehicleEngineHandler::VehicleEngineHandler(const std::string& toLoad)
      82              :     : GenericSAXHandler(engineTags, ENGINE_TAG_NOTHING, engineAttrs, ENGINE_ATTR_NOTHING, "vehicles"),
      83            0 :       skip(false), currentGear(1) {
      84            0 :     vehicleToLoad = toLoad;
      85            0 : }
      86              : 
      87              : 
      88            0 : VehicleEngineHandler::~VehicleEngineHandler() {}
      89              : 
      90              : 
      91              : void
      92            0 : VehicleEngineHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
      93            0 :     switch (element) {
      94            0 :         case ENGINE_TAG_VEHICLE:
      95            0 :             skip = attrs.getString(ENGINE_ATTR_ID) != vehicleToLoad;
      96            0 :             if (!skip) {
      97            0 :                 engineParameters.id = vehicleToLoad;
      98              :             }
      99              :             break;
     100            0 :         case ENGINE_TAG_GEAR:
     101            0 :             if (!skip) {
     102            0 :                 int number = attrs.getInt(ENGINE_ATTR_GEAR_N);
     103            0 :                 if (number != currentGear) {
     104              :                     //fatal
     105            0 :                     std::stringstream ss;
     106            0 :                     ss << "Invalid gear number " << number << ". Please check that gears are inserted in order";
     107            0 :                     throw ProcessError(ss.str());
     108            0 :                 }
     109            0 :                 gearRatios.push_back(attrs.getFloat(ENGINE_ATTR_GEAR_RATIO));
     110            0 :                 currentGear++;
     111              :             }
     112              :             break;
     113            0 :         case ENGINE_TAG_GEAR_DIFFERENTIAL:
     114            0 :             if (!skip) {
     115              :                 // Load final drive ratio
     116            0 :                 engineParameters.differentialRatio = attrs.getFloat(ENGINE_ATTR_GEAR_RATIO);
     117              :             }
     118              :             break;
     119            0 :         case ENGINE_TAG_MASS:
     120            0 :             if (!skip) {
     121              :                 // Loads mass information, i.e., mass in kg and mass factor which takes into account rotational parts of the engine
     122            0 :                 engineParameters.mass_kg = attrs.getFloat(ENGINE_ATTR_MASS);
     123            0 :                 engineParameters.massFactor = attrs.getFloat(ENGINE_ATTR_MASS_FACTOR);
     124              :             }
     125              :             break;
     126            0 :         case ENGINE_TAG_DRAG:
     127            0 :             if (!skip) {
     128              :                 // Load air drag related data such as drag coefficient and maximum vehicle section
     129            0 :                 engineParameters.cAir = attrs.getFloat(ENGINE_ATTR_DRAG_CAIR);
     130            0 :                 engineParameters.a_m2 = attrs.getFloat(ENGINE_ATTR_DRAG_SECTION);
     131              :             }
     132              :             break;
     133            0 :         case ENGINE_TAG_WHEELS:
     134            0 :             if (!skip) {
     135              :                 // Load data about vehicle's wheels, such as diameter and friction coefficient
     136            0 :                 engineParameters.wheelDiameter_m = attrs.getFloat(ENGINE_ATTR_WHEELS_DIAMETER);
     137            0 :                 engineParameters.tiresFrictionCoefficient = attrs.getFloat(ENGINE_ATTR_WHEELS_FRICTION);
     138            0 :                 engineParameters.cr1 = attrs.getFloat(ENGINE_ATTR_WHEELS_CR1);
     139            0 :                 engineParameters.cr2 = attrs.getFloat(ENGINE_ATTR_WHEELS_CR2);
     140              :             }
     141              :             break;
     142            0 :         case ENGINE_TAG_ENGINE:
     143            0 :             if (!skip) {
     144              :                 // Load data about the engine, such as efficiency factor and cylinders
     145            0 :                 engineParameters.engineEfficiency = attrs.getFloat(ENGINE_ATTR_ENGINE_EFFICIENCY);
     146            0 :                 engineParameters.cylinders = attrs.getInt(ENGINE_ATTR_ENGINE_CYLINDERS);
     147            0 :                 engineParameters.minRpm = attrs.getInt(ENGINE_ATTR_ENGINE_MINRPM);
     148            0 :                 engineParameters.maxRpm = attrs.getInt(ENGINE_ATTR_ENGINE_MAXRPM);
     149            0 :                 engineParameters.tauEx_s = attrs.getFloat(ENGINE_ATTR_ENGINE_TAU_EX);
     150            0 :                 engineParameters.fixedTauBurn = attrs.hasAttribute(ENGINE_ATTR_ENGINE_TAU_BURN);
     151            0 :                 if (engineParameters.fixedTauBurn) {
     152            0 :                     engineParameters.tauBurn_s = attrs.getFloat(ENGINE_ATTR_ENGINE_TAU_BURN);
     153              :                 }
     154            0 :                 engineParameters.maxRpm = attrs.getInt(ENGINE_ATTR_ENGINE_MAXRPM);
     155            0 :                 std::string mapType = attrs.getString(ENGINE_ATTR_ENGINE_TYPE);
     156            0 :                 if (mapType != "poly") {
     157            0 :                     throw ProcessError(TL("Invalid engine map type. Only \"poly\" is supported for now"));
     158              :                 }
     159              :             }
     160              :             break;
     161            0 :         case ENGINE_TAG_ENGINE_POWER:
     162            0 :             if (!skip) {
     163              :                 //check that the degree is within the maximum supported
     164            0 :                 if (attrs.hasAttribute("x" + toString(MAX_POLY_DEGREE))) {
     165            0 :                     std::stringstream ss;
     166            0 :                     ss << "Maximum degree for the engine polynomial is " << MAX_POLY_DEGREE << ". Please check your model's data";
     167            0 :                     throw ProcessError(ss.str());
     168            0 :                 }
     169              :                 //parse all polynomial coefficients
     170            0 :                 for (int i = 0; i < MAX_POLY_DEGREE; i++) {
     171            0 :                     const std::string param = "x" + toString(i);
     172            0 :                     if (attrs.hasAttribute(param)) {
     173            0 :                         engineParameters.engineMapping.x[i] = attrs.getFloat(param);
     174              :                     } else {
     175              :                         //save the actual degree
     176            0 :                         engineParameters.engineMapping.degree = i;
     177              :                         break;
     178              :                     }
     179              :                 }
     180              :             }
     181              :             break;
     182            0 :         case ENGINE_TAG_SHIFTING:
     183            0 :             if (!skip) {
     184            0 :                 engineParameters.shiftingRule.rpm = attrs.getFloat(ENGINE_ATTR_SHIFTING_RPM);
     185            0 :                 engineParameters.shiftingRule.deltaRpm = attrs.getFloat(ENGINE_ATTR_SHIFTING_DELTARPM);
     186              :             }
     187              :             break;
     188            0 :         case ENGINE_TAG_BRAKES:
     189            0 :             if (!skip) {
     190            0 :                 engineParameters.brakesTau_s = attrs.getFloat(ENGINE_ATTR_BRAKES_TAU);
     191              :             }
     192              :             break;
     193              :         case ENGINE_TAG_VEHICLES:
     194              :         case ENGINE_TAG_GEARS:
     195              :             break;
     196            0 :         default:
     197            0 :             WRITE_WARNINGF(TL("Unknown tag '%' while parsing."), toString(element));
     198              :     }
     199            0 : }
     200              : 
     201              : 
     202              : void
     203            0 : VehicleEngineHandler::myEndElement(int element) {
     204            0 :     switch (element) {
     205            0 :         case ENGINE_TAG_GEARS:
     206            0 :             delete[] engineParameters.gearRatios;
     207            0 :             engineParameters.gearRatios = new double[gearRatios.size()];
     208            0 :             for (int i = 0; i < (int)gearRatios.size(); i++) {
     209            0 :                 engineParameters.gearRatios[i] = gearRatios[i];
     210              :             }
     211            0 :             engineParameters.nGears = (int)gearRatios.size();
     212            0 :             break;
     213            0 :         case ENGINE_TAG_VEHICLES:
     214            0 :             engineParameters.computeCoefficients();
     215            0 :             break;
     216              :         default:
     217              :             break;
     218              :     }
     219            0 : }
     220              : 
     221              : 
     222              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1