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-2020 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, double chargeDelay) :
42  MSStoppingPlace(chargingStationID, 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(getChargingPower()) + ").")
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
79  return myChargingPower;
80 }
81 
82 
83 double
85  return myEfficiency;
86 }
87 
88 
89 bool
91  return myChargeInTransit;
92 }
93 
94 
95 double
97  return myChargeDelay;
98 }
99 
100 
101 void
102 MSChargingStation::setChargingPower(double chargingPower) {
103  if (chargingPower < 0) {
104  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargingPower) + ").")
105  } else {
106  myChargingPower = chargingPower;
107  }
108 }
109 
110 
111 void
113  if (efficency < 0 || efficency > 1) {
114  WRITE_WARNING("New " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(efficency) + ").")
115  } else {
116  myEfficiency = efficency;
117  }
118 }
119 
120 
121 void
123  myChargeInTransit = chargeInTransit;
124 }
125 
126 
127 void
129  if (chargeDelay < 0) {
130  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargeDelay) + ").")
131  } else {
132  myChargeDelay = chargeDelay;
133  }
134 }
135 
136 
137 void
139  myChargingVehicle = value;
140 }
141 
142 
143 bool
144 MSChargingStation::vehicleIsInside(const double position) const {
145  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
146  return true;
147  } else {
148  return false;
149  }
150 }
151 
152 
153 bool
155  return myChargingVehicle;
156 }
157 
158 
159 void
161  std::string status = "";
162  if (battery->getChargingStartTime() > myChargeDelay) {
163  if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
164  status = "chargingStopped";
165  } else if (myChargeInTransit == true) {
166  status = "chargingInTransit";
167  } else {
168  status = "noCharging";
169  }
170  } else {
171  if (myChargeInTransit == true) {
172  status = "waitingChargeInTransit";
173  } else if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
174  status = "waitingChargeStopped";
175  } else {
176  status = "noWaitingCharge";
177  }
178  }
179  // update total charge
180  myTotalCharge += WCharged;
181  // create charge row and insert it in myChargeValues
182  charge C(MSNet::getInstance()->getCurrentTimeStep(), battery->getHolder().getID(), battery->getHolder().getVehicleType().getID(),
183  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
185  myChargeValues.push_back(C);
186 }
187 
188 
189 void
192  output.writeAttr(SUMO_ATTR_ID, myID);
195  // start writting
196  if (myChargeValues.size() > 0) {
197  // First calculate charge for every vehicle
198  std::vector<double> charge;
199  std::vector<std::pair<SUMOTime, SUMOTime> > vectorBeginEndCharge;
200  SUMOTime firsTimeStep = myChargeValues.at(0).timeStep;
201  // set first value
202  charge.push_back(0);
203  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
204  // iterate over charging values
205  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
206  // update chargue
207  charge.back() += i->WCharged;
208  // update end time
209  vectorBeginEndCharge.back().second = i->timeStep;
210  // update timestep of charge
211  firsTimeStep += 1000;
212  // check if charge is continuous. If not, open a new vehicle tag
213  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
214  // set new firsTimeStep of charge
215  firsTimeStep = (i + 1)->timeStep;
216  charge.push_back(0);
217  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
218  }
219  }
220  // now write values
221  firsTimeStep = myChargeValues.at(0).timeStep;
222  int vehicleCounter = 0;
223  // open tag for first vehicle and write id and type of vehicle
224  output.openTag(SUMO_TAG_VEHICLE);
225  output.writeAttr(SUMO_ATTR_ID, myChargeValues.at(0).vehicleID);
226  output.writeAttr(SUMO_ATTR_TYPE, myChargeValues.at(0).vehicleType);
228  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(vectorBeginEndCharge.at(0).first));
229  output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(vectorBeginEndCharge.at(0).second));
230  // iterate over charging values
231  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
232  // open tag for timestep and write all parameters
233  output.openTag(SUMO_TAG_STEP);
234  output.writeAttr(SUMO_ATTR_TIME, time2string(i->timeStep));
235  // charge values
236  output.writeAttr(SUMO_ATTR_CHARGING_STATUS, i->status);
237  output.writeAttr(SUMO_ATTR_ENERGYCHARGED, i->WCharged);
238  output.writeAttr(SUMO_ATTR_PARTIALCHARGE, i->totalEnergyCharged);
239  // charging values of charging station in this timestep
240  output.writeAttr(SUMO_ATTR_CHARGINGPOWER, i->chargingPower);
241  output.writeAttr(SUMO_ATTR_EFFICIENCY, i->chargingEfficiency);
242  // battery status of vehicle
243  output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, i->actualBatteryCapacity);
244  output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, i->maxBatteryCapacity);
245  // close tag timestep
246  output.closeTag();
247  // update timestep of charge
248  firsTimeStep += 1000;
249  // check if charge is continuous. If not, open a new vehicle tag
250  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
251  // set new firsTimeStep of charge
252  firsTimeStep = (i + 1)->timeStep;
253  // update counter
254  vehicleCounter++;
255  // close previous vehicle tag
256  output.closeTag();
257  // open tag for new vehicle and write id and type of vehicle
258  output.openTag(SUMO_TAG_VEHICLE);
259  output.writeAttr(SUMO_ATTR_ID, (i + 1)->vehicleID);
260  output.writeAttr(SUMO_ATTR_TYPE, (i + 1)->vehicleType);
261  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(vehicleCounter));
262  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, vectorBeginEndCharge.at(vehicleCounter).first);
263  output.writeAttr(SUMO_ATTR_CHARGINGEND, vectorBeginEndCharge.at(vehicleCounter).second);
264  }
265  }
266  // close vehicle tag
267  output.closeTag();
268  }
269  // close charging station tag
270  output.closeTag();
271 }
272 
273 
274 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
long long int SUMOTime
Definition: SUMOTime.h:31
@ 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:44
double myChargingPower
Charging station's charging power.
void writeChargingStationOutput(OutputDevice &output)
write charging station values
double myTotalCharge
total energy charged by this charging station
double getChargingPower() const
Get charging station's charging power.
bool getChargeInTransit() const
Get chargeInTransit.
void setChargingVehicle(bool value)
enable or disable charging vehicle
void setEfficency(double efficency)
Set efficiency of the charging station.
double getChargeDelay() const
Get Charge Delay.
double myEfficiency
Efficiency of the charging station.
void setChargingPower(double chargingPower)
Set charging station's charging power.
void setChargeDelay(double chargeDelay)
Set charge delay of the charging station.
double myChargeDelay
Charge Delay.
bool myChargeInTransit
Allow charge in transit.
std::vector< charge > myChargeValues
vector with the charges of this charging station
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, double chargeDelay)
constructor
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
double getEfficency() const
Get efficiency of the charging station.
~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.
void setChargeInTransit(bool chargeInTransit)
Set charge in transit of the charging station.
Battery device for electric vehicles.
double getActualBatteryCapacity() const
Get the actual vehicle's Battery Capacity in kWh.
double getChargingStartTime() const
Get charging start time.
double getMaximumBatteryCapacity() const
Get the total vehicle's Battery Capacity in kWh.
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:171
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:124
const std::string & getID() const
Returns the id.
Definition: Named.h:73
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
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:239
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 cahrgingStation output