COPP C++ API
C++ interface for COPP trajectory optimization
Loading...
Searching...
No Matches
copp2_socp.cpp
Go to the documentation of this file.
1// Tutorial example: solve a second-order COPP2-SOCP problem with Clarabel.
2//
3// COPP extends TOPP by optimizing an objective list. This example uses the
4// default point-mass torque model (`tau = ddq`) and asks for expert diagnostics
5// so we can print the accepted objective value.
6
7#include <iostream>
8#include <vector>
9
10#include <copp/copp.hpp>
11
12int main()
13{
14 // Step 1: Define a small station grid and symmetric joint limits.
15 std::vector<double> s{0.0, 0.25, 0.5, 0.75, 1.0};
16 std::vector<double> upper{10.0, 10.0};
17 std::vector<double> lower{-10.0, -10.0};
18
19 // Step 2: Build a parametric path. `powi` propagates derivatives through
20 // the Jet3 scalar, so q/dq/ddq are available for constraint construction.
22 [](copp::Jet3 x) {
23 return std::vector<copp::Jet3>{
24 x,
25 copp::powi(x, 2),
26 };
27 },
28 0.0,
29 1.0);
30
31 // Step 3: Populate the robot with path derivatives and physical limits.
32 // Without a custom inverse-dynamics callback, torque limits use tau = ddq.
33 copp::Robot robot(2, s.size());
34 robot.append_s(s)
35 .set_q_from_path_2nd(path, 0, s.size())
36 .add_velocity_limits(upper, lower, 0, s.size())
37 .add_acceleration_limits(upper, lower, 0, s.size())
38 .add_torque_limits(upper, lower, 0, s.size());
39
40 namespace copp2 = copp::solver::copp2_socp;
41
42 // Step 4: Objective descriptors are copied into the problem. The robot is
43 // borrowed, so it must remain alive through the solve.
44 copp2::Problem problem{
45 robot,
46 {
48 copp::objective::ThermalEnergy(0.05, {1.0, 1.0}),
49 },
50 copp::IndexInterval{0, s.size() - 1},
51 copp::Boundary2{0.0, 0.0},
52 };
53
54 // Step 5: Adjust Clarabel acceptance/settings and request expert output.
56 options.allow_almost_solved = true;
57 options.clarabel_settings.max_iter = 200;
58
59 auto expert = copp2::solve_expert(problem, options);
60 if (!expert.a)
61 {
62 std::cerr << "Clarabel did not return an accepted profile\n";
63 return 1;
64 }
65
66 // Step 6: Convert the accepted profile into a time trajectory.
67 auto time = copp::interpolation::s_to_t_topp2(s, *expert.a, 0.0);
68 std::cout << "accepted a length = " << expert.a->size() << "\n";
69 std::cout << "t_final = " << time.t_final << "\n";
70 std::cout << "objective = " << *expert.objective_value << "\n";
71 return 0;
72}
static Path from_parametric(PathParametric parametric, double s_min, double s_max)
Build a path from a scalar-parametric formula.
Robot facade backed by Rust Robot<CppRobotModel>.
Definition robot.hpp:318
int main()
COPP_API TimeProfile s_to_t_topp2(Span< const double > s, Span< const double > a, double t0=0.0)
Convert a second-order path profile a(s) = ds/dt squared to t(s).
Objective ThermalEnergy(double weight, Span< const double > normalize)
Objective Time(double weight=1.0)
Second-order convex-objective Clarabel SOCP backend with normal profile-returning APIs and expert dia...
Jet3 powi(Jet3 x, int n) noexcept
Definition path.hpp:229
Second-order endpoint boundary values.
Definition core.hpp:77
Closed station-index interval used by TOPP/COPP problem descriptors.
Definition core.hpp:67
Third-order forward-mode automatic-differentiation scalar.
Definition path.hpp:44
Shared Clarabel options for COPP/TOPP SOCP-style solvers.
Definition clarabel.hpp:117
std::uint32_t max_iter
Definition clarabel.hpp:71