Skip to content

Commit e3eb7ab

Browse files
MPC C++ test with pybind11 plotting
1 parent 83fcf77 commit e3eb7ab

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.vscode/
33
.idea/
44
build/
5-
3rd-party/
5+
3rd-party/
6+
cmake-build-debug/
7+
cmake-build-release

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project(mpc_playground)
44

55
find_package(casadi REQUIRED)
66
find_package(Matplot++ REQUIRED)
7+
#add_subdirectory(3rd-party/matplotplusplus)
8+
find_package(pybind11 REQUIRED)
79

810
###############
911
# MPC example #
@@ -17,13 +19,20 @@ add_executable(mpc_example
1719
target_include_directories(mpc_example
1820
PUBLIC
1921
"include"
20-
)
22+
)
23+
24+
target_include_directories(mpc_example_pybind
25+
PUBLIC "include")
2126

2227
target_link_libraries(mpc_example
2328
casadi
2429
Matplot++::matplot
2530
)
2631

32+
target_link_libraries(mpc_example_pybind
33+
casadi
34+
pybind11::embed
35+
)
2736
###############
2837
# CasADi test #
2938
###############

src/mpc_interface.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <mpc_playground/mpc_interface.hpp>
22

3-
#include <matplot/matplot.h>
4-
53
namespace MPC {
64

75
ModelPredictiveController::ModelPredictiveController() {}

src/mpc_ms_ackermann_pybind.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <pybind11/embed.h>
2+
#include <pybind11/stl.h>
3+
4+
#include <mpc_playground/mpc_interface.hpp>
5+
6+
using namespace MPC;
7+
namespace py = pybind11;
8+
using namespace py::literals;
9+
10+
int main(int argc, char* argv[]) {
11+
auto mpc = ModelPredictiveController();
12+
13+
mpc.SetMass(1000.0);
14+
mpc.SetGoalXPosition(2.0);
15+
mpc.SetGoalYPosition(-4.0);
16+
mpc.SetSteeringAngleLowerBound(-0.57596);
17+
mpc.SetSteeringAngleUpperBound(0.57596);
18+
mpc.SetLongitudinalSpeedInitialValue(0.2);
19+
mpc.SetLongitudinalSpeedLowerBound(0.1);
20+
mpc.SetLongitudinalSpeedUpperBound(5.0);
21+
mpc.SetLongitudinalAccelerationLowerBound(-5.0);
22+
mpc.SetLongitudinalAccelerationUpperBound(5.0);
23+
mpc.SetLongitudinalJerkLowerBound(-5.5);
24+
mpc.SetLongitudinalJerkUpperBound(5.5);
25+
mpc.SetSteeringRateLowerBound(-1.2 * 0.57596);
26+
mpc.SetSteeringRateUpperBound(1.2 * 0.57596);
27+
28+
mpc.Initialise();
29+
mpc.Plan();
30+
31+
auto j_opt = mpc.GetOptimalLongitudinalJerk();
32+
auto delta_dot_opt = mpc.GetOptimalSteeringRate();
33+
auto x_opt = mpc.GetOptimalXPosition();
34+
auto y_opt = mpc.GetOptimalYPosition();
35+
auto t_opt = mpc.GetOptimalTimeSpan();
36+
37+
// std::vector<float> j_opt{0, 1, 2};
38+
// std::vector<float> x_opt{0, 1, 2};
39+
// std::vector<float> y_opt{0, 1, 2};
40+
// std::vector<float> t_opt{0, 1, 2};
41+
// std::vector<float> delta_dot_opt{0, 1, 2};
42+
43+
py::scoped_interpreter guard{};
44+
py::dict locals =
45+
py::dict{"x_opt"_a = x_opt, "y_opt"_a = y_opt, "j_opt"_a = j_opt, "delta_dot_opt"_a = delta_dot_opt, "t_opt"_a =
46+
t_opt};
47+
py::exec(R"(
48+
import matplotlib.pyplot as plt
49+
50+
fig, ax = plt.subplots()
51+
ax.plot(x_opt, y_opt, "-o")
52+
ax.set_title("AGV trajectory")
53+
ax.set_xlabel("X Coordinate [m]")
54+
ax.set_ylabel("Y Coordinate [m]")
55+
ax.set_aspect('equal', adjustable='box')
56+
57+
dir = 'assets/img'
58+
plt.savefig(f"""{dir}/ack-trajectory.png""")
59+
)", py::globals(), locals);
60+
61+
py::exec(R"(
62+
fig, ax = plt.subplots()
63+
ax.set_title('AGV longitudinal jerk')
64+
ax.plot(t_opt, j_opt, "-o")
65+
ax.set_xlabel("Time [s]")
66+
ax.set_ylabel("Longitudinal jerk [m/s^3]")
67+
ax.set_aspect('equal', adjustable = 'box')
68+
plt.savefig(f"""{dir}/ack-controls-1.png""")
69+
70+
fig, ax = plt.subplots()
71+
ax.set_title('AGV steering rate')
72+
ax.plot(t_opt, delta_dot_opt, "-o")
73+
ax.set_xlabel("Time [s]")
74+
ax.set_ylabel("Steering rate [rad/s]")
75+
ax.set_aspect('equal', adjustable = 'box')
76+
plt.savefig(f"""{dir}/ack-controls-2.png""")
77+
)", py::globals(), locals);
78+
79+
return EXIT_SUCCESS;
80+
}

0 commit comments

Comments
 (0)