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.org/sumo
3 // Copyright (C) 2001-2022 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 // Chargin Station for Electric vehicles
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <cassert>
27 #include <microsim/MSVehicleType.h>
30 #include <microsim/MSNet.h>
31 #include "MSChargingStation.h"
32 #include "MSTrigger.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,
41  double chargingPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay) :
42  MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
43  myChargingPower(0),
44  myEfficiency(0),
45  myChargeInTransit(chargeInTransit),
46  myChargeDelay(0),
47  myChargingVehicle(false),
48  myTotalCharge(0) {
49  if (chargingPower < 0)
50  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(chargingPower) + ").")
51  else {
52  myChargingPower = chargingPower;
53  }
54 
55  if (efficency < 0 || efficency > 1) {
56  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
57  } else {
58  myEfficiency = efficency;
59  }
60 
61  if (chargeDelay < 0) {
62  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
63  } else {
64  myChargeDelay = chargeDelay;
65  }
66 
68  WRITE_WARNING(toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " doesn't have a valid range (" + toString(getBeginLanePosition()) + " < " + toString(getEndLanePosition()) + ").");
69  }
70 }
71 
72 
74 }
75 
76 
77 double
78 MSChargingStation::getChargingPower(bool usingFuel) const {
79  if (usingFuel) {
80  return myChargingPower;
81  } else {
82  // Convert from [Ws] to [Wh] (3600s / 1h):
83  return myChargingPower / 3600;
84  }
85 }
86 
87 
88 double
90  return myEfficiency;
91 }
92 
93 
94 bool
96  return myChargeInTransit;
97 }
98 
99 
100 SUMOTime
102  return myChargeDelay;
103 }
104 
105 
106 void
108  myChargingVehicle = value;
109 }
110 
111 
112 bool
113 MSChargingStation::vehicleIsInside(const double position) const {
114  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
115  return true;
116  } else {
117  return false;
118  }
119 }
120 
121 
122 bool
124  return myChargingVehicle;
125 }
126 
127 
128 void
130  std::string status = "";
131  if (battery->getChargingStartTime() > myChargeDelay) {
132  if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
133  status = "chargingStopped";
134  } else if (myChargeInTransit == true) {
135  status = "chargingInTransit";
136  } else {
137  status = "noCharging";
138  }
139  } else {
140  if (myChargeInTransit == true) {
141  status = "waitingChargeInTransit";
142  } else if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
143  status = "waitingChargeStopped";
144  } else {
145  status = "noWaitingCharge";
146  }
147  }
148  // update total charge
149  myTotalCharge += WCharged;
150  // create charge row and insert it in myChargeValues
151  const std::string vehID = battery->getHolder().getID();
152  if (myChargeValues.count(vehID) == 0) {
153  myChargedVehicles.push_back(vehID);
154  }
155  Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
156  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
158  myChargeValues[vehID].push_back(C);
159 }
160 
161 
162 void
164  int chargingSteps = 0;
165  for (const auto& item : myChargeValues) {
166  chargingSteps += (int)item.second.size();
167  }
169  output.writeAttr(SUMO_ATTR_ID, myID);
171  output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
172  // start writting
173  if (myChargeValues.size() > 0) {
174  for (const std::string& vehID : myChargedVehicles) {
175  int iStart = 0;
176  const auto& chargeSteps = myChargeValues[vehID];
177  while (iStart < (int)chargeSteps.size()) {
178  int iEnd = iStart + 1;
179  double charged = chargeSteps[iStart].WCharged;
180  while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
181  charged += chargeSteps[iEnd].WCharged;
182  iEnd++;
183  }
184  writeVehicle(output, chargeSteps, iStart, iEnd, charged);
185  iStart = iEnd;
186  }
187  }
188  }
189  // close charging station tag
190  output.closeTag();
191 }
192 
193 void
194 MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
195  const Charge& first = chargeSteps[iStart];
197  out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
201  out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
202  for (int i = iStart; i < iEnd; i++) {
203  const Charge& c = chargeSteps[i];
204  out.openTag(SUMO_TAG_STEP);
206  // charge values
210  // charging values of charging station in this timestep
213  // battery status of vehicle
216  // close tag timestep
217  out.closeTag();
218  }
219  out.closeTag();
220 }
221 
222 
223 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
long long int SUMOTime
Definition: SUMOTime.h:32
@ 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_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.
@ 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 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 in the.
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.
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay)
constructor
bool myChargeInTransit
Allow charge in transit.
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
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
SUMOTime getChargeDelay() const
Get Charge Delay.
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.
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 getStoppingTreshold() const
Get stopping treshold.
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
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:90
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:248
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.
struct to save information for the chargingStation output