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