Eclipse SUMO - Simulation of Urban MObility
MSDevice_Transportable.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-2022 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/****************************************************************************/
23// A device which is used to keep track of persons and containers riding with a vehicle
24/****************************************************************************/
25#include <config.h>
26
29#include <microsim/MSNet.h>
30#include <microsim/MSEdge.h>
31#include <microsim/MSStop.h>
37#include "MSDevice_Taxi.h"
38
39
40// ===========================================================================
41// method definitions
42// ===========================================================================
43// ---------------------------------------------------------------------------
44// static initialisation methods
45// ---------------------------------------------------------------------------
47MSDevice_Transportable::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into, const bool isContainer) {
48 MSDevice_Transportable* device = new MSDevice_Transportable(v, isContainer ? "container_" + v.getID() : "person_" + v.getID(), isContainer);
49 into.push_back(device);
50 return device;
51}
52
53
54// ---------------------------------------------------------------------------
55// MSDevice_Transportable-methods
56// ---------------------------------------------------------------------------
57MSDevice_Transportable::MSDevice_Transportable(SUMOVehicle& holder, const std::string& id, const bool isContainer) :
58 MSVehicleDevice(holder, id),
59 myAmContainer(isContainer),
60 myTransportables(),
61 myStopped(holder.isStopped())
62{ }
63
64
66 // flush any unfortunate riders still remaining
67 for (auto it = myTransportables.begin(); it != myTransportables.end();) {
68 MSTransportable* transportable = *it;
69 WRITE_WARNING((myAmContainer ? "Removing container '" : "Removing person '") + transportable->getID() +
70 "' at removal of vehicle '" + myHolder.getID() + "'");
71 MSStageDriving* const stage = dynamic_cast<MSStageDriving*>(transportable->getCurrentStage());
72 if (stage != nullptr) {
73 stage->setVehicle(nullptr);
74 }
75 if (myAmContainer) {
77 } else {
78 MSNet::getInstance()->getPersonControl().erase(transportable);
79 }
80 it = myTransportables.erase(it);
81 }
82}
83
84void
86 const double /* frontOnLane */,
87 const double /* timeOnLane */,
88 const double /* meanSpeedFrontOnLane */,
89 const double /* meanSpeedVehicleOnLane */,
90 const double travelledDistanceFrontOnLane,
91 const double /* travelledDistanceVehicleOnLane */,
92 const double /* meanLengthOnLane */) {
93 notifyMove(const_cast<SUMOTrafficObject&>(veh), -1, travelledDistanceFrontOnLane, veh.getEdge()->getVehicleMaxSpeed(&veh));
94}
95
96bool
98 for (const MSTransportable* t : myTransportables) {
99 MSStageDriving* const stage = dynamic_cast<MSStageDriving*>(t->getCurrentStage());
100 if (stage->canLeaveVehicle(t, myHolder, stop)) {
101 return true;
102 }
103 }
104 return false;
105}
106
107bool
108MSDevice_Transportable::notifyMove(SUMOTrafficObject& /*tObject*/, double /*oldPos*/, double newPos, double newSpeed) {
109 SUMOVehicle& veh = myHolder;
110 const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
111 if (myStopped) {
112 if (!veh.isStopped()) {
113 const SUMOTime freeFlowTimeCorrection = MSGlobals::gUseMesoSim ? TIME2STEPS(newPos / newSpeed) : 0;
114 for (MSTransportable* const transportable : myTransportables) {
115 transportable->setDeparted(currentTime - freeFlowTimeCorrection);
116 }
117 myStopped = false;
118 }
119 } else {
120 if (veh.isStopped()) {
121 myStopped = true;
122 MSStop& stop = veh.getNextStop();
123 const SUMOTime boardingDuration = veh.getVehicleType().getLoadingDuration(!myAmContainer);
124 for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
125 MSTransportable* transportable = *i;
126 MSStageDriving* const stage = dynamic_cast<MSStageDriving*>(transportable->getCurrentStage());
127 if (stage->canLeaveVehicle(transportable, myHolder, stop)) {
128 MSDevice_Taxi* taxiDevice = static_cast<MSDevice_Taxi*>(myHolder.getDevice(typeid(MSDevice_Taxi)));
129 if (taxiDevice != nullptr && stop.timeToBoardNextPerson == 0 && !MSGlobals::gUseMesoSim) {
130 // taxi passengers must leave at the end of the stop duration
132 }
133 if (stop.timeToBoardNextPerson - DELTA_T > currentTime) {
134 // try deboarding again in the next step
135 myStopped = false;
136 break;
137 }
138 if (stage->getDestinationStop() != nullptr) {
139 stage->getDestinationStop()->addTransportable(transportable);
140 }
141
142 SUMOTime arrivalTime = currentTime;
144 arrivalTime += 1;
145 } else {
146 // no boarding / unboarding time in meso
147 if (stop.timeToBoardNextPerson > currentTime - DELTA_T) {
148 stop.timeToBoardNextPerson += boardingDuration;
149 } else {
150 stop.timeToBoardNextPerson = currentTime + boardingDuration;
151 }
152 }
153 //ensure that vehicle stops long enough for deboarding
154 stop.duration = MAX2(stop.duration, stop.timeToBoardNextPerson - currentTime);
155
156 i = myTransportables.erase(i); // erase first in case proceed throws an exception
157 if (taxiDevice != nullptr) {
158 taxiDevice->customerArrived(transportable);
159 }
160 if (!transportable->proceed(MSNet::getInstance(), arrivalTime)) {
161 if (myAmContainer) {
162 MSNet::getInstance()->getContainerControl().erase(transportable);
163 } else {
164 MSNet::getInstance()->getPersonControl().erase(transportable);
165 }
166 }
167 if (MSStopOut::active()) {
168 SUMOVehicle* vehicle = dynamic_cast<SUMOVehicle*>(&veh);
169 if (myAmContainer) {
171 } else {
173 }
174 }
175 continue;
176 }
177 ++i;
178 }
179 }
180 }
181 return true;
182}
183
184
185bool
188 const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
189 for (MSTransportable* const transportable : myTransportables) {
190 transportable->setDeparted(currentTime);
191 }
192 }
194 // to trigger vehicle leaving
195 notifyMove(veh, -1., -1., -1.);
196 }
197 return true;
198}
199
200
201bool
203 MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
205 for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
206 MSTransportable* transportable = *i;
207 if (transportable->getDestination() != veh.getEdge()) {
208 WRITE_WARNING((myAmContainer ? "Teleporting container '" : "Teleporting person '") + transportable->getID() +
209 "' from vehicle destination edge '" + veh.getEdge()->getID() +
210 "' to intended destination edge '" + transportable->getDestination()->getID() + "' time=" + time2string(SIMSTEP));
211 }
212 if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep(), true)) {
213 if (myAmContainer) {
214 MSNet::getInstance()->getContainerControl().erase(transportable);
215 } else {
216 MSNet::getInstance()->getPersonControl().erase(transportable);
217 }
218 }
219 i = myTransportables.erase(i);
220 }
221 }
222 return true;
223}
224
225
226void
228 myTransportables.push_back(transportable);
229 if (MSStopOut::active()) {
230 if (myAmContainer) {
232 } else {
234 }
235 }
236 MSDevice_Taxi* taxiDevice = static_cast<MSDevice_Taxi*>(myHolder.getDevice(typeid(MSDevice_Taxi)));
237 if (taxiDevice != nullptr) {
238 taxiDevice->customerEntered(transportable);
239 }
240}
241
242
243void
245 auto it = std::find(myTransportables.begin(), myTransportables.end(), transportable);
246 if (it != myTransportables.end()) {
247 myTransportables.erase(it);
249 if (myAmContainer) {
251 } else {
253 }
254 }
255 MSDevice_Taxi* taxiDevice = static_cast<MSDevice_Taxi*>(myHolder.getDevice(typeid(MSDevice_Taxi)));
256 if (taxiDevice != nullptr) {
257 taxiDevice->customerArrived(transportable);
258 }
259 }
260}
261
262
263void
267 std::vector<std::string> internals;
268 internals.push_back(toString(myStopped));
269 out.writeAttr(SUMO_ATTR_STATE, toString(internals));
270 out.closeTag();
271}
272
273
274void
276 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
277 bis >> myStopped;
278}
279
280
281std::string
282MSDevice_Transportable::getParameter(const std::string& key) const {
283 if (key == "IDList") {
284 std::vector<std::string> ids;
285 for (const MSTransportable* t : myTransportables) {
286 ids.push_back(t->getID());
287 }
288 return toString(ids);
289 }
290 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
291}
292
293
294/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define SIMSTEP
Definition: SUMOTime.h:60
#define TIME2STEPS(x)
Definition: SUMOTime.h:56
@ SUMO_TAG_DEVICE
@ SUMO_ATTR_ID
@ SUMO_ATTR_STATE
The state of a link.
T MAX2(T a, T b)
Definition: StdDefs.h:77
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Taxi.h:49
void customerArrived(const MSTransportable *person)
called by MSDevice_Transportable upon unloading a person
void customerEntered(const MSTransportable *t)
called by MSDevice_Transportable upon loading a person
void saveState(OutputDevice &out) const
Saves the state of the device.
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle is at a stop and transportable action is needed.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Passengers leaving on arrival.
bool anyLeavingAtStop(const MSStop &stop) const
static MSDevice_Transportable * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into, const bool isContainer)
Build devices for the given vehicle, if needed.
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
MSDevice_Transportable(SUMOVehicle &holder, const std::string &id, const bool isContainer)
Constructor.
std::vector< MSTransportable * > myTransportables
The passengers of the vehicle.
void notifyMoveInternal(const SUMOTrafficObject &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal()
void addTransportable(MSTransportable *transportable)
Add a passenger.
const bool myAmContainer
Whether it is a container device.
const std::string deviceName() const
return the name for this type of device
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
void removeTransportable(MSTransportable *transportable)
Remove a passenger (TraCI)
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:1068
static bool gUseMesoSim
Definition: MSGlobals.h:103
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
Notification
Definition of a vehicle state.
@ NOTIFICATION_ARRIVED
The vehicle arrived at its destination (is deleted)
@ NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:1105
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:321
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:1096
bool canLeaveVehicle(const MSTransportable *t, const SUMOVehicle &veh, const MSStop &stop)
checks whether the person may exit at the current vehicle position
void setVehicle(SUMOVehicle *v)
MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
Definition: MSStage.h:80
Definition: MSStop.h:44
SUMOTime timeToBoardNextPerson
The time at which the vehicle is able to board another person.
Definition: MSStop.h:81
SUMOTime duration
The stopping duration.
Definition: MSStop.h:67
const SUMOVehicleParameter::Stop pars
The stop parameter.
Definition: MSStop.h:65
void loadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:89
static bool active()
Definition: MSStopOut.h:54
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:84
static MSStopOut * getInstance()
Definition: MSStopOut.h:60
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:97
void loadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:76
bool addTransportable(const MSTransportable *p)
adds a transportable to this stop
virtual void erase(MSTransportable *transportable)
removes a single transportable
const MSEdge * getDestination() const
Returns the current destination.
MSStage * getCurrentStage() const
Return the current stage.
virtual bool proceed(MSNet *net, SUMOTime time, const bool vehicleArrived=false)
Abstract in-vehicle device.
SUMOVehicle & myHolder
The vehicle that stores the device.
SUMOTime getLoadingDuration(const bool isPerson) const
Get this vehicle type's loading duration.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:251
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
Representation of a vehicle, person, or container.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual bool isStopped() const =0
Returns whether the object is at a stop.
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
virtual MSStop & getNextStop()=0
SUMOTime started
the time at which this stop was reached
SUMOTime duration
The stopping duration.