forked from armando-genis/stanley_controller_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
executable file
·166 lines (132 loc) · 6.59 KB
/
Animation.cpp
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
#include <iostream>
#include <Eigen/Dense>
#include <chrono>
#include <thread>
#include <vector>
#include "Linear_Interpolation.h"
#include "matplotlibcpp.h" // Include the matplotlib-cpp header
#include "BicycleModel.h"
#include "StanleyController.h"
namespace plt = matplotlibcpp;
void plot_waypoints( const std::vector<Eigen::VectorXd> waypoints){
std::vector<double> x_vals, y_vals;
for (const auto& waypoint : waypoints) {
x_vals.push_back(waypoint[0]);
y_vals.push_back(waypoint[1]);
}
plt::plot(x_vals, y_vals, "bo-"); // Plot blue circles connected by lines
plt::xlabel("X");
plt::ylabel("Y");
plt::title("Waypoints");
plt::grid(true);
plt::show();
}
void plot_waypoints_interpolation( const std::vector<Eigen::VectorXd> waypoints){
std::vector<double> x_vals, y_vals;
for (const auto& waypoint : waypoints) {
x_vals.push_back(waypoint[0]);
y_vals.push_back(waypoint[1]);
}
plt::plot(x_vals, y_vals, "bo--"); // Plot blue circles connected by lines
plt::xlabel("X");
plt::ylabel("Y");
plt::title("Waypoints interpolated");
plt::grid(true);
plt::show();
}
void animation_car(const std::vector<Eigen::VectorXd> waypoints,const std::vector<double>& wp_distance, const std::vector<int>& wp_interp_hash, const std::vector<Eigen::VectorXd>& wp_interp){
plt::ion();
BicycleModel vehicle(0.0, 0.0, 0.0, 1.0);
StanleyController controller(waypoints);
std::vector<double> vehicle_x, vehicle_y, vehicle_theta;
double dt = 0.1;
int iterations = 100;
for (int i = 0; i < iterations; i++) {
std::cout << "### Iteration: " << i+1 << " of " << iterations << std::endl;
std::cout << "vehicle x: " << vehicle.getX() << std::endl;
std::cout << "vehicle y: " << vehicle.getY() << std::endl;
controller.findClosestWaypoint(vehicle.getX(), vehicle.getY(), wp_distance, wp_interp_hash, wp_interp);
// vehicle.update(0, 0.01, 0.1,controller.GetMaxSteer());
std::cout << "Vehicle yaw: " << vehicle.getYaw() << std::endl;
double velocity = vehicle.getV();
// Store vehicle data for plotting
vehicle_x.push_back(vehicle.getX());
vehicle_y.push_back(vehicle.getY());
vehicle_theta.push_back(vehicle.getTheta());
// Find the closest waypoint to the vehicle
size_t ClosestIndex = controller.getClosestIndex();
// std::cout << "maxSteering: " << controller.GetMaxSteer() << std::endl;
std::vector<Eigen::VectorXd> new_waypoints = controller.getNewWaypoints();
controller.computeCrossTrackError(vehicle.getX(), vehicle.getY(), vehicle.getYaw());
double target_idx = controller.GetTargetIdx();
std::cout << "target_idx: " << target_idx << std::endl;
std::cout << "error_front_axle: " << controller.GetErrorFrontAxle() << std::endl;
// std::cout << "ClosestIndex: " << ClosestIndex << std::endl;
// std::cout << "new_waypoints size: " << new_waypoints.size() << std::endl;
// std::cout << "new_waypoints in x: " << new_waypoints[target_idx](0) << std::endl;
// std::cout << "new_waypoints in y: " << new_waypoints[target_idx](1) << std::endl;
double x_target = new_waypoints[target_idx](0);
double y_target = new_waypoints[target_idx](1);
double cosestindex_x = waypoints[ClosestIndex](0);
double cosestindex_y = waypoints[ClosestIndex](1);
double target_speed = 30.0 / 3.6; // [m/s] 30 km/h to [m/s]
controller.computePID(target_speed, velocity);
controller.computeSteeringAngle(vehicle.getYaw(), velocity);
// std::cout << "new_waypoints: " << new_waypoints[1] << std::endl;
vehicle.update(controller.GetDelta(), 0.01, 0.1,controller.GetMaxSteer());
std::cout << "steering angle: " << controller.GetDelta() << std::endl;
std::cout << std::endl;
plt::clf();
std::vector<double> wp_x, wp_y;
for (const auto& waypoint : waypoints) {
wp_x.push_back(waypoint[0]);
wp_y.push_back(waypoint[1]);
}
std::vector<double> wp_x2, wp_y2;
for (const auto& waypoint : controller.getNewWaypoints()) {
wp_x2.push_back(waypoint[0]);
wp_y2.push_back(waypoint[1]);
}
std::vector<double> x_target_vec = {x_target};
std::vector<double> y_target_vec = {y_target};
std::vector<double> cosestindex_x_vec = {cosestindex_x};
std::vector<double> cosestindex_y_vec = {cosestindex_y};
plt::plot(wp_x, wp_y, "ro");
plt::plot(wp_x2, wp_y2, "mo");
plt::plot(x_target_vec, y_target_vec, "bo");
plt::plot(cosestindex_x_vec, cosestindex_y_vec, "ko");
plt::named_plot("subset of waypoints", wp_x2, wp_y2);
plt::named_plot("waypoints", wp_x, wp_y);
plt::named_plot("vehicle", vehicle_x, vehicle_y);
plt::legend(); // Show the legend to access the plot's properties
plt::xlabel("X");
plt::ylabel("Y");
plt::title("Waypoints and Vehicle Path with Orientation");
plt::grid(true);
plt::show();
try {
plt::pause(0.1);
} catch (const std::runtime_error& e) {
std::cerr << "Runtime error: " << e.what() << std::endl;
}
}
// Keep the plot window open
plt::show(true);
}
int main() {
std::vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0,6.0,6.0,6.0,6.0,6.0,6.0, 5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0,-5.0,-6.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0, -6.0,-5.0,-4.0,-3.0,-2.0};
std::vector<double> y = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,2.0,3.0,4.0,5.0,6.0,7.0, 7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0, 6.0,5.0,4.0,3.0,2.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0};
// std::vector<double> x = {0.0, 100.0, 100.0, 50.0, 60.0};
// std::vector<double> y = {0.0, 0.0, -30.0, -20.0, 0.0};
Linear_Interpolation linear_interpolation(x, y, 0.1);
linear_interpolation.interpolateWaypoints();
std::vector<Eigen::VectorXd> wp_interp = linear_interpolation.getWp_interp();
std::vector<int> wp_interp_hash = linear_interpolation.getWp_interp_hash();
std::vector<double> wp_distance = linear_interpolation.getWp_distance();
std::cout << "wp_interp size: " << wp_interp.size() << std::endl;
std::cout << "wp_interp_hash size: " << wp_interp_hash.size() << std::endl;
std::vector<Eigen::VectorXd> waypoints = linear_interpolation.getWaypoints();
// plot_waypoints(waypoints);
// plot_waypoints_interpolation(wp_interp);
animation_car(waypoints, wp_distance, wp_interp_hash, wp_interp);
}