Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Command_SaveTLSSwitches.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
20// Writes information about the green durations of a tls
21/****************************************************************************/
22#include <config.h>
23
27#include <microsim/MSNet.h>
28#include <microsim/MSLink.h>
29#include <microsim/MSLane.h>
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
39 OutputDevice& od)
40 : myOutputDevice(od), myLogics(logics) {
42 myOutputDevice.writeXMLHeader("tlsSwitches", "tlsswitches_file.xsd");
43}
44
45
48
49
53 const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks();
54 const std::string& state = light->getCurrentPhaseDef().getState();
55 for (int i = 0; i < (int) links.size(); i++) {
56 if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
57 if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
58 // was not saved before
59 myPreviousLinkStates[i] = currentTime;
60 continue;
61 }
62 } else {
63 if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
64 // was not yet green
65 continue;
66 }
67 const MSTrafficLightLogic::LinkVector& currLinks = links[i];
68 const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i);
70 for (int j = 0; j < (int) currLinks.size(); j++) {
71 MSLink* link = currLinks[j];
72 myOutputDevice << " <tlsSwitch id=\"" << light->getID()
73 << "\" programID=\"" << light->getProgramID()
74 << "\" fromLane=\"" << currLanes[j]->getID()
75 << "\" toLane=\"" << link->getLane()->getID()
76 << "\" begin=\"" << time2string(lastOn)
77 << "\" end=\"" << time2string(currentTime)
78 << "\" duration=\"" << time2string(currentTime - lastOn)
79 << "\"/>\n";
80 }
82 }
83 }
84 return DELTA_T;
85}
86
87
88/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
SUMOTime execute(SUMOTime currentTime)
Writes the output if a change occurred.
std::map< int, SUMOTime > myPreviousLinkStates
Storage for prior states; map from signal group to last green time begin.
const MSTLLogicControl::TLSLogicVariants & myLogics
The traffic light logic to use.
OutputDevice & myOutputDevice
The device to write to.
Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants &logics, OutputDevice &od)
Constructor.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition MSNet.h:481
const std::string & getState() const
Returns the state within this phase.
Storage for all programs of a single tls.
MSTrafficLightLogic * getActive() const
The parent class for traffic light logics.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
const std::string & getProgramID() const
Returns this tl-logic's id.
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
const std::string & getID() const
Returns the id.
Definition Named.h:74
Static storage of an output device and its base (abstract) implementation.
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.