Eclipse SUMO - Simulation of Urban MObility
MSRailSignalControl.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/****************************************************************************/
18// Centralized services for rail signal control (Singleton)
19/****************************************************************************/
20#include <config.h>
21
22#include <cassert>
23#include <utility>
24#include <vector>
25#include <bitset>
26#include <microsim/MSNet.h>
27#include <microsim/MSRoute.h>
28#include <microsim/MSEdge.h>
29#include "MSRailSignal.h"
30#include "MSRailSignalControl.h"
31
32
33//#define DEBUG_REGISTER_DRIVEWAY
34
35// ===========================================================================
36// static value definitions
37// ===========================================================================
39
40// ===========================================================================
41// method definitions
42// ===========================================================================
44{}
45
48 if (myInstance == nullptr) {
51 }
52 return *myInstance;
53}
54
55void
57 delete myInstance;
58 myInstance = nullptr;
59}
60
61void
63 if (myInstance != nullptr) {
64 myInstance->myUsedEdges.clear();
66 }
67}
68
69
71}
72
73void
74MSRailSignalControl::vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& /*info*/) {
75 if (isRailway(vehicle->getVClass())) {
77 for (const MSEdge* edge : vehicle->getRoute().getEdges()) {
78 myUsedEdges.insert(edge);
79 if (myProtectedDriveways.count(edge) != 0) {
80 updateDriveways(edge);
81 }
82 }
83 }
84 if (to == MSNet::VehicleState::BUILT || (!vehicle->hasDeparted() && to == MSNet::VehicleState::NEWROUTE)) {
85 // @note we could delay initialization until the departure time
87 }
88 }
89}
90
91
92void
93MSRailSignalControl::registerProtectedDriveway(MSRailSignal* rs, int driveWayID, const MSEdge* protectedBidi) {
94 myProtectedDriveways[protectedBidi].push_back(std::make_pair(rs, driveWayID));
95#ifdef DEBUG_REGISTER_DRIVEWAY
96 std::cout << "MSRailSignalControl edge=" << protectedBidi->getID() << " assumed secure by driveway " << driveWayID << " at signal " << rs->getID() << "\n";
97#endif
98}
99
100void
102 for (auto item : myProtectedDriveways[used]) {
103 item.first->updateDriveway(item.second);
104 }
105 myProtectedDriveways.erase(used);
106}
107
108void
110 mySignals.push_back(signal);
111}
112
113/****************************************************************************/
bool isRailway(SVCPermissions permissions)
Returns whether an edge with the given permission is a railway edge.
A road/street connecting two junctions.
Definition: MSEdge.h:77
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:602
@ BUILT
The vehicle was built, but has not yet departed.
@ NEWROUTE
The vehicle got a new route.
@ 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
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
Definition: MSNet.cpp:1227
A signal for rails.
void addSignal(MSRailSignal *signal)
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
std::map< const MSEdge *, std::vector< std::pair< MSRailSignal *, int > > > myProtectedDriveways
map of driveways that must perform additional checks if the key edge is used by a train route
std::vector< MSRailSignal * > mySignals
list of all rail signals
MSRailSignalControl()
Constructor.
std::set< const MSEdge * > myUsedEdges
all rail edges that are part of a known route
static MSRailSignalControl * myInstance
static MSRailSignalControl & getInstance()
void updateDriveways(const MSEdge *used)
compute additioanl deadlock-check requirements for registered driveways
static void clearState()
Perform resets events when quick-loading state.
void registerProtectedDriveway(MSRailSignal *rs, int driveWayID, const MSEdge *protectedBidi)
mark driveway that must receive additional checks if protectedBidi is ever used by a train route
A signal for rails.
Definition: MSRailSignal.h:46
static void initDriveWays(const SUMOVehicle *ego, bool update)
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:124
const std::string & getID() const
Returns the id.
Definition: Named.h:74
virtual SUMOVehicleClass getVClass() const =0
Returns the object's access class.
Representation of a vehicle.
Definition: SUMOVehicle.h:62
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual const MSRoute & getRoute() const =0
Returns the current route.