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