[go: up one dir, main page]

Menu

[ea6c30]: / world / Robot_Driver.cc  Maximize  Restore  History

Download this file

1153 lines (1016 with data), 36.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
// Robot_Driver.cc - a computer-controlled driver
//
// Vamos Automotive Simulator
// Copyright (C) 2008 Sam Varner
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "Robot_Driver.h"
#include "../body/Car.h"
#include "../body/Wheel.h"
#include "../geometry/Calculations.h"
#include "../geometry/Constants.h"
#include "../geometry/Parameter.h"
#include "../geometry/Three_Vector.h"
#include "../media/Two_D.h"
#include "../track/Strip_Track.h"
#include "World.h"
#include <algorithm>
#include <limits>
using namespace Vamos_Body;
using namespace Vamos_Geometry;
using namespace Vamos_Media;
using namespace Vamos_Track;
using namespace Vamos_World;
namespace
{
// Maximum allowed speed may be scaled. Make sure we don't exceed the exceed
// the max double if we multiply the maximum speed by a small number.
const double UNLIMITED_SPEED = 0.01 * std::numeric_limits <double>::max ();
// Decide whether to keep trying to pass or give up after this many seconds.
const double PASS_TIME = 2.0;
// Take action if cars will make contact in this many seconds.
const double CRASH_TIME_LIMIT = 2.0;
}
//-----------------------------------------------------------------------------
Robot_Driver::Robot_Driver (Car& car_in,
Strip_Track& track_in,
double gravity)
: Driver (car_in),
m_mode (RACE),
m_state (PARKED),
m_event (Event::PARK, 0.0),
mp_cars (0),
m_info_index (0),
m_speed_control (4.0, 0.0, 0.0),
m_traction_control (0.5, 0.0, 0.0),
m_brake_control (0.1, 0.0, 0.0),
m_steer_control (0.5, 0.0, 0.0),
m_front_gap_control (1.5, 0.0, 1.5),
m_target_slip (car_in.get_robot_parameters ().slip_ratio),
m_speed (0.0),
m_target_segment (0),
mp_track (&track_in),
m_shift_time (0.0),
m_timestep (1.0),
m_lane_shift (0.0),
m_lane_shift_timer (0.0),
m_interact (true),
m_show_steering_target (false),
m_road (mp_track->get_road (0)),
m_racing_line (m_road,
car_in.get_robot_parameters ().lateral_acceleration,
gravity),
m_braking (m_road,
car_in.get_robot_parameters ().deceleration,
gravity,
m_racing_line),
m_speed_factor (1.0),
m_passing (false)
{
m_traction_control.set (m_target_slip);
for (int i = 0; i < 10; i++)
m_gap [i] = 0;
}
void Robot_Driver::set_cars (const std::vector <Car_Information>* cars)
{
mp_cars = cars;
m_info_index = cars->size () - 1;
}
double
Robot_Driver::reaction_time ()
{
return Vamos_Geometry::random_in_range (0.1, 0.3);
}
void Robot_Driver::qualify ()
{
m_mode = QUALIFY;
}
void
Robot_Driver::start (double to_go)
{
if (m_state == PARKED)
set_event (Event::START_ENGINE);
else if (m_mode == QUALIFY)
set_event (Event::DRIVE, Vamos_Geometry::random_in_range (10, 60));
else if (to_go <= 0.0)
set_event (Event::DRIVE, reaction_time ());
else if (to_go <= 1.0)
set_event (Event::REV);
}
void
Robot_Driver::finish ()
{
set_event (Event::DONE);
}
void
Robot_Driver::set_event (Event::Type type, double delay)
{
// Ignore successive events of the same type.
if (type != m_event.type)
m_event = Event (type, delay);
}
void
Robot_Driver::propagate (double timestep)
{
// It's useful for other methods to know the size of the current timestep.
m_timestep = timestep;
handle_event ();
m_speed = mp_car->chassis ().cm_velocity ().magnitude();
if (is_driving ())
drive ();
}
void
Robot_Driver::handle_event ()
{
m_event.propagate (m_timestep);
if (!m_event.ready ())
return;
switch (m_event.type)
{
case Event::PARK:
set_brake (1.0);
mp_car->disengage_clutch (0.0);
mp_car->shift (0);
mp_car->start_engine ();
set_gas (0.0);
m_state = PARKED;
break;
case Event::START_ENGINE:
mp_car->disengage_clutch (0.0);
mp_car->shift (0);
mp_car->start_engine ();
set_gas (0.0);
m_state = IDLING;
break;
case Event::REV:
mp_car->disengage_clutch (0.0);
mp_car->shift (1);
set_gas (0.5);
m_state = REVVING;
break;
case Event::DRIVE:
static const double clutch_time = 2.0;
if (m_mode == QUALIFY && !has_gap ())
m_event.reset ();
else if (m_state != DRIVING)
{
if (m_mode != QUALIFY)
m_lane_shift = get_lane_shift ();
mp_car->shift (1);
mp_car->engage_clutch (clutch_time);
m_state = DRIVING;
}
break;
case Event::DONE:
m_state = COOL_DOWN;
m_traction_control.set (m_target_slip * 0.5);
m_braking.scale (0.5);
break;
case Event::NO_EVENT:
break;
default:
assert (false);
break;
}
}
double Robot_Driver::lengths (double n) const
{
return n * mp_car->length ();
}
bool Robot_Driver::has_gap () const
{
double along = info ().track_position ().x;
for (std::vector <Car_Information>::const_iterator it = mp_cars->begin ();
it != mp_cars->end ();
it++)
{
if (it->driver->is_driving ())
{
double delta = (it->track_position ().x - along);
if ((delta - m_road.length () > lengths (-100.0)) || (delta < lengths (25.0)))
return false;
}
}
return true;
}
bool Robot_Driver::is_driving () const
{
return (m_state == DRIVING || m_state == COOL_DOWN);
}
//* Drive
void Robot_Driver::drive ()
{
mp_segment = m_road.segments ()[info ().segment_index];
steer ();
choose_gear ();
accelerate ();
// Avoid collisions last since we may override steering and braking.
avoid_collisions ();
}
double Robot_Driver::get_lane_shift () const
{
return lane_shift (info ().track_position ());
}
double Robot_Driver::lane_shift (const Three_Vector& track) const
{
// Return the lane shift parameter for the current position.
double along = track.x;
double line_y = m_racing_line.from_center (along, info ().segment_index);
double offline = track.y - line_y;
if (offline > 0.0)
{
double left = m_road.racing_line ().left_width (m_road, along);
return std::min (offline / (left - line_y), 1.0);
}
else
{
double right = m_road.racing_line ().right_width (m_road, along);
return std::max (offline / (right + line_y), -1.0);
}
}
double Robot_Driver::get_offline_distance () const
{
return offline_distance (info ().m_pointer_position.x, m_lane_shift);
}
double Robot_Driver::offline_distance (double along, double lane_shift) const
{
double line_y = m_racing_line.from_center (along, info ().segment_index);
if (m_lane_shift > 0.0)
{
double left = m_road.racing_line ().left_width (m_road, along);
return m_lane_shift * (left - line_y);
}
else
{
double right = m_road.racing_line ().right_width (m_road, along);
return m_lane_shift * (right + line_y);
}
}
//** Steer
void
Robot_Driver::steer ()
{
// Steer to keep the car on the racing line. The target is ahead of the
// car as if attached to a pole that sticks out in front of the car. Trying to
// steer to keep the car's position on the racing line leads to
// instability. Using a target that's ahead of the car is very stable.
// Steer by an amount that's proportional to the cross product of the actual
// and desired directions. For small angles steering is approximately
// proportional to the angle between them. The steering wheel is set according
// to the output of a PID controller (although only the P term seems to be
// necessary).
double angle = pointer_vector ().cross (target_vector ()).z;
// 'angle' is |r_p||r_t|sin theta ~ r_t^2 theta. The additional angle to the
// off-line position is sin^-1 (r_o/r_t) ~ r_o/r_t. After scaling by r_t^2 to
// agree with 'angle' it's r_o r_t.
m_steer_control.set (angle + get_offline_distance () * mp_car->target_distance ());
set_steering (m_steer_control.propagate (mp_car->steer_angle (), m_timestep));
}
Three_Vector
Robot_Driver::pointer_vector () const
{
// Return a vector that points in the direction of the point that the driver
// tries to keep on the racing line.
return mp_car->target_position () - mp_car->center_position ();
}
Three_Vector
Robot_Driver::target_vector ()
{
/// Return a vector that points in the direction of the position of the point on
/// the track that the driver aims for.
Three_Vector target = m_racing_line.target (info ().track_position ().x,
mp_car->target_distance ());
Three_Vector center = mp_car->center_position ();
return Three_Vector (target.x - center.x,
target.y - center.y,
0.0);
}
void
Robot_Driver::set_steering (double angle)
{
// Set the steering angle to 'angle'. Clip to a reasonable range. The 'true'
// argument indicates "direct steering"; non-linearity and speed-sensitivity
// are ignored.
const double max_angle = 1.5 * target_slip_angle ();
mp_car->steer (clip (angle, -max_angle, max_angle), 0.0, true);
}
double
Robot_Driver::target_slip_angle () const
{
//! Can we use a constant instead?
return abs_max (mp_car->wheel (0)->peak_slip_angle (),
mp_car->wheel (1)->peak_slip_angle (),
mp_car->wheel (2)->peak_slip_angle (),
mp_car->wheel (3)->peak_slip_angle ());
}
//** Choose Gear
void
Robot_Driver::choose_gear ()
{
if (!mp_car->clutch ()->engaged ())
return;
// Avoid shifting too frequently.
m_shift_time += m_timestep;
if (m_shift_time < 0.3)
return;
// Save some values that show up often in this method.
const int gear = mp_car->gear ();
const double throttle = mp_car->engine ()->throttle ();
// Find current the engine power and the power at maximum throttle in 1) the
// current gear 2) the next gear up 3) 2 gears down. Two gears to prevent
// downshifting to 1st and to prevent too much engine drag under braking.
double omega = mp_car->engine ()->rotational_speed ();
double up_omega = omega
* mp_car->transmission ()->gear_ratio (gear + 1)
/ mp_car->transmission ()->gear_ratio (gear);
double down2_omega = omega
* mp_car->transmission ()->gear_ratio (gear - 2)
/ mp_car->transmission ()->gear_ratio (gear);
double current_power = mp_car->engine ()->power (throttle, omega);
double power = mp_car->engine ()->power (1.0, omega);
double up_power = mp_car->engine ()->power (1.0, up_omega);
double down2_power = mp_car->engine ()->power (1.0, down2_omega);
// Shift up if there's more power at the current revs in the next higher
// gear.
if (up_power > power)
{
mp_car->shift_up ();
}
// Shift up if there's enough power in the next higher gear even if there's
// potentially more power in the current gear. This saves fuel by lowering
// revs.
else if ((up_power > current_power)
&& (up_power > 0.95 * power)
&& (throttle > 0.1)
&& (throttle < 0.9))
{
mp_car->shift_up ();
}
// Shift down if there's more power 2 gears down.
else if ((down2_power > power)
&& (gear > 2)
&& ((m_state != COOL_DOWN) || (gear > 3)))
{
mp_car->shift_down ();
}
// Reset the timer if the gear actually changed.
if (mp_car->gear () != gear)
m_shift_time = 0.0;
}
//** Accelerate
void
Robot_Driver::accelerate ()
{
// Try to achieve the maximum safe speed for this point on the track.
// Save some values that show up often in this method. The normal vector
// calculation requires distance along the segment instead of distance along
// the track.
const double along_segment
= info ().track_position ().x - mp_segment->start_distance ();
// The 'true' argument causes kerbs to be ignored when calculating the normal
// for the racing line speed.
const Three_Vector normal = mp_segment->normal (along_segment,
info ().track_position ().y,
false);
const double drag = mp_car->chassis ().aerodynamic_drag ();
const double lift = mp_car->chassis ().aerodynamic_lift ();
double along = info ().track_position ().x;
double cornering_speed
= m_racing_line.maximum_speed (along,
m_lane_shift,
lift,
normal,
mp_car->chassis ().mass ());
double braking_speed
= m_braking.maximum_speed (m_speed,
along,
(m_speed_factor < 1.0) ? lengths (2.0) : 0.0,
m_lane_shift,
drag,
lift,
mp_car->chassis ().mass ());
set_speed (std::min (cornering_speed, braking_speed));
}
void
Robot_Driver::set_speed (double target_speed)
{
// Set the gas and brake pedal positions. Three PID controllers are involved
// in setting the pedal position: 'm_speed_control' ant 'm_brake_control'
// attempt to reach the target speed, 'm_traction_control' attempts to limit
// wheel spin. Nothing prevents the gas and brake from being applied
// simultaneously, but with only P terms of the PID controllers being used it
// does not happen in practice.
// Speed may be modulated to avoid running into the back of the car in front.
target_speed *= mp_car->grip () * m_speed_factor;
m_speed_control.set (target_speed);
double d1 = m_speed_control.propagate (m_speed, m_timestep);
double d2 = m_traction_control.propagate (total_slip (), m_timestep);
double gas = std::min (d1, d2);
if (!mp_car->clutch ()->engaged ())
{
// Keep the revs in check if the clutch is not fully engaged.
m_speed_control.set (0.0);
double error = (mp_car->engine ()->rotational_speed ()
- mp_car->engine ()->peak_engine_speed ());
gas = std::min (gas, m_speed_control.propagate (0.01 * error, m_timestep));
}
if (gas >= 1.0)
gas *= m_speed_factor;
if (m_state == COOL_DOWN)
gas = std::min (gas, 0.5);
set_gas (gas * m_speed_factor);
m_brake_control.set (std::min (target_speed, m_speed));
double b1 = -m_brake_control.propagate (m_speed, m_timestep);
double b2 = m_traction_control.propagate (total_slip (), m_timestep);
set_brake (std::min (b1, b2));
}
double
Robot_Driver::total_slip () const
{
return Three_Vector (longitudinal_slip (), transverse_slip (), 0.0).magnitude ();
}
double
Robot_Driver::longitudinal_slip () const
{
return abs_max (mp_car->wheel (0)->slip ().x,
mp_car->wheel (1)->slip ().x,
mp_car->wheel (2)->slip ().x,
mp_car->wheel (3)->slip ().x);
}
double
Robot_Driver::transverse_slip () const
{
return abs_max (mp_car->wheel (0)->slip ().y,
mp_car->wheel (1)->slip ().y,
mp_car->wheel (2)->slip ().y,
mp_car->wheel (3)->slip ().y);
}
void
Robot_Driver::set_gas (double gas)
{
if (gas <= 0.0)
{
// Erase the controllers' history.
m_speed_control.reset ();
m_traction_control.reset ();
}
mp_car->gas (clip (gas, 0.0, 1.0));
}
void
Robot_Driver::set_brake (double brake)
{
if (brake <= 0.0)
{
// Erase the controllers' history.
m_brake_control.reset ();
m_traction_control.reset ();
}
mp_car->brake (clip (brake, 0.0, 1.0));
}
//** Avoid Collisions
void
Robot_Driver::avoid_collisions ()
{
// Take action to pass or avoid potential collisions.
if (mp_cars == 0) return;
// Loop through the other cars. Each time through the loop we potentially
// update all of these variables.
double min_forward_distance_gap = 100;
double min_left_distance_gap = 100;
double min_right_distance_gap = 100;
double along = info ().track_position ().x;
double follow_lengths = ((m_state == COOL_DOWN)
? 3.0
: (maybe_passable (along, info ().segment_index)
? 0.5
: 1.5));
double crash_time = 2.0*CRASH_TIME_LIMIT;
Direction pass_side = NONE;
Three_Vector v1
= mp_car->chassis ().rotate_from_world (mp_car->chassis ().cm_velocity ());
// Break out immediately if the interact flag is false. We still need the lane
// shift code after the loop so the cars will get to the racing line after the
// start.
for (std::vector <Car_Information>::const_iterator it = mp_cars->begin ();
it != mp_cars->end () && m_interact;
it++)
{
// Don't check for collisions with yourself.
if (it->car == mp_car)
continue;
// Don't worry about cars that are far away.
if (std::abs (it->track_position ().x - along) > lengths (10))
continue;
Three_Vector v2
= it->car->chassis ().rotate_from_world (it->car->chassis ().cm_velocity ());
Three_Vector delta_v = (v2 - v1);
Three_Vector distance_gap = find_gap (info ().m_pointer_position,
it->m_pointer_position);
switch (relative_position (info ().track_position (), it->track_position ()))
{
case FORWARD:
if (distance_gap.x < min_forward_distance_gap)
{
min_forward_distance_gap = distance_gap.x;
crash_time = -distance_gap.x / delta_v.x;
if ((min_forward_distance_gap < 0.0) || (crash_time < 0.0))
{
m_passing = false;
}
else if (m_passing)
{
follow_lengths = -distance_gap.y / mp_car->length ();
}
pass_side = get_pass_side (along,
distance_gap.x,
-delta_v.x,
info ().segment_index);
}
break;
case LEFT:
if (distance_gap.y < min_left_distance_gap)
{
min_left_distance_gap = distance_gap.y;
crash_time = -distance_gap.y / delta_v.y;
}
break;
case RIGHT:
if (distance_gap.y < min_right_distance_gap)
{
min_right_distance_gap = distance_gap.y;
crash_time = distance_gap.y / delta_v.y;
}
break;
default:
break;
}
}
m_gap [4] = crash_time;
const double shift_step = 0.4 * m_timestep;
// Keep track of how line we've been off the racing line.
if (m_passing || m_lane_shift != 0.0)
m_lane_shift_timer += m_timestep;
if ((min_right_distance_gap < 0.5*mp_car->width ())
&& (min_left_distance_gap > min_right_distance_gap))
{
// Move to the left if the car on the right is too close.
m_lane_shift = std::min (1.0, m_lane_shift + shift_step);
m_lane_shift_timer = 0.0;
}
else if (min_left_distance_gap < 0.5*mp_car->width ())
{
// Move to the right if the car on the left is too close.
m_lane_shift = std::max (-1.0, m_lane_shift - shift_step);
m_lane_shift_timer = 0.0;
}
else if (m_lane_shift_timer > PASS_TIME)
{
// No danger of collision. Get back to the racing line.
m_passing = false;
if ((m_lane_shift > 0.0) && (min_right_distance_gap > 0.5*mp_car->width ()))
m_lane_shift = std::max (0.0, m_lane_shift - 0.5*shift_step);
else if ((m_lane_shift < 0.0) && (min_left_distance_gap > 0.5*mp_car->width ()))
m_lane_shift = std::min (0.0, m_lane_shift + 0.5*shift_step);
}
else if ((m_lane_shift > 0.2)
&& (std::abs (get_offline_distance ()) < 0.2*mp_car->width ()))
{
// Get back to the racing line if we're already close.
m_lane_shift_timer = 2.0*PASS_TIME;
}
if (crash_time < CRASH_TIME_LIMIT)
{
// Go offline to pass.
switch (pass_side)
{
case LEFT:
m_passing = true;
m_lane_shift = std::min (1.0, m_lane_shift + shift_step);
m_lane_shift_timer = 0.0;
break;
case RIGHT:
m_passing = true;
m_lane_shift = std::max (-1.0, m_lane_shift - shift_step);
m_lane_shift_timer = 0.0;
break;
default:
break;
}
}
m_gap [0] = min_forward_distance_gap;
m_gap [1] = min_left_distance_gap;
m_gap [2] = min_right_distance_gap;
m_gap [3] = std::min (lengths (follow_lengths), 0.5*m_speed);
// Calculate the speed factor here while we have access to the gap.
m_front_gap_control.set (std::min (lengths (follow_lengths), 0.5*m_speed));
m_speed_factor =
1.0 - clip (m_front_gap_control.propagate (min_forward_distance_gap, m_timestep),
0.0, 1.0);
}
Direction
Robot_Driver::relative_position (const Three_Vector& r1_track,
const Three_Vector& r2_track) const
{
const double corner = mp_car->width () / mp_car->length ();
Three_Vector delta = r2_track - r1_track;
const double slope = delta.y / delta.x;
if (((delta.x > mp_car->length ()) && (std::abs (delta.y) > mp_car->width ()))
|| (delta.x < -mp_car->length ()))
return NONE;
if (std::abs (slope) < corner)
return (delta.x > 0) ? FORWARD : NONE;
return (delta.y > 0) ? LEFT : RIGHT;
}
Three_Vector
Robot_Driver::find_gap (const Three_Vector& r1_track,
const Three_Vector& r2_track) const
{
return Three_Vector (m_road.distance (r2_track.x, r1_track.x) - mp_car->length (),
std::abs (r2_track.y - r1_track.y) - mp_car->width (),
0.0);
}
bool
Robot_Driver::maybe_passable (double along, size_t segment) const
{
return (pass_side (along, 0.5*m_speed, 10, segment) != NONE);
}
Direction
Robot_Driver::get_pass_side (double along, double delta_x, double delta_v,
size_t segment) const
{
// Return 'RIGHT' if we should try to pass on the right, 'LEFT' if we should
// try to pass on the left, 'NONE' if it's better not to try.
// Give up immediately if we're not closing at a significant rate. Returning
// here avoids a possible divide-by-zero below.
if (delta_v < 1e-6)
return NONE;
// We'll try to pass if the racing line stays on one side of the track far
// enough ahead for us to pull alongside the opponent's car at our current
// rate of closing, 'delta_v'.
//!! Look ahead by time, not distance to compensate for closing under braking.
//!! Account for deceleration?
double pass_distance = delta_x * m_speed / delta_v;
if (pass_distance > lengths (100))
return NONE;
return pass_side (along + pass_distance/2, pass_distance/20, 10, segment);
}
Direction
Robot_Driver::pass_side (double start, double delta, size_t n, size_t segment) const
{
// The logic here requires that *_SIDE are all single bits. Don't use the
// Direction enum.
//!! calls to get_block_side and from_center sometime increment the segment
//!! excessively.
int block = 0;
for (size_t i = 1; i <= n; i++)
block |= get_block_side (start + i*delta, segment);
if (block & LEFT_SIDE)
return (block & RIGHT_SIDE) ? NONE : RIGHT;
if (block & RIGHT_SIDE)
return LEFT;
return (m_racing_line.from_center (start, segment) > 0.0) ? RIGHT : LEFT;
}
Robot_Driver::Track_Side
Robot_Driver::get_block_side (double along, size_t segment) const
{
// The logic here requires that *_SIDE are all single bits. Don't use the
// Direction enum.
const double across = m_racing_line.from_center (along, segment);
if (across < -m_road.right_racing_line_width (along) + mp_car->width ())
return RIGHT_SIDE;
else if (across > m_road.left_racing_line_width (along) - mp_car->width ())
return LEFT_SIDE;
return NO_SIDE;
}
//** Draw
void
Robot_Driver::draw ()
{
{
double y = 94;
Two_D screen;
screen.text (20, y -= 2, "follow ", m_gap [3], "", 3);
screen.text (20, y -= 2, "crash ", m_gap [4], "s", 1);
screen.text (20, y -= 2, "pass ", m_passing, "", 0);
screen.text (20, y -= 2, "forward ", m_gap [0], "m", 3);
screen.text (20, y -= 2, "speed ", m_speed_factor, "", 3);
screen.text (20, y -= 2, "left ", m_gap [1], "m", 3);
screen.text (20, y -= 2, "right ", m_gap [2], "m", 3);
screen.text (20, y -= 2, "offline ", get_offline_distance (), "m", 3);
screen.text (20, y -= 2, "shift ", m_lane_shift, "", 3);
}
/// Optionally show the target and vector as green and red squares.
if (!m_show_steering_target)
return;
glLoadIdentity ();
glPointSize (8.0);
glBegin (GL_POINTS);
// Draw where the car is currently pointed.
const Three_Vector pointer (mp_car->center_position () + pointer_vector ());
glColor3d (0.0, 0.8, 0.0);
glVertex3d (pointer.x, pointer.y, pointer.z);
// Draw the point on the racing line.
const Three_Vector goal (mp_car->center_position () + target_vector ());
glColor3d (8.0, 0.0, 0.0);
glVertex3d (goal.x, goal.y, pointer.z);
glEnd ();
}
//-----------------------------------------------------------------------------
// The distance resolution of the braking speed calculation
static const double delta_x = 10.0;
// Braking is applied gradually. It reaches its maximum in this many meters.
static const double fade_in = 50.0;
Braking_Operation::Braking_Operation (const Road& road,
double deceleration,
double gravity,
const Robot_Racing_Line& line)
: m_start (0.0),
m_length (0.0),
m_is_braking (false),
m_road (road),
m_deceleration (deceleration),
m_gravity (gravity),
m_line (line)
{
}
Braking_Operation::~Braking_Operation ()
{
// Do the proper cleanup if we were deleted during a braking operation.
end ();
}
void
Braking_Operation::scale (double factor)
{
m_deceleration *= factor;
}
void
Braking_Operation::start (double start, double length)
{
// Use start distance and length instead of start and end to avoid issues with
// wrapping around the track.
m_start = start;
m_length = length;
m_is_braking = true;
}
void
Braking_Operation::end ()
{
m_is_braking = false;
}
bool
Braking_Operation::check_done_braking (double distance)
{
if (past_end (distance))
end ();
return !m_is_braking;
}
double
Braking_Operation::distance_from_start (double distance) const
{
if (distance >= m_start)
return (distance - m_start);
else // wrap around the track
return (distance + m_road.length () - m_start);
}
bool
Braking_Operation::past_end (double distance) const
{
return (distance_from_start (distance) > m_length);
}
double
Braking_Operation::deceleration (const Three_Vector& curvature,
double speed,
double drag,
double lift,
const Three_Vector& n_hat,
const Three_Vector& p_hat,
double mass,
double fraction) const
{
double c = curvature.magnitude ();
double mu = m_deceleration * fraction;
double v2 = speed * speed;
Three_Vector r_hat = curvature.unit ();
return (m_gravity*p_hat.z
- v2*drag/mass
+ mu * (m_gravity*n_hat.z - v2*(lift/mass + c*r_hat.dot (n_hat))));
}
Three_Vector
Braking_Operation::get_normal (double along) const
{
const Road_Segment* segment = m_road.segment_at (along);
double along_segment = along - segment->start_distance ();
return segment->normal (along_segment, 0.0);
}
double
Braking_Operation::delta_braking_speed (double speed,
double cornering_speed,
double along,
double lane_shift,
const Three_Vector& normal,
double drag,
double lift,
double mass,
double fraction) const
{
Three_Vector curvature = m_line.curvature (along, lane_shift);
Three_Vector p = m_line.tangent (along);
double a = deceleration (curvature, speed, drag, lift, normal, p, mass, fraction);
double a_par = a * (1.0 - speed / cornering_speed);
return a_par * delta_x / speed;
}
double
Braking_Operation::maximum_speed (double speed,
double distance,
double stretch,
double lane_shift,
double drag,
double lift,
double mass)
{
// Return the maximum safe speed under braking.
distance += stretch;
// See if we've past the end of a braking operation.
check_done_braking (distance);
// If we're in the middle of a braking operation, get the speed from the speed
// vs. braking curve.
if (is_braking ())
{
if (distance < m_speed_vs_distance [0].x)
distance += m_road.length ();
return m_speed_vs_distance.interpolate (distance);
}
// Calculate the car's speed as a function of distance if braking started now.
// If the projected speed exceeds the maximum cornering speed calculated from
// the racing line then braking should start now. When this happens, find the
// minimum cornering speed and calculate distance vs. speed backwards from
// there.
Two_Vector minimum_speed_vs_distance (0.0, speed);
// True if projected braking speed exceeds cornering speed anywhere.
bool too_fast = false;
// True if a minimum in the cornering speed was found in the distance range
// where projected braking speed exceeds cornering speed.
bool found_min = false;
double start_speed = speed;
// Look up to 1000 m ahead.
for (double d = 0.0; d < 1000.0; d += delta_x)
{
double along = wrap (distance + d, m_road.length ());
Three_Vector normal = get_normal (along);
double cornering_speed
= m_line.maximum_speed (along, lane_shift, lift, normal, mass);
// Apply braking gradually.
double braking_fraction = std::min (d / fade_in, 1.0);
double dv = delta_braking_speed (speed, cornering_speed, along, lane_shift,
normal, drag, lift, mass,
braking_fraction);
speed -= dv;
if (speed <= 0.0)
break;
if (speed >= cornering_speed)
{
// Already too fast, nothing we can do.
if (d == 0.0)
break;
too_fast = true;
}
else if (too_fast)
{
// We've gone from a region where braking speed is higher than
// cornering speed to one where it's lower. Keep going in case
// there's another curve up ahead that requires harder braking.
found_min = true;
too_fast = false;
}
if (too_fast && (cornering_speed < minimum_speed_vs_distance.y))
minimum_speed_vs_distance = Two_Vector (d, cornering_speed);
}
// No need to start braking yet.
if (!found_min)
return UNLIMITED_SPEED;
// Build the speed vs. distance curve by working backwards from the minimum
// speed. Start one interval beyond the end; end one interval before the
// beginning to ensure that the interpolations are good slightly beyond the
// endpoints.
too_fast = false;
std::vector <Two_Vector> points;
speed = minimum_speed_vs_distance.y;
for (double d = minimum_speed_vs_distance.x; d > -delta_x; d -= delta_x)
{
// Use un-wrapped distances so the interpolator's points are in order.
points.push_back (Two_Vector (distance + d, speed));
double along = wrap (distance + d, m_road.length ());
Three_Vector normal = get_normal (along);
double cornering_speed
= m_line.maximum_speed (along, lane_shift, lift, normal, mass);
double braking_fraction = std::min (d / fade_in, 1.0);
double dv = delta_braking_speed (speed, cornering_speed, along, lane_shift,
normal, drag, lift, mass, braking_fraction);
if (too_fast && (cornering_speed < minimum_speed_vs_distance.y))
minimum_speed_vs_distance = Two_Vector (d, cornering_speed);
if (speed >= cornering_speed)
{
if (!too_fast)
{
minimum_speed_vs_distance = Two_Vector (d, cornering_speed);
// Found an earlier curve that requires a lower speed.
too_fast = true;
}
}
else if (too_fast)
{
// Found the new minimum. Start over from there.
d = minimum_speed_vs_distance.x;
speed = minimum_speed_vs_distance.y;
points.clear ();
points.push_back (minimum_speed_vs_distance
+ Two_Vector (distance + delta_x, 0.0));
too_fast = false;
}
else
speed += dv;
}
// The interpolator requires ascending x-values. Reverse the points.
m_speed_vs_distance.clear ();
std::reverse (points.begin (), points.end ());
m_speed_vs_distance.load (points);
// Scale speed vs. distance so it matches the passed-in speed at the passed-in
// distance. This is usually a small adjustment, but can be significant when
// curves are closely spaced.
double delta_speed = start_speed - m_speed_vs_distance [0].y;
for (size_t i = 0; i < m_speed_vs_distance.size (); i++)
{
m_speed_vs_distance [i].x -=
stretch * (m_speed_vs_distance.size () - i)/m_speed_vs_distance.size ();
double fraction
= ((distance + minimum_speed_vs_distance.x - m_speed_vs_distance [i].x)
/ (distance + minimum_speed_vs_distance.x - m_speed_vs_distance [0].x));
m_speed_vs_distance [i].y += fraction * delta_speed;
}
// Mark the start of a braking operation.
start (distance, minimum_speed_vs_distance.x);
// No need to restrict speed yet. Next call will get it from the newly
// calculated speed vs. distance curve.
return UNLIMITED_SPEED;
}
//-----------------------------------------------------------------------------
Robot_Racing_Line::Robot_Racing_Line (const Road& road,
double lateral_acceleration,
double gravity)
: mp_road (&road),
m_lateral_acceleration (lateral_acceleration),
m_gravity (gravity)
{
}
Three_Vector
Robot_Racing_Line::curvature (double along, double lane_shift) const
{
return mp_road->racing_line ().curvature (along, lane_shift);
}
Three_Vector
Robot_Racing_Line::tangent (double along) const
{
return mp_road->racing_line ().tangent (along);
}
double
Robot_Racing_Line::maximum_speed (double distance,
double lane_shift,
double lift,
const Three_Vector& n_hat,
double mass) const
{
const Three_Vector curve = curvature (distance, lane_shift);
double c = curve.magnitude ();
double mu = m_lateral_acceleration;
Three_Vector r_hat = curve.unit ();
Three_Vector r_perp (-r_hat.y, r_hat.x, 0.0);
Three_Vector q_hat = n_hat.rotate (0.5 * pi * r_perp.unit ());
double upper = m_gravity*(q_hat.z + mu*n_hat.z);
double lower = c*(r_hat.dot(q_hat) + mu*r_hat.dot(n_hat)) + mu*lift/mass;
if (lower > 1e-9)
return std::sqrt (upper / lower);
else
return UNLIMITED_SPEED;
}
Three_Vector
Robot_Racing_Line::target (double along, double lead) const
{
return mp_road->racing_line ().position (along + lead);
}
double
Robot_Racing_Line::from_center (double along, size_t segment) const
{
return mp_road->track_coordinates (target (along, 0.0), segment, true).y;
}