Front Collision Avoidance
Author: Bernhard Wymann
Keywords: torcs, berniw, bernhard wymann, collision, avoidance, overtaking, opponent, collecting data

# 7.3 Front Collision Avoidance ### Introduction In this section we start avoiding collisions. With front collisions I mean the class of collisions where we hit with our front the back of an opponent. We can avoid this collisions with braking, so we will add an additional filter to the brake value. The reason why I handle front and side collisions different is that we need different commands. ### Changes in driver.h We include opponent.h and define prototypes for the classes Opponent and Opponents.
    #include "opponent.h"
    class Opponents;
    class Opponent;
We add a destructor to the public section, the method prototype for the filter and the variables in the private section.
            ~Driver();
            float filterBColl(float brake);
            Opponents *opponents;
            Opponent *opponent;
### Implementation in driver.cpp To release the opponents instance we add the destructor.
    Driver::~Driver()
    {
        delete opponents;
    }
We create the instance of Opponents in Driver::newRace() and get a pointer to the array of Opponent objects.
        /* initialize the list of opponents */
        opponents = new Opponents(s, this);
        opponent = opponents->getOpponentPtr();
Now call the filter as usual in the Driver::drive() method. Change the line
            car->ctrl.brakeCmd = filterABS(getBrake());
to
            car->ctrl.brakeCmd = filterABS(filterBColl(getBrake()));
Here we update the speed of the drivers car in track direction and the opponents data. Put this code at the end of Driver::update().
        speed = Opponent::getSpeed(car);
        opponents->update(s, this);
Finally we implement the brake filter. In a nutshell it iterates through the opponents and checks for every opponent which has been tagged with OPP_COLL if we need to brake to avoid a collision. If yes we brake full and leave the method.
    /* Brake filter for collision avoidance */
    float Driver::filterBColl(float brake)
    {
        float currentspeedsqr = car->_speed_x*car->_speed_x;
        float mu = car->_trkPos.seg->surface->kFriction;
        float cm = mu*G*mass;
        float ca = CA*mu + CW;
        int i;

        for (i = 0; i < opponents->getNOpponents(); i++) {
We set up some variables and then we enter the loop to iterate through all opponents. The presented solution is not optimal. You could replace the call to getDistance() with getChatchDist(), but additional code is necessary to make it work. The computation of the brakedistance should be familiar from previous chapters.
            if (opponent[i].getState() & OPP_COLL) {
                float allowedspeedsqr = opponent[i].getSpeed();
                allowedspeedsqr *= allowedspeedsqr;
                float brakedist = mass*(currentspeedsqr - allowedspeedsqr) /
                                  (2.0*(cm + allowedspeedsqr*ca));
                if (brakedist > opponent[i].getDistance()) {
                    return 1.0;
                }
            }
        }
        return brake;
    }
### Testdrive To check if the code works run a race with bt 3, bt 1 and bt 2 on wheel 1. The cars should drive behind each other without collisions. ### Downloads In case you got lost, you can download my robot for TORCS 1.2.0 or later. ### Summary You can run your robot "family" without collisions. You know how it works.