forked from F3de22/Steering_MPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
271 lines (223 loc) · 8.1 KB
/
Copy pathsimulation.cpp
File metadata and controls
271 lines (223 loc) · 8.1 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <deque>
#include "MPC.hpp"
#include "geometry.hpp"
using namespace std;
// Normalizzazione angoli in [-pi, pi]
double normalize_angle(double angle) {
while (angle > M_PI) angle -= 2.0 * M_PI;
while (angle < -M_PI) angle += 2.0 * M_PI;
return angle;
}
// Simulatore ritardo attuatori (usato solo per PID)
class ActuatorDelay {
std::deque<double> buffer;
int delay_steps;
public:
ActuatorDelay(int steps) : delay_steps(steps) {
for(int i=0; i<steps; i++) buffer.push_back(0.0);
}
double get_delayed_value(double new_input) {
if (delay_steps <= 0) return new_input;
buffer.push_back(new_input);
double val = buffer.front();
buffer.pop_front();
return val;
}
};
// Stato veicolo
struct State { double x, y, theta, vx, vy, omega; };
// Modello fisico del veicolo (non lineare, con Magic Formula semplificata)
State update_vehicle_physics(State s, double Fx, double delta, double dt) {
double m = 320.0;
double Iz = 98.03;
double la = 0.792;
double lb = 0.758;
double B = 10.0, C = 1.9, D = 1.0; // Parametri Magic Formula
// Saturazione input
if (Fx > 1500) Fx = 1500;
if (Fx < -1500) Fx = -1500;
double max_steer = 0.42;
if (delta > max_steer) delta = max_steer;
if (delta < -max_steer) delta = -max_steer;
// Angoli di deriva
double vx_safe = std::max(s.vx, 1.0);
double alpha_f = delta - atan2(s.vy + s.omega * la, vx_safe);
double alpha_r = -atan2(s.vy - s.omega * lb, vx_safe);
// Forze laterali (Magic Formula)
double Fyf = D * sin(C * atan(B * alpha_f)) * m * 9.81 * (lb / (la + lb));
double Fyr = D * sin(C * atan(B * alpha_r)) * m * 9.81 * (la / (la + lb));
// Equazioni del moto
double dot_vx = (Fx - Fyf * sin(delta)) / m + s.vy * s.omega;
double dot_vy = (Fyf * cos(delta) + Fyr) / m - s.vx * s.omega;
double dot_omega = (la * Fyf * cos(delta) - lb * Fyr) / Iz;
// Integrazione (Eulero)
State next = s;
next.vx += dot_vx * dt;
next.vy += dot_vy * dt;
next.omega += dot_omega * dt;
next.x += (s.vx * cos(s.theta) - s.vy * sin(s.theta)) * dt;
next.y += (s.vx * sin(s.theta) + s.vy * cos(s.theta)) * dt;
next.theta = normalize_angle(s.theta + s.omega * dt);
return next;
}
// Punto tracciato
struct TrackPoint { double x, y, v_target; };
// Generazione tracciato: rettilineo + sinusoide + rettilineo
vector<TrackPoint> generate_track() {
vector<TrackPoint> t;
double ds = 0.5;
// Rettilineo iniziale (0-40m)
for (double x = 0; x < 40.0; x += ds) {
t.push_back({x, 0.0, 25.0});
}
// Sinusoide (40-240m)
double x_start_sine = 40.0;
double length_sine = 200.0;
for (double x = 40.0; x < 240.0; x += ds) {
double angle = ((x - x_start_sine) / length_sine) * 2.0 * M_PI;
double y = 6.0 * sin(angle);
t.push_back({x, y, 15.0});
}
// Rettilineo finale (240-300m)
for (double x = 240.0; x <= 300.0; x += ds) {
t.push_back({x, 0.0, 25.0});
}
return t;
}
// Estrazione orizzonte waypoints basato sul tempo
pair<vector<Point>, double> get_horizon(State s, const vector<TrackPoint>& track, int op, double dt) {
// Trova punto più vicino
double min_d = 1e9;
int current_idx = 0;
for(int i=0; i<track.size(); i++) {
double d = sqrt(pow(s.x - track[i].x, 2) + pow(s.y - track[i].y, 2));
if(d < min_d) {
min_d = d;
current_idx = i;
}
}
// Proiezione temporale dell'orizzonte
vector<Point> wp;
double current_v_ref = track[current_idx].v_target;
double track_res = 0.5;
double proj_speed = std::max(s.vx, 5.0);
for(int i=0; i<op; i++) {
double future_dist = proj_speed * dt * (i + 1);
int idx_offset = static_cast<int>(future_dist / track_res);
int target_idx = std::min(current_idx + idx_offset, (int)track.size()-1);
wp.push_back(Point(track[target_idx].x, track[target_idx].y));
}
return {wp, current_v_ref};
}
// Controller PID semplice
class PID {
double kp, ki, kd, I=0, prev=0, limit;
public:
PID(double p, double i, double d, double lim) : kp(p), ki(i), kd(d), limit(lim) {}
double calc(double ref, double meas, double dt) {
double err = ref - meas;
I += err*dt;
// Anti-windup
if(I > 300) I=300;
if(I<-300) I=-300;
double der = (err - prev)/dt;
prev = err;
double out = kp*err + ki*I + kd*der;
return std::max(-limit, std::min(out, limit));
}
void reset() { I=0; prev=0; }
};
// Controller Pure Pursuit
class PurePursuit {
public:
double calc(State s, const vector<Point>& path) {
double ld = 5.0; // Lookahead distance
for(auto& p : path) {
double dist = sqrt(pow(p.x-s.x, 2) + pow(p.y-s.y, 2));
if(dist > ld) {
double alpha = atan2(p.y - s.y, p.x - s.x) - s.theta;
alpha = normalize_angle(alpha);
return atan(2 * 0.792 * sin(alpha) / ld);
}
}
return 0;
}
};
int main() {
// Verifica file dati
ifstream check("cornering_stiffness_vs_vertical_load.txt");
if(!check.good()) {
cerr << "Errore: file dati mancante" << endl;
return -1;
}
check.close();
auto track = generate_track();
int op = 40;
double dt = 0.05;
int steps = 600;
// Controller MPC
MPC mpc;
State s_mpc = {0, 0, 0, 1.0, 0, 0};
// Controller baseline (PID + Pure Pursuit)
PID pid_gas(400, 5, 20, 1500);
PID pid_brk(600, 0, 50, 1500);
PurePursuit pp;
State s_pid = {0, 0, 0, 1.0, 0, 0};
// Ritardi per PID (rendere più realistico)
ActuatorDelay pid_steer_delay(2);
ActuatorDelay pid_gas_delay(0);
ofstream f("comparison_results.csv");
f << "Time,Ref_Vx,Ref_X,Ref_Y,MPC_X,MPC_Y,MPC_Vx,MPC_Fx,MPC_Steer,PID_X,PID_Y,PID_Vx,PID_Fx,PID_Power\n";
cout << "Inizio simulazione MPC vs PID..." << endl;
for(int i=0; i<steps; i++) {
double t = i*dt;
// MPC
auto hor_mpc = get_horizon(s_mpc, track, op, dt);
double v_ref_mpc = hor_mpc.second;
Eigen::VectorXd x0(6);
x0 << s_mpc.x, s_mpc.y, s_mpc.theta, s_mpc.vx, s_mpc.vy, s_mpc.omega;
mpc.updateDiscretization(x0, 0.0);
pair<double,double> u_mpc;
try {
u_mpc = mpc.compute(x0, hor_mpc.first, v_ref_mpc);
} catch(...) {
u_mpc = {0, 0};
}
s_mpc = update_vehicle_physics(s_mpc, u_mpc.first, u_mpc.second, dt);
// PID + Pure Pursuit
auto hor_pid = get_horizon(s_pid, track, op, dt);
double steer_pid_req = pp.calc(s_pid, hor_pid.first);
double fx_pid_req = 0;
double err_v = hor_pid.second - s_pid.vx;
// Logica gas/freno con deadzone
if(err_v > 0.2) {
fx_pid_req = pid_gas.calc(hor_pid.second, s_pid.vx, dt);
pid_brk.reset();
} else if(err_v < -0.2) {
fx_pid_req = -pid_brk.calc(-hor_pid.second, -s_pid.vx, dt);
pid_gas.reset();
}
double real_steer_pid = pid_steer_delay.get_delayed_value(steer_pid_req);
double real_fx_pid = pid_gas_delay.get_delayed_value(fx_pid_req);
s_pid = update_vehicle_physics(s_pid, real_fx_pid, real_steer_pid, dt);
// Salva dati
f << t << "," << v_ref_mpc << ","
<< hor_mpc.first[0].x << "," << hor_mpc.first[0].y << ","
<< s_mpc.x << "," << s_mpc.y << "," << s_mpc.vx << ","
<< u_mpc.first << "," << u_mpc.second << ","
<< s_pid.x << "," << s_pid.y << "," << s_pid.vx << ","
<< real_fx_pid << "," << (real_fx_pid * s_pid.vx) << "\n";
if(i % 100 == 0) {
cout << "t = " << t << "s | MPC X=" << s_mpc.x << "m" << endl;
}
if(s_mpc.x > 295) break;
}
f.close();
cout << "Simulazione completata. File: comparison_results.csv" << endl;
return 0;
}