1 /****************************************************************************/ 2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo 3 // Copyright (C) 2001-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 MEVehicle.h 11 /// @author Daniel Krajzewicz 12 /// @date Tue, May 2005 13 /// @version $Id$ 14 /// 15 // A vehicle from the mesoscopic point of view 16 /****************************************************************************/ 17 #ifndef MEVehicle_h 18 #define MEVehicle_h 19 20 21 // =========================================================================== 22 // included modules 23 // =========================================================================== 24 #include <config.h> 25 26 #include <iostream> 27 #include <cassert> 28 #include <map> 29 #include <vector> 30 #include <microsim/MSBaseVehicle.h> 31 #include <microsim/MSEdge.h> 32 #include <utils/common/StdDefs.h> 33 #include "MESegment.h" 34 35 class MSLane; 36 class MSLink; 37 38 // =========================================================================== 39 // class definitions 40 // =========================================================================== 41 /** 42 * @class MEVehicle 43 * @brief A vehicle from the mesoscopic point of view 44 */ 45 class MEVehicle : public MSBaseVehicle { 46 public: 47 /** @brief Constructor 48 * @param[in] pars The vehicle description 49 * @param[in] route The vehicle's route 50 * @param[in] type The vehicle's type 51 * @param[in] speedFactor The factor for driven lane's speed limits 52 * @exception ProcessError If a value is wrong 53 */ 54 MEVehicle(SUMOVehicleParameter* pars, const MSRoute* route, 55 MSVehicleType* type, const double speedFactor); 56 57 58 /** @brief Get the vehicle's position along the lane 59 * @return The position of the vehicle (in m from the lane's begin) 60 */ 61 double getPositionOnLane() const; 62 63 64 /** @brief Get the vehicle's position relative to the given lane 65 * @return The back position of the vehicle (in m from the given lane's begin) 66 */ 67 double getBackPositionOnLane(const MSLane* lane) const; 68 69 70 /** @brief Returns the vehicle's direction in degrees 71 * @return The vehicle's current angle 72 */ 73 double getAngle() const; 74 75 76 /** @brief Returns the slope of the road at vehicle's position 77 * @return The slope 78 */ 79 double getSlope() const; 80 81 /** @brief Returns the lane the vehicle is on 82 * @return The vehicle's current lane 83 */ getLane()84 MSLane* getLane() const { 85 return 0; 86 } 87 88 /** @brief Return current position (x/y, cartesian) 89 * 90 * If the vehicle's myLane is 0, Position::INVALID. 91 * @param[in] offset optional offset in longitudinal direction 92 * @return The current position (in cartesian coordinates) 93 * @see myLane 94 */ 95 Position getPosition(const double offset = 0) const; 96 97 98 /** @brief Returns the vehicle's estimated speed assuming no delays 99 * @return The vehicle's estimated speed 100 * @note This is only an upper bound. The speed will be lower if the preceeding vehicle is delayed 101 */ 102 double getSpeed() const; 103 104 /** @brief Returns the vehicle's estimated average speed on the segment assuming no further delays 105 * @return The vehicle's estimated average speed 106 * @note This is only an upper bound. The speed will be lower if the preceeding vehicle is delayed 107 */ 108 double getAverageSpeed() const; 109 110 /// @brief Returns the vehicle's estimated speed after driving accross the link 111 double estimateLeaveSpeed(const MSLink* link) const; 112 113 114 /** @brief Returns the vehicle's estimated speed taking into account delays 115 * @return The vehicle's estimated speed 116 * @param[in, out] earliestArrival A lower bound on leaveTime, modified to contain new lower bound on leave Time 117 * @note This is only an upper bound. The speed may be even lower if there are further delays downstream 118 */ 119 double getConservativeSpeed(SUMOTime& earliestArrival) const; 120 121 122 /** @brief Update when the vehicle enters a new edge in the move step. 123 * @return Whether the vehicle's route has ended (due to vaporization, or because the destination was reached) 124 */ 125 bool moveRoutePointer(); 126 127 /** @brief Returns whether this vehicle has already arived 128 * (reached the arrivalPosition on its final edge) 129 */ 130 bool hasArrived() const; 131 132 /** @brief Returns the information whether the vehicle is on a road (is simulated) 133 * @return Whether the vehicle is simulated 134 */ 135 bool isOnRoad() const; 136 137 /** @brief Returns whether the vehicle is parking 138 * @return whether the vehicle is parking 139 */ 140 bool isParking() const; 141 142 /** @brief Adds a stop 143 * 144 * The stop is put into the sorted list. 145 * @param[in] stop The stop to add 146 * @return Whether the stop could be added 147 */ 148 bool addStop(const SUMOVehicleParameter::Stop& stopPar, std::string& errorMsg, SUMOTime untilOffset = 0, bool collision = false, 149 MSRouteIterator* searchStart = 0); 150 151 152 /** @brief Returns whether the vehicle is at a stop 153 * @return Whether it has stopped 154 */ 155 bool isStopped() const; 156 157 /// @brief Returns the remaining stop duration for a stopped vehicle or 0 remainingStopDuration()158 SUMOTime remainingStopDuration() const { 159 return 0; 160 } 161 162 ///@brief ends the current stop and performs loading/unloading 163 void processStop(); 164 165 /** @brief Returns whether the vehicle is on a triggered stop 166 * @return whether the vehicle is on a triggered stop 167 */ 168 bool isStoppedTriggered() const; 169 170 /** @brief return whether the given position is within range of the current stop 171 */ 172 bool isStoppedInRange(double pos) const; 173 174 /** @brief Returns whether the vehicle stops at the given stopping place */ stopsAt(MSStoppingPlace *)175 bool stopsAt(MSStoppingPlace* /*stop*/) const { 176 return false; 177 }; 178 179 /** @brief Returns until when to stop at the given segment 180 * @param[in] seg The segment in question 181 * @param[in] time the current time 182 * @return stop time for the segment 183 */ 184 SUMOTime getStoptime(const MESegment* const seg, SUMOTime time) const; 185 186 187 /** @brief Returns the list of still pending stop edges 188 */ 189 const ConstMSEdgeVector getStopEdges(double& firstPos, double& lastPos) const; 190 191 /// @brief return list of route indices for the remaining stops 192 std::vector<std::pair<int, double> > getStopIndices() const; 193 194 /// @brief get distance for coming to a stop (used for rerouting checks) getBrakeGap()195 double getBrakeGap() const { 196 return 0; 197 } 198 199 /** @brief replace the current parking area stop with a new stop with merge duration 200 */ replaceParkingArea(MSParkingArea *,std::string &)201 bool replaceParkingArea(MSParkingArea* /* parkingArea = 0 */, std::string& /*errorMsg*/) { 202 throw ProcessError("parkingZoneReroute not implemented for meso"); 203 } 204 205 /** @brief get the current parking area stop 206 */ getNextParkingArea()207 MSParkingArea* getNextParkingArea() { 208 throw ProcessError("parkingZoneReroute not implemented for meso"); 209 } 210 211 /** @brief Sets the (planned) time at which the vehicle leaves his current cell 212 * @param[in] t The leaving time 213 */ 214 inline void setEventTime(SUMOTime t, bool hasDelay = true) { 215 assert(t > myLastEntryTime); 216 if (hasDelay && mySegment != 0) { 217 mySegment->getEdge().markDelayed(); 218 } 219 myEventTime = t; 220 } 221 222 223 /** @brief Returns the (planned) time at which the vehicle leaves his current cell 224 * @return The time the vehicle thinks he leaves his cell at 225 */ getEventTime()226 inline SUMOTime getEventTime() const { 227 return myEventTime; 228 } 229 230 231 /** @brief Sets the current segment the vehicle is at together with its que 232 * @param[in] s The current segment 233 * @param[in] q The current que 234 */ 235 inline virtual void setSegment(MESegment* s, int idx = 0) { 236 mySegment = s; 237 myQueIndex = idx; 238 } 239 240 241 /** @brief Returns the current segment the vehicle is on 242 * @return The segment the vehicle is on 243 */ getSegment()244 inline MESegment* getSegment() const { 245 return mySegment; 246 } 247 248 249 /** @brief Returns the index of the que the vehicle is in 250 * @return The que index 251 */ getQueIndex()252 inline int getQueIndex() const { 253 return myQueIndex; 254 } 255 256 257 /** @brief Sets the entry time for the current segment 258 * @param[in] t The entry time 259 */ setLastEntryTime(SUMOTime t)260 inline void setLastEntryTime(SUMOTime t) { 261 myLastEntryTime = t; 262 } 263 264 265 /** @brief Returns the time the vehicle entered the current segment 266 * @return The entry time 267 */ getLastEntryTime()268 SUMOTime getLastEntryTime() const { 269 return myLastEntryTime; 270 } 271 272 273 /** @brief Sets the time at which the vehicle was blocked 274 * @param[in] t The blocking time 275 */ setBlockTime(const SUMOTime t)276 inline void setBlockTime(const SUMOTime t) { 277 assert(t > myLastEntryTime); 278 myBlockTime = t; 279 } 280 281 282 /** @brief Returns the time at which the vehicle was blocked 283 * @return The blocking time 284 */ getBlockTime()285 inline SUMOTime getBlockTime() const { 286 return myBlockTime; 287 } 288 289 290 /// @brief Returns the duration for which the vehicle was blocked getWaitingTime()291 inline SUMOTime getWaitingTime() const { 292 return MAX2(SUMOTime(0), myEventTime - myBlockTime); 293 } 294 295 /// @brief Returns the duration for which the vehicle was blocked getAccumulatedWaitingTime()296 inline SUMOTime getAccumulatedWaitingTime() const { 297 return getWaitingTime(); 298 } 299 300 301 /** @brief Returns the number of seconds waited (speed was lesser than 0.1m/s) 302 * 303 * The value is reset if the vehicle moves faster than 0.1m/s 304 * Intentional stopping does not count towards this time. 305 * @return The time the vehicle is standing 306 */ getWaitingSeconds()307 double getWaitingSeconds() const { 308 return STEPS2TIME(getWaitingTime()); 309 } 310 311 312 /// @brief Returns the earliest leave time for the current segment getEventTimeSeconds()313 double getEventTimeSeconds() const { 314 return STEPS2TIME(getEventTime()); 315 } 316 317 /// @brief Returns the entry time for the current segment getLastEntryTimeSeconds()318 double getLastEntryTimeSeconds() const { 319 return STEPS2TIME(getLastEntryTime()); 320 } 321 322 /// @brief Returns the time at which the vehicle was blocked on the current segment getBlockTimeSeconds()323 double getBlockTimeSeconds() const { 324 return STEPS2TIME(getBlockTime()); 325 } 326 327 /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty 328 double getCurrentLinkPenaltySeconds() const; 329 330 /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty 331 double getCurrentStoppingTimeSeconds() const; 332 333 /// Replaces the current route by the given one 334 bool replaceRoute(const MSRoute* route, const std::string& info, bool onInit = false, int offset = 0, bool addStops = true, bool removeStops = true); 335 336 /** @brief Returns whether the vehicle is allowed to pass the next junction 337 * @return true iff the vehicle may drive over the next junction 338 */ 339 bool mayProceed() const; 340 341 /** @brief Updates a single vehicle detector if present 342 */ 343 void updateDetectorForWriting(MSMoveReminder* rem, SUMOTime currentTime, SUMOTime exitTime); 344 345 /** @brief Updates all vehicle detectors 346 */ 347 void updateDetectors(SUMOTime currentTime, const bool isLeave, 348 const MSMoveReminder::Notification reason = MSMoveReminder::NOTIFICATION_JUNCTION); 349 350 /// @name state io 351 //@{ 352 353 /// Saves the states of a vehicle 354 void saveState(OutputDevice& out); 355 356 /** @brief Loads the state of this vehicle from the given description 357 */ 358 void loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset); 359 //@} 360 361 362 protected: 363 /// @brief The segment the vehicle is at 364 MESegment* mySegment; 365 366 /// @brief Index of the que the vehicle is in (important for multiqueue extension) 367 int myQueIndex; 368 369 /// @brief The (planned) time of leaving the segment (cell) 370 SUMOTime myEventTime; 371 372 /// @brief The time the vehicle entered its current segment 373 SUMOTime myLastEntryTime; 374 375 /// @brief The time at which the vehicle was blocked on its current segment 376 SUMOTime myBlockTime; 377 378 /// @brief where to stop 379 std::map<const MESegment* const, std::vector<SUMOVehicleParameter::Stop> > myStops; 380 381 /// @brief edges to stop 382 ConstMSEdgeVector myStopEdges; 383 384 }; 385 386 #endif 387 388 /****************************************************************************/ 389