Eclipse SUMO - Simulation of Urban MObility
MSDevice_Battery.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-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 /****************************************************************************/
16 // The Battery parameters for the vehicle
17 /****************************************************************************/
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
27 #include <utils/common/SUMOTime.h>
28 #include <utils/geom/GeomHelper.h>
30 #include <microsim/MSNet.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSVehicle.h>
34 #include "MSDevice_Tripinfo.h"
35 #include "MSDevice_Battery.h"
36 
37 #define DEFAULT_MAX_CAPACITY 35000
38 #define DEFAULT_CHARGE_RATIO 0.5
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 // ---------------------------------------------------------------------------
45 // static initialisation methods
46 // ---------------------------------------------------------------------------
47 void
49  insertDefaultAssignmentOptions("battery", "Battery", oc);
50 }
51 
52 
53 void
54 MSDevice_Battery::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
55  // Check if vehicle should get a battery
56  if (equippedByDefaultAssignmentOptions(OptionsCont::getOptions(), "battery", v, false)) {
58  const SUMOVTypeParameter& typeParams = v.getVehicleType().getParameter();
59  std::map<int, double> param;
60  // obtain maximumBatteryCapacity
61  const double maximumBatteryCapacity = typeParams.getDouble(toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY), DEFAULT_MAX_CAPACITY);
62 
63  // obtain actualBatteryCapacity
64  double actualBatteryCapacity = 0;
66  actualBatteryCapacity = maximumBatteryCapacity * DEFAULT_CHARGE_RATIO;
67  } else {
69  }
70 
71  const double powerMax = typeParams.getDouble(toString(SUMO_ATTR_MAXIMUMPOWER), 100.);
72  const double stoppingTreshold = typeParams.getDouble(toString(SUMO_ATTR_STOPPINGTRESHOLD), 0.1);
73 
84 
85  // battery constructor
86  MSDevice_Battery* device = new MSDevice_Battery(v, "battery_" + v.getID(),
87  actualBatteryCapacity, maximumBatteryCapacity, powerMax, stoppingTreshold, param);
88 
89  // Add device to vehicle
90  into.push_back(device);
91  }
92 }
93 
94 
95 bool MSDevice_Battery::notifyMove(SUMOTrafficObject& tObject, double /* oldPos */, double /* newPos */, double /* newSpeed */) {
96  if (!tObject.isVehicle()) {
97  return false;
98  }
99  SUMOVehicle& veh = static_cast<SUMOVehicle&>(tObject);
100  // Start vehicleStoppedTimer if the vehicle is stopped. In other case reset timer
101  if (veh.getSpeed() < myStoppingTreshold) {
102  // Increase vehicle stopped timer
104  } else {
105  // Reset vehicle Stopped
107  }
108 
109  // Update Energy from the battery
110  if (getMaximumBatteryCapacity() != 0) {
111  myParam[SUMO_ATTR_ANGLE] = myLastAngle == std::numeric_limits<double>::infinity() ? 0. : GeomHelper::angleDiff(myLastAngle, veh.getAngle());
113 
114  // Energy lost/gained from vehicle movement (via vehicle energy model) [Wh]
116 
117  // saturate between 0 and myMaximumBatteryCapacity [Wh]
118  if (getActualBatteryCapacity() < 0) {
120  if (getMaximumBatteryCapacity() > 0) {
121  WRITE_WARNING("Battery of vehicle '" + veh.getID() + "' is depleted.")
122  }
125  }
126  myLastAngle = veh.getAngle();
127  }
128 
129  // Check if vehicle has under their position one charge Station
130  const std::string chargingStationID = MSNet::getInstance()->getStoppingPlaceID(veh.getLane(), veh.getPositionOnLane(), SUMO_TAG_CHARGING_STATION);
131 
132  // If vehicle is over a charging station
133  if (chargingStationID != "") {
134  // if the vehicle is almost stopped, or charge in transit is enabled, then charge vehicle
135  MSChargingStation* const cs = static_cast<MSChargingStation*>(MSNet::getInstance()->getStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION));
136  if ((veh.getSpeed() < myStoppingTreshold) || cs->getChargeInTransit()) {
137  // Set Flags Stopped/intransit to
138  if (veh.getSpeed() < myStoppingTreshold) {
139  // vehicle ist almost stopped, then is charging stopped
140  myChargingStopped = true;
141 
142  // therefore isn't charging in transit
143  myChargingInTransit = false;
144  } else {
145  // vehicle is moving, and the Charging station allow charge in transit
146  myChargingStopped = false;
147 
148  // Therefore charge in transit
149  myChargingInTransit = true;
150  }
151 
152  // get pointer to charging station
154 
155  // Only update charging start time if vehicle allow charge in transit, or in other case
156  // if the vehicle not allow charge in transit but it's stopped.
158  // Update Charging start time
160  }
161 
162  // time it takes the vehicle at the station < charging station time delay?
164  // Enable charging vehicle
166 
167  // Calulate energy charged
169 
170  // Convert from [Ws] to [Wh] (3600s / 1h):
171  myEnergyCharged /= 3600;
172 
173  // Update Battery charge
176  } else {
178  }
179  }
180  // add charge value for output to myActChargingStation
182  }
183  }
184  // In other case, vehicle will be not charged
185  else {
186  // Disable flags
187  myChargingInTransit = false;
188  myChargingStopped = false;
189 
190  // Disable charging vehicle
191  if (myActChargingStation != nullptr) {
193  }
194 
195  // Set charging station pointer to NULL
196  myActChargingStation = nullptr;
197 
198  // Set energy charged to 0
199  myEnergyCharged = 0.00;
200 
201  // Reset timer
203  }
204 
205  // Always return true.
206  return true;
207 }
208 
209 
210 // ---------------------------------------------------------------------------
211 // MSDevice_Battery-methods
212 // ---------------------------------------------------------------------------
213 MSDevice_Battery::MSDevice_Battery(SUMOVehicle& holder, const std::string& id, const double actualBatteryCapacity, const double maximumBatteryCapacity,
214  const double powerMax, const double stoppingTreshold, const std::map<int, double>& param) :
215  MSVehicleDevice(holder, id),
216  myActualBatteryCapacity(0), // [actualBatteryCapacity <= maximumBatteryCapacity]
217  myMaximumBatteryCapacity(0), // [maximumBatteryCapacity >= 0]
218  myPowerMax(0), // [maximumPower >= 0]
219  myStoppingTreshold(0), // [stoppingTreshold >= 0]
220  myParam(param),
221  myLastAngle(std::numeric_limits<double>::infinity()),
222  myChargingStopped(false), // Initially vehicle don't charge stopped
223  myChargingInTransit(false), // Initially vehicle don't charge in transit
224  myConsum(0), // Initially the vehicle is stopped and therefore the consum is zero.
225  myActChargingStation(nullptr), // Initially the vehicle isn't over a Charging Station
226  myEnergyCharged(0), // Initially the energy charged is zero
227  myVehicleStopped(0) { // Initially the vehicle is stopped and the corresponding variable is 0
228 
229  if (maximumBatteryCapacity < 0) {
230  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + ").")
231  } else {
232  myMaximumBatteryCapacity = maximumBatteryCapacity;
233  }
234 
235  if (actualBatteryCapacity > maximumBatteryCapacity) {
236  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' has a " + toString(SUMO_ATTR_ACTUALBATTERYCAPACITY) + " (" + toString(actualBatteryCapacity) + ") greater than it's " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + "). A max battery capacity value will be asigned");
238  } else {
239  myActualBatteryCapacity = actualBatteryCapacity;
240  }
241 
242  if (powerMax < 0) {
243  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMPOWER) + " (" + toString(powerMax) + ").")
244  } else {
245  myPowerMax = powerMax;
246  }
247 
248  if (stoppingTreshold < 0) {
249  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_STOPPINGTRESHOLD) + " (" + toString(stoppingTreshold) + ").")
250  } else {
251  myStoppingTreshold = stoppingTreshold;
252  }
253 
264 }
265 
266 
268 }
269 
270 
271 void
272 MSDevice_Battery::checkParam(const SumoXMLAttr paramKey, const double lower, const double upper) {
273  if (myParam.find(paramKey) == myParam.end() || myParam.find(paramKey)->second < lower || myParam.find(paramKey)->second > upper) {
274  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(paramKey) + " (" + toString(myParam[paramKey]) + ").");
276  }
277 }
278 
279 
280 void
281 MSDevice_Battery::setActualBatteryCapacity(const double actualBatteryCapacity) {
282  if (actualBatteryCapacity < 0) {
284  } else if (actualBatteryCapacity > myMaximumBatteryCapacity) {
286  } else {
287  myActualBatteryCapacity = actualBatteryCapacity;
288  }
289 }
290 
291 
292 void
293 MSDevice_Battery::setMaximumBatteryCapacity(const double maximumBatteryCapacity) {
294  if (myMaximumBatteryCapacity < 0) {
295  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + ").")
296  } else {
297  myMaximumBatteryCapacity = maximumBatteryCapacity;
298  }
299 }
300 
301 
302 void
303 MSDevice_Battery::setPowerMax(const double powerMax) {
304  if (myPowerMax < 0) {
305  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_MAXIMUMPOWER) + " (" + toString(powerMax) + ").")
306  } else {
307  myPowerMax = powerMax;
308  }
309 }
310 
311 
312 void
313 MSDevice_Battery::setStoppingTreshold(const double stoppingTreshold) {
314  if (stoppingTreshold < 0) {
315  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_STOPPINGTRESHOLD) + " (" + toString(stoppingTreshold) + ").")
316  } else {
317  myStoppingTreshold = stoppingTreshold;
318  }
319 }
320 
321 
322 void
325 }
326 
327 
328 void
331 }
332 
333 
334 void
336  myVehicleStopped = 0;
337 }
338 
339 
340 void
343 }
344 
345 
346 double
349 }
350 
351 
352 double
355 }
356 
357 
358 double
360  return myPowerMax;
361 }
362 
363 
364 double
366  return myConsum;
367 }
368 
369 
370 bool
372  return myChargingStopped;
373 }
374 
375 
376 bool
378  return myChargingInTransit;
379 }
380 
381 
382 double
384  return myChargingStartTime;
385 }
386 
387 
388 std::string
390  if (myActChargingStation != nullptr) {
391  return myActChargingStation->getID();
392  } else {
393  return "NULL";
394  }
395 }
396 
397 double
399  return myEnergyCharged;
400 }
401 
402 
403 int
405  return myVehicleStopped;
406 }
407 
408 
409 double
411  return myStoppingTreshold;
412 }
413 
414 
415 std::string
416 MSDevice_Battery::getParameter(const std::string& key) const {
419  } else if (key == toString(SUMO_ATTR_ENERGYCONSUMED)) {
420  return toString(getConsum());
421  } else if (key == toString(SUMO_ATTR_ENERGYCHARGED)) {
422  return toString(getEnergyCharged());
423  } else if (key == toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY)) {
425  } else if (key == toString(SUMO_ATTR_CHARGINGSTATIONID)) {
426  return getChargingStationID();
427  } else if (key == toString(SUMO_ATTR_VEHICLEMASS)) {
428  return toString(myParam.find(SUMO_ATTR_VEHICLEMASS)->second);
429  }
430  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
431 }
432 
433 
434 void
435 MSDevice_Battery::setParameter(const std::string& key, const std::string& value) {
436  double doubleValue;
437  try {
438  doubleValue = StringUtils::toDouble(value);
439  } catch (NumberFormatException&) {
440  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
441  }
443  setActualBatteryCapacity(doubleValue);
444  } else if (key == toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY)) {
445  setMaximumBatteryCapacity(doubleValue);
446  } else if (key == toString(SUMO_ATTR_VEHICLEMASS)) {
447  myParam[SUMO_ATTR_VEHICLEMASS] = doubleValue;
448  } else {
449  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
450  }
451 }
452 
453 /****************************************************************************/
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key ...
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Internal moment of inertia.
MSChargingStation * myActChargingStation
Parameter, Pointer to current charging station in which vehicle is placed (by default is NULL) ...
double getStoppingTreshold() const
Get stopping treshold.
double getMaximumBatteryCapacity() const
Get the total vehicle&#39;s Battery Capacity in kWh.
std::map< int, double > myParam
Parameter collection.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
double myActualBatteryCapacity
Parameter, The actual vehicles&#39;s Battery Capacity in kWh, [myActualBatteryCapacity <= myMaximumBatter...
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:899
std::string getStoppingPlaceID(const MSLane *lane, const double pos, const SumoXMLTag category) const
Returns the stop of the given category close to the given position.
Definition: MSNet.cpp:908
void increaseVehicleStoppedTimer()
Increase myVehicleStopped.
#define DEFAULT_MAX_CAPACITY
double getChargingStartTime() const
Get charging start time.
double getActualBatteryCapacity() const
Get the actual vehicle&#39;s Battery Capacity in kWh.
Structure representing possible vehicle parameter.
virtual double getAcceleration() const =0
Returns the vehicle&#39;s acceleration.
MSDevice_Battery(SUMOVehicle &holder, const std::string &id, const double actualBatteryCapacity, const double maximumBatteryCapacity, const double powerMax, const double stoppingTreshold, const std::map< int, double > &param)
Constructor.
void checkParam(const SumoXMLAttr paramKey, const double lower=0., const double upper=std::numeric_limits< double >::infinity())
virtual bool isVehicle() const =0
Get the vehicle&#39;s ID.
int myVehicleStopped
Parameter, How many timestep the vehicle is stopped.
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
tgotal of Energy charged
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
std::string getChargingStationID() const
Get current Charging Station ID.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
const std::string & getID() const
Returns the id.
Definition: Named.h:77
#define TS
Definition: SUMOTime.h:44
int getVehicleStopped() const
Get number of timestep that vehicle is stopped.
bool myChargingStopped
Parameter, Flag: Vehicles it&#39;s charging stopped (by default is false)
void setPowerMax(const double new_Pmax)
Set maximum power when accelerating.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool isChargingStopped() const
Get true if Vehicle is charging, false if not.
Radial drag coefficient.
Helper methods for energy-based electricity consumption computation based on the battery device...
Definition: HelpersEnergy.h:43
void setMaximumBatteryCapacity(const double maximumBatteryCapacity)
Set total vehicle&#39;s Battery Capacity in kWh.
bool getChargeInTransit() const
Get chargeInTransit.
void setActualBatteryCapacity(const double actualBatteryCapacity)
Set actual vehicle&#39;s Battery Capacity in kWh.
void resetVehicleStoppedTimer()
Reset myVehicleStopped.
double myChargingStartTime
Parameter, Moment, wich the vehicle has beging to charging.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Representation of a vehicle.
Definition: SUMOVehicle.h:61
#define DEFAULT_CHARGE_RATIO
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
Recuperation efficiency (constant)
double getDefaultParam(int paramKey) const
Definition: HelpersEnergy.h:65
void resetChargingStartTime()
Reset charging start time.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
void setChargingVehicle(bool value)
enable or disable charging vehicle
double getChargeDelay() const
Get Charge Delay.
virtual double getSlope() const =0
Returns the slope of the road at vehicle&#39;s position.
double getEnergyCharged() const
Get charged energy.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:126
double myMaximumBatteryCapacity
Parameter, The total vehicles&#39;s Battery Capacity in kWh, [myMaximumBatteryCapacity >= 0]...
const std::string deviceName() const
return the name for this type of device
void setStoppingTreshold(const double stoppingTreshold)
Set vehicle&#39;s stopping treshold.
bool myChargingInTransit
Parameter, Flag: Vehicles it&#39;s charging in transit (by default is false)
const SUMOVTypeParameter & getParameter() const
double myConsum
Parameter, Vehicle consum during a time step (by default is 0.)
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
double getEfficency() const
Get efficiency of the charging station.
virtual double getAngle() const =0
Get the vehicle&#39;s angle.
Battery device for electric vehicles.
double getMaximumPower() const
Get the maximum power when accelerating.
Representation of a vehicle or person.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.h:204
double myStoppingTreshold
Parameter, stopping vehicle treshold [myStoppingTreshold >= 0].
void increaseChargingStartTime()
Increase Charging Start time.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
double myLastAngle
Parameter, Vehicle&#39;s last angle.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
double getChargingPower() const
Get charging station&#39;s charging power.
A storage for options typed value containers)
Definition: OptionsCont.h:90
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Abstract in-vehicle device.
Recuperation efficiency (by deceleration)
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
bool isChargingInTransit() const
Get true if Vehicle it&#39;s charging, false if not.
double myEnergyCharged
Parameter, Energy charged in each timestep.
double getConsum() const
Get consum.
static double angleDiff(const double angle1, const double angle2)
Returns the difference of the second angle to the first angle in radiants.
Definition: GeomHelper.cpp:181
double myPowerMax
Parameter, The Maximum Power when accelerating, [myPowerMax >= 0].
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
static const HelpersEnergy & getEnergyHelper()
get energy helper
double compute(const SUMOEmissionClass c, const PollutantsInterface::EmissionType e, const double v, const double a, const double slope, const std::map< int, double > *param) const
Computes the emitted pollutant amount using the given speed and acceleration.
~MSDevice_Battery()
Destructor.