-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleTrack.h
247 lines (174 loc) · 7.72 KB
/
DoubleTrack.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef _DOUBLETRACK_H
#define _DOUBLETRACK_H
#include <iostream>
#include <math.h>
typedef struct DoubleTrack_PARAM_struct
{
float m_fMassKg;
float m_fInertiaMomentKgM2;
float m_fWheelbaseM;
float m_fFrontWheelbaseM;
float m_fRearWheelbaseM;
float m_fFrontTrackWidthM;
float m_fRearTrackWidthM;
float m_fSteeringRatio;
float m_fFrontCorneringStiffnessN;
float m_fRearCorneringStiffnessN;
float m_fWheelRadiusM;
float m_fBrakeBalance;
float m_fMaxEngineTorqueNm;
float m_fGearRatio;
float m_fTransmissionEfficiency;
/* Parameters of the DoubleTrack */
float m_fToeRad;
float m_fCamberRad;
/* Magic Formula */
float m_fMagicFormulaD;
float m_fMagicFormulaC;
float m_fMagicFormulaB;
float m_fMagicFormulaE;
/* Aeroodynamic Parameters */
float m_fAirDensityKgm3;
float m_fFrontSurfaceM2;
float m_fDragCoeffCx;
float m_fDownForceCoeffCz1;
float m_fDownForceCoeffCz2;
float m_fTimeStepS;
} DoubleTrack_PARAM_t;
struct DoubleTrack_STEER_struct
{
float m_fDragForceXaN;
float m_fDownForceZ1N;
float m_fDownForceZ2N;
} DoubleTrack_STEER_t;
struct DoubleTrack_AERO_struct
{
float m_fDragForceXaN;
float m_fDownForceZ1N;
float m_fDownForceZ2N;
} DoubleTrack_AERO_t;
struct DoubleTrack_LOAD_struct
{
float m_fLongLoadTransferN;
float m_fFrontLateralLoadTransferN;
float m_fRearLateralLoadTransferN;
} DoubleTrack_LOAD_t;
struct DoubleTrack_VERTFORCES_struct
{
float m_fFrontLeftVertForceZ11;
float m_fFrontRightVertForceZ12;
float m_fRearLeftVertForceZ21;
float m_fRearRightVertForceZ22;
} DoubleTrack_VERTFORCES_t;
/* ------------- Definition of class DoubleTrack ------------- */
class DoubleTrack : public RacingCar {
private:
DoubleTrack_PARAM_t CarParam;
public:
/* Class constructor */
DoubleTrack(float p_fTimeStepS) : RacingCar(p_fTimeStepS) {}
/* Function to iniziale the object and set all the parameters of the car */
bool SetParameters(DoubleTrack_PARAM_t *CarInit)
{
if (CarInit->m_fMassKg <= 0 || CarInit->m_fWheelbaseM <= 0 ||
CarInit->m_fInertiaMomentKgM2 <= 0 || CarInit->m_fWheelbaseM ||
CarInit->m_fFrontWheelbaseM <= 0 || CarInit->m_fRearWheelbaseM <= 0 ||
CarInit->m_fFrontTrackWidthM <= 0 || CarInit->m_fRearTrackWidthM <= 0)
{
return false;
}
else
{
this->CarParam.m_fMassKg = CarInit->m_fMassKg;
this->CarParam.m_fInertiaMomentKgM2 = CarInit->m_fInertiaMomentKgM2;
this->CarParam.m_fWheelbaseM = CarInit->m_fWheelbaseM;
this->CarParam.m_fFrontWheelbaseM = CarInit->m_fFrontWheelbaseM;
this->CarParam.m_fRearWheelbaseM = CarInit->m_fRearWheelbaseM;
this->CarParam.m_fFrontTrackWidthM = CarInit->m_fFrontTrackWidthM;
this->CarParam.m_fRearTrackWidthM = CarInit->m_fRearTrackWidthM;
this->CarParam.m_fSteeringRatio = CarInit->m_fSteeringRatio;
this->CarParam.m_fFrontCorneringStiffnessN = CarInit->m_fFrontCorneringStiffnessN;
this->CarParam.m_fRearCorneringStiffnessN = CarInit->m_fRearCorneringStiffnessN;
this->CarParam.m_fBrakeBalance = CarInit->m_fBrakeBalance;
this->CarParam.m_fTimeStepS = CarInit->m_fTimeStepS;
this->CarParam.m_fToeRad = CarInit->m_fToeRad;
this->CarParam.m_fCamberRad = CarInit->m_fCamberRad;
this->CarParam.m_fMagicFormulaD = CarInit->m_fMagicFormulaD;
this->CarParam.m_fMagicFormulaC = CarInit->m_fMagicFormulaC;
this->CarParam.m_fMagicFormulaB = CarInit->m_fMagicFormulaB;
this->CarParam.m_fMagicFormulaE = CarInit->m_fMagicFormulaE;
this->CarParam.m_fAirDensityKgm3 = CarInit->m_fAirDensityKgm3;
this->CarParam.m_fFrontSurfaceM2 = CarInit->m_fFrontSurfaceM2;
this->CarParam.m_fDragCoeffCx = CarInit->m_fDragCoeffCx;
this->CarParam.m_fDownForceCoeffCz1 = CarInit->m_fDownForceCoeffCz1;
this->CarParam.m_fDownForceCoeffCz2 = CarInit->m_fDownForceCoeffCz2;
}
return true;
}
/*
void GetSteerAngles(float p_fDeltaRad, DoubleTrack_STEER_t *SteerAngles)
{
}
void GetAerodynamics(float p_fLongVelocity, DoubleTrack_AERO_t *AeroForces)
{
}
float MagicFormula(float p_fSlip, float p_fVerticalForceN)
{
}
float GetLoadTransfers()
{
}
void GetVerticalForces(float p_fLongVelocity, DoubleTrack_LOAD_t LoadTransfers, DoubleTrack_VERTFORCES_t *VertForces)
{
}
*/
void Dynamics(RacingCar_ThrottleDriven_INP_t DynInp)
{
float Fx1, Fx2; // overall longitudinal force (front and rear)
float alpha1, alpha2; // slip angles
float u, v, r;
float uDot, vDot, rDot;
float delta, throttle;
float m, J, a, b, dt;
float C1, C2; // cornering stiffness
u = this->CarState.m_fLongitudinalVelocityMs;
v = this->CarState.m_fLateralVelocityMs;
r = this->CarState.m_fYawRateRadS;
delta = DynInp.m_fSteeringAngleRad;
throttle = DynInp.m_fThrottlePedal;
m = this->CarParam.m_fMassKg;
J = this->CarParam.m_fInertiaMomentKgM2;
a = this->CarParam.m_fFrontWheelbaseM;
b = this->CarParam.m_fRearWheelbaseM;
C1 = this->CarParam.m_fFrontCorneringStiffnessN;
C2 = this->CarParam.m_fRearCorneringStiffnessN;
dt = this->CarParam.m_fTimeStepS;
Fx1 = this->CarParam.m_fBrakeBalance * DynInp.m_fBrakePedal;
Fx2 = this->FromThrottleToEngineForce(throttle) + (1 - this->CarParam.m_fBrakeBalance) * DynInp.m_fBrakePedal;
/* Slip Angles */
alpha1 = delta - ((v + r*a) / u);
alpha2 = - (v - r*b) / u;
/* Velocity derivatives, Single Track Model */
uDot = (1/m) * (Fx1*cos(delta) + Fx2 + C1*alpha1*sin(delta)) + v*r;
vDot = (1/m) * (C1*alpha1*cos(delta) + C2*alpha2 - Fx1*sin(delta)) - u*r;
rDot = (1/J) * (C1*alpha1*cos(delta)*a - C2*alpha2*b + Fx1*sin(delta)*a);
/* Update output */
this->DynVar.m_fLongitudinalVelocityMs = u + uDot * dt;
this->DynVar.m_fLateralVelocityMs = v + vDot * dt;
this->DynVar.m_fYawRateRadS = r + rDot * dt;
this->DynVar.m_fLongitudinalVelocityDotMs2 = uDot;
this->DynVar.m_fLateralVelocityDotMs2 = vDot;
this->DynVar.m_fYawAccelerationRadS2 = rDot;
this->CarState.m_fLongitudinalVelocityMs = this->DynVar.m_fLongitudinalVelocityMs;
this->CarState.m_fLateralVelocityMs = this->DynVar.m_fLateralVelocityMs;
this->CarState.m_fYawRateRadS = this->DynVar.m_fYawRateRadS;
return;
}
void Run(RacingCar_ThrottleDriven_INP_t Inp, RacingCar_OUT_t *CarOutput)
{
Dynamics(Inp);
RacingCar::Kynematics();
RacingCar::SetModelOutput(CarOutput);
}
};
#endif