Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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/****************************************************************************/
21// Charging Station for Electric vehicles
22/****************************************************************************/
23#include <config.h>
24
25#include <cassert>
32#include <microsim/MSNet.h>
33#include "MSChargingStation.h"
34
35
36// ===========================================================================
37// member method definitions
38// ===========================================================================
39
40MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
41 const std::string& name, double chargingPower, double efficency, bool chargeInTransit,
42 SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
43 MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
44 myChargeInTransit(chargeInTransit), myChargeType(stringToChargeType(chargeType)) {
45 if (chargingPower < 0) {
46 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
47 } else {
48 myChargingPower = chargingPower;
49 }
50 if (efficency < 0 || efficency > 1) {
51 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
52 } else {
53 myEfficiency = efficency;
54 }
55 if (chargeDelay < 0) {
56 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
57 } else {
58 myChargeDelay = chargeDelay;
59 }
60 if (waitingTime < 0) {
61 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
62 } else {
63 myWaitingTime = waitingTime;
64 }
66 WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
67 }
68}
69
70
71MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
72 double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
73 MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
74 name, chargingPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
75 myParkingArea = parkingArea;
76}
77
78
81
82
83double
85 if (usingFuel) {
86 return myChargingPower;
87 } else {
88 // Convert from [Ws] to [Wh] (3600s / 1h):
89 return myChargingPower / 3600;
90 }
91}
92
93
94double
98
99
100bool
104
105
110
111
116
117
122
123
124const MSParkingArea*
128
129
130void
132 myChargingPower = chargingPower;
133}
134
135
136void
138 myEfficiency = efficiency;
139}
140
141
142void
146
147
148void
152
153
154void
158
159
160bool
161MSChargingStation::vehicleIsInside(const double position) const {
162 if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
163 return true;
164 } else {
165 return false;
166 }
167}
168
169
170bool
174
175
176void
178 if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
179 return;
180 }
181 std::string status = "";
182 if (battery->getChargingStartTime() > myChargeDelay) {
183 if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
184 status = "chargingStopped";
185 } else if (myChargeInTransit) {
186 status = "chargingInTransit";
187 } else {
188 status = "noCharging";
189 }
190 } else {
191 if (myChargeInTransit) {
192 status = "waitingChargeInTransit";
193 } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
194 status = "waitingChargeStopped";
195 } else {
196 status = "noWaitingCharge";
197 }
198 }
199 // update total charge
200 myTotalCharge += WCharged;
201 // create charge row and insert it in myChargeValues
202 const std::string vehID = battery->getHolder().getID();
203 if (myChargeValues.count(vehID) == 0) {
204 myChargedVehicles.push_back(vehID);
205 }
206 Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
207 status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
209 myChargeValues[vehID].push_back(C);
210}
211
212
213void
215 int chargingSteps = 0;
216 for (const auto& item : myChargeValues) {
217 chargingSteps += (int)item.second.size();
218 }
220 output.writeAttr(SUMO_ATTR_ID, myID);
222 output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
223 // start writing
224 if (myChargeValues.size() > 0) {
225 for (const std::string& vehID : myChargedVehicles) {
226 int iStart = 0;
227 const auto& chargeSteps = myChargeValues[vehID];
228 while (iStart < (int)chargeSteps.size()) {
229 int iEnd = iStart + 1;
230 double charged = chargeSteps[iStart].WCharged;
231 while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
232 charged += chargeSteps[iEnd].WCharged;
233 iEnd++;
234 }
235 writeVehicle(output, chargeSteps, iStart, iEnd, charged);
236 iStart = iEnd;
237 }
238 }
239 }
240 // close charging station tag
241 output.closeTag();
242}
243
244
245void
247 std::vector<std::string> terminatedChargers;
248 for (const auto& item : myChargeValues) {
249 const Charge& lastCharge = item.second.back();
250 // no charge during the last time step == has stopped charging
251 bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
252 if (finished || includeUnfinished) {
253 if (finished) {
254 terminatedChargers.push_back(item.first);
255 }
256 // aggregate values
257 double charged = 0.;
258 double minPower = lastCharge.chargingPower;
259 double maxPower = lastCharge.chargingPower;
260 double minCharge = lastCharge.WCharged;
261 double maxCharge = lastCharge.WCharged;
262 double minEfficiency = lastCharge.chargingEfficiency;
263 double maxEfficiency = lastCharge.chargingEfficiency;
264 for (const auto& charge : item.second) {
265 charged += charge.WCharged;
266 if (charge.chargingPower < minPower) {
267 minPower = charge.chargingPower;
268 }
269 if (charge.chargingPower > maxPower) {
270 maxPower = charge.chargingPower;
271 }
272 if (charge.WCharged < minCharge) {
273 minCharge = charge.WCharged;
274 }
275 if (charge.WCharged > maxCharge) {
276 maxCharge = charge.WCharged;
277 }
278 if (charge.chargingEfficiency < minEfficiency) {
279 minEfficiency = charge.chargingEfficiency;
280 }
281 if (charge.chargingEfficiency > maxEfficiency) {
282 maxEfficiency = charge.chargingEfficiency;
283 }
284 }
285 // actually write the data
288 output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
289 output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
291 output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
292 if (finished) {
294 }
297 output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
298 output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
299 output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
300 output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
301 output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
302 output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
303 output.closeTag();
304 }
305 }
306
307 // clear charging data of vehicles which terminated charging
308 for (auto vehID : terminatedChargers) {
309 myChargeValues.erase(vehID);
310 }
311}
312
313
314void
315MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
316 const Charge& first = chargeSteps[iStart];
318 out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
322 out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
323 for (int i = iStart; i < iEnd; i++) {
324 const Charge& c = chargeSteps[i];
327 // charge values
331 // charging values of charging station in this timestep
334 // battery status of vehicle
337 // close tag timestep
338 out.closeTag();
339 }
340 out.closeTag();
341}
342
343
344/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#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
#define SIMSTEP
Definition SUMOTime.h:61
@ SUMO_TAG_CHARGING_STATION
A Charging Station.
@ SUMO_TAG_CHARGING_EVENT
A charging event (charging a vehicle at a charging station)
@ SUMO_TAG_STEP
trigger: a step description
@ SUMO_TAG_VEHICLE
description of a vehicle
@ SUMO_ATTR_PARTIALCHARGE
energy provided by charging station at certain timestep
@ SUMO_ATTR_TOTALENERGYCHARGED
@ SUMO_ATTR_MINPOWER
minimum charging power encountered during the charging event
@ SUMO_ATTR_WAITINGTIME
@ SUMO_ATTR_MAXIMUMBATTERYCAPACITY
Maxium battery capacity.
@ SUMO_ATTR_MINEFFICIENCY
minimum charging efficiency encountered during the charging event
@ SUMO_ATTR_MAXCHARGE
maximum energy charged during one time step of the charging event
@ SUMO_ATTR_MAXPOWER
@ SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE
total energy charged into a single vehicle
@ SUMO_ATTR_CHARGINGSTATIONID
Charging Station ID.
@ SUMO_ATTR_ACTUALBATTERYCAPACITY
@ SUMO_ATTR_ENERGYCHARGED
tgotal of Energy charged
@ SUMO_ATTR_CHARGINGPOWER
@ SUMO_ATTR_MAXEFFICIENCY
maximum charging efficiency encountered during the charging event
@ SUMO_ATTR_VEHICLE
@ 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_MINCHARGE
minimum energy charged during one time step of the charging event
@ 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.
const ChargeType myChargeType
charge type
bool getChargeInTransit() const
Get chargeInTransit.
void setChargingVehicle(bool value)
enable or disable charging vehicle
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 writeAggregatedChargingStationOutput(OutputDevice &output, bool includeUnfinished=false)
write ungrouped output (flush data after writing)
void setChargeDelay(SUMOTime delay)
set charging delay of the charging station
double getEfficency() const
Get efficiency of the charging station.
ChargeType getChargeType() const
Get charge type.
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:186
A lane area vehicles can halt at.
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.
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 OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual double getSpeed() const =0
Returns the object's current speed.
Definition json.hpp:4471
struct to save information for the chargingStation output