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-2017 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 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  const std::vector<MSLane*>& destLanes,
58  const std::string& file) :
59  MSTrigger(id),
60  SUMOSAXHandler(file),
61  myDestLanes(destLanes),
62  myCurrentSpeed(destLanes[0]->getSpeedLimit()),
63  myDefaultSpeed(destLanes[0]->getSpeedLimit()),
64  myAmOverriding(false),
65  mySpeedOverrideValue(destLanes[0]->getSpeedLimit()),
66  myDidInit(false) {
67  if (file != "") {
68  if (!XMLSubSys::runParser(*this, file)) {
69  throw ProcessError();
70  }
71  if (!myDidInit) {
72  init();
73  }
74  }
75 }
76 
77 void
79  // set it to the right value
80  // assert there is at least one
81  if (myLoadedSpeeds.size() == 0) {
82  myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
83  }
84  // set the process to the begin
86  // pass previous time steps
88  while ((*myCurrentEntry).first < now && myCurrentEntry != myLoadedSpeeds.end()) {
89  processCommand(true, now);
90  }
91 
92  // add the processing to the event handler
95  (*myCurrentEntry).first);
96  myDidInit = true;
97 }
98 
99 
101 
102 
103 SUMOTime
105  return processCommand(true, currentTime);
106 }
107 
108 
109 SUMOTime
110 MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
111  UNUSED_PARAMETER(currentTime);
112  std::vector<MSLane*>::iterator i;
113  const double speed = getCurrentSpeed();
115  if (myDestLanes.size() > 0 && myDestLanes.front()->getSpeedLimit() != speed) {
116  myDestLanes.front()->getEdge().setMaxSpeed(speed);
117  MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge(myDestLanes.front()->getEdge());
118  while (first != 0) {
119  first->setSpeed(speed, currentTime, -1);
120  first = first->getNextSegment();
121  }
122  }
123  } else {
124  for (i = myDestLanes.begin(); i != myDestLanes.end(); ++i) {
125  (*i)->setMaxSpeed(speed);
126  }
127  }
128  if (!move2next) {
129  // changed from the gui
130  return 0;
131  }
132  if (myCurrentEntry != myLoadedSpeeds.end()) {
133  ++myCurrentEntry;
134  }
135  if (myCurrentEntry != myLoadedSpeeds.end()) {
136  return ((*myCurrentEntry).first) - ((*(myCurrentEntry - 1)).first);
137  } else {
138  return 0;
139  }
140 }
141 
142 
143 void
145  const SUMOSAXAttributes& attrs) {
146  // check whether the correct tag is read
147  if (element != SUMO_TAG_STEP) {
148  return;
149  }
150  // extract the values
151  bool ok = true;
152  SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
153  double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
154  // check the values
155  if (next < 0) {
156  WRITE_ERROR("Wrong time in vss '" + getID() + "'.");
157  return;
158  }
159  if (speed < 0) {
160  speed = myDefaultSpeed;
161  }
162  // set the values for the next step if they are valid
163  if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
164  WRITE_WARNING("Time " + time2string(next) + " was set twice for vss '" + getID() + "'; replacing first entry.");
165  myLoadedSpeeds.back().second = speed;
166  } else {
167  myLoadedSpeeds.push_back(std::make_pair(next, speed));
168  }
169 }
170 
171 
172 void
174  if (element == SUMO_TAG_VSS && !myDidInit) {
175  init();
176  }
177 }
178 
179 
180 double
182  return myDefaultSpeed;
183 }
184 
185 
186 void
188  myAmOverriding = val;
189  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
190 }
191 
192 
193 void
195  mySpeedOverrideValue = val;
196  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
197 }
198 
199 
200 double
202  if (myCurrentEntry != myLoadedSpeeds.begin()) {
203  return (*(myCurrentEntry - 1)).second;
204  } else {
205  return (*myCurrentEntry).second;
206  }
207 }
208 
209 
210 double
212  if (myAmOverriding) {
213  return mySpeedOverrideValue;
214  } else {
216  // ok, maybe the first shall not yet be the valid one
217  if (myCurrentEntry == myLoadedSpeeds.begin() && (*myCurrentEntry).first > now) {
218  return myDefaultSpeed;
219  }
220  // try the loaded
221  if (myCurrentEntry != myLoadedSpeeds.end() && (*myCurrentEntry).first <= now) {
222  return (*myCurrentEntry).second;
223  } else {
224  // we have run past the end of the loaded steps or the current step is not yet active:
225  // -> use the value of the previous step
226  return (*(myCurrentEntry - 1)).second;
227  }
228  }
229 }
230 
231 
232 /****************************************************************************/
233 
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:157
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:613
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:60
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:158
const std::string & getID() const
Returns the id.
Definition: Named.h:66
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:110
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:38
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::vector< std::pair< SUMOTime, double > > myLoadedSpeeds
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:403
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:257
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:206
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:57
SUMOTime processCommand(bool move2next, SUMOTime currentTime)
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:113
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:52
static bool gUseMesoSim
Definition: MSGlobals.h:98
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.