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-2018 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 
83 
84  // battery constructor
85  MSDevice_Battery* device = new MSDevice_Battery(v, "battery_" + v.getID(),
86  actualBatteryCapacity, maximumBatteryCapacity, powerMax, stoppingTreshold, param);
87 
88  // Add device to vehicle
89  into.push_back(device);
90  }
91 }
92 
93 
94 bool MSDevice_Battery::notifyMove(SUMOVehicle& veh, double /* oldPos */, double /* newPos */, double /* newSpeed */) {
95  // Start vehicleStoppedTimer if the vehicle is stopped. In other case reset timer
96  if (veh.getSpeed() < myStoppingTreshold) {
97  // Increase vehicle stopped timer
99  } else {
100  // Reset vehicle Stopped
102  }
103 
104  // Update Energy from the battery
105  if (getMaximumBatteryCapacity() != 0) {
106  myParam[SUMO_ATTR_ANGLE] = myLastAngle == std::numeric_limits<double>::infinity() ? 0. : GeomHelper::angleDiff(myLastAngle, veh.getAngle());
108 
109  // Energy lost/gained from vehicle movement (via vehicle energy model) [Wh]
111 
112  // saturate between 0 and myMaximumBatteryCapacity [Wh]
113  if (getActualBatteryCapacity() < 0) {
115  if (getMaximumBatteryCapacity() > 0) {
116  WRITE_WARNING("Battery of vehicle '" + veh.getID() + "' is depleted.")
117  }
120  }
121  myLastAngle = veh.getAngle();
122  }
123 
124  // Check if vehicle has under their position one charge Station
125  const std::string chargingStationID = MSNet::getInstance()->getStoppingPlaceID(veh.getLane(), veh.getPositionOnLane(), SUMO_TAG_CHARGING_STATION);
126 
127  // If vehicle is over a charging station
128  if (chargingStationID != "") {
129  // if the vehicle is almost stopped, or charge in transit is enabled, then charge vehicle
130  MSChargingStation* const cs = static_cast<MSChargingStation*>(MSNet::getInstance()->getStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION));
131  if ((veh.getSpeed() < myStoppingTreshold) || cs->getChargeInTransit()) {
132  // Set Flags Stopped/intransit to
133  if (veh.getSpeed() < myStoppingTreshold) {
134  // vehicle ist almost stopped, then is charging stopped
135  myChargingStopped = true;
136 
137  // therefore isn't charging in transit
138  myChargingInTransit = false;
139  } else {
140  // vehicle is moving, and the Charging station allow charge in transit
141  myChargingStopped = false;
142 
143  // Therefore charge in transit
144  myChargingInTransit = true;
145  }
146 
147  // get pointer to charging station
149 
150  // Only update charging start time if vehicle allow charge in transit, or in other case
151  // if the vehicle not allow charge in transit but it's stopped.
153  // Update Charging start time
155  }
156 
157  // time it takes the vehicle at the station < charging station time delay?
159  // Enable charging vehicle
161 
162  // Calulate energy charged
164 
165  // Convert from [Ws] to [Wh] (3600s / 1h):
166  myEnergyCharged /= 3600;
167 
168  // Update Battery charge
171  } else {
173  }
174  }
175  // add charge value for output to myActChargingStation
177  }
178  }
179  // In other case, vehicle will be not charged
180  else {
181  // Disable flags
182  myChargingInTransit = false;
183  myChargingStopped = false;
184 
185  // Disable charging vehicle
186  if (myActChargingStation != nullptr) {
188  }
189 
190  // Set charging station pointer to NULL
191  myActChargingStation = nullptr;
192 
193  // Set energy charged to 0
194  myEnergyCharged = 0.00;
195 
196  // Reset timer
198  }
199 
200  // Always return true.
201  return true;
202 }
203 
204 
205 // ---------------------------------------------------------------------------
206 // MSDevice_Battery-methods
207 // ---------------------------------------------------------------------------
208 MSDevice_Battery::MSDevice_Battery(SUMOVehicle& holder, const std::string& id, const double actualBatteryCapacity, const double maximumBatteryCapacity,
209  const double powerMax, const double stoppingTreshold, const std::map<int, double>& param) :
210  MSVehicleDevice(holder, id),
211  myActualBatteryCapacity(0), // [actualBatteryCapacity <= maximumBatteryCapacity]
212  myMaximumBatteryCapacity(0), // [maximumBatteryCapacity >= 0]
213  myPowerMax(0), // [maximumPower >= 0]
214  myStoppingTreshold(0), // [stoppingTreshold >= 0]
215  myParam(param),
216  myLastAngle(std::numeric_limits<double>::infinity()),
217  myChargingStopped(false), // Initially vehicle don't charge stopped
218  myChargingInTransit(false), // Initially vehicle don't charge in transit
219  myConsum(0), // Initially the vehicle is stopped and therefore the consum is zero.
220  myActChargingStation(nullptr), // Initially the vehicle isn't over a Charging Station
221  myEnergyCharged(0), // Initially the energy charged is zero
222  myVehicleStopped(0) { // Initially the vehicle is stopped and the corresponding variable is 0
223 
224  if (maximumBatteryCapacity < 0) {
225  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + ").")
226  } else {
227  myMaximumBatteryCapacity = maximumBatteryCapacity;
228  }
229 
230  if (actualBatteryCapacity > maximumBatteryCapacity) {
231  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");
233  } else {
234  myActualBatteryCapacity = actualBatteryCapacity;
235  }
236 
237  if (powerMax < 0) {
238  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_MAXIMUMPOWER) + " (" + toString(powerMax) + ").")
239  } else {
240  myPowerMax = powerMax;
241  }
242 
243  if (stoppingTreshold < 0) {
244  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(SUMO_ATTR_STOPPINGTRESHOLD) + " (" + toString(stoppingTreshold) + ").")
245  } else {
246  myStoppingTreshold = stoppingTreshold;
247  }
248 
258 }
259 
260 
262 }
263 
264 
265 void
266 MSDevice_Battery::checkParam(const SumoXMLAttr paramKey, const double lower, const double upper) {
267  if (myParam.find(paramKey) == myParam.end() || myParam.find(paramKey)->second < lower || myParam.find(paramKey)->second > upper) {
268  WRITE_WARNING("Battery builder: Vehicle '" + getID() + "' doesn't have a valid value for parameter " + toString(paramKey) + " (" + toString(myParam[paramKey]) + ").");
270  }
271 }
272 
273 
274 void
275 MSDevice_Battery::setActualBatteryCapacity(const double actualBatteryCapacity) {
276  if (actualBatteryCapacity < 0) {
278  } else if (actualBatteryCapacity > myMaximumBatteryCapacity) {
280  } else {
281  myActualBatteryCapacity = actualBatteryCapacity;
282  }
283 }
284 
285 
286 void
287 MSDevice_Battery::setMaximumBatteryCapacity(const double maximumBatteryCapacity) {
288  if (myMaximumBatteryCapacity < 0) {
289  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY) + " (" + toString(maximumBatteryCapacity) + ").")
290  } else {
291  myMaximumBatteryCapacity = maximumBatteryCapacity;
292  }
293 }
294 
295 
296 void
297 MSDevice_Battery::setPowerMax(const double powerMax) {
298  if (myPowerMax < 0) {
299  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_MAXIMUMPOWER) + " (" + toString(powerMax) + ").")
300  } else {
301  myPowerMax = powerMax;
302  }
303 }
304 
305 
306 void
307 MSDevice_Battery::setStoppingTreshold(const double stoppingTreshold) {
308  if (stoppingTreshold < 0) {
309  WRITE_WARNING("Trying to set into the battery device of vehicle '" + getID() + "' an invalid " + toString(SUMO_ATTR_STOPPINGTRESHOLD) + " (" + toString(stoppingTreshold) + ").")
310  } else {
311  myStoppingTreshold = stoppingTreshold;
312  }
313 }
314 
315 
316 void
319 }
320 
321 
322 void
325 }
326 
327 
328 void
330  myVehicleStopped = 0;
331 }
332 
333 
334 void
337 }
338 
339 
340 double
343 }
344 
345 
346 double
349 }
350 
351 
352 double
354  return myPowerMax;
355 }
356 
357 
358 double
360  return myConsum;
361 }
362 
363 
364 bool
366  return myChargingStopped;
367 }
368 
369 
370 bool
372  return myChargingInTransit;
373 }
374 
375 
376 double
378  return myChargingStartTime;
379 }
380 
381 
382 std::string
384  if (myActChargingStation != nullptr) {
385  return myActChargingStation->getID();
386  } else {
387  return "NULL";
388  }
389 }
390 
391 double
393  return myEnergyCharged;
394 }
395 
396 
397 int
399  return myVehicleStopped;
400 }
401 
402 
403 double
405  return myStoppingTreshold;
406 }
407 
408 
409 std::string
410 MSDevice_Battery::getParameter(const std::string& key) const {
413  } else if (key == toString(SUMO_ATTR_ENERGYCONSUMED)) {
414  return toString(getConsum());
415  } else if (key == toString(SUMO_ATTR_ENERGYCHARGED)) {
416  return toString(getEnergyCharged());
417  } else if (key == toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY)) {
419  } else if (key == toString(SUMO_ATTR_CHARGINGSTATIONID)) {
420  return getChargingStationID();
421  } else if (key == toString(SUMO_ATTR_VEHICLEMASS)) {
422  return toString(myParam.find(SUMO_ATTR_VEHICLEMASS)->second);
423  }
424  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
425 }
426 
427 
428 void
429 MSDevice_Battery::setParameter(const std::string& key, const std::string& value) {
430  double doubleValue;
431  try {
432  doubleValue = StringUtils::toDouble(value);
433  } catch (NumberFormatException&) {
434  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
435  }
437  setActualBatteryCapacity(doubleValue);
438  } else if (key == toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY)) {
439  setMaximumBatteryCapacity(doubleValue);
440  } else if (key == toString(SUMO_ATTR_VEHICLEMASS)) {
441  myParam[SUMO_ATTR_VEHICLEMASS] = doubleValue;
442  } else {
443  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
444  }
445 }
446 
447 /****************************************************************************/
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...
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:869
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:878
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.
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:165
std::string getChargingStationID() const
Get current Charging Station ID.
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:78
#define TS
Definition: SUMOTime.h:45
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:241
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:49
Representation of a vehicle.
Definition: SUMOVehicle.h:60
#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 ...
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.
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:121
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.
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:208
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:92
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Abstract in-vehicle device.
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:167
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.