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 /****************************************************************************/
16 // The ToC Device controls the transition of control between automated and manual driving.
17 //
18 /****************************************************************************/
19 #ifndef MSDevice_ToC_h
20 #define MSDevice_ToC_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <random>
29 #include <queue>
30 #include "MSVehicleDevice.h"
31 #include <utils/common/SUMOTime.h>
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class SUMOVehicle;
39 class MSVehicle;
40 class Command_ToCTrigger;
41 class Command_ToCProcess;
42 class RGBColor;
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
54 class MSDevice_ToC : public MSVehicleDevice {
55 private:
56  // All currently existing ToC device instances
57  static std::set<MSDevice_ToC*, ComparatorNumericalIdLess> myInstances;
58  // All files, that receive ToC output (TODO: check if required)
59  static std::set<std::string> createdOutputFiles;
60 
61  struct OpenGapParams {
64  double changeRate;
65  double maxDecel;
66  bool active;
67  OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active) :
69  {};
70  };
71 
72 public:
76  static void insertOptions(OptionsCont& oc);
77 
78 
89  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
90 
93  static const std::set<MSDevice_ToC*, ComparatorNumericalIdLess>& getInstances() {
94  return myInstances;
95  };
96 
99  static void cleanup();
100 
101 private:
102 
110  enum ToCState {
112  MANUAL = 1,
114  PREPARING_TOC = 3, // this applies only to the transition AUTOMATED -> MANUAL !
115  MRM = 4,
117  };
118 
121  static double getDynamicMRMProbability(const SUMOVehicle& v, const OptionsCont& oc);
122  static std::string getOutputFilename(const SUMOVehicle& v, const OptionsCont& oc);
123  static OpenGapParams getOpenGapParams(const SUMOVehicle& v, const OptionsCont& oc);
124 
125  static ToCState _2ToCState(const std::string&);
126  static std::string _2string(ToCState state);
128 
129 
130 public:
132  ~MSDevice_ToC();
133 
135  const std::string deviceName() const override {
136  return "toc";
137  }
138 
140  bool notifyMove(SUMOTrafficObject& veh,
141  double oldPos,
142  double newPos,
143  double newSpeed) override;
144 
146  std::string getParameter(const std::string& key) const override;
147 
149  void setParameter(const std::string& key, const std::string& value) override;
150 
151 
154 
157 
160 
163 
166 
169 
171  void writeOutput();
172 
175  return myOutputFile != nullptr;
176  }
177 
178  static std::mt19937* getResponseTimeRNG() {
179  return &myResponseTimeRNG;
180  }
181 private:
199  MSDevice_ToC(SUMOVehicle& holder, const std::string& id, const std::string& outputFilename,
200  const std::string& manualType, const std::string& automatedType, SUMOTime responseTime, double recoveryRate,
201  double lcAbstinence, double initialAwareness, double mrmDecel,
202  double dynamicToCThreshold, double dynamicMRMProbability, double maxPreparationAccel,
203  bool mrmKeepRight, const std::string& mrmSafeSpot, SUMOTime mrmSafeSpotDuration, bool useColorScheme, OpenGapParams ogp);
204 
209  void initColorScheme();
210 
212  void setAwareness(double value);
213 
215  void setState(ToCState state);
216 
217  // @brief Sets the device holder's color corresponding to the current state
218  void setVehicleColor();
219 
229  void requestToC(SUMOTime timeTillMRM, SUMOTime responseTime = -1000);
230 
233  void requestMRM();
234 
236  void switchHolderType(const std::string& targetTypeID);
237 
239  void descheduleMRM();
241  void descheduleToC();
245  void descheduleRecovery();
246 
248  void resetDeliberateLCs();
251 
253  bool isManuallyDriven();
255  bool isAutomated();
256 
260  bool checkDynamicToC();
261 
262 private:
265 
267  std::string myManualTypeID;
269  std::string myAutomatedTypeID;
270 
271 
280 
282  double myMRMDecel;
283 
286 
288  std::map<ToCState, RGBColor> myColorScheme;
289 
292 
295 
297 
300 
310 
313 
315  std::queue<std::pair<SUMOTime, std::string> > myEvents;
316 
318  std::queue<std::pair<std::string, double> > myEventLanes;
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 
371  static std::mt19937 myResponseTimeRNG;
372 
375  double sampleResponseTime(double leadTime) const;
376 
379  static double interpolateVariance(double leadTime, double pMRM);
380 
381 private:
383  MSDevice_ToC(const MSDevice_ToC&);
384 
387 
388 
389 };
390 
391 
392 #endif
393 
394 /****************************************************************************/
395 
MSDevice_ToC::OpenGapParams::newTimeHeadway
double newTimeHeadway
Definition: MSDevice_ToC.h:62
MSDevice_ToC::myUseColorScheme
bool myUseColorScheme
Whether a coloring scheme shall by applied to indicate the different toc stages,.
Definition: MSDevice_ToC.h:291
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
MSDevice_ToC::myEvents
std::queue< std::pair< SUMOTime, std::string > > myEvents
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:315
MSDevice_ToC::myManualTypeID
std::string myManualTypeID
vehicle type ID for manual driving
Definition: MSDevice_ToC.h:267
MSDevice_ToC::myMRMSafeSpot
std::string myMRMSafeSpot
stop vehicle tries to reach during MRM
Definition: MSDevice_ToC.h:346
MSDevice_ToC::isAutomated
bool isAutomated()
Whether the current operation mode is automated.
Definition: MSDevice_ToC.cpp:1072
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
MSDevice_ToC::ToCState
ToCState
Enum describing the different regimes for the device,.
Definition: MSDevice_ToC.h:110
SUMOTime.h
MSDevice_ToC::myPreviousLCMode
int myPreviousLCMode
LC mode overridden during MRM, stored for restoration.
Definition: MSDevice_ToC.h:321
MSDevice_ToC::descheduleMRM
void descheduleMRM()
Break MRM Process or remove MRM-Trigger command from the event-queue.
Definition: MSDevice_ToC.cpp:665
MSDevice_ToC::triggerUpwardToC
SUMOTime triggerUpwardToC(SUMOTime t)
Trigger execution of a ToC X-->AUTOMATED ("upwards")
Definition: MSDevice_ToC.cpp:602
MSDevice_ToC::OpenGapParams::maxDecel
double maxDecel
Definition: MSDevice_ToC.h:65
MSDevice_ToC::OpenGapParams::OpenGapParams
OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active)
Definition: MSDevice_ToC.h:67
MSDevice_ToC::OpenGapParams
Definition: MSDevice_ToC.h:61
MSDevice_ToC::generatesOutput
bool generatesOutput()
Whether this device requested to write output.
Definition: MSDevice_ToC.h:174
MSVehicleDevice.h
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSDevice_ToC::myHolderMS
MSVehicle * myHolderMS
The holder vehicle casted to MSVehicle*.
Definition: MSDevice_ToC.h:299
MSDevice_ToC::sampleResponseTime
double sampleResponseTime(double leadTime) const
Samples a random driver response time from a truncated Gaussian with parameters according to the look...
Definition: MSDevice_ToC.cpp:1165
MSDevice_ToC::MRM
@ MRM
Definition: MSDevice_ToC.h:115
MSDevice_ToC::myExecuteMRMCommand
WrappingCommand< MSDevice_ToC > * myExecuteMRMCommand
Definition: MSDevice_ToC.h:307
MSDevice_ToC::myMRMSafeSpotDuration
SUMOTime myMRMSafeSpotDuration
duration at stop vehicle tries to reach during MRM
Definition: MSDevice_ToC.h:349
MSDevice_ToC::triggerDownwardToC
SUMOTime triggerDownwardToC(SUMOTime t)
Trigger execution of a ToC X-->MANUAL ("downwards")
Definition: MSDevice_ToC.cpp:631
MSDevice_ToC::requestToC
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 ...
Definition: MSDevice_ToC.cpp:503
WrappingCommand.h
MSDevice_ToC::myCurrentAwareness
double myCurrentAwareness
Current awareness-level of the driver in [0,1].
Definition: MSDevice_ToC.h:285
MSDevice_ToC::myEventLanes
std::queue< std::pair< std::string, double > > myEventLanes
Storage for events to be written to the output.
Definition: MSDevice_ToC.h:318
MSDevice_ToC::descheduleRecovery
void descheduleRecovery()
Remove ongoing awareness recovery process from the event-queue.
Definition: MSDevice_ToC.cpp:701
MSDevice_ToC::deviceName
const std::string deviceName() const override
return the name for this type of device
Definition: MSDevice_ToC.h:135
MSDevice_ToC::myMaxPreparationAccel
double myMaxPreparationAccel
Maximal acceleration that may be applied during the ToC preparation phase TODO: Make effective.
Definition: MSDevice_ToC.h:353
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
MSDevice_ToC::responseTimeMean
static double responseTimeMean(double leadTime)
Mean of the response time distribution. (Only depends on given lead time)
Definition: MSDevice_ToC.h:362
WrappingCommand< MSDevice_ToC >
MSDevice_ToC::myResponseTimeRNG
static std::mt19937 myResponseTimeRNG
Random generator for ToC devices.
Definition: MSDevice_ToC.h:371
MSDevice_ToC::MSDevice_ToC
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.
Definition: MSDevice_ToC.cpp:265
MSDevice_ToC::_2ToCState
static ToCState _2ToCState(const std::string &)
Definition: MSDevice_ToC.cpp:970
MSDevice_ToC::setVehicleColor
void setVehicleColor()
Definition: MSDevice_ToC.cpp:483
MSDevice_ToC::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_ToC.cpp:154
MSDevice_ToC::getDynamicMRMProbability
static double getDynamicMRMProbability(const SUMOVehicle &v, const OptionsCont &oc)
Definition: MSDevice_ToC.cpp:209
MSDevice_ToC::UNDEFINED
@ UNDEFINED
Definition: MSDevice_ToC.h:111
MSDevice_ToC::setState
void setState(ToCState state)
Set the ToC device's state.
Definition: MSDevice_ToC.cpp:436
MSDevice_ToC::initColorScheme
void initColorScheme()
Initialize vehicle colors for different states.
Definition: MSDevice_ToC.cpp:382
MSDevice_ToC::resetDeliberateLCs
void resetDeliberateLCs()
Resets the holder's LC mode to the last differing to LCModeMRM.
Definition: MSDevice_ToC.cpp:1042
MSDevice_ToC::OpenGapParams::active
bool active
Definition: MSDevice_ToC.h:66
MSDevice_ToC
The ToC Device controls transition of control between automated and manual driving.
Definition: MSDevice_ToC.h:54
MSDevice_ToC::~MSDevice_ToC
~MSDevice_ToC()
Destructor.
Definition: MSDevice_ToC.cpp:393
RGBColor
Definition: RGBColor.h:39
MSDevice_ToC::lookupResponseTimeLeadTimes
static std::vector< double > lookupResponseTimeLeadTimes
Definition: MSDevice_ToC.h:360
MSDevice_ToC::switchHolderType
void switchHolderType(const std::string &targetTypeID)
Switch the device holder's vehicle type.
Definition: MSDevice_ToC.cpp:711
MSDevice_ToC::myDynamicToCThreshold
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
MSDevice_ToC::lookupResponseTimeMRMProbs
static std::vector< double > lookupResponseTimeMRMProbs
Grid of the response time distribution.
Definition: MSDevice_ToC.h:359
MSDevice_ToC::RECOVERING
@ RECOVERING
Definition: MSDevice_ToC.h:116
MSDevice_ToC::triggerMRM
SUMOTime triggerMRM(SUMOTime t)
Trigger execution of an MRM.
Definition: MSDevice_ToC.cpp:555
MSDevice_ToC::myMRMProbability
double myMRMProbability
Probability of an MRM to occur after a dynamically triggered ToC.
Definition: MSDevice_ToC.h:334
MSDevice_ToC::myInitialAwareness
double myInitialAwareness
Average awareness the driver has initially after a ToC.
Definition: MSDevice_ToC.h:279
MSDevice_ToC::myColorScheme
std::map< ToCState, RGBColor > myColorScheme
Coloring scheme,.
Definition: MSDevice_ToC.h:288
MSDevice_ToC::interpolateVariance
static double interpolateVariance(double leadTime, double pMRM)
Two-dimensional interpolation of variance from lookup table assumes pMRM >= 0, leadTime >= 0.
Definition: MSDevice_ToC.cpp:1189
MSDevice_ToC::lookupResponseTimeVariances
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
MSDevice_ToC::myState
ToCState myState
Current state of the device.
Definition: MSDevice_ToC.h:294
MSDevice_ToC::AUTOMATED
@ AUTOMATED
Definition: MSDevice_ToC.h:113
MSDevice_ToC::setAwareness
void setAwareness(double value)
Set the awareness to the given value.
Definition: MSDevice_ToC.cpp:416
MSDevice_ToC::myOutputFile
OutputDevice * myOutputFile
The file the devices output goes to.
Definition: MSDevice_ToC.h:312
MSDevice_ToC::myOriginalMaxAccel
double myOriginalMaxAccel
Storage for original maximal acceleration of vehicle.
Definition: MSDevice_ToC.h:356
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice_ToC::ToCPreparationStep
SUMOTime ToCPreparationStep(SUMOTime t)
Continue the ToC preparation for one time step.
Definition: MSDevice_ToC.cpp:725
MSDevice_ToC::MRMExecutionStep
SUMOTime MRMExecutionStep(SUMOTime t)
Continue the MRM for one time step.
Definition: MSDevice_ToC.cpp:742
MSDevice_ToC::myMRMKeepRight
bool myMRMKeepRight
Whether vehicle tries to change to the right during an MRM.
Definition: MSDevice_ToC.h:343
MSDevice_ToC::descheduleToCPreparation
void descheduleToCPreparation()
Remove ongoing ToC-Preparation process from the event-queue.
Definition: MSDevice_ToC.cpp:689
MSDevice_ToC::myInstances
static std::set< MSDevice_ToC *, ComparatorNumericalIdLess > myInstances
Definition: MSDevice_ToC.h:57
MSDevice_ToC::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
Definition: MSDevice_ToC.cpp:108
MSDevice_ToC::operator=
MSDevice_ToC & operator=(const MSDevice_ToC &)
Invalidated assignment operator.
MSDevice_ToC::descheduleToC
void descheduleToC()
Remove scheduled ToC-Trigger command from the event-queue.
Definition: MSDevice_ToC.cpp:681
MSDevice_ToC::notifyMove
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.
Definition: MSDevice_ToC.cpp:805
MSDevice_ToC::isManuallyDriven
bool isManuallyDriven()
Whether the current operation mode is manual.
Definition: MSDevice_ToC.cpp:1067
MSDevice_ToC::_2string
static std::string _2string(ToCState state)
Definition: MSDevice_ToC.cpp:991
MSDevice_ToC::checkDynamicToC
bool checkDynamicToC()
Check if the vehicle should induce a ToC due to internal reasons. That is, if the route cannot be fol...
Definition: MSDevice_ToC.cpp:1077
MSDevice_ToC::getOpenGapParams
static OpenGapParams getOpenGapParams(const SUMOVehicle &v, const OptionsCont &oc)
Definition: MSDevice_ToC.cpp:221
MSDevice_ToC::MANUAL
@ MANUAL
Definition: MSDevice_ToC.h:112
MSDevice_ToC::getParameter
std::string getParameter(const std::string &key) const override
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition: MSDevice_ToC.cpp:837
MSDevice_ToC::requestMRM
void requestMRM()
Request an MRM to be initiated immediately. No downward ToC will be scheduled.
Definition: MSDevice_ToC.cpp:490
MSDevice_ToC::deactivateDeliberateLCs
void deactivateDeliberateLCs()
Resets the holder's LC mode to the operational LC-mode of the ToC Device (.
Definition: MSDevice_ToC.cpp:1054
config.h
MSDevice_ToC::createdOutputFiles
static std::set< std::string > createdOutputFiles
Definition: MSDevice_ToC.h:59
MSDevice_ToC::myDynamicToCLane
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
MSDevice_ToC::getOutputFilename
static std::string getOutputFilename(const SUMOVehicle &v, const OptionsCont &oc)
Definition: MSDevice_ToC.cpp:186
MSDevice_ToC::myPrepareToCCommand
WrappingCommand< MSDevice_ToC > * myPrepareToCCommand
Definition: MSDevice_ToC.h:308
MSDevice_ToC::myTriggerMRMCommand
WrappingCommand< MSDevice_ToC > * myTriggerMRMCommand
Definition: MSDevice_ToC.h:304
MSDevice_ToC::PREPARING_TOC
@ PREPARING_TOC
Definition: MSDevice_ToC.h:114
MSDevice_ToC::OpenGapParams::changeRate
double changeRate
Definition: MSDevice_ToC.h:64
MSDevice_ToC::myOpenGapParams
OpenGapParams myOpenGapParams
Parameters for the openGap mechanism applied during ToC preparation phase.
Definition: MSDevice_ToC.h:327
MSDevice_ToC::myMRMDecel
double myMRMDecel
Deceleration rate applied during MRM.
Definition: MSDevice_ToC.h:282
MSDevice_ToC::myAutomatedTypeID
std::string myAutomatedTypeID
vehicle type ID for automated driving
Definition: MSDevice_ToC.h:269
MSDevice_ToC::myRecoveryRate
double myRecoveryRate
Recovery rate for the driver's awareness after a ToC.
Definition: MSDevice_ToC.h:275
MSDevice_ToC::setParameter
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
Definition: MSDevice_ToC.cpp:886
MSDevice_ToC::myDynamicToCActive
bool myDynamicToCActive
Switch for considering dynamic ToCs,.
Definition: MSDevice_ToC.h:336
MSDevice_ToC::myRecoverAwarenessCommand
WrappingCommand< MSDevice_ToC > * myRecoverAwarenessCommand
Definition: MSDevice_ToC.h:306
MSDevice_ToC::writeOutput
void writeOutput()
Write output to file given by option device.toc.file.
Definition: MSDevice_ToC.cpp:1012
MSDevice_ToC::getResponseTimeRNG
static std::mt19937 * getResponseTimeRNG()
Definition: MSDevice_ToC.h:178
MSDevice_ToC::myTriggerToCCommand
WrappingCommand< MSDevice_ToC > * myTriggerToCCommand
Definition: MSDevice_ToC.h:305
MSDevice_ToC::OpenGapParams::newSpaceHeadway
double newSpaceHeadway
Definition: MSDevice_ToC.h:63
MSDevice_ToC::LCModeMRM
static int LCModeMRM
LC mode operational during an MRM.
Definition: MSDevice_ToC.h:324
MSDevice_ToC::getInstances
static const std::set< MSDevice_ToC *, ComparatorNumericalIdLess > & getInstances()
returns all currently existing ToC devices
Definition: MSDevice_ToC.h:93
MSDevice_ToC::cleanup
static void cleanup()
Closes root tags of output files.
Definition: MSDevice_ToC.cpp:1031
MSDevice_ToC::awarenessRecoveryStep
SUMOTime awarenessRecoveryStep(SUMOTime t)
Continue the awareness recovery for one time step.
Definition: MSDevice_ToC.cpp:778
MSDevice_ToC::myIssuedDynamicToC
bool myIssuedDynamicToC
Flag to indicate that a dynamically triggered ToC is in preparation.
Definition: MSDevice_ToC.h:338
MSVehicle
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
MSDevice_ToC::myResponseTime
SUMOTime myResponseTime
Average response time needed by the driver to take back control.
Definition: MSDevice_ToC.h:273
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:54
MSDevice_ToC::myLCAbstinence
double myLCAbstinence
Level of the awareness below which no lane-changes are performed.
Definition: MSDevice_ToC.h:277