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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Chargin Station for Electric vehicles
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <cassert>
35 #include <microsim/MSVehicleType.h>
38 #include <microsim/MSNet.h>
39 #include "MSChargingStation.h"
40 #include "MSTrigger.h"
41 
42 
43 // ===========================================================================
44 // member method definitions
45 // ===========================================================================
46 
47 MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
48  double chargingPower, double efficency, bool chargeInTransit, double chargeDelay) :
49  MSStoppingPlace(chargingStationID, std::vector<std::string>(), lane, startPos, endPos),
50  myChargingPower(0),
51  myEfficiency(0),
52  myChargeInTransit(chargeInTransit),
53  myChargeDelay(0),
54  myChargingVehicle(false),
55  myTotalCharge(0) {
56  if (chargingPower < 0)
57  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getChargingPower()) + ").")
58  else {
59  myChargingPower = chargingPower;
60  }
61 
62  if (efficency < 0 || efficency > 1) {
63  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
64  } else {
65  myEfficiency = efficency;
66  }
67 
68  if (chargeDelay < 0) {
69  WRITE_WARNING("Parameter " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " is invalid (" + toString(getEfficency()) + ").")
70  } else {
71  myChargeDelay = chargeDelay;
72  }
73 
75  WRITE_WARNING(toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " doesn't have a valid range (" + toString(getBeginLanePosition()) + " < " + toString(getEndLanePosition()) + ").");
76  }
77 }
78 
79 
81 }
82 
83 
84 double
86  return myChargingPower;
87 }
88 
89 
90 double
92  return myEfficiency;
93 }
94 
95 
96 bool
98  return myChargeInTransit;
99 }
100 
101 
102 double
104  return myChargeDelay;
105 }
106 
107 
108 void
109 MSChargingStation::setChargingPower(double chargingPower) {
110  if (chargingPower < 0) {
111  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGINGPOWER) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargingPower) + ").")
112  } else {
113  myChargingPower = chargingPower;
114  }
115 }
116 
117 
118 void
120  if (efficency < 0 || efficency > 1) {
121  WRITE_WARNING("New " + toString(SUMO_ATTR_EFFICIENCY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(efficency) + ").")
122  } else {
123  myEfficiency = efficency;
124  }
125 }
126 
127 
128 void
130  myChargeInTransit = chargeInTransit;
131 }
132 
133 
134 void
136  if (chargeDelay < 0) {
137  WRITE_WARNING("New " + toString(SUMO_ATTR_CHARGEDELAY) + " for " + toString(SUMO_TAG_CHARGING_STATION) + " with ID = " + getID() + " isn't valid (" + toString(chargeDelay) + ").")
138  } else {
139  myChargeDelay = chargeDelay;
140  }
141 }
142 
143 
144 void
146  myChargingVehicle = value;
147 }
148 
149 
150 bool
151 MSChargingStation::vehicleIsInside(const double position) const {
152  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
153  return true;
154  } else {
155  return false;
156  }
157 }
158 
159 
160 bool
162  return myChargingVehicle;
163 }
164 
165 
166 void
168  std::string status = "";
169  if (battery->getChargingStartTime() > myChargeDelay) {
170  if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
171  status = "chargingStopped";
172  } else if (myChargeInTransit == true) {
173  status = "chargingInTransit";
174  } else {
175  status = "noCharging";
176  }
177  } else {
178  if (myChargeInTransit == true) {
179  status = "waitingChargeInTransit";
180  } else if (battery->getHolder().getSpeed() < battery->getStoppingTreshold()) {
181  status = "waitingChargeStopped";
182  } else {
183  status = "noWaitingCharge";
184  }
185  }
186  // update total charge
187  myTotalCharge += WCharged;
188  // create charge row and insert it in myChargeValues
189  charge C(MSNet::getInstance()->getCurrentTimeStep(), battery->getHolder().getID(), battery->getHolder().getVehicleType().getID(),
190  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
192  myChargeValues.push_back(C);
193 }
194 
195 
196 void
199  output.writeAttr(SUMO_ATTR_ID, myID);
202  // start writting
203  if (myChargeValues.size() > 0) {
204  // First calculate charge for every vehicle
205  std::vector<double> charge;
206  std::vector<std::pair<SUMOTime, SUMOTime> > vectorBeginEndCharge;
207  SUMOTime firsTimeStep = myChargeValues.at(0).timeStep;
208  // set first value
209  charge.push_back(0);
210  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
211  // iterate over charging values
212  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
213  // update chargue
214  charge.back() += i->WCharged;
215  // update end time
216  vectorBeginEndCharge.back().second = i->timeStep;
217  // update timestep of charge
218  firsTimeStep += 1000;
219  // check if charge is continuous. If not, open a new vehicle tag
220  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
221  // set new firsTimeStep of charge
222  firsTimeStep = (i + 1)->timeStep;
223  charge.push_back(0);
224  vectorBeginEndCharge.push_back(std::pair<SUMOTime, SUMOTime>(firsTimeStep, 0));
225  }
226  }
227  // now write values
228  firsTimeStep = myChargeValues.at(0).timeStep;
229  int vehicleCounter = 0;
230  // open tag for first vehicle and write id and type of vehicle
231  output.openTag(SUMO_TAG_VEHICLE);
232  output.writeAttr(SUMO_ATTR_ID, myChargeValues.at(0).vehicleID);
233  output.writeAttr(SUMO_ATTR_TYPE, myChargeValues.at(0).vehicleType);
234  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(0));
235  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(vectorBeginEndCharge.at(0).first));
236  output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(vectorBeginEndCharge.at(0).second));
237  // iterate over charging values
238  for (std::vector<MSChargingStation::charge>::const_iterator i = myChargeValues.begin(); i != myChargeValues.end(); i++) {
239  // open tag for timestep and write all parameters
240  output.openTag(SUMO_TAG_STEP);
241  output.writeAttr(SUMO_ATTR_TIME, time2string(i->timeStep));
242  // charge values
243  output.writeAttr(SUMO_ATTR_CHARGING_STATUS, i->status);
244  output.writeAttr(SUMO_ATTR_ENERGYCHARGED, i->WCharged);
245  output.writeAttr(SUMO_ATTR_PARTIALCHARGE, i->totalEnergyCharged);
246  // charging values of charging station in this timestep
247  output.writeAttr(SUMO_ATTR_CHARGINGPOWER, i->chargingPower);
248  output.writeAttr(SUMO_ATTR_EFFICIENCY, i->chargingEfficiency);
249  // battery status of vehicle
250  output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, i->actualBatteryCapacity);
251  output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, i->maxBatteryCapacity);
252  // close tag timestep
253  output.closeTag();
254  // update timestep of charge
255  firsTimeStep += 1000;
256  // check if charge is continuous. If not, open a new vehicle tag
257  if (((i + 1) != myChargeValues.end()) && (((i + 1)->timeStep) != firsTimeStep)) {
258  // set new firsTimeStep of charge
259  firsTimeStep = (i + 1)->timeStep;
260  // update counter
261  vehicleCounter++;
262  // close previous vehicle tag
263  output.closeTag();
264  // open tag for new vehicle and write id and type of vehicle
265  output.openTag(SUMO_TAG_VEHICLE);
266  output.writeAttr(SUMO_ATTR_ID, (i + 1)->vehicleID);
267  output.writeAttr(SUMO_ATTR_TYPE, (i + 1)->vehicleType);
268  output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charge.at(vehicleCounter));
269  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, vectorBeginEndCharge.at(vehicleCounter).first);
270  output.writeAttr(SUMO_ATTR_CHARGINGEND, vectorBeginEndCharge.at(vehicleCounter).second);
271  }
272  }
273  // close vehicle tag
274  output.closeTag();
275  }
276  // close charging station tag
277  output.closeTag();
278 }
279 
280 /****************************************************************************/
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:260
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:59
tgotal of Energy charged
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
const std::string & getID() const
Returns the id.
Definition: Named.h:74
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
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:55
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:126
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.
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:70
Eficiency of the charge in Charging Stations.
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:51
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:77
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:119
trigger: a step description
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.