@@ -203,18 +203,21 @@ DifferentialTrajectoryGenerator::DifferentialTrajectoryGenerator(
203203 {θ.at (index + 1 )},
204204 {vl.at (index + 1 )},
205205 {vr.at (index + 1 )}};
206+ slp::VariableMatrix u_k_1{{Fl.at (index + 1 )}, {Fr.at (index + 1 )}};
206207
207- // Dynamics constraints - RK4
208- slp::VariableMatrix k1 = f (x_k, u_k);
209- slp::VariableMatrix k2 = f (x_k + dt * 0.5 * k1, u_k);
210- slp::VariableMatrix k3 = f (x_k + dt * 0.5 * k2, u_k);
211- slp::VariableMatrix k4 = f (x_k + dt * k3, u_k);
212- problem.SubjectTo (x_k_1 ==
213- x_k + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4));
214-
215- auto dx_dt = k1;
216- problem.SubjectTo (al.at (index) == dx_dt (3 ));
217- problem.SubjectTo (ar.at (index) == dx_dt (4 ));
208+ // Dynamics constraints - direct collocation
209+ // (https://mec560sbu.github.io/2016/09/30/direct_collocation/)
210+ auto xdot_k = f (x_k, u_k);
211+ auto xdot_k_1 = f (x_k_1, u_k_1);
212+ auto xdot_c = -3 / (2 * dt) * (x_k - x_k_1) - 0.25 * (xdot_k + xdot_k_1);
213+
214+ auto x_c = 0.5 * (x_k + x_k_1) + dt / 8 * (xdot_k - xdot_k_1);
215+ auto u_c = 0.5 * (u_k + u_k_1);
216+
217+ problem.SubjectTo (xdot_c == f (x_c, u_c));
218+
219+ problem.SubjectTo (al.at (index) == xdot_k (3 ));
220+ problem.SubjectTo (ar.at (index) == xdot_k (4 ));
218221 }
219222 }
220223
0 commit comments