Eclipse 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.dev/sumo
3// Copyright (C) 2001-2023 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
105 myInstances.erase(getID());
106}
107
108
111 return processCommand(true, currentTime);
112}
113
114
116MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
117 const double 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 != nullptr) {
123 first->setSpeed(speed, currentTime, -1);
124 first = first->getNextSegment();
125 }
126 }
127 } else {
128 for (MSLane* const lane : myDestLanes) {
129 lane->setMaxSpeed(speed);
130 }
131 }
132 if (!move2next) {
133 // changed from the gui
134 return 0;
135 }
136 if (myCurrentSpeedEntry != myLoadedSpeeds.end()) {
138 }
139 if (myCurrentSpeedEntry != myLoadedSpeeds.end()) {
140 return myCurrentSpeedEntry->first - currentTime;
141 }
142 return 0;
143}
144
145
148 const double friction = getCurrentFriction();
149 for (MSLane* const lane : myDestLanes) {
150 lane->setFrictionCoefficient(friction);
151 }
154 }
156 return myCurrentFrictionEntry->first - currentTime;
157 }
158 return 0;
159}
160
161
162void
164 // check whether the correct tag is read
165 if (element != SUMO_TAG_STEP) {
166 return;
167 }
168 // extract the values
169 bool ok = true;
170 const SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
171 double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
172 double friction = attrs.getOpt<double>(SUMO_ATTR_FRICTION, getID().c_str(), ok, -1);
173 // check the values
174 if (next < 0 || (speed > 0 && !myLoadedSpeeds.empty() && myLoadedSpeeds.back().first > next) ||
175 (friction > 0 && !myLoadedFrictions.empty() && myLoadedFrictions.back().first > next)) {
176 WRITE_ERRORF(TL("Invalid or unsorted time entry in vss '%'."), getID());
177 return;
178 }
179 if (speed < 0 && friction < 0) {
180 speed = myDefaultSpeed;
181 friction = myDefaultFriction;
182 }
183 if (speed < 0 && attrs.hasAttribute(SUMO_ATTR_SPEED)) {
184 speed = myDefaultSpeed;
185 }
186 if (friction < 0 && attrs.hasAttribute(SUMO_ATTR_FRICTION)) {
187 friction = myDefaultFriction;
188 }
189 // set the values for the next step if they are valid
190 if (speed >= 0) {
191 if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
192 WRITE_WARNINGF(TL("Time % was set twice for vss '%'; replacing first entry."), time2string(next), getID());
193 myLoadedSpeeds.back().second = speed;
194 } else {
195 myLoadedSpeeds.push_back(std::make_pair(next, speed));
196 }
197 }
198 if (friction >= 0) {
199 myLoadedFrictions.push_back(std::make_pair(next, friction));
200 }
201}
202
203
204void
206 if (element == SUMO_TAG_VSS && !myDidInit) {
207 init();
208 }
209}
210
211
212double
214 return myDefaultSpeed;
215}
216
217
218void
220 myAmOverriding = val;
221 processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
222}
223
224
225void
228 processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
229}
230
231
232double
234 if (myLoadedSpeeds.empty()) {
235 return myDefaultSpeed;
236 }
237 if (myCurrentSpeedEntry != myLoadedSpeeds.begin()) {
238 return (*(myCurrentSpeedEntry - 1)).second;
239 } else {
240 return myCurrentSpeedEntry->second;
241 }
242}
243
244
245double
247 if (myAmOverriding) {
249 } else {
250 if (myLoadedSpeeds.empty()) {
251 return myDefaultSpeed;
252 }
254 // ok, maybe the first shall not yet be the valid one
255 if (myCurrentSpeedEntry == myLoadedSpeeds.begin() && myCurrentSpeedEntry->first > now) {
256 return myDefaultSpeed;
257 }
258 // try the loaded
259 if (myCurrentSpeedEntry != myLoadedSpeeds.end() && myCurrentSpeedEntry->first <= now) {
260 return myCurrentSpeedEntry->second;
261 } else {
262 // we have run past the end of the loaded steps or the current step is not yet active:
263 // -> use the value of the previous step
264 return (*(myCurrentSpeedEntry - 1)).second;
265 }
266 }
267}
268
269
270double
272 if (myLoadedFrictions.empty()) {
273 return myDefaultFriction;
274 }
276 // ok, maybe the first shall not yet be the valid one
277 if (myCurrentFrictionEntry == myLoadedFrictions.begin() && myCurrentFrictionEntry->first > now) {
278 return myDefaultFriction;
279 }
280 // try the loaded
282 return myCurrentFrictionEntry->second;
283 } else {
284 // we have run past the end of the loaded steps or the current step is not yet active:
285 // -> use the value of the previous step
286 return (*(myCurrentFrictionEntry - 1)).second;
287 }
288}
289
290
291/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:271
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:280
#define TL(string)
Definition: MsgHandler.h:287
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
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:325
A single mesoscopic segment (cell)
Definition: MESegment.h:49
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:727
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:234
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static bool gUseMesoSim
Definition: MSGlobals.h:103
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:109
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
void setOverridingValue(double val)
void setOverriding(bool 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:183
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:473
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:322
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.
Definition: XMLSubSys.cpp:157