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-2018 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  double chargingPower, double efficency, bool chargeInTransit, double chargeDelay) :
43  MSStoppingPlace(chargingStationID, std::vector<std::string>(), lane, startPos, endPos),
44  myChargingPower(0),
45  myEfficiency(0),
46  myChargeInTransit(chargeInTransit),
47  myChargeDelay(0),
48  myChargingVehicle(false),
49  myTotalCharge(0) {
50  if (chargingPower < 0)
51  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getChargingPower()) + ").")
52  else {
53  myChargingPower = chargingPower;
54  }
55 
56  if (efficency < 0 || efficency > 1) {
57  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
58  } else {
59  myEfficiency = efficency;
60  }
61 
62  if (chargeDelay < 0) {
63  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
64  } else {
65  myChargeDelay = chargeDelay;
66  }
67 
69  WRITE_WARNING(toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " doesn't have a valid range (" + toString(getBeginLanePosition()) + " < " + toString(getEndLanePosition()) + ").");
70  }
71 }
72 
73 
75 }
76 
77 
78 double
80  return myChargingPower;
81 }
82 
83 
84 double
86  return myEfficiency;
87 }
88 
89 
90 bool
92  return myChargeInTransit;
93 }
94 
95 
96 double
98  return myChargeDelay;
99 }
100 
101 
102 void
103 MSChargingStation::setChargingPower(double chargingPower) {
104  if (chargingPower < 0) {
105  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargingPower) + ").")
106  } else {
107  myChargingPower = chargingPower;
108  }
109 }
110 
111 
112 void
114  if (efficency < 0 || efficency > 1) {
115  WRITE_WARNING("New " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(efficency) + ").")
116  } else {
117  myEfficiency = efficency;
118  }
119 }
120 
121 
122 void
124  myChargeInTransit = chargeInTransit;
125 }
126 
127 
128 void
130  if (chargeDelay < 0) {
131  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargeDelay) + ").")
132  } else {
133  myChargeDelay = chargeDelay;
134  }
135 }
136 
137 
138 void
140  myChargingVehicle = value;
141 }
142 
143 
144 bool
145 MSChargingStation::vehicleIsInside(const double position) const {
146  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
147  return true;
148  } else {
149  return false;
150  }
151 }
152 
153 
154 bool
156  return myChargingVehicle;
157 }
158 
159 
160 void
162  std::string status = "";
163  if (battery->getChargingStartTime() > myChargeDelay) {
164  if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
165  status = "chargingStopped";
166  } else if (myChargeInTransit == true) {
167  status = "chargingInTransit";
168  } else {
169  status = "noCharging";
170  }
171  } else {
172  if (myChargeInTransit == true) {
173  status = "waitingChargeInTransit";
174  } else if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
175  status = "waitingChargeStopped";
176  } else {
177  status = "noWaitingCharge";
178  }
179  }
180  // update total charge
181  myTotalCharge += WCharged;
182  // create charge row and insert it in myChargeValues
183  charge C(MSNet::getInstance()->getCurrentTimeStep(), battery->getHolder().getID(), battery->getHolder().getVehicleType().getID(),
184  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
186  myChargeValues.push_back(C);
187 }
188 
189 
190 void
193  output.writeAttr(SUMO_ATTR_ID, myID);
196  // start writting
197  if (myChargeValues.size() > 0) {
198  // First calculate charge for every vehicle
199  std::vector<double> charge;
200  std::vector<std::pair<SUMOTime, SUMOTime> > vectorBeginEndCharge;
201  SUMOTime firsTimeStep = myChargeValues.at(0).timeStep;
202  // set first value
203  charge.push_back(0);
204  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
205  // iterate over charging values
206  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
207  // update chargue
208  charge.back() += i->WCharged;
209  // update end time
210  vectorBeginEndCharge.back().second = i->timeStep;
211  // update timestep of charge
212  firsTimeStep += 1000;
213  // check if charge is continuous. If not, open a new vehicle tag
214  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
215  // set new firsTimeStep of charge
216  firsTimeStep = (i + 1)->timeStep;
217  charge.push_back(0);
218  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
219  }
220  }
221  // now write values
222  firsTimeStep = myChargeValues.at(0).timeStep;
223  int vehicleCounter = 0;
224  // open tag for first vehicle and write id and type of vehicle
225  output.openTag(SUMO_TAG_VEHICLE);
226  output.writeAttr(SUMO_ATTR_ID, myChargeValues.at(0).vehicleID);
227  output.writeAttr(SUMO_ATTR_TYPE, myChargeValues.at(0).vehicleType);
228  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(0));
229  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(vectorBeginEndCharge.at(0).first));
230  output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(vectorBeginEndCharge.at(0).second));
231  // iterate over charging values
232  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
233  // open tag for timestep and write all parameters
234  output.openTag(SUMO_TAG_STEP);
235  output.writeAttr(SUMO_ATTR_TIME, time2string(i->timeStep));
236  // charge values
237  output.writeAttr(SUMO_ATTR_CHARGING_STATUS, i->status);
238  output.writeAttr(SUMO_ATTR_ENERGYCHARGED, i->WCharged);
239  output.writeAttr(SUMO_ATTR_PARTIALCHARGE, i->totalEnergyCharged);
240  // charging values of charging station in this timestep
241  output.writeAttr(SUMO_ATTR_CHARGINGPOWER, i->chargingPower);
242  output.writeAttr(SUMO_ATTR_EFFICIENCY, i->chargingEfficiency);
243  // battery status of vehicle
244  output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, i->actualBatteryCapacity);
245  output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, i->maxBatteryCapacity);
246  // close tag timestep
247  output.closeTag();
248  // update timestep of charge
249  firsTimeStep += 1000;
250  // check if charge is continuous. If not, open a new vehicle tag
251  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
252  // set new firsTimeStep of charge
253  firsTimeStep = (i + 1)->timeStep;
254  // update counter
255  vehicleCounter++;
256  // close previous vehicle tag
257  output.closeTag();
258  // open tag for new vehicle and write id and type of vehicle
259  output.openTag(SUMO_TAG_VEHICLE);
260  output.writeAttr(SUMO_ATTR_ID, (i + 1)->vehicleID);
261  output.writeAttr(SUMO_ATTR_TYPE, (i + 1)->vehicleType);
262  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(vehicleCounter));
263  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, vectorBeginEndCharge.at(vehicleCounter).first);
264  output.writeAttr(SUMO_ATTR_CHARGINGEND, vectorBeginEndCharge.at(vehicleCounter).second);
265  }
266  }
267  // close vehicle tag
268  output.closeTag();
269  }
270  // close charging station tag
271  output.closeTag();
272 }
273 
274 /****************************************************************************/
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:36
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
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:67
tgotal of Energy charged
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
const std::string & getID() const
Returns the id.
Definition: Named.h:69
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
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:49
timesteps in which charging ends
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, double chargingPower, double efficency, bool chargeInTransit, double chargeDelay)
constructor
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.
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:121
trigger: the time of the step
double getEfficency() const
Get efficiency of the charging station.
Battery device for electric vehicles.
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:71
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
double myChargingPower
Charging station&#39;s charging power.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
Definition: MSDevice.h:118
trigger: a step description
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.