Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2010-2024 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 : /****************************************************************************/
14 : /// @file MSSOTLWaveTrafficLightLogic.cpp
15 : /// @author Riccardo Belletti
16 : /// @author Anna Chiara Bellini
17 : /// @date Sep 2013
18 : ///
19 : // The class for SOTL Platoon logics
20 : /****************************************************************************/
21 :
22 : #include "MSSOTLWaveTrafficLightLogic.h"
23 :
24 16 : MSSOTLWaveTrafficLightLogic::MSSOTLWaveTrafficLightLogic(
25 : MSTLLogicControl& tlcontrol, const std::string& id,
26 : const std::string& programID, const Phases& phases, int step,
27 : SUMOTime delay,
28 16 : const Parameterised::Map& parameters) :
29 : MSSOTLTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::SOTL_WAVE, phases, step, delay,
30 16 : parameters) {
31 32 : MsgHandler::getMessageInstance()->inform(
32 16 : "*** Intersection " + id
33 16 : + " will run using MSSOTLWaveTrafficLightLogic ***");
34 : //sets the lastDuration of every phase to the same value as the default duration of that phase
35 112 : for (int i = 0; i < getPhaseNumber(); i++) {
36 96 : (*myPhases[i]).lastDuration = (*myPhases[i]).duration;
37 : }
38 16 : }
39 :
40 0 : MSSOTLWaveTrafficLightLogic::MSSOTLWaveTrafficLightLogic(
41 : MSTLLogicControl& tlcontrol, const std::string& id,
42 : const std::string& programID, const Phases& phases, int step,
43 : SUMOTime delay, const Parameterised::Map& parameters,
44 0 : MSSOTLSensors* sensors) :
45 : MSSOTLTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::SOTL_WAVE, phases, step, delay,
46 0 : parameters, sensors) {
47 : //sets the lastDuration of every phase to the same value as the default duration of that phase
48 0 : for (int i = 0; i < getPhaseNumber(); i++) {
49 0 : (*myPhases[i]).lastDuration = (*myPhases[i]).duration;
50 : }
51 0 : }
52 :
53 14675 : bool MSSOTLWaveTrafficLightLogic::canRelease() {
54 :
55 : //10% of lastDuration
56 14675 : SUMOTime delta = 10 * getCurrentPhaseDef().lastDuration / 100;
57 :
58 : //this allows a minimum variation of +-1s
59 14675 : if (delta < 1000) {
60 : delta = 1000;
61 : }
62 14675 : if (getCurrentPhaseElapsed() >= getCurrentPhaseDef().minDuration) {
63 10702 : if (getCurrentPhaseElapsed()
64 10702 : >= getCurrentPhaseDef().lastDuration - delta) {
65 1814 : if ((countVehicles() == 0) //no other vehicles approaching green lights
66 1664 : || (getCurrentPhaseElapsed()
67 1664 : >= getCurrentPhaseDef().lastDuration + delta) //maximum value of the window surrounding lastDuration
68 3378 : || (getCurrentPhaseElapsed()
69 1564 : >= getCurrentPhaseDef().maxDuration) //declared maximum duration has been reached
70 : ) {
71 :
72 369 : (*myPhases[getCurrentPhaseIndex()]).lastDuration =
73 369 : getCurrentPhaseElapsed();
74 369 : return true;
75 : }
76 : }
77 : }
78 : return false;
79 : }
80 :
81 1814 : int MSSOTLWaveTrafficLightLogic::countVehicles() {
82 1814 : std::string state = getCurrentPhaseDef().getState();
83 : int vehicles = 0;
84 9070 : for (int i = 0; i < (int)getLaneVectors().size(); i++) {
85 0 : if (i > 0
86 7256 : && ((getLaneVectors()[i][0]->getID()).compare(
87 5442 : getLaneVectors()[i - 1][0]->getID()) == 0)) {
88 0 : continue;
89 : }
90 7256 : if (state[i] != 'r') {
91 3628 : vehicles += getSensors()->countVehicles(getLaneVectors()[i][0]);
92 : }
93 :
94 : }
95 1814 : return vehicles;
96 : }
|