MCPcopy Create free account
hub / github.com/TinyMPC/TinyMPC / main

Function main

examples/cartpole_example.cpp:28–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26typedef Matrix<tinytype, NSTATES, 1> tiny_VectorNx;
27
28int main()
29{
30 TinySolver *solver;
31
32 float rho_value = 1.0;
33
34 tinytype Adyn_data[NSTATES * NSTATES] = {1.0, 0.01, 0.0, 0.0, 0.0, 1.0, 0.039, 0.0, 0.0, 0.0, 1.002, 0.01, 0.0, 0.0, 0.458, 1.002};
35 tinytype Bdyn_data[NSTATES * NINPUTS] = {0.0, 0.02, 0.0, 0.067};
36 tinytype Q_data[NSTATES] = {10.0, 1.0, 10.0, 1.0};
37 tinytype R_data[NINPUTS] = {1.0};
38
39 tinyMatrix Adyn = Map<Matrix<tinytype, NSTATES, NSTATES, RowMajor>>(Adyn_data);
40 tinyMatrix Bdyn = Map<Matrix<tinytype, NSTATES, NINPUTS>>(Bdyn_data);
41 tinyVector fdyn = tiny_VectorNx::Zero();
42 tinyVector Q = Map<Matrix<tinytype, NSTATES, 1>>(Q_data);
43 tinyVector R = Map<Matrix<tinytype, NINPUTS, 1>>(R_data);
44
45 tinyMatrix x_min = tiny_MatrixNxNh::Constant(-1e17);
46 tinyMatrix x_max = tiny_MatrixNxNh::Constant(1e17);
47 tinyMatrix u_min = tiny_MatrixNuNhm1::Constant(-1e17);
48 tinyMatrix u_max = tiny_MatrixNuNhm1::Constant(1e17);
49
50 // Set up problem
51 int status = tiny_setup(&solver,
52 Adyn, Bdyn, fdyn, Q.asDiagonal(), R.asDiagonal(),
53 rho_value, NSTATES, NINPUTS, NHORIZON, 1);
54 // Set bound constraints
55 status = tiny_set_bound_constraints(solver, x_min, x_max, u_min, u_max);
56
57 // Update whichever settings we'd like
58 solver->settings->max_iter = 100;
59
60 // Alias solver->work for brevity
61 TinyWorkspace *work = solver->work;
62
63 // Initial state
64 tiny_VectorNx x0;
65 x0 << 0.5, 0.0, 0.0, 0.0;
66
67 // Reference trajectory
68 tiny_VectorNx Xref_origin;
69 Xref_origin << 1.0, 0, 0, 0;
70 work->Xref = Xref_origin.replicate<1, 10>();
71
72 for (int k = 0; k < NTOTAL - NHORIZON; ++k)
73 {
74 std::cout << "tracking error: " << (x0 - work->Xref.col(1)).norm() << std::endl;
75
76 // 1. Update measurement
77 tiny_set_x0(solver, x0);
78
79 // 2. Solve MPC problem
80 tiny_solve(solver);
81
82 // 3. Simulate forward
83 x0 = work->Adyn * x0 + work->Bdyn * work->u.col(0);
84 }
85

Callers

nothing calls this directly

Calls 7

tiny_setupFunction · 0.85
tiny_set_x0Function · 0.85
tiny_solveFunction · 0.85
asDiagonalMethod · 0.80
normMethod · 0.45
colMethod · 0.45

Tested by

no test coverage detected