Skip to content

Commit

Permalink
added tutorial driver
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJonas committed Nov 4, 2017
1 parent 2d3df90 commit 6e1d87d
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 0 deletions.
Binary file added src/drivers/pid_driver/155-DTM.rgb
Binary file not shown.
33 changes: 33 additions & 0 deletions src/drivers/pid_driver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##############################################################################
#
# file : Makefile
# created : Sat Nov 4 20:28:35 CET 2017
# copyright : (C) 2002 Jonas Natzer
#
##############################################################################
#
# 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.
#
##############################################################################

# I need those environment variables
# export TORCS_BASE=~/torcs-1.3.7
# export MAKE_DEFAULT=${TORCS_BASE}/Make-default.mk

ROBOT = pid_driver
MODULE = ${ROBOT}.so
MODULEDIR = drivers/${ROBOT}
SOURCES = ${ROBOT}.cpp

SHIPDIR = drivers/${ROBOT}
SHIP = ${ROBOT}.xml 155-DTM.rgb logo.rgb
SHIPSUBDIRS =

PKGSUBDIRS = ${SHIPSUBDIRS}
src-robots-pid_driver_PKGFILES = $(shell find * -maxdepth 0 -type f -print)
src-robots-pid_driver_PKGDIR = ${PACKAGE}-${VERSION}/$(subst ${TORCS_BASE},,$(shell pwd))

include ${MAKE_DEFAULT}
Binary file added src/drivers/pid_driver/logo.rgb
Binary file not shown.
124 changes: 124 additions & 0 deletions src/drivers/pid_driver/pid_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/***************************************************************************
file : pid_driver.cpp
created : Sat Nov 4 20:28:35 CET 2017
copyright : (C) 2002 Jonas Natzer
***************************************************************************/

/***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifdef _WIN32
#include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <tgf.h>
#include <track.h>
#include <car.h>
#include <raceman.h>
#include <robottools.h>
#include <robot.h>

static tTrack *curTrack;

static void initTrack(int index, tTrack* track, void *carHandle, void **carParmHandle, tSituation *s);
static void newrace(int index, tCarElt* car, tSituation *s);
static void drive(int index, tCarElt* car, tSituation *s);
static void endrace(int index, tCarElt *car, tSituation *s);
static void shutdown(int index);
static int InitFuncPt(int index, void *pt);


/*
* Module entry point
*/
extern "C" int
pid_driver(tModInfo *modInfo)
{
memset(modInfo, 0, 10*sizeof(tModInfo));

modInfo->name = strdup("pid_driver"); /* name of the module (short) */
modInfo->desc = strdup(""); /* description of the module (can be long) */
modInfo->fctInit = InitFuncPt; /* init function */
modInfo->gfId = ROB_IDENT; /* supported framework version */
modInfo->index = 1;

return 0;
}

/* Module interface initialization. */
static int
InitFuncPt(int index, void *pt)
{
tRobotItf *itf = (tRobotItf *)pt;

itf->rbNewTrack = initTrack; /* Give the robot the track view called */
/* for every track change or new race */
itf->rbNewRace = newrace; /* Start a new race */
itf->rbDrive = drive; /* Drive during race */
itf->rbPitCmd = NULL;
itf->rbEndRace = endrace; /* End of the current race */
itf->rbShutdown = shutdown; /* Called before the module is unloaded */
itf->index = index; /* Index used if multiple interfaces */
return 0;
}

/* Called for every track change or new race. */
static void
initTrack(int index, tTrack* track, void *carHandle, void **carParmHandle, tSituation *s)
{
curTrack = track;
*carParmHandle = NULL;
}

/* Start a new race. */
static void
newrace(int index, tCarElt* car, tSituation *s)
{
}

/* Drive during race. */
static void
drive(int index, tCarElt* car, tSituation *s)
{
memset((void *)&car->ctrl, 0, sizeof(tCarCtrl));
car->ctrl.brakeCmd = 1.0; /* all brakes on ... */

float angle;
const float SC = 1.0;

angle = RtTrackSideTgAngleL(&(car->_trkPos)) - car->_yaw;
NORM_PI_PI(angle); // put the angle back in the range from -PI to PI
angle -= SC*car->_trkPos.toMiddle/car->_trkPos.seg->width;

// set up the values to return
car->ctrl.steer = angle / car->_steerLock;
car->ctrl.gear = 1; // first gear
car->ctrl.accelCmd = 0.3; // 30% accelerator pedal
car->ctrl.brakeCmd = 0.0; // no brakes
}

/* End of the current race */
static void
endrace(int index, tCarElt *car, tSituation *s)
{
}

/* Called before the module is unloaded */
static void
shutdown(int index)
{
}

19 changes: 19 additions & 0 deletions src/drivers/pid_driver/pid_driver.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
;
; file : pid_driver.def
; created : Sat Nov 4 20:28:35 CET 2017
; copyright : (C) 2002 Jonas Natzer
;
;
; 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.
;

LIBRARY pid_driver

SECTIONS .data READ WRITE

EXPORTS
pid_driver

Loading

0 comments on commit 6e1d87d

Please sign in to comment.