SUMO - Simulation of Urban MObility
MSDevice_Battery.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // The Battery parameters for the vehicle
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
34 #include <utils/common/SUMOTime.h>
35 #include <utils/geom/GeomHelper.h>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSEdge.h>
40 #include <microsim/MSVehicle.h>
41 #include "MSDevice_Tripinfo.h"
42 #include "MSDevice_Battery.h"
43 
44 #define DEFAULT_MAX_CAPACITY 35000
45 #define DEFAULT_CHARGE_RATIO 0.5
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // static initialisation methods
53 // ---------------------------------------------------------------------------
54 void
56  insertDefaultAssignmentOptions("battery", "Battery", oc);
57 }
58 
59 
60 void
61 MSDevice_Battery::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
62  // Check if vehicle should get a battery
65  const SUMOVTypeParameter& typeParams = v.getVehicleType().getParameter();
66  std::map<int, double> param;
67  // obtain maximumBatteryCapacity
68  const double maximumBatteryCapacity = typeParams.getDouble(toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY), DEFAULT_MAX_CAPACITY);
69 
70  // obtain actualBatteryCapacity
71  double actualBatteryCapacity = 0;
73  actualBatteryCapacity = maximumBatteryCapacity * DEFAULT_CHARGE_RATIO;
74  } else {
76  }
77 
78  const double powerMax = typeParams.getDouble(toString(SUMO_ATTR_MAXIMUMPOWER), 100.);
79  const double stoppingTreshold = typeParams.getDouble(toString(SUMO_ATTR_STOPPINGTRESHOLD), 0.1);
80 
90 
91  // battery constructor
92  MSDevice_Battery* device = new MSDevice_Battery(v, "battery_" + v.getID(),
93  actualBatteryCapacity, maximumBatteryCapacity, powerMax, stoppingTreshold, param);
94 
95  // Add device to vehicle
96  into.push_back(device);
97  }
98 }
99 
100 
101 bool MSDevice_Battery::notifyMove(SUMOVehicle& veh, double /* oldPos */, double /* newPos */, double /* newSpeed */) {
102  // Start vehicleStoppedTimer if the vehicle is stopped. In other case reset timer
103  if (veh.getSpeed() < myStoppingTreshold) {
104  // Increase vehicle stopped timer
106  } else {
107  // Reset vehicle Stopped
109  }
110 
111  // Update Energy from the battery
112  if (getMaximumBatteryCapacity() != 0) {
113  myParam[SUMO_ATTR_ANGLE] = myLastAngle == std::numeric_limits<double>::infinity() ? 0. : GeomHelper::angleDiff(myLastAngle, veh.getAngle());
115 
116  // Energy lost/gained from vehicle movement (via vehicle energy model) [Wh]
118 
119  // saturate between 0 and myMaximumBatteryCapacity [Wh]
120  if (getActualBatteryCapacity() < 0) {
122  if (getMaximumBatteryCapacity() > 0) {
123  WRITE_WARNING("Battery of vehicle '" + veh.getID() + "' is depleted.")
124  }
127  }
128  myLastAngle = veh.getAngle();
129  }
130 
131  // Check if vehicle has under their position one charge Station
132  const std::string chargingStationID = MSNet::getInstance()->getChargingStationID(veh.getLane(), veh.getPositionOnLane());
133 
134  // If vehicle is over a charging station
135  if (chargingStationID != "") {
136  // if the vehicle is almost stopped, or charge in transit is enabled, then charge vehicle
137  if ((veh.getSpeed() < myStoppingTreshold) || (MSNet::getInstance()->getChargingStation(chargingStationID)->getChargeInTransit() == 1)) {
138  // Set Flags Stopped/intransit to
139  if (veh.getSpeed() < myStoppingTreshold) {
140  // vehicle ist almost stopped, then is charging stopped
141  myChargingStopped = true;
142 
143  // therefore isn't charging in transit
144  myChargingInTransit = false;
145  } else {
146  // vehicle is moving, and the Charging station allow charge in transit
147  myChargingStopped = false;
148 
149  // Therefore charge in transit
150  myChargingInTransit = true;
151  }
152 
153  // get pointer to charging station
155 
156  // Only update charging start time if vehicle allow charge in transit, or in other case
157  // if the vehicle not allow charge in transit but it's stopped.
158  if ((myActChargingStation->getChargeInTransit() == true) || (veh.getSpeed() < myStoppingTreshold)) {
159  // Update Charging start time
161  }
162 
163  // time it takes the vehicle at the station < charging station time delay?
165  // Enable charging vehicle
167 
168  // Calulate energy charged
170 
171  // Convert from [Ws] to [Wh] (3600s / 1h):
172  myEnergyCharged /= 3600;
173 
174  // Update Battery charge
177  } else {
179  }
180  }
181  // add charge value for output to myActChargingStation
183  }
184  }
185  // In other case, vehicle will be not charged
186  else {
187  // Disable flags
188  myChargingInTransit = false;
189  myChargingStopped = false;
190 
191  // Disable charging vehicle
192  if (myActChargingStation != NULL) {
194  }
195 
196  // Set charging station pointer to NULL
197  myActChargingStation = NULL;
198 
199  // Set energy charged to 0
200  myEnergyCharged = 0.00;
201 
202  // Reset timer
204  }
205 
206  // Always return true.
207  return true;
208 }
209 
210 
211 // ---------------------------------------------------------------------------
212 // MSDevice_Battery-methods
213 // ---------------------------------------------------------------------------
214 MSDevice_Battery::MSDevice_Battery(SUMOVehicle& holder, const std::string& id, const double actualBatteryCapacity, const double maximumBatteryCapacity,
215  const double powerMax, const double stoppingTreshold, const std::map<int, double>& param) :
216  MSDevice(holder, id),
217  myActualBatteryCapacity(0), // [actualBatteryCapacity <= maximumBatteryCapacity]
218  myMaximumBatteryCapacity(0), // [maximumBatteryCapacity >= 0]
219  myPowerMax(0), // [maximumPower >= 0]
220  myStoppingTreshold(0), // [stoppingTreshold >= 0]
221  myParam(param),
222  myLastAngle(std::numeric_limits<double>::infinity()),
223  myChargingStopped(false), // Initially vehicle don't charge stopped
224  myChargingInTransit(false), // Initially vehicle don't charge in transit
225  myConsum(0), // Initially the vehicle is stopped and therefore the consum is zero.
226  myActChargingStation(NULL), // Initially the vehicle isn't over a Charging Station
227  myEnergyCharged(0), // Initially the energy charged is zero
228  myVehicleStopped(0) { // Initially the vehicle is stopped and the corresponding variable is 0
229 
230  if (maximumBatteryCapacity < 0) {
231  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + ").")
232  } else {
233  myMaximumBatteryCapacity = maximumBatteryCapacity;
234  }
235 
236  if (actualBatteryCapacity > maximumBatteryCapacity) {
237  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");
239  } else {
240  myActualBatteryCapacity = actualBatteryCapacity;
241  }
242 
243  if (powerMax < 0) {
244  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMPOWER) + " (" + toString(powerMax) + ").")
245  } else {
246  myPowerMax = powerMax;
247  }
248 
249  if (stoppingTreshold < 0) {
250  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_STOPPINGTRESHOLD) + " (" + toString(stoppingTreshold) + ").")
251  } else {
252  myStoppingTreshold = stoppingTreshold;
253  }
254 
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 != NULL) {
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  }
428  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
429 }
430 
431 
432 void
433 MSDevice_Battery::setParameter(const std::string& key, const std::string& value) {
434  double doubleValue;
435  try {
436  doubleValue = TplConvert::_2double(value.c_str());
437  } catch (NumberFormatException) {
438  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
439  }
441  setActualBatteryCapacity(doubleValue);
442  } else if (key == toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY)) {
443  setMaximumBatteryCapacity(doubleValue);
444  } else {
445  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
446  }
447 }
448 
449 /****************************************************************************/
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(SUMOVehicle &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.
double myActualBatteryCapacity
Parameter, The actual vehicles&#39;s Battery Capacity in kWh, [myActualBatteryCapacity <= myMaximumBatter...
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.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Structure representing possible vehicle parameter.
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())
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:158
std::string getChargingStationID() const
Get current Charging Station ID.
std::string getChargingStationID(const MSLane *lane, const double pos) const
Returns the charging station close to the given position.
Definition: MSNet.cpp:903
virtual double getSlope() const =0
Returns the slope of the road at vehicle&#39;s position.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
int getVehicleStopped() const
Get number of timestep that vehicle is stopped.
MSChargingStation * getChargingStation(const std::string &id) const
Returns the named charging station.
Definition: MSNet.cpp:897
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:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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:50
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:56
Representation of a vehicle.
Definition: SUMOVehicle.h:67
#define DEFAULT_CHARGE_RATIO
double getDefaultParam(int paramKey) const
Definition: HelpersEnergy.h:72
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.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:93
double getEnergyCharged() const
Get charged energy.
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.
Abstract in-vehicle device.
Definition: MSDevice.h:71
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:107
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.
double myStoppingTreshold
Parameter, stopping vehicle treshold [myStoppingTreshold >= 0].
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
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:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
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:174
double myPowerMax
Parameter, The Maximum Power when accelerating, [myPowerMax >= 0].
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static const HelpersEnergy & getEnergyHelper()
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.
virtual double getAcceleration() const =0
Returns the vehicle&#39;s acceleration.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.