00001
00002 #include "stdafx.h"
00003 #include "IDM.h"
00004
00005 #include "ConfigData.h"
00006 extern CConfigData g_ConfigData;
00007
00008 IDM::IDM()
00009 {
00010 LANECHANGE_MIN_GAP = g_ConfigData.IDM.LANECHANGE_MIN_GAP;
00011 SAFE_BRAKING = g_ConfigData.IDM.SAFE_BRAKING;
00012
00013 A_FLAG = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.A_FLAG;
00014 A_MIN = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.A_MIN;
00015 B_FLAG = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.B_FLAG;
00016 T_SLOPE = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.T_SLOPE;
00017 V0_MIN = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.V0_MIN;
00018 V0_SLOPE = g_ConfigData.Road.RoadFeatures.Gradient.IDM_Modifiers.V0_SLOPE;
00019
00020 m_T = 0.0;
00021 m_A = 0.0;
00022 m_B = 0.0;
00023 m_S0 = 0.0;
00024 m_S1 = 0.0;
00025 m_V0 = 0.0;
00026 m_Delta = 0.0;
00027 m_Polite = 0.0;
00028 m_Bias = 0.0;
00029 m_DeltaAth = 0.0;
00030
00031 m_V0_BeforeSpeedLimit = 0.0;
00032 m_V0_BeforeGradient = 0.0;
00033 m_T_BeforeGradient = 0.0;
00034 m_A_BeforeGradient = 0.0;
00035 m_B_BeforeGradient = 0.0;
00036
00037 m_V0_Initial = 0.0;
00038 }
00039
00040 IDM::~IDM()
00041 {
00042
00043 }
00044
00045
00046
00056 double IDM::update(double vel, double dist)
00057 {
00058 double accel = dvdt(vel, dist);
00059 return accel;
00060 }
00061
00062 double IDM::dvdt(double dv, double s)
00063 {
00064 double s_str = s_star(dv);
00065 double intel_acc = m_A*(1-pow(m_speed/m_V0,m_Delta));
00066 double intel_dec = -m_A*pow(s_str/s,2);
00067
00068 double net_acc = intel_acc + intel_dec;
00069
00070
00071
00072 if(net_acc < SAFE_BRAKING)
00073 net_acc = SAFE_BRAKING;
00074
00075 return net_acc;
00076 }
00077
00078 double IDM::s_star(double dv)
00079 {
00080
00081 ASSERT(m_V0 > 0);
00082 ASSERT(m_A > 0);
00083 ASSERT(m_B > 0);
00084 double s_str = m_S0 + m_S1 * sqrt(m_speed/m_V0)
00085 + m_T*m_speed + (m_speed*dv)/(2*sqrt(m_A*m_B));
00086
00087 return s_str;
00088 }
00089
00090
00091
00096 void IDM::setVel(double vel)
00097 {
00098 m_speed = vel;
00099 }
00100
00105 double IDM::getDesiredVel()
00106 {
00107 return m_V0;
00108 }
00109
00113
00129 double IDM::LaneChange(double GapToFront, double GapToBack, double FrontChangeAccel,
00130 double CurrentBackAccel, double ProposedBackAccel, bool overtake)
00131 {
00132 double advantage = 0.0;
00133 double othersDisadv = 0.0;
00134 double netAdvantage = 0.0;
00135
00136 if(GapToFront >= LANECHANGE_MIN_GAP && GapToBack >= LANECHANGE_MIN_GAP && ProposedBackAccel >= SAFE_BRAKING)
00137 {
00138 advantage = FrontChangeAccel + (overtake ? -1 : 1) * m_Bias;
00139 othersDisadv = CurrentBackAccel - ProposedBackAccel;
00140 }
00141
00142 if(othersDisadv < 0.0)
00143 othersDisadv = 0.0;
00144
00145 netAdvantage = advantage - othersDisadv * m_Polite;
00146
00147 if(netAdvantage <= m_DeltaAth)
00148 netAdvantage = 0.0;
00149
00150 return netAdvantage;
00151 }
00152
00153
00155
00157
00158
00159
00160
00161
00162 void IDM::set_T(double val)
00163 {
00164
00165
00166 m_T = val;
00167 }
00168
00169 double IDM::get_T()
00170 {
00171
00172
00173 return m_T;
00174 }
00175
00176
00177
00178 void IDM::set_A(double val)
00179 {
00180
00181
00182 m_A = val;
00183 ASSERT(m_A > 0);
00184 }
00185
00186 double IDM::get_A()
00187 {
00188
00189
00190 return m_A;
00191 }
00192
00193
00194
00195 void IDM::set_B(double val)
00196 {
00197
00198
00199 m_B = val;
00200 ASSERT(m_B > 0);
00201 }
00202
00203 double IDM::get_B()
00204 {
00205
00206
00207 return m_B;
00208 }
00209
00210
00211
00212 void IDM::set_S0(double val)
00213 {
00214
00215
00216 m_S0 = val;
00217 }
00218
00219 double IDM::get_S0()
00220 {
00221
00222
00223 return m_S0;
00224 }
00225
00226
00227
00228 void IDM::set_S1(double val)
00229 {
00230
00231
00232 m_S1 = val;
00233 }
00234
00235 double IDM::get_S1()
00236 {
00237
00238
00239 return m_S1;
00240 }
00241
00242
00243
00244 void IDM::set_V0(double val)
00245 {
00246
00247
00248 m_V0 = val * KM_PER_H_TO_M_PER_S;
00249 m_V0_Initial = m_V0;
00250 ASSERT(m_V0 > 0);
00251 }
00252
00253 double IDM::get_V0()
00254 {
00255
00256
00257 return m_V0;
00258 }
00259
00260
00261
00262 void IDM::set_Delta(double val)
00263 {
00264
00265
00266 m_Delta = val;
00267 }
00268
00269 double IDM::get_Delta()
00270 {
00271
00272
00273 return m_Delta;
00274 }
00275
00276
00277
00278 void IDM::set_Polite(double val)
00279 {
00280
00281
00282 m_Polite = val;
00283 }
00284
00285 double IDM::get_Polite()
00286 {
00287
00288
00289 return m_Polite;
00290 }
00291
00292
00293
00294 void IDM::set_Bias(double val)
00295 {
00296
00297
00298 m_Bias = val;
00299 }
00300
00301 double IDM::get_Bias()
00302 {
00303
00304
00305 return m_Bias;
00306 }
00307
00308
00309
00310 void IDM::set_DeltaAth(double val)
00311 {
00312
00313
00314 m_DeltaAth = val;
00315 }
00316
00317 double IDM::get_DeltaAth()
00318 {
00319
00320
00321 return m_DeltaAth;
00322 }
00323
00335 void IDM::SetSpeedLimit(double newV0)
00336 {
00337 m_bInSpeedLimit = true;
00338 m_V0_BeforeSpeedLimit = m_V0;
00339
00340 m_V0 = newV0;
00341 ASSERT(m_V0 > 0);
00342 ASSERT(m_V0_BeforeSpeedLimit > 0);
00343 }
00344
00345
00346
00347
00348 void IDM::SetGradient(double gradient)
00349 {
00350 m_bInGradient = true;
00351 m_A_BeforeGradient = m_A; m_T_BeforeGradient = m_T;
00352 m_B_BeforeGradient = m_B; m_V0_BeforeGradient = m_V0;
00353
00354
00355 double ABSgradient = gradient < 0 ? -gradient : gradient;
00356 m_T += ABSgradient/T_SLOPE;
00357
00358
00359
00360
00361 if(A_FLAG)
00362 {
00363 m_A -= gradient/10;
00364 m_A = m_A < A_MIN ? A_MIN : m_A;
00365 }
00366
00367
00368 if(B_FLAG && gradient > 0)
00369 m_B += gradient/10;
00370
00371
00372 if(gradient > 0)
00373 {
00374
00375
00376 if(!m_bInSpeedLimit)
00377 m_V0 -= (gradient/V0_SLOPE)*KM_PER_H_TO_M_PER_S;
00378 else
00379 {
00380 double FreeV0 = m_V0_Initial;
00381 double SpeedLimitV0 = m_V0;
00382 double GradientV0 = FreeV0 - (gradient/V0_SLOPE)*KM_PER_H_TO_M_PER_S;
00383 m_V0 = SpeedLimitV0 < GradientV0 ? SpeedLimitV0 : GradientV0;
00384 }
00385 if(m_V0 < V0_MIN)
00386 {
00387
00388 m_V0 = V0_MIN;
00389 }
00390 }
00391 }
00392
00400 void IDM::ClearSpeedLimit()
00401 {
00402 if(!m_bInSpeedLimit)
00403 TRACE("***SL: Removed but not in\n");
00404 ASSERT(m_V0_BeforeSpeedLimit > 0);
00405 m_bInSpeedLimit = false;
00406 m_V0 = m_V0_BeforeSpeedLimit;
00407 }
00408
00417 void IDM::ClearGradient()
00418 {
00419 if(!m_bInGradient)
00420 TRACE("***Grad: Removed but not in\n");
00421 ASSERT(m_V0_BeforeGradient > 0);
00422
00423 m_bInGradient = false;
00424 m_T = m_T_BeforeGradient;
00425 m_A = m_A_BeforeGradient;
00426 m_B = m_B_BeforeGradient;
00427 m_V0 = m_V0_BeforeGradient;
00428 }
00429
00439 void IDM::ClearRestriction()
00440 {
00441
00442
00443 m_V0 = m_V0_Initial;
00444 }