Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSLaneSpeedTrigger.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
22// Changes the speed allowed on a set of lanes
23/****************************************************************************/
24#include <config.h>
25
26#include <string>
31#include <utils/xml/XMLSubSys.h>
34#include <microsim/MSLane.h>
35#include <microsim/MSNet.h>
36#include <microsim/MSEdge.h>
37#include "MSLaneSpeedTrigger.h"
38
39#include <microsim/MSGlobals.h>
40#include <mesosim/MELoop.h>
41#include <mesosim/MESegment.h>
42
43
44// ===========================================================================
45// static members
46// ===========================================================================
47std::map<std::string, MSLaneSpeedTrigger*> MSLaneSpeedTrigger::myInstances;
48
49// ===========================================================================
50// method definitions
51// ===========================================================================
53 const std::vector<MSLane*>& destLanes,
54 const std::string& file) :
55 Named(id),
56 SUMOSAXHandler(file),
57 myDestLanes(destLanes),
58 myDefaultSpeed(destLanes[0]->getSpeedLimit()),
59 myDefaultFriction(destLanes[0]->getFrictionCoefficient()),
60 myAmOverriding(false),
61 mySpeedOverrideValue(destLanes[0]->getSpeedLimit()),
62 myDidInit(false) {
63 myInstances[id] = this;
64 if (file != "") {
65 if (!XMLSubSys::runParser(*this, file)) {
66 throw ProcessError();
67 }
68 if (!myDidInit) {
69 init();
70 }
71 }
72}
73
74
75void
77 // set the process to the begin
80 // pass previous time steps
82 while (myCurrentSpeedEntry != myLoadedSpeeds.end() && myCurrentSpeedEntry->first < now) {
83 processCommand(true, now);
84 }
85 while (myCurrentFrictionEntry != myLoadedFrictions.end() && myCurrentFrictionEntry->first < now) {
87 }
88
89 // add the processing to the event handler
93 myCurrentSpeedEntry->first);
94 }
99 }
100 myDidInit = true;
101}
102
103
107
108
111 return processCommand(true, currentTime);
112}
113
114
116MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
117 const double speed = getCurrentSpeed();
118 const bool altered = speed != myDefaultSpeed;
119 for (MSLane* const lane : myDestLanes) {
120 lane->setMaxSpeed(speed, altered);
121 }
122 if (!move2next) {
123 // changed from the gui
124 return 0;
125 }
126 if (myCurrentSpeedEntry != myLoadedSpeeds.end()) {
128 }
129 if (myCurrentSpeedEntry != myLoadedSpeeds.end()) {
130 return myCurrentSpeedEntry->first - currentTime;
131 }
132 return 0;
133}
134
135
138 const double friction = getCurrentFriction();
139 for (MSLane* const lane : myDestLanes) {
140 lane->setFrictionCoefficient(friction);
141 }
144 }
146 return myCurrentFrictionEntry->first - currentTime;
147 }
148 return 0;
149}
150
151
152void
154 // check whether the correct tag is read
155 if (element != SUMO_TAG_STEP) {
156 return;
157 }
158 // extract the values
159 bool ok = true;
160 const SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
161 double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
162 double friction = attrs.getOpt<double>(SUMO_ATTR_FRICTION, getID().c_str(), ok, -1);
163 // check the values
164 if (next < 0 || (speed > 0 && !myLoadedSpeeds.empty() && myLoadedSpeeds.back().first > next) ||
165 (friction > 0 && !myLoadedFrictions.empty() && myLoadedFrictions.back().first > next)) {
166 WRITE_ERRORF(TL("Invalid or unsorted time entry in vss '%'."), getID());
167 return;
168 }
169 if (speed < 0 && friction < 0) {
170 speed = myDefaultSpeed;
171 friction = myDefaultFriction;
172 }
173 if (speed < 0 && attrs.hasAttribute(SUMO_ATTR_SPEED)) {
174 speed = myDefaultSpeed;
175 }
176 if (friction < 0 && attrs.hasAttribute(SUMO_ATTR_FRICTION)) {
177 friction = myDefaultFriction;
178 }
179 // set the values for the next step if they are valid
180 if (speed >= 0) {
181 if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
182 WRITE_WARNINGF(TL("Time % was set twice for vss '%'; replacing first entry."), time2string(next), getID());
183 myLoadedSpeeds.back().second = speed;
184 } else {
185 myLoadedSpeeds.push_back(std::make_pair(next, speed));
186 }
187 }
188 if (friction >= 0) {
189 myLoadedFrictions.push_back(std::make_pair(next, friction));
190 }
191}
192
193
194void
196 if (element == SUMO_TAG_VSS && !myDidInit) {
197 init();
198 }
199}
200
201
202double
206
207
208void
210 myAmOverriding = val;
211 processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
212}
213
214
215void
218 processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
219}
220
221
222double
224 if (myLoadedSpeeds.empty()) {
225 return myDefaultSpeed;
226 }
227 if (myCurrentSpeedEntry != myLoadedSpeeds.begin()) {
228 return (*(myCurrentSpeedEntry - 1)).second;
229 } else {
230 return myCurrentSpeedEntry->second;
231 }
232}
233
234
235double
237 if (myAmOverriding) {
239 } else {
240 if (myLoadedSpeeds.empty()) {
241 return myDefaultSpeed;
242 }
244 // ok, maybe the first shall not yet be the valid one
245 if (myCurrentSpeedEntry == myLoadedSpeeds.begin() && myCurrentSpeedEntry->first > now) {
246 return myDefaultSpeed;
247 }
248 // try the loaded
249 if (myCurrentSpeedEntry != myLoadedSpeeds.end() && myCurrentSpeedEntry->first <= now) {
250 return myCurrentSpeedEntry->second;
251 } else {
252 // we have run past the end of the loaded steps or the current step is not yet active:
253 // -> use the value of the previous step
254 return (*(myCurrentSpeedEntry - 1)).second;
255 }
256 }
257}
258
259
260double
262 if (myLoadedFrictions.empty()) {
263 return myDefaultFriction;
264 }
266 // ok, maybe the first shall not yet be the valid one
267 if (myCurrentFrictionEntry == myLoadedFrictions.begin() && myCurrentFrictionEntry->first > now) {
268 return myDefaultFriction;
269 }
270 // try the loaded
272 return myCurrentFrictionEntry->second;
273 } else {
274 // we have run past the end of the loaded steps or the current step is not yet active:
275 // -> use the value of the previous step
276 return (*(myCurrentFrictionEntry - 1)).second;
277 }
278}
279
280
281/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define WRITE_ERRORF(...)
Definition MsgHandler.h:305
#define TL(string)
Definition MsgHandler.h:315
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
@ SUMO_TAG_STEP
trigger: a step description
@ SUMO_TAG_VSS
A variable speed sign.
@ SUMO_ATTR_SPEED
@ SUMO_ATTR_FRICTION
@ SUMO_ATTR_TIME
trigger: the time of the step
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Representation of a lane in the micro simulation.
Definition MSLane.h:84
void setOverridingValue(double val)
static std::map< std::string, MSLaneSpeedTrigger * > myInstances
MSLaneSpeedTrigger(const std::string &id, const std::vector< MSLane * > &destLanes, const std::string &file)
Constructor.
std::vector< std::pair< SUMOTime, double > > myLoadedSpeeds
virtual void myEndElement(int element)
Called on the closing of a tag;.
double getCurrentFriction() const
Returns the current friction.
double getCurrentSpeed() const
Returns the current speed.
double mySpeedOverrideValue
The speed to use if overriding the read speed.
SUMOTime executeFrictionChange(SUMOTime currentTime)
virtual ~MSLaneSpeedTrigger()
Destructor.
const double myDefaultFriction
The original friction on the lanes.
std::vector< std::pair< SUMOTime, double > >::iterator myCurrentSpeedEntry
std::vector< MSLane * > myDestLanes
std::vector< std::pair< SUMOTime, double > > myLoadedFrictions
std::vector< std::pair< SUMOTime, double > >::iterator myCurrentFrictionEntry
bool myAmOverriding
The information whether the read speed shall be overridden.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
SUMOTime executeSpeedChange(SUMOTime currentTime)
Executes a switch command.
const double myDefaultSpeed
The original speed allowed on the lanes.
SUMOTime processCommand(bool move2next, SUMOTime currentTime)
double getDefaultSpeed() const
bool myDidInit
The information whether init was called.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:185
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition MSNet.h:471
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:320
Base class for objects which have an id.
Definition Named.h:54
const std::string & getID() const
Returns the id.
Definition Named.h:74
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
SAX-handler base for SUMO-files.
A wrapper for a Command function.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.