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-2019 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 /****************************************************************************/
17 // Chargin Station for Electric vehicles
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 
24 #include <config.h>
25 
26 #include <cassert>
29 #include <microsim/MSVehicleType.h>
32 #include <microsim/MSNet.h>
33 #include "MSChargingStation.h"
34 #include "MSTrigger.h"
35 
36 
37 // ===========================================================================
38 // member method definitions
39 // ===========================================================================
40 
41 MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
42  const std::string& name,
43  double chargingPower, double efficency, bool chargeInTransit, double chargeDelay) :
44  MSStoppingPlace(chargingStationID, std::vector<std::string>(), lane, startPos, endPos, name),
45  myChargingPower(0),
46  myEfficiency(0),
47  myChargeInTransit(chargeInTransit),
48  myChargeDelay(0),
49  myChargingVehicle(false),
50  myTotalCharge(0) {
51  if (chargingPower < 0)
52  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getChargingPower()) + ").")
53  else {
54  myChargingPower = chargingPower;
55  }
56 
57  if (efficency < 0 || efficency > 1) {
58  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
59  } else {
60  myEfficiency = efficency;
61  }
62 
63  if (chargeDelay < 0) {
64  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
65  } else {
66  myChargeDelay = chargeDelay;
67  }
68 
70  WRITE_WARNING(toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " doesn't have a valid range (" + toString(getBeginLanePosition()) + " < " + toString(getEndLanePosition()) + ").");
71  }
72 }
73 
74 
76 }
77 
78 
79 double
81  return myChargingPower;
82 }
83 
84 
85 double
87  return myEfficiency;
88 }
89 
90 
91 bool
93  return myChargeInTransit;
94 }
95 
96 
97 double
99  return myChargeDelay;
100 }
101 
102 
103 void
104 MSChargingStation::setChargingPower(double chargingPower) {
105  if (chargingPower < 0) {
106  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargingPower) + ").")
107  } else {
108  myChargingPower = chargingPower;
109  }
110 }
111 
112 
113 void
115  if (efficency < 0 || efficency > 1) {
116  WRITE_WARNING("New " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(efficency) + ").")
117  } else {
118  myEfficiency = efficency;
119  }
120 }
121 
122 
123 void
125  myChargeInTransit = chargeInTransit;
126 }
127 
128 
129 void
131  if (chargeDelay < 0) {
132  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargeDelay) + ").")
133  } else {
134  myChargeDelay = chargeDelay;
135  }
136 }
137 
138 
139 void
141  myChargingVehicle = value;
142 }
143 
144 
145 bool
146 MSChargingStation::vehicleIsInside(const double position) const {
147  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
148  return true;
149  } else {
150  return false;
151  }
152 }
153 
154 
155 bool
157  return myChargingVehicle;
158 }
159 
160 
161 void
163  std::string status = "";
164  if (battery->getChargingStartTime() > myChargeDelay) {
165  if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
166  status = "chargingStopped";
167  } else if (myChargeInTransit == true) {
168  status = "chargingInTransit";
169  } else {
170  status = "noCharging";
171  }
172  } else {
173  if (myChargeInTransit == true) {
174  status = "waitingChargeInTransit";
175  } else if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
176  status = "waitingChargeStopped";
177  } else {
178  status = "noWaitingCharge";
179  }
180  }
181  // update total charge
182  myTotalCharge += WCharged;
183  // create charge row and insert it in myChargeValues
184  charge C(MSNet::getInstance()->getCurrentTimeStep(), battery->getHolder().getID(), battery->getHolder().getVehicleType().getID(),
185  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
187  myChargeValues.push_back(C);
188 }
189 
190 
191 void
194  output.writeAttr(SUMO_ATTR_ID, myID);
197  // start writting
198  if (myChargeValues.size() > 0) {
199  // First calculate charge for every vehicle
200  std::vector<double> charge;
201  std::vector<std::pair<SUMOTime, SUMOTime> > vectorBeginEndCharge;
202  SUMOTime firsTimeStep = myChargeValues.at(0).timeStep;
203  // set first value
204  charge.push_back(0);
205  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
206  // iterate over charging values
207  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
208  // update chargue
209  charge.back() += i->WCharged;
210  // update end time
211  vectorBeginEndCharge.back().second = i->timeStep;
212  // update timestep of charge
213  firsTimeStep += 1000;
214  // check if charge is continuous. If not, open a new vehicle tag
215  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
216  // set new firsTimeStep of charge
217  firsTimeStep = (i + 1)->timeStep;
218  charge.push_back(0);
219  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
220  }
221  }
222  // now write values
223  firsTimeStep = myChargeValues.at(0).timeStep;
224  int vehicleCounter = 0;
225  // open tag for first vehicle and write id and type of vehicle
226  output.openTag(SUMO_TAG_VEHICLE);
227  output.writeAttr(SUMO_ATTR_ID, myChargeValues.at(0).vehicleID);
228  output.writeAttr(SUMO_ATTR_TYPE, myChargeValues.at(0).vehicleType);
229  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(0));
230  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(vectorBeginEndCharge.at(0).first));
231  output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(vectorBeginEndCharge.at(0).second));
232  // iterate over charging values
233  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
234  // open tag for timestep and write all parameters
235  output.openTag(SUMO_TAG_STEP);
236  output.writeAttr(SUMO_ATTR_TIME, time2string(i->timeStep));
237  // charge values
238  output.writeAttr(SUMO_ATTR_CHARGING_STATUS, i->status);
239  output.writeAttr(SUMO_ATTR_ENERGYCHARGED, i->WCharged);
240  output.writeAttr(SUMO_ATTR_PARTIALCHARGE, i->totalEnergyCharged);
241  // charging values of charging station in this timestep
242  output.writeAttr(SUMO_ATTR_CHARGINGPOWER, i->chargingPower);
243  output.writeAttr(SUMO_ATTR_EFFICIENCY, i->chargingEfficiency);
244  // battery status of vehicle
245  output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, i->actualBatteryCapacity);
246  output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, i->maxBatteryCapacity);
247  // close tag timestep
248  output.closeTag();
249  // update timestep of charge
250  firsTimeStep += 1000;
251  // check if charge is continuous. If not, open a new vehicle tag
252  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
253  // set new firsTimeStep of charge
254  firsTimeStep = (i + 1)->timeStep;
255  // update counter
256  vehicleCounter++;
257  // close previous vehicle tag
258  output.closeTag();
259  // open tag for new vehicle and write id and type of vehicle
260  output.openTag(SUMO_TAG_VEHICLE);
261  output.writeAttr(SUMO_ATTR_ID, (i + 1)->vehicleID);
262  output.writeAttr(SUMO_ATTR_TYPE, (i + 1)->vehicleType);
263  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(vehicleCounter));
264  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, vectorBeginEndCharge.at(vehicleCounter).first);
265  output.writeAttr(SUMO_ATTR_CHARGINGEND, vectorBeginEndCharge.at(vehicleCounter).second);
266  }
267  }
268  // close vehicle tag
269  output.closeTag();
270  }
271  // close charging station tag
272  output.closeTag();
273 }
274 
275 /****************************************************************************/
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
number of steps that a vehicle is charging
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
long long int SUMOTime
Definition: SUMOTime.h:35
double getStoppingTreshold() const
Get stopping treshold.
double getBeginLanePosition() const
Returns the begin position of this stop.
double getMaximumBatteryCapacity() const
Get the total vehicle&#39;s Battery Capacity in kWh.
double myTotalCharge
total energy charged by this charging station
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
A lane area vehicles can halt at.
double getChargingStartTime() const
Get charging start time.
~MSChargingStation()
destructor
double getActualBatteryCapacity() const
Get the actual vehicle&#39;s Battery Capacity in kWh.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
tgotal of Energy charged
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
const std::string & getID() const
Returns the id.
Definition: Named.h:77
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
void setEfficency(double efficency)
Set efficiency of the charging station.
bool getChargeInTransit() const
Get chargeInTransit.
bool myChargingVehicle
Check if in the current TimeStep chargingStation is charging a vehicle.
void setChargeDelay(double chargeDelay)
Set charge delay of the charging station.
double getEndLanePosition() const
Returns the end position of this stop.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
timesteps in which charging ends
void setChargingVehicle(bool value)
enable or disable charging vehicle
double getChargeDelay() const
Get Charge Delay.
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
energy provied by charging station at certain timestep
void setChargeInTransit(bool chargeInTransit)
Set charge in transit of the charging station.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
void writeChargingStationOutput(OutputDevice &output)
write charging station values
timestep in which charging begins
double myChargeDelay
Charge Delay.
void setChargingPower(double chargingPower)
Set charging station&#39;s charging power.
struct to save information for the cahrgingStation output
std::string myID
The name of the object.
Definition: Named.h:134
trigger: the time of the step
double getEfficency() const
Get efficiency of the charging station.
Battery device for electric vehicles.
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, double chargeDelay)
constructor
bool myChargeInTransit
Allow charge in transit.
double getChargingPower() const
Get charging station&#39;s charging power.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:94
total energy charged into a single vehicle
double myEfficiency
Efficiency of the charging station.
std::vector< charge > myChargeValues
vector with the charges of this charging station
description of a vehicle
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
Eficiency of the charge in Charging Stations.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Delay in the charge of charging stations.
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
double myChargingPower
Charging station&#39;s charging power.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
trigger: a step description