SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
14 // Copyright (C) 2001-2012 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 #ifdef HAVE_MESOSIM
49 #include <microsim/MSGlobals.h>
50 #include <mesosim/MELoop.h>
51 #include <mesosim/MESegment.h>
52 #endif
53 
54 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
63  const std::vector<MSLane*> &destLanes,
64  const std::string& file)
65  : MSTrigger(id), SUMOSAXHandler(file),
66  myDestLanes(destLanes), myAmOverriding(false), myDidInit(false) {
67  myCurrentSpeed = destLanes[0]->getMaxSpeed();
68  if (file != "") {
69  if (!XMLSubSys::runParser(*this, file)) {
70  throw ProcessError();
71  }
72  if (!myDidInit) {
73  init();
74  }
75  }
76 }
77 
78 void
80  // set it to the right value
81  // assert there is at least one
82  if (myLoadedSpeeds.size() == 0) {
83  myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
84  }
85  // set the process to the begin
87  // pass previous time steps
88  while ((*myCurrentEntry).first < MSNet::getInstance()->getCurrentTimeStep() && myCurrentEntry != myLoadedSpeeds.end()) {
89  processCommand(true, MSNet::getInstance()->getCurrentTimeStep());
90  }
91 
92  // add the processing to the event handler
95  (*myCurrentEntry).first, MSEventControl::NO_CHANGE);
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 SUMOReal speed = getCurrentSpeed();
114  for (i = myDestLanes.begin(); i != myDestLanes.end(); ++i) {
115 #ifdef HAVE_MESOSIM
117  MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge((*i)->getEdge());
118  while (first != 0) {
119  first->setSpeed(speed, currentTime);
120  first = first->getNextSegment();
121  }
122  continue;
123  }
124 #endif
125  (*i)->setMaxSpeed(speed);
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  SUMOReal speed = attrs.getOptSUMORealReporting(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  WRITE_ERROR("Wrong speed in vss '" + getID() + "'.");
160  return;
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 SUMOReal
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 SUMOReal
202  if (myCurrentEntry != myLoadedSpeeds.begin()) {
203  return (*(myCurrentEntry - 1)).second;
204  } else {
205  return (*myCurrentEntry).second;
206  }
207 }
208 
209 
210 SUMOReal
212  if (myAmOverriding) {
213  return mySpeedOverrideValue;
214  } else {
215  // ok, maybe the first shall not yet be the valid one
216  if (myCurrentEntry == myLoadedSpeeds.begin() && (*myCurrentEntry).first > MSNet::getInstance()->getCurrentTimeStep()) {
217  return myDefaultSpeed;
218  }
219  // try the loaded
220  if (myCurrentEntry != myLoadedSpeeds.end() && (*myCurrentEntry).first <= MSNet::getInstance()->getCurrentTimeStep()) {
221  return (*myCurrentEntry).second;
222  } else {
223  return (*(myCurrentEntry - 1)).second;
224  }
225  }
226 }
227 
228 
229 /****************************************************************************/
230