Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 : // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 : /****************************************************************************/
5 : //
6 : // This program and the accompanying materials
7 : // are made available under the terms of the Eclipse Public License v2.0
8 : // which accompanies this distribution, and is available at
9 : // http://www.eclipse.org/legal/epl-v20.html
10 : //
11 : /****************************************************************************/
12 : /// @file TraCI_TrafficLight.cpp
13 : /// @author Daniel Krajzewicz
14 : /// @author Mario Krumnow
15 : /// @author Jakob Erdmann
16 : /// @author Michael Behrisch
17 : /// @date 30.05.2012
18 : /// @version $Id$
19 : ///
20 : // C++ TraCI client API implementation
21 : /****************************************************************************/
22 :
23 :
24 : // ===========================================================================
25 : // included modules
26 : // ===========================================================================
27 : #ifdef _MSC_VER
28 : #include <windows_config.h>
29 : #else
30 : #include <config.h>
31 : #endif
32 :
33 : #include <microsim/MSLane.h>
34 : #include <microsim/MSNet.h>
35 : #include <microsim/traffic_lights/MSTLLogicControl.h>
36 : #include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>
37 : #include "TraCI_TrafficLight.h"
38 :
39 :
40 : // ===========================================================================
41 : // member definitions
42 : // ===========================================================================
43 : std::vector<std::string>
44 34 : TraCI_TrafficLight::getIDList() {
45 34 : return MSNet::getInstance()->getTLSControl().getAllTLIds();
46 : }
47 :
48 :
49 : int
50 9 : TraCI_TrafficLight::getIDCount() {
51 9 : return (int)getIDList().size();
52 : }
53 :
54 :
55 : std::string
56 23 : TraCI_TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
57 23 : return getTLS(tlsID).getActive()->getCurrentPhaseDef().getState();
58 : }
59 :
60 :
61 : std::vector<TraCILogic>
62 9 : TraCI_TrafficLight::getCompleteRedYellowGreenDefinition(const std::string& tlsID) {
63 9 : std::vector<TraCILogic> result;
64 18 : const std::vector<MSTrafficLightLogic*> logics = getTLS(tlsID).getAllLogics();
65 27 : for (MSTrafficLightLogic* logic : logics) {
66 18 : TraCILogic l(logic->getProgramID(), 0, logic->getCurrentPhaseIndex());
67 18 : l.subParameter = logic->getMap();
68 127 : for (int j = 0; j < logic->getPhaseNumber(); ++j) {
69 109 : MSPhaseDefinition phase = logic->getPhase(j);
70 109 : l.phases.emplace_back(TraCIPhase(phase.duration, phase.minDuration, phase.maxDuration, phase.getState()));
71 109 : }
72 18 : result.emplace_back(l);
73 18 : }
74 18 : return result;
75 : }
76 :
77 :
78 : std::vector<std::string>
79 0 : TraCI_TrafficLight::getControlledJunctions(const std::string& tlsID) {
80 0 : std::set<std::string> junctionIDs;
81 0 : const MSTrafficLightLogic::LinkVectorVector& links = getTLS(tlsID).getActive()->getLinks();
82 0 : for (const MSTrafficLightLogic::LinkVector& llinks : links) {
83 0 : for (const MSLink* l : llinks) {
84 0 : junctionIDs.insert(l->getJunction()->getID());
85 : }
86 : }
87 0 : return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
88 : }
89 :
90 :
91 : std::vector<std::string>
92 9 : TraCI_TrafficLight::getControlledLanes(const std::string& tlsID) {
93 9 : std::vector<std::string> laneIDs;
94 9 : const MSTrafficLightLogic::LaneVectorVector& lanes = getTLS(tlsID).getActive()->getLaneVectors();
95 144 : for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
96 270 : for (const MSLane* l : llanes) {
97 135 : laneIDs.push_back(l->getID());
98 : }
99 : }
100 9 : return laneIDs;
101 : }
102 :
103 :
104 : std::vector<std::vector<TraCILink> >
105 9 : TraCI_TrafficLight::getControlledLinks(const std::string& tlsID) {
106 9 : std::vector<std::vector<TraCILink> > result;
107 9 : const MSTrafficLightLogic::LaneVectorVector& lanes = getTLS(tlsID).getActive()->getLaneVectors();
108 9 : const MSTrafficLightLogic::LinkVectorVector& links = getTLS(tlsID).getActive()->getLinks();
109 144 : for (int i = 0; i < (int)lanes.size(); ++i) {
110 135 : std::vector<TraCILink> subList;
111 135 : const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
112 135 : const MSTrafficLightLogic::LinkVector& llinks = links[i];
113 : // number of links controlled by this signal (signal i)
114 270 : for (int j = 0; j < (int)llanes.size(); ++j) {
115 135 : MSLink* link = llinks[j];
116 : // approached non-internal lane (if any)
117 135 : const std::string to = link->getLane() != 0 ? link->getLane()->getID() : "";
118 : // approached "via", internal lane (if any)
119 270 : const std::string via = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
120 135 : subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
121 135 : }
122 135 : result.emplace_back(subList);
123 135 : }
124 9 : return result;
125 : }
126 :
127 :
128 : std::string
129 17 : TraCI_TrafficLight::getProgram(const std::string& tlsID) {
130 17 : return getTLS(tlsID).getActive()->getProgramID();
131 : }
132 :
133 :
134 : int
135 22078 : TraCI_TrafficLight::getPhase(const std::string& tlsID) {
136 22078 : return getTLS(tlsID).getActive()->getCurrentPhaseIndex();
137 : }
138 :
139 :
140 : SUMOTime
141 6 : TraCI_TrafficLight::getPhaseDuration(const std::string& tlsID) {
142 6 : return getTLS(tlsID).getActive()->getCurrentPhaseDef().duration;
143 : }
144 :
145 :
146 : SUMOTime
147 13 : TraCI_TrafficLight::getNextSwitch(const std::string& tlsID) {
148 13 : return getTLS(tlsID).getActive()->getNextSwitchTime();
149 : }
150 :
151 :
152 : std::string
153 8 : TraCI_TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
154 8 : return getTLS(tlsID).getActive()->getParameter(paramName, "");
155 : }
156 :
157 :
158 : void
159 6 : TraCI_TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
160 6 : getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
161 6 : }
162 :
163 :
164 : void
165 4844 : TraCI_TrafficLight::setPhase(const std::string& tlsID, const int index) {
166 4844 : MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
167 4843 : if (index < 0 || active->getPhaseNumber() <= index) {
168 4 : throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
169 6 : + toString(active->getPhaseNumber() - 1) + "].");
170 : }
171 4841 : const SUMOTime cTime = MSNet::getInstance()->getCurrentTimeStep();
172 4841 : const SUMOTime duration = active->getPhase(index).duration;
173 4841 : active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
174 4841 : }
175 :
176 :
177 : void
178 10 : TraCI_TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
179 : try {
180 10 : getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
181 1 : } catch (ProcessError& e) {
182 1 : throw TraCIException(e.what());
183 : }
184 9 : }
185 :
186 :
187 : void
188 4 : TraCI_TrafficLight::setPhaseDuration(const std::string& tlsID, const SUMOTime phaseDuration) {
189 4 : MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
190 4 : const SUMOTime cTime = MSNet::getInstance()->getCurrentTimeStep();
191 4 : const int index = active->getCurrentPhaseIndex();
192 4 : active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, phaseDuration);
193 4 : }
194 :
195 :
196 : void
197 13 : TraCI_TrafficLight::setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCILogic& logic) {
198 13 : MSTLLogicControl::TLSLogicVariants& vars = getTLS(tlsID);
199 : // make sure index and phaseNo are consistent
200 13 : if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
201 1 : throw TraCIException("set program: parameter index must be less than parameter phase number.");
202 : }
203 12 : std::vector<MSPhaseDefinition*> phases;
204 47 : for (TraCIPhase phase : logic.phases) {
205 35 : phases.push_back(new MSPhaseDefinition(phase.duration, phase.duration1, phase.duration2, phase.phase));
206 35 : }
207 12 : if (vars.getLogic(logic.subID) == 0) {
208 6 : MSTrafficLightLogic* mslogic = new MSSimpleTrafficLightLogic(MSNet::getInstance()->getTLSControl(), tlsID, logic.subID, phases, logic.currentPhaseIndex, 0, logic.subParameter);
209 6 : vars.addLogic(logic.subID, mslogic, true, true);
210 : } else {
211 6 : static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.subID))->setPhases(phases, logic.currentPhaseIndex);
212 12 : }
213 12 : }
214 :
215 :
216 : void
217 4 : TraCI_TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
218 4 : return getTLS(tlsID).getActive()->setParameter(paramName, value);
219 : }
220 :
221 :
222 : MSTLLogicControl::TLSLogicVariants&
223 27062 : TraCI_TrafficLight::getTLS(const std::string& id) {
224 27062 : if (!MSNet::getInstance()->getTLSControl().knows(id)) {
225 3 : throw TraCIException("Traffic light '" + id + "' is not known");
226 : }
227 27059 : return MSNet::getInstance()->getTLSControl().get(id);
228 43554 : }
229 :
230 :
231 : /****************************************************************************/
|