Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 : /****************************************************************************/
14 : /// @file MEVehicle.h
15 : /// @author Daniel Krajzewicz
16 : /// @date Tue, May 2005
17 : ///
18 : // A vehicle from the mesoscopic point of view
19 : /****************************************************************************/
20 : #pragma once
21 : #include <config.h>
22 :
23 : #include <iostream>
24 : #include <cassert>
25 : #include <map>
26 : #include <vector>
27 : #include <microsim/MSBaseVehicle.h>
28 : #include <microsim/MSEdge.h>
29 : #include <utils/common/StdDefs.h>
30 : #include "MESegment.h"
31 :
32 : class MSLane;
33 : class MSLink;
34 :
35 : // ===========================================================================
36 : // class definitions
37 : // ===========================================================================
38 : /**
39 : * @class MEVehicle
40 : * @brief A vehicle from the mesoscopic point of view
41 : */
42 367631 : class MEVehicle : public MSBaseVehicle {
43 : public:
44 : /** @brief Constructor
45 : * @param[in] pars The vehicle description
46 : * @param[in] route The vehicle's route
47 : * @param[in] type The vehicle's type
48 : * @param[in] speedFactor The factor for driven lane's speed limits
49 : * @exception ProcessError If a value is wrong
50 : */
51 : MEVehicle(SUMOVehicleParameter* pars, ConstMSRoutePtr route,
52 : MSVehicleType* type, const double speedFactor);
53 :
54 :
55 : /** @brief Get the vehicle's position along the lane
56 : * @return The position of the vehicle (in m from the lane's begin)
57 : */
58 : double getPositionOnLane() const;
59 :
60 :
61 : /** @brief Get the vehicle's position relative to the given lane
62 : * @return The back position of the vehicle (in m from the given lane's begin)
63 : */
64 : double getBackPositionOnLane(const MSLane* lane) const;
65 :
66 :
67 : /** @brief Returns the vehicle's direction in degrees
68 : * @return The vehicle's current angle
69 : */
70 : double getAngle() const;
71 :
72 :
73 : /** @brief Returns the slope of the road at vehicle's position in degrees
74 : * @return The slope
75 : */
76 : double getSlope() const;
77 :
78 : /** @brief Returns the edge the vehicle is currently at (possibly an
79 : * internal edge)
80 : */
81 : const MSEdge* getCurrentEdge() const;
82 :
83 : /** @brief Returns the lane the vehicle is on
84 : * @return The vehicle's current lane
85 : */
86 13200490 : const MSLane* getLane() const {
87 13200490 : return nullptr;
88 : }
89 :
90 5147 : const MSLane* getBackLane() const {
91 5147 : return nullptr;
92 : }
93 :
94 : /** @brief Return current position (x/y, cartesian)
95 : *
96 : * If the vehicle's myLane is 0, Position::INVALID.
97 : * @param[in] offset optional offset in longitudinal direction
98 : * @return The current position (in cartesian coordinates)
99 : * @see myLane
100 : */
101 : Position getPosition(const double offset = 0) const;
102 :
103 : /// @brief get bounding rectangle
104 : PositionVector getBoundingBox(double offset = 0) const;
105 :
106 : /** @brief Returns the vehicle's estimated speed assuming no delays
107 : * @return The vehicle's estimated speed
108 : * @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed
109 : */
110 : double getSpeed() const;
111 :
112 : /** @brief Returns the vehicle's estimated average speed on the segment assuming no further delays
113 : * @return The vehicle's estimated average speed
114 : * @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed
115 : */
116 : double getAverageSpeed() const;
117 :
118 : /// @brief Returns the vehicle's estimated speed after driving across the link
119 : double estimateLeaveSpeed(const MSLink* link) const;
120 :
121 :
122 : /** @brief Returns the vehicle's estimated speed taking into account delays
123 : * @return The vehicle's estimated speed
124 : * @param[in, out] earliestArrival A lower bound on leaveTime, modified to contain new lower bound on leave Time
125 : * @note This is only an upper bound. The speed may be even lower if there are further delays downstream
126 : */
127 : double getConservativeSpeed(SUMOTime& earliestArrival) const;
128 :
129 : /// @name insertion/removal
130 : //@{
131 :
132 : /** @brief Called when the vehicle is removed from the network.
133 : *
134 : * Moves along work reminders and
135 : * informs all devices about quitting. Calls "leaveLane" then.
136 : *
137 : * @param[in] reason why the vehicle leaves (reached its destination, parking, teleport)
138 : */
139 : void onRemovalFromNet(const MSMoveReminder::Notification reason);
140 : //@}
141 :
142 :
143 : /** @brief Update when the vehicle enters a new edge in the move step.
144 : * @return Whether the vehicle's route has ended (due to vaporization, or because the destination was reached)
145 : */
146 : bool moveRoutePointer();
147 :
148 : /** @brief Returns whether this vehicle has already arrived
149 : * (reached the arrivalPosition on its final edge)
150 : */
151 : bool hasArrived() const;
152 :
153 : /** @brief Returns the information whether the vehicle is on a road (is simulated)
154 : * @return Whether the vehicle is simulated
155 : */
156 : bool isOnRoad() const;
157 :
158 : /** @brief Returns whether the vehicle is trying to re-enter the net
159 : * @return true if the vehicle is trying to enter the net (eg after parking)
160 : */
161 : virtual bool isIdling() const;
162 :
163 :
164 : /** @brief registers vehicle with the given link
165 : *
166 : * @param[in] link the link on which the car shall register its approach
167 : */
168 : void setApproaching(MSLink* link);
169 :
170 : /// @brief Returns the remaining stop duration for a stopped vehicle or 0
171 0 : SUMOTime remainingStopDuration() const {
172 0 : return 0;
173 : }
174 :
175 5488 : bool instantStopping() const {
176 5488 : return true;
177 : }
178 :
179 : ///@brief ends the current stop and performs loading/unloading
180 : void processStop();
181 :
182 : /** @brief Returns until when to stop at the current segment and sets the information that the stop has been reached
183 : * @param[in] time the current time
184 : * @return stop time for the segment
185 : */
186 : SUMOTime checkStop(SUMOTime time);
187 :
188 : /**
189 : * resumes a vehicle from stopping
190 : * @return true on success, the resuming fails if the vehicle wasn't parking in the first place
191 : */
192 : bool resumeFromStopping();
193 :
194 : /// @brief get distance for coming to a stop (used for rerouting checks)
195 158414 : double getBrakeGap(bool delayed = false) const {
196 : UNUSED_PARAMETER(delayed);
197 158414 : return mySegment == nullptr || myQueIndex == MESegment::PARKING_QUEUE ? 0 : mySegment->getLength();
198 : }
199 :
200 : /** @brief Sets the (planned) time at which the vehicle leaves its current segment
201 : * @param[in] t The leaving time
202 : */
203 : inline void setEventTime(SUMOTime t, bool hasDelay = true) {
204 : assert(t > myLastEntryTime);
205 56798144 : if (hasDelay && mySegment != nullptr) {
206 : mySegment->getEdge().markDelayed();
207 : }
208 18263755 : myEventTime = t;
209 11832571 : }
210 :
211 :
212 : /** @brief Returns the (planned) time at which the vehicle leaves its current segment
213 : * @return The time the vehicle thinks it leaves its segment at
214 : */
215 : inline SUMOTime getEventTime() const {
216 128629993 : return myEventTime;
217 : }
218 :
219 : /** @brief Sets the minimum time at which the vehicle leaves its current segment (this includes planned stops)
220 : * @param[in] t The leaving time
221 : */
222 : inline void setUnqueuedEventTime(SUMOTime t) {
223 26701818 : myUnqueuedEventTime = t;
224 : }
225 : /** @brief Returns the minimum time at which the vehicle leaves its current segment
226 : * @return The minimum time the vehicle could leave its segment at
227 : */
228 : inline SUMOTime getUnqueuedEventTime() const {
229 : return myUnqueuedEventTime;
230 : }
231 :
232 :
233 : /** @brief Sets the current segment the vehicle is at together with its que
234 : * @param[in] s The current segment
235 : * @param[in] q The current que
236 : */
237 54242649 : inline virtual void setSegment(MESegment* s, int idx = 0) {
238 54242649 : mySegment = s;
239 54242649 : myQueIndex = idx;
240 54242649 : }
241 :
242 :
243 : /** @brief Returns the current segment the vehicle is on
244 : * @return The segment the vehicle is on
245 : */
246 : inline MESegment* getSegment() const {
247 51638552 : return mySegment;
248 : }
249 :
250 : int getSegmentIndex() const;
251 :
252 : /** @brief Returns the index of the que the vehicle is in
253 : * @return The que index
254 : */
255 0 : inline int getQueIndex() const {
256 175675856 : return myQueIndex;
257 : }
258 :
259 : /** @brief Get the vehicle's lateral position on the edge of the given lane
260 : * (or its current edge if lane == 0)
261 : * @return The lateral position of the vehicle (in m distance between right
262 : * side of vehicle and ride side of edge
263 : */
264 : double getRightSideOnEdge(const MSLane* /*lane*/) const;
265 :
266 : /** @brief Sets the entry time for the current segment
267 : * @param[in] t The entry time
268 : */
269 : inline void setLastEntryTime(SUMOTime t) {
270 26722978 : myLastEntryTime = t;
271 : }
272 :
273 :
274 : /** @brief Returns the time the vehicle entered the current segment
275 : * @return The entry time
276 : */
277 : SUMOTime getLastEntryTime() const {
278 36871894 : return myLastEntryTime;
279 : }
280 :
281 : /** @brief Returns the time of the vehicle's last action point.
282 : * @return The time of the last action point
283 : */
284 5952 : SUMOTime getLastActionTime() const {
285 5952 : return SIMSTEP;
286 : }
287 :
288 : /** @brief Sets the time at which the vehicle was blocked
289 : * @param[in] t The blocking time
290 : */
291 : inline void setBlockTime(const SUMOTime t) {
292 : assert(t > myLastEntryTime);
293 27087924 : myBlockTime = t;
294 364946 : }
295 :
296 :
297 : /** @brief Returns the time at which the vehicle was blocked
298 : * @return The blocking time
299 : */
300 : inline SUMOTime getBlockTime() const {
301 13422280 : return myBlockTime;
302 : }
303 :
304 :
305 : /// @brief Returns the duration for which the vehicle was blocked
306 160609995 : inline SUMOTime getWaitingTime(const bool accumulated = false) const {
307 : UNUSED_PARAMETER(accumulated);
308 160609995 : return MAX2(SUMOTime(0), myEventTime - myBlockTime);
309 : }
310 :
311 : /// @brief Returns the time lost compare to free flow
312 : inline SUMOTime getQueuingTimeLoss() const {
313 0 : return MAX2(SUMOTime(0), myEventTime - myUnqueuedEventTime);
314 : }
315 :
316 64204 : inline SUMOTime getTimeLoss() const {
317 : // slow-downs while driving are not modelled
318 64204 : return getWaitingTime();
319 : }
320 :
321 : /// @brief Returns the earliest leave time for the current segment
322 0 : double getEventTimeSeconds() const {
323 5732 : return STEPS2TIME(getEventTime());
324 : }
325 :
326 : /// @brief Returns the entry time for the current segment
327 0 : double getLastEntryTimeSeconds() const {
328 1894767 : return STEPS2TIME(getLastEntryTime());
329 : }
330 :
331 : /// @brief Returns the time at which the vehicle was blocked on the current segment
332 0 : double getBlockTimeSeconds() const {
333 20 : return STEPS2TIME(getBlockTime());
334 : }
335 :
336 : /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty
337 : double getCurrentLinkPenaltySeconds() const;
338 :
339 : /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty
340 : double getCurrentStoppingTimeSeconds() const;
341 :
342 : /// Replaces the current route by the given one
343 : bool replaceRoute(ConstMSRoutePtr route, const std::string& info, bool onInit = false, int offset = 0, bool addRouteStops = true, bool removeStops = true, std::string* msgReturn = nullptr);
344 :
345 : /** @brief Returns whether the vehicle is allowed to pass the next junction, checks also for triggered stops
346 : * @return true iff the vehicle may drive over the next junction
347 : */
348 : bool mayProceed();
349 :
350 : /** @brief Updates a single vehicle detector if present
351 : */
352 : void updateDetectorForWriting(MSMoveReminder* rem, SUMOTime currentTime, SUMOTime exitTime);
353 :
354 : /** @brief Updates all vehicle detectors
355 : */
356 : void updateDetectors(const SUMOTime currentTime, const SUMOTime exitTime, const bool isLeave,
357 : const MSMoveReminder::Notification reason = MSMoveReminder::NOTIFICATION_JUNCTION);
358 :
359 : /** @brief Returns the velocity/lane influencer
360 : *
361 : * If no influencer was existing before, one is built, first
362 : * @return Reference to this vehicle's speed influencer
363 : */
364 : BaseInfluencer& getBaseInfluencer();
365 :
366 : const BaseInfluencer* getBaseInfluencer() const;
367 :
368 9593212 : bool hasInfluencer() const {
369 9593212 : return myInfluencer != nullptr;
370 : }
371 :
372 : void markJammed(bool jammed) {
373 20395610 : myWasJammed = jammed;
374 20395610 : }
375 :
376 : bool wasJammed() const {
377 25885152 : return myWasJammed;
378 : }
379 :
380 : /// @name state io
381 : //@{
382 :
383 : /// Saves the states of a vehicle
384 : void saveState(OutputDevice& out);
385 :
386 : /** @brief Loads the state of this vehicle from the given description
387 : */
388 : void loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset);
389 : //@}
390 :
391 :
392 : protected:
393 : /// @brief The segment the vehicle is at
394 : MESegment* mySegment;
395 :
396 : /// @brief Index of the que the vehicle is in (important for multiqueue extension)
397 : int myQueIndex;
398 :
399 : /// @brief The (planned) time of leaving the segment (cell)
400 : SUMOTime myEventTime;
401 :
402 : /// @brief The time the vehicle entered its current segment
403 : SUMOTime myLastEntryTime;
404 :
405 : /// @brief The time at which the vehicle was blocked on its current segment
406 : SUMOTime myBlockTime;
407 :
408 : /// @brief The expected time of leaving the segment when following the leader at free-flow headway
409 : SUMOTime myUnqueuedEventTime;
410 :
411 : /// @brief Whether this vehicle was jammed on it's previous normal segment
412 : bool myWasJammed;
413 :
414 : /// @brief An instance of a velocity/lane influencing instance; built in "getInfluencer"
415 : BaseInfluencer* myInfluencer;
416 :
417 : };
|