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.org/sumo
3 // Copyright (C) 2013-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The ToC Device controls the transition of control between automated and manual driving.
18 //
19 /****************************************************************************/
20 #ifndef MSDevice_ToC_h
21 #define MSDevice_ToC_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <random>
30 #include <queue>
31 #include "MSVehicleDevice.h"
32 #include <utils/common/SUMOTime.h>
34 
35 
36 // ===========================================================================
37 // class declarations
38 // ===========================================================================
39 class SUMOVehicle;
40 class MSVehicle;
41 class Command_ToCTrigger;
42 class Command_ToCProcess;
43 class RGBColor;
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
55 class MSDevice_ToC : public MSVehicleDevice {
56 private:
57  // All currently existing ToC device instances
58  static std::set<MSDevice_ToC*, ComparatorNumericalIdLess> myInstances;
59  // All files, that receive ToC output (TODO: check if required)
60  static std::set<std::string> createdOutputFiles;
61 
62  struct OpenGapParams {
65  double changeRate;
66  double maxDecel;
67  bool active;
68  OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active) :
69  newTimeHeadway(timegap), newSpaceHeadway(spacing), changeRate(changeRate), maxDecel(maxDecel), active(active)
70  {};
71  };
72 
73 public:
77  static void insertOptions(OptionsCont& oc);
78 
79 
90  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
91 
94  static const std::set<MSDevice_ToC*, ComparatorNumericalIdLess>& getInstances() {
95  return myInstances;
96  };
97 
100  static void cleanup();
101 
102 private:
103 
111  enum ToCState {
113  MANUAL = 1,
115  PREPARING_TOC = 3, // this applies only to the transition AUTOMATED -> MANUAL !
116  MRM = 4,
118  };
119 
122  static std::string getManualType(const SUMOVehicle& v, const OptionsCont& oc);
123  static std::string getAutomatedType(const SUMOVehicle& v, const OptionsCont& oc);
124  static double getResponseTime(const SUMOVehicle& v, const OptionsCont& oc);
125  static double getRecoveryRate(const SUMOVehicle& v, const OptionsCont& oc);
126  static double getLCAbstinence(const SUMOVehicle& v, const OptionsCont& oc);
127  static double getInitialAwareness(const SUMOVehicle& v, const OptionsCont& oc);
128  static double getMRMDecel(const SUMOVehicle& v, const OptionsCont& oc);
129  static double getDynamicToCThreshold(const SUMOVehicle& v, const OptionsCont& oc);
130  static double getDynamicMRMProbability(const SUMOVehicle& v, const OptionsCont& oc);
131  static double getMaxPreparationAccel(const SUMOVehicle& v, const OptionsCont& oc);
132  static bool getMRMKeepRight(const SUMOVehicle& v, const OptionsCont& oc);
133  static bool useColorScheme(const SUMOVehicle& v, const OptionsCont& oc);
134  static std::string getOutputFilename(const SUMOVehicle& v, const OptionsCont& oc);
135  static OpenGapParams getOpenGapParams(const SUMOVehicle& v, const OptionsCont& oc);
136 
137  static ToCState _2ToCState(const std::string&);
138  static std::string _2string(ToCState state);
140 
141 
142 public:
144  ~MSDevice_ToC();
145 
147  const std::string deviceName() const override {
148  return "toc";
149  }
150 
152  bool notifyMove(SUMOTrafficObject& veh,
153  double oldPos,
154  double newPos,
155  double newSpeed) override;
156 
158  std::string getParameter(const std::string& key) const override;
159 
161  void setParameter(const std::string& key, const std::string& value) override;
162 
163 
166 
169 
172 
175 
178 
181 
183  void writeOutput();
184 
187  return myOutputFile != nullptr;
188  }
189 
190  static std::mt19937* getResponseTimeRNG() {
191  return &myResponseTimeRNG;
192  }
193 private:
209  MSDevice_ToC(SUMOVehicle& holder, const std::string& id, const std::string& outputFilename,
210  std::string manualType, std::string automatedType, SUMOTime responseTime, double recoveryRate,
211  double lcAbstinence, double initialAwareness, double mrmDecel,
212  double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel,
213  bool mrmKeepRight, bool useColorScheme, OpenGapParams ogp);
214 
219  void initColorScheme();
220 
222  void setAwareness(double value);
223 
225  void setState(ToCState state);
226 
227  // @brief Sets the device holder's color corresponding to the current state
228  void setVehicleColor();
229 
239  void requestToC(SUMOTime timeTillMRM, SUMOTime responseTime = -1000);
240 
243  void requestMRM();
244 
246  void switchHolderType(const std::string& targetTypeID);
247 
249  void descheduleMRM();
251  void descheduleToC();
255  void descheduleRecovery();
256 
258  void resetDeliberateLCs();
261 
263  bool isManuallyDriven();
265  bool isAutomated();
266 
270  bool checkDynamicToC();
271 
272 private:
275 
277  std::string myManualTypeID;
279  std::string myAutomatedTypeID;
280 
281 
290 
292  double myMRMDecel;
293 
296 
298  std::map<ToCState, RGBColor> myColorScheme;
299 
302 
305 
307 
310 
320 
323 
325  std::queue<std::pair<SUMOTime, std::string> > myEvents;
326 
329 
331  static int LCModeMRM;
332 
335 
340  // (Note that these MRMs will not induce full stops in most cases)
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 
372  static std::mt19937 myResponseTimeRNG;
373 
376  double sampleResponseTime(double leadTime) const;
377 
380  static double interpolateVariance(double leadTime, double pMRM);
381 
382 private:
384  MSDevice_ToC(const MSDevice_ToC&);
385 
388 
389 
390 };
391 
392 
393 #endif
394 
395 /****************************************************************************/
396 
int myPreviousLCMode
LC mode overridden during MRM, stored for restoration.
Definition: MSDevice_ToC.h:328
static int LCModeMRM
LC mode operational during an MRM.
Definition: MSDevice_ToC.h:331
double myOriginalMaxAccel
Storage for original maximal acceleration of vehicle.
Definition: MSDevice_ToC.h:357
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 ...
MSDevice_ToC(SUMOVehicle &holder, const std::string &id, const std::string &outputFilename, std::string manualType, std::string automatedType, SUMOTime responseTime, double recoveryRate, double lcAbstinence, double initialAwareness, double mrmDecel, double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel, bool mrmKeepRight, bool useColorScheme, OpenGapParams ogp)
Constructor.
static bool getMRMKeepRight(const SUMOVehicle &v, const OptionsCont &oc)
static std::set< MSDevice_ToC *, ComparatorNumericalIdLess > myInstances
Definition: MSDevice_ToC.h:58
static double getMaxPreparationAccel(const SUMOVehicle &v, const OptionsCont &oc)
static std::string getManualType(const SUMOVehicle &v, const OptionsCont &oc)
bool myIssuedDynamicToC
Flag to indicate that a dynamically triggered ToC is in preparation.
Definition: MSDevice_ToC.h:345
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:35
void descheduleRecovery()
Remove ongoing awareness recovery process from the event-queue.
ToCState myState
Current state of the device.
Definition: MSDevice_ToC.h:304
std::queue< std::pair< SUMOTime, std::string > > myEvents
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:325
bool isAutomated()
Whether the current operation mode is automated.
static double responseTimeMean(double leadTime)
Mean of the response time distribution. (Only depends on given lead time)
Definition: MSDevice_ToC.h:363
bool checkDynamicToC()
Check if the vehicle should induce a ToC due to internal reasons. That is, if the route cannot be fol...
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
static double getLCAbstinence(const SUMOVehicle &v, const OptionsCont &oc)
static double getRecoveryRate(const SUMOVehicle &v, const OptionsCont &oc)
SUMOTime MRMExecutionStep(SUMOTime t)
Continue the MRM for one time step.
bool isManuallyDriven()
Whether the current operation mode is manual.
void descheduleToCPreparation()
Remove ongoing ToC-Preparation process from the event-queue.
static ToCState _2ToCState(const std::string &)
void descheduleMRM()
Break MRM Process or remove MRM-Trigger command from the event-queue.
SUMOTime triggerUpwardToC(SUMOTime t)
Trigger execution of a ToC X–>AUTOMATED ("upwards")
static OpenGapParams getOpenGapParams(const SUMOVehicle &v, const OptionsCont &oc)
MSDevice_ToC & operator=(const MSDevice_ToC &)
Invalidated assignment operator.
WrappingCommand< MSDevice_ToC > * myExecuteMRMCommand
Definition: MSDevice_ToC.h:317
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...
static double getResponseTime(const SUMOVehicle &v, const OptionsCont &oc)
static std::set< std::string > createdOutputFiles
Definition: MSDevice_ToC.h:60
void initColorScheme()
Initialize vehicle colors for different states.
static std::string _2string(ToCState state)
SUMOTime triggerDownwardToC(SUMOTime t)
Trigger execution of a ToC X–>MANUAL ("downwards")
static bool useColorScheme(const SUMOVehicle &v, const OptionsCont &oc)
std::string myAutomatedTypeID
vehicle type ID for automated driving
Definition: MSDevice_ToC.h:279
const std::string deviceName() const override
return the name for this type of device
Definition: MSDevice_ToC.h:147
void requestMRM()
Request an MRM to be initiated immediately. No downward ToC will be scheduled.
void deactivateDeliberateLCs()
Resets the holder&#39;s LC mode to the operational LC-mode of the ToC Device (.
bool myDynamicToCActive
Switch for considering dynamic ToCs,.
Definition: MSDevice_ToC.h:343
static std::mt19937 myResponseTimeRNG
Random generator for ToC devices.
Definition: MSDevice_ToC.h:372
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static std::vector< double > lookupResponseTimeMRMProbs
Grid of the response time distribution.
Definition: MSDevice_ToC.h:360
WrappingCommand< MSDevice_ToC > * myTriggerMRMCommand
Definition: MSDevice_ToC.h:314
static void cleanup()
Closes root tags of output files.
static std::string getOutputFilename(const SUMOVehicle &v, const OptionsCont &oc)
void setAwareness(double value)
Set the awareness to the given value.
static std::mt19937 * getResponseTimeRNG()
Definition: MSDevice_ToC.h:190
bool myUseColorScheme
Whether a coloring scheme shall by applied to indicate the different toc stages,. ...
Definition: MSDevice_ToC.h:301
Representation of a vehicle.
Definition: SUMOVehicle.h:61
OutputDevice * myOutputFile
The file the devices output goes to.
Definition: MSDevice_ToC.h:322
double myLCAbstinence
Level of the awareness below which no lane-changes are performed.
Definition: MSDevice_ToC.h:287
ToCState
Enum describing the different regimes for the device,.
Definition: MSDevice_ToC.h:111
bool myMRMKeepRight
Whether vehicle tries to change to the right during an MRM.
Definition: MSDevice_ToC.h:350
MSVehicle * myHolderMS
The holder vehicle casted to MSVehicle*.
Definition: MSDevice_ToC.h:309
T MIN2(T a, T b)
Definition: StdDefs.h:74
double myMRMProbability
Probability of an MRM to occur after a dynamically triggered ToC.
Definition: MSDevice_ToC.h:341
SUMOTime myResponseTime
Average response time needed by the driver to take back control.
Definition: MSDevice_ToC.h:283
static double getMRMDecel(const SUMOVehicle &v, const OptionsCont &oc)
std::string myManualTypeID
vehicle type ID for manual driving
Definition: MSDevice_ToC.h:277
double sampleResponseTime(double leadTime) const
Samples a random driver response time from a truncated Gaussian with parameters according to the look...
double myCurrentAwareness
Current awareness-level of the driver in [0,1].
Definition: MSDevice_ToC.h:295
static double getInitialAwareness(const SUMOVehicle &v, const OptionsCont &oc)
SUMOTime ToCPreparationStep(SUMOTime t)
Continue the ToC preparation for one time step.
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 ...
Representation of a vehicle or person.
OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active)
Definition: MSDevice_ToC.h:68
void setVehicleColor()
double myMaxPreparationAccel
Maximal acceleration that may be applied during the ToC preparation phase TODO: Make effective...
Definition: MSDevice_ToC.h:354
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:347
A storage for options typed value containers)
Definition: OptionsCont.h:90
Abstract in-vehicle device.
void setState(ToCState state)
Set the ToC device&#39;s state.
void descheduleToC()
Remove scheduled ToC-Trigger command from the event-queue.
double myRecoveryRate
Recovery rate for the driver&#39;s awareness after a ToC.
Definition: MSDevice_ToC.h:285
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
std::string getParameter(const std::string &key) const override
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
static const std::set< MSDevice_ToC *, ComparatorNumericalIdLess > & getInstances()
returns all currently existing ToC devices
Definition: MSDevice_ToC.h:94
WrappingCommand< MSDevice_ToC > * myRecoverAwarenessCommand
Definition: MSDevice_ToC.h:316
SUMOTime awarenessRecoveryStep(SUMOTime t)
Continue the awareness recovery for one time step.
~MSDevice_ToC()
Destructor.
void resetDeliberateLCs()
Resets the holder&#39;s LC mode to the last differing to LCModeMRM.
WrappingCommand< MSDevice_ToC > * myTriggerToCCommand
Definition: MSDevice_ToC.h:315
static double getDynamicMRMProbability(const SUMOVehicle &v, const OptionsCont &oc)
static double getDynamicToCThreshold(const SUMOVehicle &v, const OptionsCont &oc)
static std::vector< double > lookupResponseTimeLeadTimes
Definition: MSDevice_ToC.h:361
void switchHolderType(const std::string &targetTypeID)
Switch the device holder&#39;s vehicle type.
static double interpolateVariance(double leadTime, double pMRM)
Two-dimensional interpolation of variance from lookup table assumes pMRM >= 0, leadTime >= 0...
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
OpenGapParams myOpenGapParams
Parameters for the openGap mechanism applied during ToC preparation phase.
Definition: MSDevice_ToC.h:334
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:338
SUMOTime triggerMRM(SUMOTime t)
Trigger execution of an MRM.
WrappingCommand< MSDevice_ToC > * myPrepareToCCommand
Definition: MSDevice_ToC.h:318
std::map< ToCState, RGBColor > myColorScheme
Coloring scheme,.
Definition: MSDevice_ToC.h:298
bool generatesOutput()
Whether this device requested to write output.
Definition: MSDevice_ToC.h:186
The ToC Device controls transition of control between automated and manual driving.
Definition: MSDevice_ToC.h:55
void writeOutput()
Write output to file given by option device.toc.file.
double myMRMDecel
Deceleration rate applied during MRM.
Definition: MSDevice_ToC.h:292
double myInitialAwareness
Average awareness the driver has initially after a ToC.
Definition: MSDevice_ToC.h:289
static std::string getAutomatedType(const SUMOVehicle &v, const OptionsCont &oc)