-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperBaseDriver.cpp
More file actions
158 lines (145 loc) · 4.63 KB
/
WrapperBaseDriver.cpp
File metadata and controls
158 lines (145 loc) · 4.63 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
/***************************************************************************
file : WrapperBaseDriver.cpp
copyright : (C) 2007 Daniele Loiacono
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "WrapperBaseDriver.h"
string
WrapperBaseDriver::drive(string sensors)
{
CarState cs(sensors);
print_debug_in(cs);
CarControl cc = wDrive(cs);
validate_gear(cc, cs);
print_debug_out(cc);
return cc.toString();
}
void WrapperBaseDriver::validate_gear(CarControl& cc, const CarState& cs) const
{
static int t = 0;
if(cc.getGear() == -1 || cs.getGear() == -1)
return ;
int gear = cs.getGear();
double clutch = 0.00;
t+= cs.getRpm() > 9000 && cs.getCurLapTime() > 0;
if(((gear == 1 && last_clutch != 0) || gear == 0 ) && t < 20)
{
clutch = clutchMax;
clutching(cs, clutch);
gear = 1;
}
else if(cs.getRpm() > 9000 && gear < max_gear)
gear++;
else if(cs.getRpm() < 6000 - (gear == 2)*3000 - (gear == 3)*2000 && gear > 1)
gear--;
cc.setGear(gear);
cc.setClutch(clutch);
if(cs.getCurLapTime() < 0)
last_clutch = 1;
else
last_clutch = clutch;
}
void
WrapperBaseDriver::print_debug_in(const CarState& cs) const
{
if(debug_mode & IN_DAMAGE)
cerr<<"DMG: "<<cs.getDamage()<<"\n";
if(debug_mode & IN_GEAR)
cerr<<"GEAR: "<<cs.getGear()<<"\n";
if(debug_mode & IN_RPM)
cerr<<"RPM: "<<cs.getRpm()<<"\n";
if(debug_mode & IN_FOCUS)
{
cerr<<"Focus sensor: ";
for(int i = 0; i < FOCUS_SENSORS_NUM; i++)
cerr<<cs.getFocus(i)<<" ";
cerr<<"\n";
}
if(debug_mode & IN_TRACK)
{
cerr<<"Track sensor ("<<TRACK_SENSORS_NUM<<"):";
for(int i = 0; i < TRACK_SENSORS_NUM; i+= TRACK_SENSORS_NUM/4)
cerr<<cs.getTrack(i)<<" ";
cerr<<"\n";
}
if(debug_mode & IN_OPPONENTS)
{
cerr<<"Opponents sensor: ";
for(int i = 0; i < OPPONENTS_SENSORS_NUM; i++)
cerr<<cs.getOpponents(i)<<" ";
cerr<<"\n";
}
}
// Initialization of the desired angles for the rangefinders
void WrapperBaseDriver::init2(float *angles)
{
cerr<<"otwieranie\n";
ifstream dane("dane.txt");
cerr<<"Inicjalizowanie bazy osobników\n";
int length;
while(dane>>length)
{
cerr<<"Osobnik "<<length<<": ";
vector<BaseDriver::Action> act;
int tmp;
for(int i = 0; i < length; i++)
{
dane>>tmp;
act.push_back(static_cast<BaseDriver::Action>(tmp));
}
}
}
void
WrapperBaseDriver::print_debug_out(const CarControl& cc) const
{
if(debug_mode & OUT_ACCEL)
cerr<<"ACC: "<<cc.getAccel()<<"\n";
if(debug_mode & OUT_BRAKE)
cerr<<"BREAK: "<<cc.getBrake()<<"\n";
if(debug_mode & OUT_STEER)
cerr<<"Steer: "<<cc.getSteer()<<"\n";
if(debug_mode & OUT_CLUTCH)
cerr<<"Clutch: "<<cc.getClutch()<<" (0 - 100% trakcji, 1 - brak trakcji)\n";
}
void WrapperBaseDriver::clutching(const CarState &cs, double &clutch) const {
double maxClutch = clutchMax;
// Check if the current situation is the race start
if (cs.getDistRaced() < clutchDeltaRaced)
clutch = maxClutch;
// Adjust the current value of the clutch
if(clutch > 0) {
double delta = clutchDelta;
if (cs.getGear() < 2) {
// Apply a stronger clutch output when the gear is one and the race is just started
delta /= 2;
maxClutch *= clutchMaxModifier;
if (cs.getCurLapTime() < clutchMaxTime)
clutch = maxClutch;
}
// check clutch is not bigger than maximum values
clutch = min(maxClutch,double(clutch));
// if clutch is not at max value decrease it quite quickly
if (clutch != maxClutch) {
clutch -= delta;
clutch = max(0.0,double(clutch));
}
// if clutch is at max value decrease it very slowly
else
clutch -= clutchDec;
}
}
const float WrapperBaseDriver::clutchMax=0.5;
const float WrapperBaseDriver::clutchDelta=0.05;
const float WrapperBaseDriver::clutchRange=0.82;
const float WrapperBaseDriver::clutchDeltaTime=0.02;
const float WrapperBaseDriver::clutchDeltaRaced=10;
const float WrapperBaseDriver::clutchDec=0.04;
const float WrapperBaseDriver::clutchMaxModifier=1.3;
const float WrapperBaseDriver::clutchMaxTime=1.5;