1 // -*- Mode: C++ -*-
2 /***************************************************************************
3 
4     file                 : driver.h
5     created              : Thu Dec 20 01:20:19 CET 2002
6     copyright            : (C) 2002-2004 Bernhard Wymann
7     email                : berniw@bluewin.ch
8     version              : $Id: driver.h,v 1.11.2.2 2011/12/31 02:51:47 berniw Exp $
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  *                                                                         *
14  *   This program is free software; you can redistribute it and/or modify  *
15  *   it under the terms of the GNU General Public License as published by  *
16  *   the Free Software Foundation; either version 2 of the License, or     *
17  *   (at your option) any later version.                                   *
18  *                                                                         *
19  ***************************************************************************/
20 
21 #ifndef _DRIVER_H_
22 #define _DRIVER_H_
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 
28 #include <tgf.h>
29 #include <track.h>
30 #include <car.h>
31 #include <raceman.h>
32 #include <robottools.h>
33 #include <robot.h>
34 
35 #include "linalg.h"
36 #include "opponent.h"
37 #include "pit.h"
38 #include "learn.h"
39 #include "strategy.h"
40 #include "cardata.h"
41 #include "geometry.h"
42 
43 #define OLETHROS_SECT_PRIV "olethros private"
44 #define OLETHROS_ATT_FUELPERLAP "fuel per lap"
45 #define OLETHROS_ATT_FUELCONSUMPTION "fuel consumption"
46 #define OLETHROS_ATT_MUFACTOR "mufactor"
47 
48 #ifdef USE_OLETHROS_NAMESPACE
49 namespace olethros
50 {
51 #endif
52 
53 class Opponents;
54 class Opponent;
55 class Pit;
56 class AbstractStrategy;
57 
58 /**
59    \brief the Driver class
60 */
61 class Driver {
62 public:
63 	Driver(int index);
64 	~Driver();
65 
66 	/// Callback functions called from TORCS.
67 	void initTrack(tTrack* t, void *carHandle, void **carParmHandle, tSituation *s);
68 	void newRace(tCarElt* car, tSituation *s);
69 	void drive(tSituation *s);
70 	int pitCommand(tSituation *s);
71 	void endRace(tSituation *s);
72 
getCarPtr()73 	tCarElt *getCarPtr() { return car; }
getTrackPtr()74 	tTrack *getTrackPtr() { return track; }
75 	float* max_speed_list;
getSpeed()76 	float getSpeed() { return mycardata->getSpeedInTrackDirection(); /*speed;*/ }
77 
78 protected:
79 	// Utility functions.
80 	bool isStuck();
81 	void update(tSituation *s);
82 	void prepareTrack();
83 	float getAllowedSpeed(tTrackSeg *segment);
84 	float getAccel();
85 	float getDistToSegEnd();
86 	float getBrake();
87 	int getGear();
88 	float EstimateTorque (float rpm);
89 	float getSteer();
90 	float getClutch();
91 	v2d getTargetPoint();
92 	float getOffset();
93 	float brakedist(float allowedspeed, float mu);
94 
95 	float filterOverlap(float accel);
96 	float filterBColl(float brake);
97 	float filterABS(float brake);
98 	float filterBPit(float brake);
99 	float filterBrakeSpeed(float brake);
100 	float filterTurnSpeed(float brake);
101 
102 	float filterTCL(float accel);
103 	float filterTrk(tSituation* s, float accel);
104 	float filterAPit(float accel);
105 
106 	float filterSColl(float steer);
107 
108 	float filterTCL_RWD();
109 	float filterTCL_FWD();
110 	float filterTCL_4WD();
111 	void initTCLfilter();
112 
113 	void initCa();
114 	void initCw();
115 	void initTireMu();
116 
117 	void computeRadius(float *radius);
118 	int isAlone();
119 
120 	float EstimateRadius2 (tTrackSeg* seg);
121 	float EstimateRadius (tTrackSeg* seg, tTrackSeg* prev_seg, tTrackSeg* next_seg);
122 	float FindCurveTarget(tTrackSeg* seg, Vector* C, float rmax);
123 	float FindStraightTarget(tTrackSeg* curve, tTrackSeg* seg, Vector* C, float rmax, bool& flag);
124 	// Misc functions
125 	void AdjustRadi(tTrackSeg* cs, tTrackSeg* ce, float* radi);
126 	void ShowPaths();
127 
128 	float current_allowed_speed;
129 	// Per robot global data.
130 	int race_type;
131 	int stuck;
132 	float speedangle;		///< the angle of the speed vector relative to trackangle, > 0.0 points to right.
133 	float mass;				///< Mass of car + fuel.
134 	float myoffset;			///< Offset to the track middle.
135 	tCarElt *car;			///< Pointer to tCarElt struct.
136 
137 	Opponents *opponents;	///< The container for opponents.
138 	Opponent *opponent;		///< The array of opponents.
139 
140 	Pit *pit;						///< Pointer to the pit instance.
141 	float pit_exit_timer;
142 	AbstractStrategy *strategy;		///< Pit stop strategy.
143 
144 	static Cardata *cardata;		///< Data about all cars shared by all instances.
145 	SingleCardata *mycardata;		///< Pointer to "global" data about my car.
146 	static double currentsimtime;	///< Store time to avoid useless updates.
147 
148 	float currentspeedsqr;	///< Square of the current speed_x.
149 	float clutchtime;		///< Clutch timer.
150 	float oldlookahead;		///< Lookahead for steering in the previous step.
151 
152 	float speed_factor; ///< speed factor to use.
153 
154 	float *seg_alpha; ///< targets for segments
155 	float *seg_alpha_new; ///< new targets for segments
156 	float *radius; ///< segment radi
157 	float *ideal_radius; ///< ideal radius (according to circle fit)
158 	SegLearn *learn; ///< handle to learning module
159 	int alone; ///< whether we are alone
160 	bool overtaking; ///< are we overtaking?
161 	float prev_steer; ///< for steering filter
162 	float prev_toleft; ///< previous left margin
163 	float prev_toright; ///< previous right margin
164 	float u_toleft; ///< speed to left side
165 	float u_toright; ///< speed to right side
166 	float dt; ///< delta time
167 	float my_pitch;
168 	float TCL_status; ///< traction control
169 	float alone_count;
170 
171 	// Data that should stay constant after first initialization.
172 	int MAX_UNSTUCK_COUNT;
173 	int INDEX;
174 	float CARMASS;		///< Mass of the car only [kg].
175 	float CA;			///< Aerodynamic downforce coefficient.
176 	float CW;			///< Aerodynamic drag coefficient.
177 	float TIREMU;		///< Friction coefficient of tires.
178 	float (Driver::*GET_DRIVEN_WHEEL_SPEED)();
179 	float OVERTAKE_OFFSET_INC;		///< [m/timestep]
180 	float MU_FACTOR;				///< [-]
181 
182 	// Class constants.
183 	static const float MAX_UNSTUCK_ANGLE;
184 	static const float UNSTUCK_TIME_LIMIT;
185 	static const float MAX_UNSTUCK_SPEED;
186 	static const float MIN_UNSTUCK_DIST;
187 	static const float G;
188 	static const float FULL_ACCEL_MARGIN;
189 	static const float SHIFT;
190 	static const float SHIFT_MARGIN;
191 	static const float ABS_SLIP;
192 	static const float ABS_RANGE ;
193 	static const float ABS_MINSPEED;
194 	static const float TCL_SLIP;
195 	static const float LOOKAHEAD_CONST;
196 	static const float LOOKAHEAD_FACTOR;
197 	static const float WIDTHDIV;
198 	static const float SIDECOLL_MARGIN;
199 	static const float BORDER_OVERTAKE_MARGIN;
200 	static const float OVERTAKE_OFFSET_SPEED;
201 	static const float OVERTAKE_TIME;
202 	static const float PIT_LOOKAHEAD;
203 	static const float PIT_BRAKE_AHEAD;
204 	static const float PIT_MU;
205 	static const float MAX_SPEED;
206 	static const float TCL_RANGE;
207 	static const float MAX_FUEL_PER_METER;
208 	static const float CLUTCH_SPEED;
209 	static const float CENTERDIV;
210 	static const float DISTCUTOFF;
211 	static const float MAX_INC_FACTOR;
212 	static const float CATCH_FACTOR;
213 	static const float CLUTCH_FULL_MAX_TIME;
214 	static const float USE_LEARNED_OFFSET_RANGE;
215 	static const float ACCELERATOR_LETGO_TIME;
216 	static const float MIN_BRAKE_FOLLOW_DISTANCE;
217 	static const float MAX_BRAKE_FOLLOW_DISTANCE;
218 	static const float STEER_DIRECTION_GAIN;
219 	static const float STEER_PREDICT_GAIN;
220 	static const float STEER_DRIFT_GAIN;
221 	static const float STEER_AVOIDANCE_GAIN;
222 	static const float STEER_EMERGENCY_GAIN;
223 	static const float FILTER_STEER_FEEDBACK;
224 	static const float FILTER_PREDICT_FEEDBACK;
225 	static const float FILTER_TARGET_FEEDBACK;
226 	static const bool USE_NEW_ALPHA;
227 	// Track variables.
228 	tTrack* track;
229 };
230 
231 #ifdef USE_OLETHROS_NAMESPACE
232 }
233 #endif
234 
235 #endif // _DRIVER_H_
236 
237