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 /****************************************************************************/ 10 /// @file MSDevice_ToC.h 11 /// @author Leonhard Luecken 12 /// @author Daniel Krajzewicz 13 /// @author Jakob Erdmann 14 /// @date 01.04.2018 15 /// @version $Id$ 16 /// 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 "MSVehicleDevice.h" 30 #include <utils/common/SUMOTime.h> 31 #include <utils/common/WrappingCommand.h> 32 33 34 // =========================================================================== 35 // class declarations 36 // =========================================================================== 37 class SUMOVehicle; 38 class MSVehicle; 39 class Command_ToCTrigger; 40 class Command_ToCProcess; 41 class RGBColor; 42 43 // =========================================================================== 44 // class definitions 45 // =========================================================================== 46 /** 47 * @class MSDevice_ToC 48 * 49 * @brief The ToC Device controls transition of control between automated and manual driving. 50 * 51 * @see MSDevice 52 */ 53 class MSDevice_ToC : public MSVehicleDevice { 54 private: 55 // All currently existing ToC device instances 56 static std::set<MSDevice_ToC*> instances; 57 // All files, that receive ToC output (TODO: check if required) 58 static std::set<std::string> createdOutputFiles; 59 60 struct OpenGapParams { 61 double newTimeHeadway; 62 double newSpaceHeadway; 63 double changeRate; 64 double maxDecel; 65 bool active; OpenGapParamsOpenGapParams66 OpenGapParams(double timegap, double spacing, double changeRate, double maxDecel, bool active) : 67 newTimeHeadway(timegap), newSpaceHeadway(spacing), changeRate(changeRate), maxDecel(maxDecel), active(active) 68 {}; 69 }; 70 71 public: 72 /** @brief Inserts MSDevice_ToC-options 73 * @param[filled] oc The options container to add the options to 74 */ 75 static void insertOptions(OptionsCont& oc); 76 77 78 /** @brief Build devices for the given vehicle, if needed 79 * 80 * The options are read and evaluated whether a ToC-device shall be built 81 * for the given vehicle. 82 * 83 * The built device is stored in the given vector. 84 * 85 * @param[in] v The vehicle for which a device may be built 86 * @param[filled] into The vector to store the built device in 87 */ 88 static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into); 89 90 /** @brief returns all currently existing ToC devices 91 */ getInstances()92 static const std::set<MSDevice_ToC*>& getInstances() { 93 return instances; 94 }; 95 96 /** @brief Closes root tags of output files 97 */ 98 static void cleanup(); 99 100 private: 101 102 /// @brief Enum describing the different regimes for the device, @see myState 103 /// Possible transitions: 104 /// AUTOMATED -> PREPARING_TOC 105 /// PREPARING_TOC -> PERFORMING_MRM 106 /// PREPARING_TOC -> MANUAL 107 /// PERFORMING_MRM -> MANUAL 108 /// MANUAL -> AUTOMATED 109 enum ToCState { 110 UNDEFINED = 0, 111 MANUAL = 1, 112 AUTOMATED = 2, 113 PREPARING_TOC = 3, // this applies only to the transition AUTOMATED -> MANUAL ! 114 MRM = 4, 115 RECOVERING = 5 116 }; 117 118 /// @name Helpers for parameter parsing 119 /// @{ 120 static std::string getManualType(const SUMOVehicle& v, const OptionsCont& oc); 121 static std::string getAutomatedType(const SUMOVehicle& v, const OptionsCont& oc); 122 static double getResponseTime(const SUMOVehicle& v, const OptionsCont& oc); 123 static double getRecoveryRate(const SUMOVehicle& v, const OptionsCont& oc); 124 static double getLCAbstinence(const SUMOVehicle& v, const OptionsCont& oc); 125 static double getInitialAwareness(const SUMOVehicle& v, const OptionsCont& oc); 126 static double getMRMDecel(const SUMOVehicle& v, const OptionsCont& oc); 127 static bool useColorScheme(const SUMOVehicle& v, const OptionsCont& oc); 128 static std::string getOutputFilename(const SUMOVehicle& v, const OptionsCont& oc); 129 static OpenGapParams getOpenGapParams(const SUMOVehicle& v, const OptionsCont& oc); 130 131 static ToCState _2ToCState(const std::string&); 132 static std::string _2string(ToCState state); 133 /// @} 134 135 136 public: 137 /// @brief Destructor. 138 ~MSDevice_ToC(); 139 140 /// @brief return the name for this type of device deviceName()141 const std::string deviceName() const { 142 return "toc"; 143 } 144 145 /// @brief try to retrieve the given parameter from this device. Throw exception for unsupported key 146 std::string getParameter(const std::string& key) const; 147 148 /// @brief try to set the given parameter for this device. Throw exception for unsupported key 149 void setParameter(const std::string& key, const std::string& value); 150 151 152 /// @brief Trigger execution of an MRM 153 SUMOTime triggerMRM(SUMOTime t); 154 155 /// @brief Trigger execution of a ToC X-->AUTOMATED ("upwards") 156 SUMOTime triggerUpwardToC(SUMOTime t); 157 158 /// @brief Trigger execution of a ToC X-->MANUAL ("downwards") 159 SUMOTime triggerDownwardToC(SUMOTime t); 160 161 /// @brief Continue the ToC preparation for one time step 162 SUMOTime ToCPreparationStep(SUMOTime t); 163 164 /// @brief Continue the MRM for one time step 165 SUMOTime MRMExecutionStep(SUMOTime t); 166 167 /// @brief Continue the awareness recovery for one time step 168 SUMOTime awarenessRecoveryStep(SUMOTime t); 169 170 /// @brief Write output to file given by option device.toc.file 171 void writeOutput(); 172 173 /// @brief Whether this device requested to write output generatesOutput()174 bool generatesOutput() { 175 return myOutputFile != nullptr; 176 } 177 178 private: 179 /** @brief Constructor 180 * 181 * @param[in] holder The vehicle that holds this device 182 * @param[in] id The ID of the device 183 * @param[in] file The file to write the device's output to 184 * @param[in] manualType vType that models manual driving 185 * @param[in] automatedType vType that models automated driving 186 * @param[in] responseTime time lapse until vType switch after request was received 187 * @param[in] recoveryRate rate at which the awareness increases after the takeover 188 * @param[in] lcAbstinence awareness level below which no lane changes are taken out 189 * @param[in] initialAwareness value to which the awareness is set after takeover 190 * @param[in] mrmDecel constant deceleration rate assumed to be applied during an MRM 191 * @param[in] useColorScheme whether the color of the vehicle should be changed according to its current ToC-state 192 * @param[in] ogp parameters for the openGap mechanism applied during ToC preparation phase 193 */ 194 MSDevice_ToC(SUMOVehicle& holder, const std::string& id, const std::string& outputFilename, 195 std::string manualType, std::string automatedType, SUMOTime responseTime, double recoveryRate, 196 double lcAbstinence, double initialAwareness, double mrmDecel, bool useColorScheme, OpenGapParams ogp); 197 198 /** @brief Initialize vehicle colors for different states 199 * @note For MANUAL and AUTOMATED, the color of the given types are used, 200 * and for the other states hardcoded colors are given. 201 */ 202 void initColorScheme(); 203 204 /// @brief Set the awareness to the given value 205 void setAwareness(double value); 206 207 /// @brief Set the ToC device's state 208 void setState(ToCState state); 209 210 // @brief Sets the device holder's color corresponding to the current state 211 void setVehicleColor(); 212 213 /// @brief Request a ToC. 214 /// If the device is in AUTOMATED or MRM state, a driver response time is sampled 215 /// and the ToC is scheduled. If the response is larger than timeTillMRM, 216 /// an MRM is scheduled as well. 217 /// If the device is in MANUAL or UNDEFINED state, it switches to AUTOMATED. 218 /// The request is ignored if the state is already PREPARING_TOC. 219 void requestToC(SUMOTime timeTillMRM); 220 221 /// @brief Request an MRM to be initiated immediately. No downward ToC will be scheduled. 222 /// @note The initiated MRM process will run forever until a new ToC is requested. 223 void requestMRM(); 224 225 /// @brief Switch the device holder's vehicle type 226 void switchHolderType(const std::string& targetTypeID); 227 228 /// @brief Break MRM Process or remove MRM-Trigger command from the event-queue. 229 void descheduleMRM(); 230 /// @brief Remove scheduled ToC-Trigger command from the event-queue. 231 void descheduleToC(); 232 /// @brief Remove ongoing ToC-Preparation process from the event-queue. 233 void descheduleToCPreparation(); 234 /// @brief Remove ongoing awareness recovery process from the event-queue. 235 void descheduleRecovery(); 236 237 /// @brief Resets the holder's LC mode to the last differing to LCModeMRM 238 void resetDeliberateLCs(); 239 /// @brief Resets the holder's LC mode to the operational LC-mode of the ToC Device (@see LCModeMRM) 240 void deactivateDeliberateLCs(); 241 242 /// @brief Whether the current operation mode is manual 243 bool isManuallyDriven(); 244 /// @brief Whether the current operation mode is automated 245 bool isAutomated(); 246 247 private: 248 /// @name private state members of the ToC device 249 /// @{ 250 251 /// @brief vehicle type ID for manual driving 252 std::string myManualTypeID; 253 /// @brief vehicle type ID for automated driving 254 std::string myAutomatedTypeID; 255 256 /// @brief Average response time needed by the driver to take back control 257 SUMOTime myResponseTime; 258 /// @brief Recovery rate for the driver's awareness after a ToC 259 double myRecoveryRate; 260 /// @brief Level of the awareness below which no lane-changes are performed 261 double myLCAbstinence; 262 /// @brief Average awareness the driver has initially after a ToC 263 double myInitialAwareness; 264 265 /// @brief Deceleration rate applied during MRM 266 double myMRMDecel; 267 268 /// @brief Current awareness-level of the driver in [0,1] 269 double myCurrentAwareness; 270 271 /// @brief Coloring scheme, @see initColorScheme() 272 std::map<ToCState, RGBColor> myColorScheme; 273 274 /// @brief Whether a coloring scheme shall by applied to indicate the different toc stages, @see initColorScheme() 275 bool myUseColorScheme; 276 277 /// @brief Current state of the device 278 ToCState myState; 279 280 /// @} 281 282 /// @brief The holder vehicle casted to MSVehicle* 283 MSVehicle* myHolderMS; 284 285 /// @name Commands sent to the EventControl (used for cleanup) 286 /// @note Must be removed in destructor. 287 /// @{ 288 WrappingCommand<MSDevice_ToC>* myTriggerMRMCommand; 289 WrappingCommand<MSDevice_ToC>* myTriggerToCCommand; 290 WrappingCommand<MSDevice_ToC>* myRecoverAwarenessCommand; 291 WrappingCommand<MSDevice_ToC>* myExecuteMRMCommand; 292 WrappingCommand<MSDevice_ToC>* myPrepareToCCommand; 293 /// @} 294 295 /// @brief The file the devices output goes to 296 OutputDevice* myOutputFile; 297 298 /// @brief Storage for events to be written to the output 299 std::queue<std::pair<SUMOTime, std::string> > myEvents; 300 301 /// @brief LC mode overridden during MRM, stored for restoration 302 int myPreviousLCMode; 303 304 /// @brief LC mode operational during an MRM 305 static int LCModeMRM; 306 307 /// @brief Parameters for the openGap mechanism applied during ToC preparation phase 308 OpenGapParams myOpenGapParams; 309 310 private: 311 /// @brief Invalidated copy constructor. 312 MSDevice_ToC(const MSDevice_ToC&); 313 314 /// @brief Invalidated assignment operator. 315 MSDevice_ToC& operator=(const MSDevice_ToC&); 316 317 318 }; 319 320 321 #endif 322 323 /****************************************************************************/ 324 325