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 MSCFModel_ACC.cpp
15 : /// @author Kallirroi Porfyri
16 : /// @date Feb 2018
17 : ///
18 : // ACC car-following model based on [1], [2].
19 : // [1] Milanes, V., and S. E. Shladover. Handling Cut-In Vehicles in Strings
20 : // of Cooperative Adaptive Cruise Control Vehicles. Journal of Intelligent
21 : // Transportation Systems, Vol. 20, No. 2, 2015, pp. 178-191.
22 : // [2] Xiao, L., M. Wang and B. van Arem. Realistic Car-Following Models for
23 : // Microscopic Simulation of Adaptive and Cooperative Adaptive Cruise
24 : // Control Vehicles. Transportation Research Record: Journal of the
25 : // Transportation Research Board, No. 2623, 2017. (DOI: 10.3141/2623-01).
26 : //[3] Xiao, L., Wang, M., Schakel, W., & van Arem, B. (2018). Unravelling
27 : // effects of cooperative adaptive cruise control deactivation on
28 : // traffic flow characteristics at merging bottlenecks. Transportation
29 : // Research Part C: Emerging Technologies, 96, 380–397.
30 : // <https://doi.org/10.1016/j.trc.2018.10.008>
31 : /****************************************************************************/
32 : #include <config.h>
33 :
34 : #include <stdio.h>
35 : #include <iostream>
36 :
37 : #include "MSCFModel_ACC.h"
38 : #include <microsim/MSVehicle.h>
39 : #include <microsim/MSLane.h>
40 : #include <utils/common/RandHelper.h>
41 : #include <utils/common/SUMOTime.h>
42 : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
43 : #include <math.h>
44 : #include <microsim/MSNet.h>
45 :
46 : // ===========================================================================
47 : // debug flags
48 : // ===========================================================================
49 : //#define DEBUG_ACC
50 : //#define DEBUG_COND (true)
51 : //#define DEBUG_COND (veh->isSelected())
52 :
53 :
54 : // ===========================================================================
55 : // defaults
56 : // ===========================================================================
57 : #define DEFAULT_SC_GAIN -0.4
58 : #define DEFAULT_GCC_GAIN_SPEED 0.8
59 : #define DEFAULT_GCC_GAIN_SPACE 0.04
60 : #define DEFAULT_GC_GAIN_SPEED 0.07
61 : #define DEFAULT_GC_GAIN_SPACE 0.23
62 : #define DEFAULT_CA_GAIN_SPACE 0.8
63 : #define DEFAULT_CA_GAIN_SPEED 0.23
64 :
65 : // ===========================================================================
66 : // thresholds
67 : // ===========================================================================
68 : #define GAP_THRESHOLD_SPEEDCTRL 120
69 : #define GAP_THRESHOLD_GAPCTRL 100
70 : // override followSpeed when deemed unsafe by the given margin (the value was selected to reduce the number of necessary interventions)
71 : #define DEFAULT_EMERGENCY_OVERRIDE_THRESHOLD 2.0
72 :
73 : /// @todo: add attributes for myCollisionAvoidanceGainSpeed and myCollisionAvoidanceGainSpace
74 :
75 : // ===========================================================================
76 : // method definitions
77 : // ===========================================================================
78 792 : MSCFModel_ACC::MSCFModel_ACC(const MSVehicleType* vtype) :
79 : MSCFModel(vtype),
80 792 : mySpeedControlGain(vtype->getParameter().getCFParam(SUMO_ATTR_SC_GAIN, DEFAULT_SC_GAIN)),
81 792 : myGapClosingControlGainSpeed(vtype->getParameter().getCFParam(SUMO_ATTR_GCC_GAIN_SPEED, DEFAULT_GCC_GAIN_SPEED)),
82 792 : myGapClosingControlGainSpace(vtype->getParameter().getCFParam(SUMO_ATTR_GCC_GAIN_SPACE, DEFAULT_GCC_GAIN_SPACE)),
83 792 : myGapControlGainSpeed(vtype->getParameter().getCFParam(SUMO_ATTR_GC_GAIN_SPEED, DEFAULT_GC_GAIN_SPEED)),
84 792 : myGapControlGainSpace(vtype->getParameter().getCFParam(SUMO_ATTR_GC_GAIN_SPACE, DEFAULT_GC_GAIN_SPACE)),
85 792 : myCollisionAvoidanceGainSpeed(vtype->getParameter().getCFParam(SUMO_ATTR_CA_GAIN_SPEED, DEFAULT_CA_GAIN_SPEED)),
86 792 : myCollisionAvoidanceGainSpace(vtype->getParameter().getCFParam(SUMO_ATTR_CA_GAIN_SPACE, DEFAULT_CA_GAIN_SPACE)),
87 792 : myApplyDriverstate(vtype->getParameter().getCFParam(SUMO_ATTR_APPLYDRIVERSTATE, 0)),
88 1584 : myEmergencyThreshold(vtype->getParameter().getCFParam(SUMO_ATTR_CA_OVERRIDE, DEFAULT_EMERGENCY_OVERRIDE_THRESHOLD)) {
89 : // ACC does not drive very precise and often violates minGap
90 792 : myCollisionMinGapFactor = vtype->getParameter().getCFParam(SUMO_ATTR_COLLISION_MINGAP_FACTOR, 0.1);
91 792 : }
92 :
93 1145 : MSCFModel_ACC::~MSCFModel_ACC() {}
94 :
95 :
96 : double
97 31866058 : MSCFModel_ACC::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred, const CalcReason /*usage*/) const {
98 31866058 : if (myApplyDriverstate) {
99 52060 : applyHeadwayAndSpeedDifferencePerceptionErrors(veh, speed, gap2pred, predSpeed, predMaxDecel, pred);
100 : }
101 31866058 : const double desSpeed = MIN2(veh->getLane()->getSpeedLimit(), veh->getMaxSpeed());
102 31866058 : const double vACC = _v(veh, gap2pred, speed, predSpeed, desSpeed, true);
103 31866058 : const double vSafe = maximumSafeFollowSpeed(gap2pred, speed, predSpeed, predMaxDecel);
104 31866058 : if (vSafe + myEmergencyThreshold < vACC) {
105 : //ACCVehicleVariables* vars = (ACCVehicleVariables*)veh->getCarFollowVariables();
106 : //std::cout << SIMTIME << " veh=" << veh->getID() << " v=" << speed << " vL=" << predSpeed << " gap=" << gap2pred << " vACC=" << vACC << " vSafe=" << vSafe << " cm=" << vars->ACC_ControlMode << "\n";
107 352638 : return vSafe + myEmergencyThreshold;
108 : }
109 : return vACC;
110 : }
111 :
112 :
113 : double
114 11595789 : MSCFModel_ACC::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
115 11595789 : if (myApplyDriverstate) {
116 2728 : applyHeadwayPerceptionError(veh, speed, gap);
117 : }
118 : // NOTE: This allows return of smaller values than minNextSpeed().
119 : // Only relevant for the ballistic update: We give the argument headway=TS, to assure that
120 : // the stopping position is approached with a uniform deceleration also for tau!=TS.
121 11595789 : return MIN2(maximumSafeStopSpeed(gap, decel, speed, false, veh->getActionStepLengthSecs()), maxNextSpeed(speed, veh));
122 : }
123 :
124 :
125 : double
126 27518362 : MSCFModel_ACC::getSecureGap(const MSVehicle* const /*veh*/, const MSVehicle* const /*pred*/, const double speed, const double leaderSpeed, const double /* leaderMaxDecel */) const {
127 : // Accel in gap mode should vanish:
128 : // 0 = myGapControlGainSpeed * (leaderSpeed - speed) + myGapControlGainSpace * (g - myHeadwayTime * speed);
129 : // <=> myGapControlGainSpace * g = - myGapControlGainSpeed * (leaderSpeed - speed) + myGapControlGainSpace * myHeadwayTime * speed;
130 : // <=> g = - myGapControlGainSpeed * (leaderSpeed - speed) / myGapControlGainSpace + myHeadwayTime * speed;
131 27518362 : return myGapControlGainSpeed * (speed - leaderSpeed) / myGapControlGainSpace + myHeadwayTime * speed;
132 : }
133 :
134 :
135 : double
136 123340 : MSCFModel_ACC::insertionFollowSpeed(const MSVehicle* const v, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const /*pred*/) const {
137 : //#ifdef DEBUG_ACC
138 : // std::cout << "MSCFModel_ACC::insertionFollowSpeed(), speed="<<speed<< std::endl;
139 : //#endif
140 : // iterate to find a stationary value for
141 : // speed = followSpeed(v, speed, gap2pred, predSpeed, predMaxDecel, nullptr, CalcReason::FUTURE)
142 : const int max_iter = 50;
143 : int n_iter = 0;
144 : const double tol = 0.1;
145 : const double damping = 0.1;
146 :
147 : double res = speed;
148 3843723 : while (n_iter < max_iter) {
149 : // proposed acceleration
150 3808539 : const double a = SPEED2ACCEL(followSpeed(v, res, gap2pred, predSpeed, predMaxDecel, nullptr, CalcReason::FUTURE) - res);
151 3808539 : res = res + damping * a;
152 : //#ifdef DEBUG_ACC
153 : // std::cout << " n_iter=" << n_iter << ", a=" << a << ", res=" << res << std::endl;
154 : //#endif
155 3808539 : if (fabs(a) < tol) {
156 : break;
157 : } else {
158 3720383 : n_iter++;
159 : }
160 : }
161 123340 : return res;
162 : }
163 :
164 :
165 : /// @todo update interactionGap logic
166 : double
167 0 : MSCFModel_ACC::interactionGap(const MSVehicle* const /*veh */, double /* vL */) const {
168 : /*maximum radar range is ACC is enabled*/
169 0 : return 250;
170 : }
171 :
172 14027036 : double MSCFModel_ACC::accelSpeedControl(double vErr) const {
173 : // Speed control law
174 14027036 : return mySpeedControlGain * vErr;
175 : }
176 :
177 : double
178 17891663 : MSCFModel_ACC::accelGapControl(const MSVehicle* const /* veh */, const double gap2pred, const double speed, const double predSpeed, double vErr) const {
179 : // Gap control law
180 : double gclAccel = 0.0;
181 17891663 : const double deltaVel = predSpeed - speed;
182 :
183 : // see dynamic gap margin definition from (Xiao et. al, 2018)[3], equation 5 reformulated as min/max to avoid discontinuities
184 17891663 : const double d0 = MAX2(0., MIN2(75. / speed - 5., 2.));
185 : // this is equation 4, gap2pred is the difference in vehicle positions minus the length
186 17891663 : const double spacingErr = gap2pred - myHeadwayTime * speed - d0;
187 :
188 :
189 17891663 : if (fabs(spacingErr) < 0.2 && fabs(vErr) < 0.1) {
190 : // gap mode
191 19429 : gclAccel = myGapControlGainSpeed * deltaVel + myGapControlGainSpace * spacingErr;
192 : #ifdef DEBUG_ACC
193 : if (DEBUG_COND) {
194 : std::cout << " applying gap control: spacingErr=" << spacingErr << " speedErr=" << vErr << std::endl;
195 : }
196 : #endif
197 17872234 : } else if (spacingErr < 0) {
198 : // collision avoidance mode
199 3623053 : gclAccel = myCollisionAvoidanceGainSpeed * deltaVel + myCollisionAvoidanceGainSpace * spacingErr;
200 : #ifdef DEBUG_ACC
201 : if (DEBUG_COND) {
202 : std::cout << " applying collision avoidance: spacingErr=" << spacingErr << " speedErr=" << vErr << std::endl;
203 : }
204 : #endif
205 : } else {
206 : // gap closing mode
207 14249181 : gclAccel = myGapClosingControlGainSpeed * deltaVel + myGapClosingControlGainSpace * spacingErr;
208 : #ifdef DEBUG_ACC
209 : if (DEBUG_COND) {
210 : std::cout << " applying gap closing: spacingErr=" << spacingErr << " speedErr=" << vErr << std::endl;
211 : }
212 : #endif
213 : }
214 17891663 : return gclAccel;
215 : }
216 :
217 :
218 : double
219 31918699 : MSCFModel_ACC::_v(const MSVehicle* const veh, const double gap2pred, const double speed,
220 : const double predSpeed, const double desSpeed, const bool /* respectMinGap */) const {
221 :
222 : double accelACC = 0;
223 : double gapLimit_SC = GAP_THRESHOLD_SPEEDCTRL; // lower gap limit in meters to enable speed control law
224 : double gapLimit_GC = GAP_THRESHOLD_GAPCTRL; // upper gap limit in meters to enable gap control law
225 :
226 : #ifdef DEBUG_ACC
227 : if (DEBUG_COND) {
228 : std::cout << SIMTIME << " MSCFModel_ACC::_v() for veh '" << veh->getID() << "'\n"
229 : << " gap=" << gap2pred << " speed=" << speed << " predSpeed=" << predSpeed
230 : << " desSpeed=" << desSpeed << " tau=" << myHeadwayTime << std::endl;
231 : }
232 : #endif
233 :
234 :
235 : /* Velocity error */
236 31918699 : double vErr = speed - desSpeed;
237 : int setControlMode = 0;
238 : ACCVehicleVariables* vars = (ACCVehicleVariables*) veh->getCarFollowVariables();
239 31918699 : if (vars->lastUpdateTime != MSNet::getInstance()->getCurrentTimeStep()) {
240 4269426 : vars->lastUpdateTime = MSNet::getInstance()->getCurrentTimeStep();
241 : setControlMode = 1;
242 : }
243 31918699 : if (gap2pred > gapLimit_SC) {
244 :
245 : #ifdef DEBUG_ACC
246 : if (DEBUG_COND) {
247 : std::cout << " applying speedControl" << std::endl;
248 : }
249 : #endif
250 : // Find acceleration - Speed control law
251 14026650 : accelACC = accelSpeedControl(vErr);
252 : // Set cl to vehicle parameters
253 14026650 : if (setControlMode) {
254 22648 : vars->ACC_ControlMode = 0;
255 : }
256 17892049 : } else if (gap2pred < gapLimit_GC) {
257 : // Find acceleration - Gap control law
258 17454695 : accelACC = accelGapControl(veh, gap2pred, speed, predSpeed, vErr);
259 : // Set cl to vehicle parameters
260 17454695 : if (setControlMode) {
261 4246153 : vars->ACC_ControlMode = 1;
262 : }
263 : } else {
264 : // Follow previous applied law
265 437354 : int cm = vars->ACC_ControlMode;
266 437354 : if (!cm) {
267 :
268 : #ifdef DEBUG_ACC
269 : if (DEBUG_COND) {
270 : std::cout << " applying speedControl" << std::endl;
271 : }
272 : #endif
273 386 : accelACC = accelSpeedControl(vErr);
274 : } else {
275 436968 : accelACC = accelGapControl(veh, gap2pred, speed, predSpeed, vErr);
276 : }
277 :
278 : }
279 :
280 31918699 : double newSpeed = speed + ACCEL2SPEED(accelACC);
281 :
282 : #ifdef DEBUG_ACC
283 : if (DEBUG_COND) {
284 : std::cout << " result: accel=" << accelACC << " newSpeed=" << newSpeed << std::endl;
285 : }
286 : #endif
287 :
288 31918699 : return MAX2(0., newSpeed);
289 : }
290 :
291 :
292 : MSCFModel*
293 0 : MSCFModel_ACC::duplicate(const MSVehicleType* vtype) const {
294 0 : return new MSCFModel_ACC(vtype);
295 : }
|