Eclipse SUMO - Simulation of Urban MObility
MSDevice_ToC.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2013-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 /****************************************************************************/
20 // The ToC Device controls the transition of control between automated and manual driving.
21 //
22 /****************************************************************************/
23 #pragma once
24 #include <config.h>
25 
26 #include <random>
27 #include <queue>
28 #include "MSVehicleDevice.h"
29 #include <utils/common/SUMOTime.h>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class SUMOVehicle;
37 class MSVehicle;
38 class Command_ToCTrigger;
39 class Command_ToCProcess;
40 class RGBColor;
41 
42 // ===========================================================================
43 // class definitions
44 // ===========================================================================
52 class MSDevice_ToC : public MSVehicleDevice {
53 private:
54  // All currently existing ToC device instances
55  static std::set<MSDevice_ToC*, ComparatorNumericalIdLess> myInstances;
56  // All files, that receive ToC output (TODO: check if required)
57  static std::set<std::string> createdOutputFiles;
58 
59  struct OpenGapParams {
62  double changeRate;
63  double maxDecel;
64  bool active;
65  OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active) :
67  {};
68  };
69 
70 public:
74  static void insertOptions(OptionsCont& oc);
75 
76 
87  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
88 
91  static const std::set<MSDevice_ToC*, ComparatorNumericalIdLess>& getInstances() {
92  return myInstances;
93  };
94 
97  static void cleanup();
98 
99 private:
100 
108  enum ToCState {
110  MANUAL = 1,
112  PREPARING_TOC = 3, // this applies only to the transition AUTOMATED -> MANUAL !
113  MRM = 4,
114  RECOVERING = 5
115  };
116 
119  static double getDynamicMRMProbability(const SUMOVehicle& v, const OptionsCont& oc);
120  static std::string getOutputFilename(const SUMOVehicle& v, const OptionsCont& oc);
121  static OpenGapParams getOpenGapParams(const SUMOVehicle& v, const OptionsCont& oc);
122 
123  static ToCState _2ToCState(const std::string&);
124  static std::string _2string(ToCState state);
126 
127 
128 public:
130  ~MSDevice_ToC();
131 
133  const std::string deviceName() const override {
134  return "toc";
135  }
136 
138  bool notifyMove(SUMOTrafficObject& veh,
139  double oldPos,
140  double newPos,
141  double newSpeed) override;
142 
144  std::string getParameter(const std::string& key) const override;
145 
147  void setParameter(const std::string& key, const std::string& value) override;
148 
149 
152 
155 
158 
161 
164 
167 
169  void writeOutput();
170 
173  return myOutputFile != nullptr;
174  }
175 
177  return &myResponseTimeRNG;
178  }
179 private:
197  MSDevice_ToC(SUMOVehicle& holder, const std::string& id, const std::string& outputFilename,
198  const std::string& manualType, const std::string& automatedType, SUMOTime responseTime, double recoveryRate,
199  double lcAbstinence, double initialAwareness, double mrmDecel,
200  double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel,
201  bool mrmKeepRight, const std::string& mrmSafeSpot, SUMOTime mrmSafeSpotDuration, bool useColorScheme, OpenGapParams ogp);
202 
207  void initColorScheme();
208 
210  void setAwareness(double value);
211 
213  void setState(ToCState state);
214 
215  // @brief Sets the device holder's color corresponding to the current state
216  void setVehicleColor();
217 
227  void requestToC(SUMOTime timeTillMRM, SUMOTime responseTime = -1000);
228 
231  void requestMRM();
232 
234  void switchHolderType(const std::string& targetTypeID);
235 
237  void descheduleMRM();
239  void descheduleToC();
243  void descheduleRecovery();
244 
246  void resetDeliberateLCs();
249 
251  bool isManuallyDriven();
253  bool isAutomated();
254 
258  bool checkDynamicToC();
259 
260 private:
263 
265  std::string myManualTypeID;
267  std::string myAutomatedTypeID;
268 
269 
278 
280  double myMRMDecel;
281 
284 
286  std::map<ToCState, RGBColor> myColorScheme;
287 
290 
293 
295 
298 
308 
311 
313  std::queue<std::pair<SUMOTime, std::string> > myEvents;
314 
316  std::queue<std::pair<std::string, double> > myEventLanes;
317 
319  std::queue<std::pair<double, double>> myEventXY;
320 
323 
325  static int LCModeMRM;
326 
329 
334  // (Note that these MRMs will not induce full stops in most cases)
342 
345 
347  std::string myMRMSafeSpot;
348 
351 
355 
358 
360  static std::vector<double> lookupResponseTimeMRMProbs;
361  static std::vector<double> lookupResponseTimeLeadTimes;
363  static double responseTimeMean(double leadTime) {
364  return MIN2(2 * sqrt(leadTime), 0.7 * leadTime);
365  };
369  static std::vector<std::vector<double> > lookupResponseTimeVariances;
370 
373 
376  double sampleResponseTime(double leadTime) const;
377 
380  static double interpolateVariance(double leadTime, double pMRM);
381 
382 private:
385 
388 
389 
390 };
long long int SUMOTime
Definition: GUI.h:35
T MIN2(T a, T b)
Definition: StdDefs.h:76
The ToC Device controls transition of control between automated and manual driving.
Definition: MSDevice_ToC.h:52
SUMOTime MRMExecutionStep(SUMOTime t)
Continue the MRM for one time step.
std::string myAutomatedTypeID
vehicle type ID for automated driving
Definition: MSDevice_ToC.h:267
void requestMRM()
Request an MRM to be initiated immediately. No downward ToC will be scheduled.
std::string myManualTypeID
vehicle type ID for manual driving
Definition: MSDevice_ToC.h:265
static void cleanup()
Closes root tags of output files.
double myRecoveryRate
Recovery rate for the driver's awareness after a ToC.
Definition: MSDevice_ToC.h:273
bool myDynamicToCActive
Switch for considering dynamic ToCs,.
Definition: MSDevice_ToC.h:337
double myMRMDecel
Deceleration rate applied during MRM.
Definition: MSDevice_ToC.h:280
WrappingCommand< MSDevice_ToC > * myTriggerToCCommand
Definition: MSDevice_ToC.h:303
static double interpolateVariance(double leadTime, double pMRM)
Two-dimensional interpolation of variance from lookup table assumes pMRM >= 0, leadTime >= 0.
static int LCModeMRM
LC mode operational during an MRM.
Definition: MSDevice_ToC.h:325
double myInitialAwareness
Average awareness the driver has initially after a ToC.
Definition: MSDevice_ToC.h:277
bool myUseColorScheme
Whether a coloring scheme shall by applied to indicate the different toc stages,.
Definition: MSDevice_ToC.h:289
OpenGapParams myOpenGapParams
Parameters for the openGap mechanism applied during ToC preparation phase.
Definition: MSDevice_ToC.h:328
static std::vector< std::vector< double > > lookupResponseTimeVariances
Variances of the response time distribution. Given the lead time and the MRM probability the variance...
Definition: MSDevice_ToC.h:365
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed) override
Return value indicates whether the device still wants to be notified about the vehicle movement.
SUMOTime myMRMSafeSpotDuration
duration at stop vehicle tries to reach during MRM
Definition: MSDevice_ToC.h:350
void resetDeliberateLCs()
Resets the holder's LC mode to the last differing to LCModeMRM.
double myOriginalMaxAccel
Storage for original maximal acceleration of vehicle.
Definition: MSDevice_ToC.h:357
const std::string deviceName() const override
return the name for this type of device
Definition: MSDevice_ToC.h:133
static std::vector< double > lookupResponseTimeLeadTimes
Definition: MSDevice_ToC.h:361
WrappingCommand< MSDevice_ToC > * myExecuteMRMCommand
Definition: MSDevice_ToC.h:305
static std::set< std::string > createdOutputFiles
Definition: MSDevice_ToC.h:57
static const std::set< MSDevice_ToC *, ComparatorNumericalIdLess > & getInstances()
returns all currently existing ToC devices
Definition: MSDevice_ToC.h:91
SUMOTime ToCPreparationStep(SUMOTime t)
Continue the ToC preparation for one time step.
~MSDevice_ToC()
Destructor.
bool myIssuedDynamicToC
Flag to indicate that a dynamically triggered ToC is in preparation.
Definition: MSDevice_ToC.h:339
double myMRMProbability
Probability of an MRM to occur after a dynamically triggered ToC.
Definition: MSDevice_ToC.h:335
static std::set< MSDevice_ToC *, ComparatorNumericalIdLess > myInstances
Definition: MSDevice_ToC.h:55
void descheduleRecovery()
Remove ongoing awareness recovery process from the event-queue.
int myPreviousLCMode
LC mode overridden during MRM, stored for restoration.
Definition: MSDevice_ToC.h:322
void requestToC(SUMOTime timeTillMRM, SUMOTime responseTime=-1000)
Request a ToC. If the device is in AUTOMATED or MRM state, a driver response time is sampled and the ...
double sampleResponseTime(double leadTime) const
Samples a random driver response time from a truncated Gaussian with parameters according to the look...
static OpenGapParams getOpenGapParams(const SUMOVehicle &v, const OptionsCont &oc)
std::queue< std::pair< std::string, double > > myEventLanes
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:316
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
MSDevice_ToC & operator=(const MSDevice_ToC &)
Invalidated assignment operator.
bool myMRMKeepRight
Whether vehicle tries to change to the right during an MRM.
Definition: MSDevice_ToC.h:344
bool isManuallyDriven()
Whether the current operation mode is manual.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static SumoRNG * getResponseTimeRNG()
Definition: MSDevice_ToC.h:176
void setVehicleColor()
ToCState myState
Current state of the device.
Definition: MSDevice_ToC.h:292
double myMaxPreparationAccel
Maximal acceleration that may be applied during the ToC preparation phase TODO: Make effective.
Definition: MSDevice_ToC.h:354
static ToCState _2ToCState(const std::string &)
static std::string getOutputFilename(const SUMOVehicle &v, const OptionsCont &oc)
MSDevice_ToC(SUMOVehicle &holder, const std::string &id, const std::string &outputFilename, const std::string &manualType, const std::string &automatedType, SUMOTime responseTime, double recoveryRate, double lcAbstinence, double initialAwareness, double mrmDecel, double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel, bool mrmKeepRight, const std::string &mrmSafeSpot, SUMOTime mrmSafeSpotDuration, bool useColorScheme, OpenGapParams ogp)
Constructor.
int myDynamicToCLane
Lane, on which the ongoing dynamic ToC was issued. It can only be aborted if the lane was changed.
Definition: MSDevice_ToC.h:341
bool isAutomated()
Whether the current operation mode is automated.
std::map< ToCState, RGBColor > myColorScheme
Coloring scheme,.
Definition: MSDevice_ToC.h:286
static std::string _2string(ToCState state)
static SumoRNG myResponseTimeRNG
Random generator for ToC devices.
Definition: MSDevice_ToC.h:372
void setState(ToCState state)
Set the ToC device's state.
SUMOTime myResponseTime
Average response time needed by the driver to take back control.
Definition: MSDevice_ToC.h:271
void setParameter(const std::string &key, const std::string &value) override
try to set the given parameter for this device. Throw exception for unsupported key
bool generatesOutput()
Whether this device requested to write output.
Definition: MSDevice_ToC.h:172
double myLCAbstinence
Level of the awareness below which no lane-changes are performed.
Definition: MSDevice_ToC.h:275
SUMOTime triggerDownwardToC(SUMOTime t)
Trigger execution of a ToC X-->MANUAL ("downwards")
void initColorScheme()
Initialize vehicle colors for different states.
MSVehicle * myHolderMS
The holder vehicle casted to MSVehicle*.
Definition: MSDevice_ToC.h:297
WrappingCommand< MSDevice_ToC > * myPrepareToCCommand
Definition: MSDevice_ToC.h:306
std::string getParameter(const std::string &key) const override
try to retrieve the given parameter from this device. Throw exception for unsupported key
void descheduleMRM()
Break MRM Process or remove MRM-Trigger command from the event-queue.
static double responseTimeMean(double leadTime)
Mean of the response time distribution. (Only depends on given lead time)
Definition: MSDevice_ToC.h:363
void descheduleToC()
Remove scheduled ToC-Trigger command from the event-queue.
void descheduleToCPreparation()
Remove ongoing ToC-Preparation process from the event-queue.
MSDevice_ToC(const MSDevice_ToC &)
Invalidated copy constructor.
void deactivateDeliberateLCs()
Resets the holder's LC mode to the operational LC-mode of the ToC Device (.
WrappingCommand< MSDevice_ToC > * myRecoverAwarenessCommand
Definition: MSDevice_ToC.h:304
void setAwareness(double value)
Set the awareness to the given value.
WrappingCommand< MSDevice_ToC > * myTriggerMRMCommand
Definition: MSDevice_ToC.h:302
double myDynamicToCThreshold
Duration in s. for which the vehicle needs to be able to follow its route without a lane change to co...
Definition: MSDevice_ToC.h:332
bool checkDynamicToC()
Check if the vehicle should induce a ToC due to internal reasons. That is, if the route cannot be fol...
ToCState
Enum describing the different regimes for the device,.
Definition: MSDevice_ToC.h:108
static double getDynamicMRMProbability(const SUMOVehicle &v, const OptionsCont &oc)
void writeOutput()
Write output to file given by option device.toc.file.
std::queue< std::pair< double, double > > myEventXY
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:319
OutputDevice * myOutputFile
The file the devices output goes to.
Definition: MSDevice_ToC.h:310
double myCurrentAwareness
Current awareness-level of the driver in [0,1].
Definition: MSDevice_ToC.h:283
SUMOTime triggerMRM(SUMOTime t)
Trigger execution of an MRM.
SUMOTime triggerUpwardToC(SUMOTime t)
Trigger execution of a ToC X-->AUTOMATED ("upwards")
std::queue< std::pair< SUMOTime, std::string > > myEvents
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:313
SUMOTime awarenessRecoveryStep(SUMOTime t)
Continue the awareness recovery for one time step.
std::string myMRMSafeSpot
stop vehicle tries to reach during MRM
Definition: MSDevice_ToC.h:347
static std::vector< double > lookupResponseTimeMRMProbs
Grid of the response time distribution.
Definition: MSDevice_ToC.h:360
void switchHolderType(const std::string &targetTypeID)
Switch the device holder's vehicle type.
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
A storage for options typed value containers)
Definition: OptionsCont.h:89
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active)
Definition: MSDevice_ToC.h:65