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);
120  static OpenGapParams getOpenGapParams(const SUMOVehicle& v);
121 
122  static ToCState _2ToCState(const std::string&);
123  static std::string _2string(ToCState state);
125 
126 
127 public:
129  ~MSDevice_ToC();
130 
132  const std::string deviceName() const override {
133  return "toc";
134  }
135 
137  bool notifyMove(SUMOTrafficObject& veh,
138  double oldPos,
139  double newPos,
140  double newSpeed) override;
141 
143  std::string getParameter(const std::string& key) const override;
144 
146  void setParameter(const std::string& key, const std::string& value) override;
147 
148 
151 
154 
157 
160 
163 
166 
168  void writeOutput();
169 
172  return myOutputFile != nullptr;
173  }
174 
176  return &myResponseTimeRNG;
177  }
178 private:
196  MSDevice_ToC(SUMOVehicle& holder, const std::string& id, const std::string& outputFilename,
197  const std::string& manualType, const std::string& automatedType, SUMOTime responseTime, double recoveryRate,
198  double lcAbstinence, double initialAwareness, double mrmDecel,
199  double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel,
200  bool mrmKeepRight, const std::string& mrmSafeSpot, SUMOTime mrmSafeSpotDuration, bool useColorScheme, OpenGapParams ogp);
201 
206  void initColorScheme();
207 
209  void setAwareness(double value);
210 
212  void setState(ToCState state);
213 
214  // @brief Sets the device holder's color corresponding to the current state
215  void setVehicleColor();
216 
226  void requestToC(SUMOTime timeTillMRM, SUMOTime responseTime = -1000);
227 
230  void requestMRM();
231 
233  void switchHolderType(const std::string& targetTypeID);
234 
236  void descheduleMRM();
238  void descheduleToC();
242  void descheduleRecovery();
243 
245  void resetDeliberateLCs();
248 
250  bool isManuallyDriven();
252  bool isAutomated();
253 
257  bool checkDynamicToC();
258 
259 private:
262 
264  std::string myManualTypeID;
266  std::string myAutomatedTypeID;
267 
268 
277 
279  double myMRMDecel;
280 
283 
285  std::map<ToCState, RGBColor> myColorScheme;
286 
289 
292 
294 
297 
307 
310 
312  std::queue<std::pair<SUMOTime, std::string> > myEvents;
313 
315  std::queue<std::pair<std::string, double> > myEventLanes;
316 
318  std::queue<std::pair<double, double>> myEventXY;
319 
322 
324  static int LCModeMRM;
325 
328 
333  // (Note that these MRMs will not induce full stops in most cases)
341 
344 
346  std::string myMRMSafeSpot;
347 
350 
354 
357 
359  static std::vector<double> lookupResponseTimeMRMProbs;
360  static std::vector<double> lookupResponseTimeLeadTimes;
362  static double responseTimeMean(double leadTime) {
363  return MIN2(2 * sqrt(leadTime), 0.7 * leadTime);
364  };
368  static std::vector<std::vector<double> > lookupResponseTimeVariances;
369 
372 
375  double sampleResponseTime(double leadTime) const;
376 
379  static double interpolateVariance(double leadTime, double pMRM);
380 
381 private:
384 
387 
388 
389 };
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:266
void requestMRM()
Request an MRM to be initiated immediately. No downward ToC will be scheduled.
static double getDynamicMRMProbability(const SUMOVehicle &v)
std::string myManualTypeID
vehicle type ID for manual driving
Definition: MSDevice_ToC.h:264
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:272
bool myDynamicToCActive
Switch for considering dynamic ToCs,.
Definition: MSDevice_ToC.h:336
double myMRMDecel
Deceleration rate applied during MRM.
Definition: MSDevice_ToC.h:279
WrappingCommand< MSDevice_ToC > * myTriggerToCCommand
Definition: MSDevice_ToC.h:302
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:324
double myInitialAwareness
Average awareness the driver has initially after a ToC.
Definition: MSDevice_ToC.h:276
bool myUseColorScheme
Whether a coloring scheme shall by applied to indicate the different toc stages,.
Definition: MSDevice_ToC.h:288
OpenGapParams myOpenGapParams
Parameters for the openGap mechanism applied during ToC preparation phase.
Definition: MSDevice_ToC.h:327
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:364
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:349
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:356
const std::string deviceName() const override
return the name for this type of device
Definition: MSDevice_ToC.h:132
static std::vector< double > lookupResponseTimeLeadTimes
Definition: MSDevice_ToC.h:360
WrappingCommand< MSDevice_ToC > * myExecuteMRMCommand
Definition: MSDevice_ToC.h:304
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:338
double myMRMProbability
Probability of an MRM to occur after a dynamically triggered ToC.
Definition: MSDevice_ToC.h:334
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:321
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...
std::queue< std::pair< std::string, double > > myEventLanes
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:315
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:343
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:175
void setVehicleColor()
ToCState myState
Current state of the device.
Definition: MSDevice_ToC.h:291
double myMaxPreparationAccel
Maximal acceleration that may be applied during the ToC preparation phase TODO: Make effective.
Definition: MSDevice_ToC.h:353
static ToCState _2ToCState(const std::string &)
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:340
bool isAutomated()
Whether the current operation mode is automated.
std::map< ToCState, RGBColor > myColorScheme
Coloring scheme,.
Definition: MSDevice_ToC.h:285
static std::string _2string(ToCState state)
static SumoRNG myResponseTimeRNG
Random generator for ToC devices.
Definition: MSDevice_ToC.h:371
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:270
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:171
double myLCAbstinence
Level of the awareness below which no lane-changes are performed.
Definition: MSDevice_ToC.h:274
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:296
WrappingCommand< MSDevice_ToC > * myPrepareToCCommand
Definition: MSDevice_ToC.h:305
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:362
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:303
void setAwareness(double value)
Set the awareness to the given value.
WrappingCommand< MSDevice_ToC > * myTriggerMRMCommand
Definition: MSDevice_ToC.h:301
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:331
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
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:318
OutputDevice * myOutputFile
The file the devices output goes to.
Definition: MSDevice_ToC.h:309
double myCurrentAwareness
Current awareness-level of the driver in [0,1].
Definition: MSDevice_ToC.h:282
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:312
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:346
static OpenGapParams getOpenGapParams(const SUMOVehicle &v)
static std::vector< double > lookupResponseTimeMRMProbs
Grid of the response time distribution.
Definition: MSDevice_ToC.h:359
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:62
OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active)
Definition: MSDevice_ToC.h:65