SUMO - Simulation of Urban MObility
MSLaneSpeedTrigger.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-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 /****************************************************************************/
21 // Changes the speed allowed on a set of lanes
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
39 #include <utils/xml/XMLSubSys.h>
42 #include <microsim/MSLane.h>
43 #include <microsim/MSNet.h>
44 #include <microsim/MSEdge.h>
45 #include "MSLaneSpeedTrigger.h"
46 
47 #include <microsim/MSGlobals.h>
48 #include <mesosim/MELoop.h>
49 #include <mesosim/MESegment.h>
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
56  const std::vector<MSLane*>& destLanes,
57  const std::string& file) :
58  MSTrigger(id),
59  SUMOSAXHandler(file),
60  myDestLanes(destLanes),
61  myCurrentSpeed(destLanes[0]->getSpeedLimit()),
62  myDefaultSpeed(destLanes[0]->getSpeedLimit()),
63  myAmOverriding(false),
64  mySpeedOverrideValue(destLanes[0]->getSpeedLimit()),
65  myDidInit(false) {
66  if (file != "") {
67  if (!XMLSubSys::runParser(*this, file)) {
68  throw ProcessError();
69  }
70  if (!myDidInit) {
71  init();
72  }
73  }
74 }
75 
76 void
78  // set it to the right value
79  // assert there is at least one
80  if (myLoadedSpeeds.size() == 0) {
81  myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
82  }
83  // set the process to the begin
85  // pass previous time steps
87  while ((*myCurrentEntry).first < now && myCurrentEntry != myLoadedSpeeds.end()) {
88  processCommand(true, now);
89  }
90 
91  // add the processing to the event handler
94  (*myCurrentEntry).first);
95  myDidInit = true;
96 }
97 
98 
100 
101 
102 SUMOTime
104  return processCommand(true, currentTime);
105 }
106 
107 
108 SUMOTime
109 MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
110  UNUSED_PARAMETER(currentTime);
111  std::vector<MSLane*>::iterator i;
112  const double speed = getCurrentSpeed();
114  if (myDestLanes.size() > 0 && myDestLanes.front()->getSpeedLimit() != speed) {
115  myDestLanes.front()->getEdge().setMaxSpeed(speed);
116  MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge(myDestLanes.front()->getEdge());
117  while (first != 0) {
118  first->setSpeed(speed, currentTime, -1);
119  first = first->getNextSegment();
120  }
121  }
122  } else {
123  for (i = myDestLanes.begin(); i != myDestLanes.end(); ++i) {
124  (*i)->setMaxSpeed(speed);
125  }
126  }
127  if (!move2next) {
128  // changed from the gui
129  return 0;
130  }
131  if (myCurrentEntry != myLoadedSpeeds.end()) {
132  ++myCurrentEntry;
133  }
134  if (myCurrentEntry != myLoadedSpeeds.end()) {
135  return ((*myCurrentEntry).first) - ((*(myCurrentEntry - 1)).first);
136  } else {
137  return 0;
138  }
139 }
140 
141 
142 void
144  const SUMOSAXAttributes& attrs) {
145  // check whether the correct tag is read
146  if (element != SUMO_TAG_STEP) {
147  return;
148  }
149  // extract the values
150  bool ok = true;
151  SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
152  double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
153  // check the values
154  if (next < 0) {
155  WRITE_ERROR("Wrong time in vss '" + getID() + "'.");
156  return;
157  }
158  if (speed < 0) {
159  speed = myDefaultSpeed;
160  }
161  // set the values for the next step if they are valid
162  if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
163  WRITE_WARNING("Time " + time2string(next) + " was set twice for vss '" + getID() + "'; replacing first entry.");
164  myLoadedSpeeds.back().second = speed;
165  } else {
166  myLoadedSpeeds.push_back(std::make_pair(next, speed));
167  }
168 }
169 
170 
171 void
173  if (element == SUMO_TAG_VSS && !myDidInit) {
174  init();
175  }
176 }
177 
178 
179 double
181  return myDefaultSpeed;
182 }
183 
184 
185 void
187  myAmOverriding = val;
188  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
189 }
190 
191 
192 void
194  mySpeedOverrideValue = val;
195  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
196 }
197 
198 
199 double
201  if (myCurrentEntry != myLoadedSpeeds.begin()) {
202  return (*(myCurrentEntry - 1)).second;
203  } else {
204  return (*myCurrentEntry).second;
205  }
206 }
207 
208 
209 double
211  if (myAmOverriding) {
212  return mySpeedOverrideValue;
213  } else {
215  // ok, maybe the first shall not yet be the valid one
216  if (myCurrentEntry == myLoadedSpeeds.begin() && (*myCurrentEntry).first > now) {
217  return myDefaultSpeed;
218  }
219  // try the loaded
220  if (myCurrentEntry != myLoadedSpeeds.end() && (*myCurrentEntry).first <= now) {
221  return (*myCurrentEntry).second;
222  } else {
223  // we have run past the end of the loaded steps or the current step is not yet active:
224  // -> use the value of the previous step
225  return (*(myCurrentEntry - 1)).second;
226  }
227  }
228 }
229 
230 
231 /****************************************************************************/
232 
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:156
std::vector< MSLane * > myDestLanes
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:289
void setSpeed(double newSpeed, SUMOTime currentTime, double jamThresh=DO_NOT_PATCH_JAM_THRESHOLD)
reset mySpeed and patch the speed of all vehicles in it. Also set/recompute myJamThreshold ...
Definition: MESegment.cpp:612
double myDefaultSpeed
The original speed allowed on the lanes.
double getCurrentSpeed() const
Returns the current speed.
std::vector< std::pair< SUMOTime, double > >::iterator myCurrentEntry
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:167
const std::string & getID() const
Returns the id.
Definition: Named.h:74
SAX-handler base for SUMO-files.
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:109
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
std::vector< std::pair< SUMOTime, double > > myLoadedSpeeds
An abstract device that changes the state of the micro simulation.
Definition: MSTrigger.h:47
Encapsulated SAX-Attributes.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:399
A wrapper for a Command function.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
bool myAmOverriding
The information whether the read speed shall be overridden.
MSLaneSpeedTrigger(const std::string &id, const std::vector< MSLane *> &destLanes, const std::string &file)
Constructor.
void setOverriding(bool val)
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
trigger: the time of the step
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.
A single mesoscopic segment (cell)
Definition: MESegment.h:56
SUMOTime processCommand(bool move2next, SUMOTime currentTime)
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:112
virtual ~MSLaneSpeedTrigger()
Destructor.
double getDefaultSpeed() const
bool myDidInit
The information whether init was called.
A variable speed sign.
long long int SUMOTime
Definition: TraCIDefs.h:51
static bool gUseMesoSim
Definition: MSGlobals.h:97
virtual void myEndElement(int element)
Called on the closing of a tag;.
trigger: a step description
void setOverridingValue(double val)
double mySpeedOverrideValue
The speed to use if overriding the read speed.