Eclipse SUMO - Simulation of Urban MObility
MSVehicleTransfer.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-2023 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/****************************************************************************/
20// A mover of vehicles that got stucked due to grid locks
21/****************************************************************************/
22#include <config.h>
23
24#include <iostream>
27#include "MSNet.h"
28#include "MSLane.h"
29#include "MSEdge.h"
30#include "MSVehicle.h"
31#include "MSParkingArea.h"
33#include "MSVehicleControl.h"
34#include "MSInsertionControl.h"
35#include "MSVehicleTransfer.h"
36
37
38// ===========================================================================
39// static member definitions
40// ===========================================================================
43
44
45// ===========================================================================
46// member method definitions
47// ===========================================================================
48bool
50 return myVeh->getNumericalID() < v2.myVeh->getNumericalID();
51}
52
53
54void
56 const bool jumping = veh->isJumping();
57 const SUMOTime proceed = jumping ? t + veh->getPastStops().back().jump : -1;
58 if (veh->isParking()) {
62 } else {
65 if (veh->succEdge(1) == nullptr) {
66 WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."), veh->getID(), veh->getEdge()->getID(), time2string(t));
69 return;
70 }
72 veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
73 }
74 myVehicles.push_back(VehicleInformation(t, veh, proceed, veh->isParking(), jumping));
75}
76
77
78void
80 auto& vehInfos = myVehicles.getContainer();
81 for (auto i = vehInfos.begin(); i != vehInfos.end(); ++i) {
82 if (i->myVeh == veh) {
83 if (i->myParking) {
84 veh->getMutableLane()->removeParking(veh);
85 }
86 vehInfos.erase(i);
87 break;
88 }
89 }
90 myVehicles.unlock();
91}
92
93
94void
96 // go through vehicles
97 auto& vehInfos = myVehicles.getContainer();
98 std::sort(vehInfos.begin(), vehInfos.end());
99 for (auto i = vehInfos.begin(); i != vehInfos.end();) {
100 // vehicle information cannot be const because we need to assign the proceed time
101 VehicleInformation& desc = *i;
102
103 if (desc.myParking) {
104 // handle parking vehicles
105 if (time != desc.myTransferTime) {
106 // avoid calling processNextStop twice in the transfer step
107 const MSLane* lane = desc.myVeh->getLane();
108 // lane must be locked because pedestrians may be added in during stop processing while existing passengers are being drawn simultaneously
109 if (lane != nullptr) {
110 lane->getVehiclesSecure();
111 }
112 desc.myVeh->processNextStop(1);
114 if (lane != nullptr) {
115 lane->releaseVehicles();
116 }
117 }
118 if (desc.myVeh->keepStopping(true)) {
119 i++;
120 continue;
121 }
122 // parking finished, head back into traffic
123 }
124 const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
125 const MSEdge* e = desc.myVeh->getEdge();
126 const MSEdge* nextEdge = desc.myVeh->succEdge(1);
127
128
129 if (desc.myParking) {
131 const double departPos = pa != nullptr ? pa->getInsertionPosition(*desc.myVeh) : desc.myVeh->getPositionOnLane();
132 // handle parking vehicles
133 desc.myVeh->setIdling(true);
134 if (desc.myVeh->getMutableLane()->isInsertionSuccess(desc.myVeh, 0, departPos, desc.myVeh->getLateralPositionOnLane(),
138 // at this point we are in the lane, blocking traffic & if required we configure the exit manoeuvre
141 }
142 desc.myVeh->setIdling(false);
143 i = vehInfos.erase(i);
144 } else {
145 // blocked from entering the road - engine assumed to be idling.
148 // signal wish to re-enter the road
150 if (pa) {
151 // update freePosition so other vehicles can help with insertion
153 }
154 }
155 i++;
156 }
157 } else if (desc.myJumping && desc.myProceedTime > time) {
158 ++i;
159 } else {
160 const double departPos = 0;
161 // get the lane on which this vehicle should continue
162 // first select all the lanes which allow continuation onto nextEdge
163 // then pick the one which is least occupied
164 MSLane* l = (nextEdge != nullptr ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
165 e->getFreeLane(nullptr, vclass, departPos));
166 // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
167 if (l != nullptr && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), 0, MSMoveReminder::NOTIFICATION_TELEPORT)) {
168 if (!desc.myJumping) {
169 WRITE_WARNINGF(TL("Vehicle '%' ends teleporting on edge '%', time=%."), desc.myVeh->getID(), e->getID(), time2string(time));
170 }
172 i = vehInfos.erase(i);
173 } else {
174 // vehicle is visible while show-route is active. Make it's state more obvious
175 desc.myVeh->computeAngle();
176 desc.myVeh->setLateralPositionOnLane(-desc.myVeh->getLane()->getWidth() / 2);
178 // could not insert. maybe we should proceed in virtual space
179 if (desc.myProceedTime < 0) {
180 // initialize proceed time (delayed to avoid lane-order dependency in executeMove)
182 } else if (desc.myProceedTime < time) {
183 if (desc.myVeh->succEdge(1) == nullptr) {
184 WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."), desc.myVeh->getID(), e->getID(), time2string(time));
187 i = vehInfos.erase(i);
188 continue;
189 }
190 // let the vehicle move to the next edge
192 // active move reminders (i.e. rerouters)
193 desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
194 // use current travel time to determine when to move the vehicle forward
196 }
197 ++i;
198 }
199 }
200 }
201 myVehicles.unlock();
202}
203
204
207 if (myInstance == nullptr) {
209 }
210 return myInstance;
211}
212
213
215
216
218 myInstance = nullptr;
219}
220
221
222void
224 for (const VehicleInformation& vehInfo : myVehicles.getContainer()) {
226 out.writeAttr(SUMO_ATTR_ID, vehInfo.myVeh->getID());
227 out.writeAttr(SUMO_ATTR_DEPART, vehInfo.myProceedTime);
228 if (vehInfo.myParking) {
229 out.writeAttr(SUMO_ATTR_PARKING, vehInfo.myVeh->getLane()->getID());
230 }
231 if (vehInfo.myJumping) {
232 out.writeAttr(SUMO_ATTR_JUMP, true);
233 }
234 out.closeTag();
235 }
236 myVehicles.unlock();
237}
238
239
240void
242 myVehicles.clear();
243}
244
245
246void
248 MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
249 if (veh == nullptr) {
250 // deleted
251 return;
252 }
253 SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
254 MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
255 bool ok = true;
256 const bool jumping = attrs.getOpt<bool>(SUMO_ATTR_JUMP, veh->getID().c_str(), ok, false);
257 myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr, jumping));
258 if (parkingLane != nullptr) {
259 parkingLane->addParking(veh);
260 veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
261 veh->processNextStop(veh->getSpeed());
262 }
264}
265
266
267/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:268
#define TL(string)
Definition: MsgHandler.h:284
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define TIME2STEPS(x)
Definition: SUMOTime.h:56
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SUMO_TAG_VEHICLETRANSFER
@ SUMO_ATTR_PARKING
@ SUMO_ATTR_DEPART
@ SUMO_ATTR_JUMP
@ SUMO_ATTR_ID
T MIN2(T a, T b)
Definition: StdDefs.h:76
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)
double getMaxSpeed() const
Returns the maximum speed (the minimum of desired and technical maximum speed)
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs'th successor of edge the vehicle is currently at.
bool isJumping() const
Returns whether the vehicle is perform a jump.
bool isParking() const
Returns whether the vehicle is parking.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
const std::vector< SUMOVehicleParameter::Stop > & getPastStops() const
NumericalID getNumericalID() const
return the numerical ID which is only for internal usage
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:168
const std::vector< MSLane * > * allowedLanes(const MSEdge &destination, SUMOVehicleClass vclass=SVC_IGNORING) const
Get the allowed lanes to reach the destination-edge.
Definition: MSEdge.cpp:439
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:929
MSLane * getFreeLane(const std::vector< MSLane * > *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:485
static bool gModelParkingManoeuver
whether parking simulation includes manoeuver time and any associated lane blocking
Definition: MSGlobals.h:157
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition: MSGlobals.h:169
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
virtual void removeParking(MSBaseVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:3381
void addParking(MSBaseVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:3375
bool isInsertionSuccess(MSVehicle *vehicle, double speed, double pos, double posLat, bool recheckNextLanes, MSMoveReminder::Notification notification)
Tries to insert the given vehicle with the given state (speed and pos)
Definition: MSLane.cpp:775
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:579
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:2234
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:474
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:504
double getWidth() const
Returns the lane's width.
Definition: MSLane.h:622
bool freeInsertion(MSVehicle &veh, double speed, double posLat, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle on any place.
Definition: MSLane.cpp:471
@ NOTIFICATION_TELEPORT_ARRIVED
The vehicle was teleported out of the net.
@ NOTIFICATION_PARKING
The vehicle starts or ends parking.
@ NOTIFICATION_TELEPORT
The vehicle is being teleported.
@ ENDING_PARKING
The vehicle ends to park.
@ STARTING_PARKING
The vehicles starts to park.
@ STARTING_TELEPORT
The vehicle started to teleport.
@ ENDING_TELEPORT
The vehicle ended being teleported.
@ MANEUVERING
Vehicle maneuvering either entering or exiting a parking space.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to, const std::string &info="")
Informs all added listeners about a vehicle's state change.
Definition: MSNet.cpp:1244
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:431
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
A lane area vehicles can halt at.
Definition: MSParkingArea.h:58
void notifyEgressBlocked()
update state so that vehicles wishing to enter cooperate with exiting vehicles
double getInsertionPosition(const SUMOVehicle &forVehicle) const
Returns the insertion position of a parked vehicle.
The class responsible for building and deletion of vehicles.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
void scheduleVehicleRemoval(SUMOVehicle *veh, bool checkDuplicate=false)
Removes a vehicle after it has ended.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
double computeAngle() const
compute the current vehicle angle
Definition: MSVehicle.cpp:1405
void setTentativeLaneAndPosition(MSLane *lane, double pos, double posLat=0)
set tentative lane and position during insertion to ensure that all cfmodels work (some of them requi...
Definition: MSVehicle.cpp:6397
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:5510
bool signalSet(int which) const
Returns whether the given signal is on.
Definition: MSVehicle.h:1200
void enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:5180
MSParkingArea * getCurrentParkingArea()
get the current parking area stop or nullptr
Definition: MSVehicle.cpp:1553
void setIdling(bool amIdling)
access function for Idling flag used to record whether vehicle is waiting to enter lane (after parkin...
Definition: MSVehicle.h:614
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:1020
void leaveLane(const MSMoveReminder::Notification reason, const MSLane *approachedLane=0)
Update of members if vehicle leaves a new lane in the lane change step or at arrival.
Definition: MSVehicle.cpp:5424
bool setExitManoeuvre()
accessor function to myManoeuvre equivalent
Definition: MSVehicle.cpp:7353
@ VEH_SIGNAL_BLINKER_RIGHT
Right blinker lights are switched on.
Definition: MSVehicle.h:1122
@ VEH_SIGNAL_BLINKER_LEFT
Left blinker lights are switched on.
Definition: MSVehicle.h:1124
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:584
void invalidateCachedPosition()
Definition: MSVehicle.h:424
MSLane * getMutableLane() const
Returns the lane the vehicle is on Non const version indicates that something volatile is going on.
Definition: MSVehicle.h:592
double getLateralPositionOnLane() const
Get the vehicle's lateral position on the lane.
Definition: MSVehicle.h:416
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:493
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1573
void workOnIdleReminders()
cycle through vehicle devices invoking notifyIdle
Definition: MSVehicle.cpp:1151
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1608
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:377
void setLateralPositionOnLane(double posLat)
Definition: MSVehicle.h:420
void switchOnSignal(int signal)
Switches the given signal on.
Definition: MSVehicle.h:1175
void updateParkingState()
update state while parking
Definition: MSVehicle.cpp:4642
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
virtual ~MSVehicleTransfer()
Destructor.
void add(const SUMOTime t, MSVehicle *veh)
Adds a vehicle to this transfer object.
void saveState(OutputDevice &out)
Saves the current state into the given stream.
void checkInsertions(SUMOTime time)
Checks "movement" of stored vehicles.
MFXSynchQue< VehicleInformation, std::vector< VehicleInformation > > myVehicles
The information about stored vehicles to move virtually.
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
MSVehicleTransfer()
Constructor.
static MSVehicleTransfer * myInstance
The static singleton-instance.
void clearState()
Remove all vehicles before quick-loading state.
static const double TeleportMinSpeed
The minimum speed while teleporting.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type's vehicle class.
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:254
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.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
virtual long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
Holds the information needed to move the vehicle over the network.
bool myParking
whether the vehicle is or was parking
bool myJumping
whether the vehicle is or was jumping
SUMOTime myTransferTime
the time at which this vehicle was removed from the network
MSVehicle * myVeh
The vehicle itself.
SUMOTime myProceedTime
The time at which the vehicle should be moved virtually one edge further.
bool operator<(const VehicleInformation &v2) const
sort by vehicle ID for repeatable parallel simulation