Eclipse SUMO - Simulation of Urban MObility
MSChargingStation.cpp
Go to the documentation of this file.
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 /****************************************************************************/
20 // Charging Station for Electric vehicles
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <cassert>
27 #include <microsim/MSParkingArea.h>
28 #include <microsim/MSVehicleType.h>
31 #include <microsim/MSNet.h>
32 #include "MSChargingStation.h"
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 
39 MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
40  const std::string& name, double chargingPower, double efficency, bool chargeInTransit,
41  SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
42  MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
43  myChargeInTransit(chargeInTransit) {
44  if (chargingPower < 0) {
45  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
46  } else {
47  myChargingPower = chargingPower;
48  }
49  if (efficency < 0 || efficency > 1) {
50  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
51  } else {
52  myEfficiency = efficency;
53  }
54  if (chargeDelay < 0) {
55  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
56  } else {
57  myChargeDelay = chargeDelay;
58  }
59  if ((chargeType != "normal") && (chargeType != "electric") && (chargeType != "fuel")) {
60  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGETYPE), getID(), chargeType))
61  } else {
62  myChargeDelay = chargeDelay;
63  }
64  if (waitingTime < 0) {
65  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
66  } else {
67  myChargeDelay = chargeDelay;
68  }
70  WRITE_WARNING(TLF("ChargingStation with ID='getID()' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
71  }
72 }
73 
74 
75 MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
76  double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
77  MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
78  name, chargingPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
79  myParkingArea = parkingArea;
80 }
81 
82 
84 }
85 
86 
87 double
88 MSChargingStation::getChargingPower(bool usingFuel) const {
89  if (usingFuel) {
90  return myChargingPower;
91  } else {
92  // Convert from [Ws] to [Wh] (3600s / 1h):
93  return myChargingPower / 3600;
94  }
95 }
96 
97 
98 double
100  return myEfficiency;
101 }
102 
103 
104 bool
106  return myChargeInTransit;
107 }
108 
109 
110 SUMOTime
112  return myChargeDelay;
113 }
114 
115 
116 const std::string&
118  return myChargeType;
119 }
120 
121 
122 SUMOTime
124  return myWaitingTime;
125 }
126 
127 
128 const MSParkingArea*
130  return myParkingArea;
131 }
132 
133 
134 void
135 MSChargingStation::setChargingPower(double chargingPower) {
136  myChargingPower = chargingPower;
137 }
138 
139 
140 void
142  myEfficiency = efficiency;
143 }
144 
145 
146 void
148  myChargeDelay = delay;
149 }
150 
151 
152 void
154  myChargeInTransit = value;
155 }
156 
157 
158 void
160  myChargingVehicle = value;
161 }
162 
163 
164 bool
165 MSChargingStation::vehicleIsInside(const double position) const {
166  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
167  return true;
168  } else {
169  return false;
170  }
171 }
172 
173 
174 bool
176  return myChargingVehicle;
177 }
178 
179 
180 void
182  std::string status = "";
183  if (battery->getChargingStartTime() > myChargeDelay) {
184  if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
185  status = "chargingStopped";
186  } else if (myChargeInTransit) {
187  status = "chargingInTransit";
188  } else {
189  status = "noCharging";
190  }
191  } else {
192  if (myChargeInTransit) {
193  status = "waitingChargeInTransit";
194  } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
195  status = "waitingChargeStopped";
196  } else {
197  status = "noWaitingCharge";
198  }
199  }
200  // update total charge
201  myTotalCharge += WCharged;
202  // create charge row and insert it in myChargeValues
203  const std::string vehID = battery->getHolder().getID();
204  if (myChargeValues.count(vehID) == 0) {
205  myChargedVehicles.push_back(vehID);
206  }
207  Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
208  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
210  myChargeValues[vehID].push_back(C);
211 }
212 
213 
214 void
216  int chargingSteps = 0;
217  for (const auto& item : myChargeValues) {
218  chargingSteps += (int)item.second.size();
219  }
221  output.writeAttr(SUMO_ATTR_ID, myID);
223  output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
224  // start writting
225  if (myChargeValues.size() > 0) {
226  for (const std::string& vehID : myChargedVehicles) {
227  int iStart = 0;
228  const auto& chargeSteps = myChargeValues[vehID];
229  while (iStart < (int)chargeSteps.size()) {
230  int iEnd = iStart + 1;
231  double charged = chargeSteps[iStart].WCharged;
232  while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
233  charged += chargeSteps[iEnd].WCharged;
234  iEnd++;
235  }
236  writeVehicle(output, chargeSteps, iStart, iEnd, charged);
237  iStart = iEnd;
238  }
239  }
240  }
241  // close charging station tag
242  output.closeTag();
243 }
244 
245 void
246 MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
247  const Charge& first = chargeSteps[iStart];
249  out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
253  out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
254  for (int i = iStart; i < iEnd; i++) {
255  const Charge& c = chargeSteps[i];
256  out.openTag(SUMO_TAG_STEP);
258  // charge values
262  // charging values of charging station in this timestep
265  // battery status of vehicle
268  // close tag timestep
269  out.closeTag();
270  }
271  out.closeTag();
272 }
273 
274 
275 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:295
#define TLF(string,...)
Definition: MsgHandler.h:317
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
@ SUMO_TAG_CHARGING_STATION
A Charging Station.
@ SUMO_TAG_STEP
trigger: a step description
@ SUMO_TAG_VEHICLE
description of a vehicle
@ SUMO_ATTR_PARTIALCHARGE
energy provied by charging station at certain timestep
@ SUMO_ATTR_TOTALENERGYCHARGED
@ SUMO_ATTR_WAITINGTIME
@ SUMO_ATTR_CHARGETYPE
Charge type (fuel or electric)
@ SUMO_ATTR_MAXIMUMBATTERYCAPACITY
Maxium battery capacity.
@ SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE
total energy charged into a single vehicle
@ SUMO_ATTR_ACTUALBATTERYCAPACITY
@ SUMO_ATTR_ENERGYCHARGED
tgotal of Energy charged
@ SUMO_ATTR_CHARGINGPOWER
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_CHARGINGSTEPS
number of steps that a vehicle is charging
@ SUMO_ATTR_CHARGINGEND
timesteps in which charging ends
@ SUMO_ATTR_EFFICIENCY
Eficiency of the charge in Charging Stations.
@ SUMO_ATTR_CHARGINGBEGIN
timestep in which charging begins
@ SUMO_ATTR_ID
@ SUMO_ATTR_CHARGEDELAY
Delay in the charge of charging stations (different of waiting time)
@ SUMO_ATTR_TIME
trigger: the time of the step
@ SUMO_ATTR_CHARGING_STATUS
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double myChargingPower
Charging station's charging power.
void setChargeInTransit(bool value)
set charging in transit
void writeChargingStationOutput(OutputDevice &output)
write charging station values
double myTotalCharge
total energy charged by this charging station
static void writeVehicle(OutputDevice &out, const std::vector< Charge > &chargeSteps, int iStart, int iEnd, double charged)
double getChargingPower(bool usingFuel) const
Get charging station's charging power.
bool getChargeInTransit() const
Get chargeInTransit.
const std::string & getChargeType() const
Get charge type.
void setChargingVehicle(bool value)
enable or disable charging vehicle
const std::string myChargeType
charge type
std::vector< std::string > myChargedVehicles
order vehicles by time of first charge
double myEfficiency
Efficiency of the charging station.
void setEfficiency(double efficiency)
set efficiency of the charging station
void setChargingPower(double chargingPower)
set charging station's charging power
SUMOTime myWaitingTime
waiting time
bool myChargeInTransit
Allow charge in transit.
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
const MSParkingArea * myParkingArea
parkingArea the charging station is placed on
std::map< std::string, std::vector< Charge > > myChargeValues
map with the charges of this charging station (key = vehicleID)
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string &chargeType, SUMOTime waitingTime)
constructor
SUMOTime getChargeDelay() const
Get Charge Delay.
void setChargeDelay(SUMOTime delay)
set charging delay of the charging station
double getEfficency() const
Get efficiency of the charging station.
SUMOTime myChargeDelay
Charge Delay.
~MSChargingStation()
destructor
bool myChargingVehicle
Check if in the current TimeStep chargingStation is charging a vehicle.
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
SUMOTime getWaitingTime() const
Get waiting time.
const MSParkingArea * getParkingArea() const
Get the parking area the charging station is placed on.
Battery device for electric vehicles.
SUMOTime getChargingStartTime() const
Get charging start time.
double getActualBatteryCapacity() const
Get the actual vehicle's Battery Capacity in Wh.
double getMaximumBatteryCapacity() const
Get the total vehicle's Battery Capacity in Wh.
double getStoppingThreshold() const
Get stopping threshold.
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
A lane area vehicles can halt at.
Definition: MSParkingArea.h:60
A lane area vehicles can halt at.
double getBeginLanePosition() const
Returns the begin position of this stop.
double getEndLanePosition() const
Returns the end position of this stop.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:91
std::string myID
The name of the object.
Definition: Named.h:125
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
Definition: json.hpp:4471
struct to save information for the chargingStation output