SUMO - Simulation of Urban MObility
MSLaneSpeedTrigger.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Changes the speed allowed on a set of lanes
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
40 #include <utils/xml/XMLSubSys.h>
43 #include <microsim/MSLane.h>
44 #include <microsim/MSNet.h>
45 #include <microsim/MSEdge.h>
46 #include "MSLaneSpeedTrigger.h"
47 
48 #include <microsim/MSGlobals.h>
49 #include <mesosim/MELoop.h>
50 #include <mesosim/MESegment.h>
51 
52 #ifdef CHECK_MEMORY_LEAKS
53 #include <foreign/nvwa/debug_new.h>
54 #endif // CHECK_MEMORY_LEAKS
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
61  const std::vector<MSLane*>& destLanes,
62  const std::string& file) :
63  MSTrigger(id),
64  SUMOSAXHandler(file),
65  myDestLanes(destLanes),
66  myCurrentSpeed(destLanes[0]->getSpeedLimit()),
67  myDefaultSpeed(destLanes[0]->getSpeedLimit()),
68  myAmOverriding(false),
69  mySpeedOverrideValue(destLanes[0]->getSpeedLimit()),
70  myDidInit(false) {
71  if (file != "") {
72  if (!XMLSubSys::runParser(*this, file)) {
73  throw ProcessError();
74  }
75  if (!myDidInit) {
76  init();
77  }
78  }
79 }
80 
81 void
83  // set it to the right value
84  // assert there is at least one
85  if (myLoadedSpeeds.size() == 0) {
86  myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
87  }
88  // set the process to the begin
90  // pass previous time steps
92  while ((*myCurrentEntry).first < now && myCurrentEntry != myLoadedSpeeds.end()) {
93  processCommand(true, now);
94  }
95 
96  // add the processing to the event handler
99  (*myCurrentEntry).first, MSEventControl::NO_CHANGE);
100  myDidInit = true;
101 }
102 
103 
105 
106 
107 SUMOTime
109  return processCommand(true, currentTime);
110 }
111 
112 
113 SUMOTime
114 MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
115  UNUSED_PARAMETER(currentTime);
116  std::vector<MSLane*>::iterator i;
117  const SUMOReal speed = getCurrentSpeed();
119  if (myDestLanes.size() > 0 && myDestLanes.front()->getSpeedLimit() != speed) {
120  myDestLanes.front()->getEdge().setMaxSpeed(speed);
121  MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge(myDestLanes.front()->getEdge());
122  while (first != 0) {
123  first->setSpeed(speed, currentTime, -1);
124  first = first->getNextSegment();
125  }
126  }
127  } else {
128  for (i = myDestLanes.begin(); i != myDestLanes.end(); ++i) {
129  (*i)->setMaxSpeed(speed);
130  }
131  }
132  if (!move2next) {
133  // changed from the gui
134  return 0;
135  }
136  if (myCurrentEntry != myLoadedSpeeds.end()) {
137  ++myCurrentEntry;
138  }
139  if (myCurrentEntry != myLoadedSpeeds.end()) {
140  return ((*myCurrentEntry).first) - ((*(myCurrentEntry - 1)).first);
141  } else {
142  return 0;
143  }
144 }
145 
146 
147 void
149  const SUMOSAXAttributes& attrs) {
150  // check whether the correct tag is read
151  if (element != SUMO_TAG_STEP) {
152  return;
153  }
154  // extract the values
155  bool ok = true;
156  SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
157  SUMOReal speed = attrs.getOpt<SUMOReal>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
158  // check the values
159  if (next < 0) {
160  WRITE_ERROR("Wrong time in vss '" + getID() + "'.");
161  return;
162  }
163  if (speed < 0) {
164  speed = myDefaultSpeed;
165  }
166  // set the values for the next step if they are valid
167  if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
168  WRITE_WARNING("Time " + time2string(next) + " was set twice for vss '" + getID() + "'; replacing first entry.");
169  myLoadedSpeeds.back().second = speed;
170  } else {
171  myLoadedSpeeds.push_back(std::make_pair(next, speed));
172  }
173 }
174 
175 
176 void
178  if (element == SUMO_TAG_VSS && !myDidInit) {
179  init();
180  }
181 }
182 
183 
184 SUMOReal
186  return myDefaultSpeed;
187 }
188 
189 
190 void
192  myAmOverriding = val;
193  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
194 }
195 
196 
197 void
199  mySpeedOverrideValue = val;
200  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
201 }
202 
203 
204 SUMOReal
206  if (myCurrentEntry != myLoadedSpeeds.begin()) {
207  return (*(myCurrentEntry - 1)).second;
208  } else {
209  return (*myCurrentEntry).second;
210  }
211 }
212 
213 
214 SUMOReal
216  if (myAmOverriding) {
217  return mySpeedOverrideValue;
218  } else {
220  // ok, maybe the first shall not yet be the valid one
221  if (myCurrentEntry == myLoadedSpeeds.begin() && (*myCurrentEntry).first > now) {
222  return myDefaultSpeed;
223  }
224  // try the loaded
225  if (myCurrentEntry != myLoadedSpeeds.end() && (*myCurrentEntry).first <= now) {
226  return (*myCurrentEntry).second;
227  } else {
228  // we have run past the end of the loaded steps or the current step is not yet active:
229  // -> use the value of the previous step
230  return (*(myCurrentEntry - 1)).second;
231  }
232  }
233 }
234 
235 
236 /****************************************************************************/
237 
void setOverridingValue(SUMOReal val)
std::vector< MSLane * > myDestLanes
long long int SUMOTime
Definition: SUMOTime.h:43
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
SUMOTime execute(SUMOTime currentTime)
Executes a switch command.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
SAX-handler base for SUMO-files.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:114
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
SUMOReal getCurrentSpeed() const
Returns the current speed.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
SUMOReal myDefaultSpeed
The original speed allowed on the lanes.
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:158
An abstract device that changes the state of the micro simulation.
Definition: MSTrigger.h:48
Encapsulated SAX-Attributes.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:389
A wrapper for a Command function.
SUMOReal getDefaultSpeed() const
bool myAmOverriding
The information whether the read speed shall be overridden.
std::vector< std::pair< SUMOTime, SUMOReal > >::iterator myCurrentEntry
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
void setOverriding(bool val)
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
void setSpeed(SUMOReal newSpeed, SUMOTime currentTime, SUMOReal jamThresh=DO_NOT_PATCH_JAM_THRESHOLD)
reset mySpeed and patch the speed of all vehicles in it. Also set/recompute myJamThreshold ...
Definition: MESegment.cpp:569
MSLaneSpeedTrigger(const std::string &id, const std::vector< MSLane * > &destLanes, const std::string &file)
Constructor.
SUMOReal mySpeedOverrideValue
The speed to use if overriding the read speed.
A single mesoscopic segment (cell)
Definition: MESegment.h:57
MESegment * getSegmentForEdge(const MSEdge &e, SUMOReal pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:288
SUMOTime processCommand(bool move2next, SUMOTime currentTime)
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:99
virtual ~MSLaneSpeedTrigger()
Destructor.
std::vector< std::pair< SUMOTime, SUMOReal > > myLoadedSpeeds
bool myDidInit
The information whether init was called.
A variable speed sign.
#define SUMOReal
Definition: config.h:213
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
static bool gUseMesoSim
Definition: MSGlobals.h:87
virtual void myEndElement(int element)
Called on the closing of a tag;.