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);
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 /****************************************************************************/
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
MSChargingStation::getChargingPower
double getChargingPower() const
Get charging station's charging power.
Definition: MSChargingStation.cpp:80
MSDevice_Battery::getMaximumBatteryCapacity
double getMaximumBatteryCapacity() const
Get the total vehicle's Battery Capacity in kWh.
Definition: MSDevice_Battery.cpp:353
MSVehicleType::getID
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:94
MSStoppingPlace
A lane area vehicles can halt at.
Definition: MSStoppingPlace.h:60
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSDevice_Battery::getStoppingTreshold
double getStoppingTreshold() const
Get stopping treshold.
Definition: MSDevice_Battery.cpp:410
MSStoppingPlace::getEndLanePosition
double getEndLanePosition() const
Returns the end position of this stop.
Definition: MSStoppingPlace.cpp:70
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
MSDevice_Battery::getActualBatteryCapacity
double getActualBatteryCapacity() const
Get the actual vehicle's Battery Capacity in kWh.
Definition: MSDevice_Battery.cpp:347
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
MSStoppingPlace::getBeginLanePosition
double getBeginLanePosition() const
Returns the begin position of this stop.
Definition: MSStoppingPlace.cpp:64
MSChargingStation::myChargeInTransit
bool myChargeInTransit
Allow charge in transit.
Definition: MSChargingStation.h:156
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSChargingStation::setChargeDelay
void setChargeDelay(double chargeDelay)
Set charge delay of the charging station.
Definition: MSChargingStation.cpp:130
SUMO_ATTR_MAXIMUMBATTERYCAPACITY
Maxium battery capacity.
Definition: SUMOXMLDefinitions.h:483
MSChargingStation.h
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
MSChargingStation::myChargeValues
std::vector< charge > myChargeValues
vector with the charges of this charging station
Definition: MSChargingStation.h:168
MSDevice_Battery.h
MSChargingStation::charge::WCharged
double WCharged
Definition: MSChargingStation.h:136
MSChargingStation::myChargeDelay
double myChargeDelay
Charge Delay.
Definition: MSChargingStation.h:159
MSChargingStation::~MSChargingStation
~MSChargingStation()
destructor
Definition: MSChargingStation.cpp:75
SUMO_ATTR_TOTALENERGYCHARGED
Definition: SUMOXMLDefinitions.h:533
MSChargingStation::charge
struct to save information for the cahrgingStation output
Definition: MSChargingStation.h:111
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
MSChargingStation::setChargingVehicle
void setChargingVehicle(bool value)
enable or disable charging vehicle
Definition: MSChargingStation.cpp:140
MSVehicleType.h
MSChargingStation::setChargeInTransit
void setChargeInTransit(bool chargeInTransit)
Set charge in transit of the charging station.
Definition: MSChargingStation.cpp:124
MSChargingStation::getEfficency
double getEfficency() const
Get efficiency of the charging station.
Definition: MSChargingStation.cpp:86
MSChargingStation::myTotalCharge
double myTotalCharge
total energy charged by this charging station
Definition: MSChargingStation.h:165
MSChargingStation::setEfficency
void setEfficency(double efficency)
Set efficiency of the charging station.
Definition: MSChargingStation.cpp:114
SUMO_ATTR_CHARGEDELAY
Delay in the charge of charging stations.
Definition: SUMOXMLDefinitions.h:475
SUMO_ATTR_CHARGINGSTEPS
number of steps that a vehicle is charging
Definition: SUMOXMLDefinitions.h:535
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
SUMOVehicle.h
SUMO_ATTR_CHARGINGPOWER
Definition: SUMOXMLDefinitions.h:469
SUMO_TAG_CHARGING_STATION
A Charging Station.
Definition: SUMOXMLDefinitions.h:112
SUMO_ATTR_CHARGINGEND
timesteps in which charging ends
Definition: SUMOXMLDefinitions.h:541
MSChargingStation::writeChargingStationOutput
void writeChargingStationOutput(OutputDevice &output)
write charging station values
Definition: MSChargingStation.cpp:192
MSChargingStation::myEfficiency
double myEfficiency
Efficiency of the charging station.
Definition: MSChargingStation.h:153
SUMO_ATTR_ACTUALBATTERYCAPACITY
Definition: SUMOXMLDefinitions.h:481
MSChargingStation::myChargingPower
double myChargingPower
Charging station's charging power.
Definition: MSChargingStation.h:150
SUMO_TAG_STEP
trigger: a step description
Definition: SUMOXMLDefinitions.h:158
MSVehicleDevice::getHolder
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
Definition: MSVehicleDevice.h:75
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
MSDevice_Battery
Battery device for electric vehicles.
Definition: MSDevice_Battery.h:46
MSTrigger.h
SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE
total energy charged into a single vehicle
Definition: SUMOXMLDefinitions.h:537
SUMO_TAG_VEHICLE
description of a vehicle
Definition: SUMOXMLDefinitions.h:120
SUMO_ATTR_TIME
trigger: the time of the step
Definition: SUMOXMLDefinitions.h:673
MSChargingStation::myChargingVehicle
bool myChargingVehicle
Check if in the current TimeStep chargingStation is charging a vehicle.
Definition: MSChargingStation.h:162
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:240
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
SUMO_ATTR_CHARGING_STATUS
Definition: SUMOXMLDefinitions.h:513
MSChargingStation::MSChargingStation
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, double chargeDelay)
constructor
Definition: MSChargingStation.cpp:41
MSChargingStation::setChargingPower
void setChargingPower(double chargingPower)
Set charging station's charging power.
Definition: MSChargingStation.cpp:104
MSChargingStation::isCharging
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
Definition: MSChargingStation.cpp:156
SUMO_ATTR_EFFICIENCY
Eficiency of the charge in Charging Stations.
Definition: SUMOXMLDefinitions.h:471
MSChargingStation::vehicleIsInside
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
Definition: MSChargingStation.cpp:146
SUMO_ATTR_PARTIALCHARGE
energy provied by charging station at certain timestep
Definition: SUMOXMLDefinitions.h:543
config.h
MSChargingStation::addChargeValueForOutput
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
Definition: MSChargingStation.cpp:162
SUMO_ATTR_ENERGYCHARGED
tgotal of Energy charged
Definition: SUMOXMLDefinitions.h:519
Named::myID
std::string myID
The name of the object.
Definition: Named.h:134
MSChargingStation::getChargeDelay
double getChargeDelay() const
Get Charge Delay.
Definition: MSChargingStation.cpp:98
MSStoppingPlace.h
MSDevice_Battery::getChargingStartTime
double getChargingStartTime() const
Get charging start time.
Definition: MSDevice_Battery.cpp:383
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
SUMO_ATTR_CHARGINGBEGIN
timestep in which charging begins
Definition: SUMOXMLDefinitions.h:539
MSChargingStation::getChargeInTransit
bool getChargeInTransit() const
Get chargeInTransit.
Definition: MSChargingStation.cpp:92
SUMOTrafficObject::getSpeed
virtual double getSpeed() const =0
Returns the vehicle's current speed.