Skip to content

Commit

Permalink
All working!
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJonas committed Nov 14, 2017
1 parent 948f3c1 commit 20a65ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
1 change: 0 additions & 1 deletion setup_linux.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#! /bin/bash

[ -z "$1" ] && exit 1
[ ! -d "$1" ] && exit 1

mkdir -p $1/drivers/human 2>/dev/null
if [ ! -e $1/drivers/human/car.xml ] || [ drivers/human/car.xml -nt $1/drivers/human/car.xml ]
Expand Down
41 changes: 25 additions & 16 deletions src/drivers/pid_driver/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void Driver::newRace(tCarElt *car, tSituation *s) {
this->car = car;
CARMASS = GfParmGetNum(car->_carHandle, SECT_CAR, PRM_MASS, NULL, 1000.0);
initCa();
initCw();

MAX_UNSTUCK_COUNT = int(UNSTUCK_TIME_LIMIT / RCM_MAX_DT_ROBOTS);
stuck = 0;
Expand All @@ -32,12 +33,12 @@ void Driver::newRace(tCarElt *car, tSituation *s) {
}

/* Drive during race. */
void Driver::drive(tSituation *s) {
update(s);
void Driver::drive(tCarElt *car, tSituation *s) {
update(car, s);

memset(&car->ctrl, 0, sizeof(tCarCtrl));

if (isStuck()) {
if (isStuck(car)) {
car->ctrl.steer = -angle / car->_steerLock;
car->ctrl.gear = -1; // reverse gear
car->ctrl.accelCmd = 0.5; // 50% accelerator pedal
Expand All @@ -46,18 +47,18 @@ void Driver::drive(tSituation *s) {
float steerangle = angle - car->_trkPos.toMiddle / car->_trkPos.seg->width;

car->ctrl.steer = steerangle / car->_steerLock;
car->ctrl.gear = getGear();
car->ctrl.brakeCmd = getBrake();
car->ctrl.gear = getGear(car);
car->ctrl.brakeCmd = getBrake(car);
if (car->ctrl.brakeCmd == 0.0) {
car->ctrl.accelCmd = getAccel();
car->ctrl.accelCmd = getAccel(car);
} else {
car->ctrl.accelCmd = 0.0;
}
}
}

/* Update my private data every timestep */
void Driver::update(tSituation *s) {
void Driver::update(tCarElt *car, tSituation *s) {
trackangle = RtTrackSideTgAngleL(&(car->_trkPos));
angle = trackangle - car->_yaw;
NORM_PI_PI(angle);
Expand All @@ -66,7 +67,7 @@ void Driver::update(tSituation *s) {
}

/* Compute the length to the end of the segment */
float Driver::getDistToSegEnd() {
float Driver::getDistToSegEnd(tCarElt *car) {
if (car->_trkPos.seg->type == TR_STR) {
return car->_trkPos.seg->length - car->_trkPos.toStart;
} else {
Expand All @@ -75,7 +76,7 @@ float Driver::getDistToSegEnd() {
}

/* Compute gear */
int Driver::getGear() {
int Driver::getGear(tCarElt *car) {
if (car->_gear <= 0)
return 1;
float gr_up = car->_gearRatio[car->_gear + car->_gearOffset];
Expand All @@ -96,7 +97,7 @@ int Driver::getGear() {
}

/* Compute fitting acceleration */
float Driver::getAccel() {
float Driver::getAccel(tCarElt *car) {
float allowedspeed = getAllowedSpeed(car->_trkPos.seg);
float gr = car->_gearRatio[car->_gear + car->_gearOffset];
float rm = car->_enginerpmRedLine;
Expand All @@ -118,17 +119,17 @@ float Driver::getAllowedSpeed(tTrackSeg *segment) {
}

/* Set pitstop commands. */
int Driver::pitCommand(tSituation *s) { return ROB_PIT_IM; /* return immediately */ }
int Driver::pitCommand(tCarElt *car, tSituation *s) { return ROB_PIT_IM; /* return immediately */ }

/* End of the current race */
void Driver::endRace(tSituation *s) {}
void Driver::endRace(tCarElt *car, tSituation *s) {}

float Driver::getBrake() {
float Driver::getBrake(tCarElt *car) {
tTrackSeg *segptr = car->_trkPos.seg;
float currentspeedsqr = car->_speed_x * car->_speed_x;
float mu = segptr->surface->kFriction;
float maxlookaheaddist = currentspeedsqr / (2.0 * mu * G);
float lookaheaddist = getDistToSegEnd();
float lookaheaddist = getDistToSegEnd(car);
float allowedspeed = getAllowedSpeed(segptr);
if (allowedspeed < car->_speed_x)
return 1.0;
Expand All @@ -137,7 +138,8 @@ float Driver::getBrake() {
allowedspeed = getAllowedSpeed(segptr);
if (allowedspeed < car->_speed_x) {
float allowedspeedsqr = allowedspeed * allowedspeed;
float brakedist = (currentspeedsqr - allowedspeedsqr) / (2.0 * mu * G);
float brakedist = mass * (currentspeedsqr - allowedspeedsqr) /
(2.0 * (mu * G * mass + allowedspeedsqr * (CA * mu + CW)));
if (brakedist > lookaheaddist) {
return 1.0;
}
Expand All @@ -149,7 +151,7 @@ float Driver::getBrake() {
}

/* Check if I'm stuck */
bool Driver::isStuck() {
bool Driver::isStuck(tCarElt *car) {
if (fabs(angle) > MAX_UNSTUCK_ANGLE && car->_speed_x < MAX_UNSTUCK_SPEED &&
fabs(car->_trkPos.toMiddle) > MIN_UNSTUCK_DIST) {
if (stuck > MAX_UNSTUCK_COUNT && car->_trkPos.toMiddle * angle < 0.0) {
Expand All @@ -164,6 +166,13 @@ bool Driver::isStuck() {
}
}

/* Compute aerodynamic drag coefficient CW */
void Driver::initCw() {
float cx = GfParmGetNum(car->_carHandle, SECT_AERODYNAMICS, PRM_CX, (char *)NULL, 0.0);
float frontarea = GfParmGetNum(car->_carHandle, SECT_AERODYNAMICS, PRM_FRNTAREA, (char *)NULL, 0.0);
CW = 0.645 * cx * frontarea;
}

/* Compute aerodynamic downforce coefficient CA */
void Driver::initCa() {
char *WheelSect[4] = {SECT_FRNTRGTWHEEL, SECT_FRNTLFTWHEEL, SECT_REARRGTWHEEL, SECT_REARLFTWHEEL};
Expand Down
20 changes: 11 additions & 9 deletions src/drivers/pid_driver/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,28 @@ class Driver {
/* callback functions called from TORCS */
void initTrack(tTrack *t, void *carHandle, void **carParmHandle, tSituation *s);
void newRace(tCarElt *car, tSituation *s);
void drive(tSituation *s);
int pitCommand(tSituation *s);
void endRace(tSituation *s);
void drive(tCarElt *car, tSituation *s);
int pitCommand(tCarElt *car, tSituation *s);
void endRace(tCarElt *car, tSituation *s);

tCarElt *getCarPtr() { return car; }
tTrack *getTrackPtr() { return track; }
float getSpeed() { return speed; }

float getAllowedSpeed(tTrackSeg *segment);
float getDistToSegEnd();
float getAccel();
float getDistToSegEnd(tCarElt *car);
float getAccel(tCarElt *car);

float getBrake();
int getGear();
float getBrake(tCarElt *car);
int getGear(tCarElt *car);

void initCa();
void initCw();

private:
/* utility functions */
bool isStuck();
void update(tSituation *s);
bool isStuck(tCarElt *car);
void update(tCarElt *car, tSituation *s);

/* per robot global data */
int stuck;
Expand Down Expand Up @@ -77,6 +78,7 @@ class Driver {
float mass; /* mass of car + fuel */
float CARMASS; /* mass of the car only */
float CA; /* aerodynamic downforce coefficient */
float CW; /* aerodynamic drag coefficient */

float speed; /* speed in track direction */
};
Expand Down

0 comments on commit 20a65ce

Please sign in to comment.