COPP C++ API
C++ interface for COPP trajectory optimization
Loading...
Searching...
No Matches
topp2_ra.cpp
Go to the documentation of this file.
1// Tutorial example: solve a second-order TOPP2-RA problem.
2//
3// This demonstrates the common workflow: construct a path, sample it on a
4// station grid, add velocity/acceleration limits, solve for `a(s) = dot{s}^2`,
5// then convert the station profile into a time trajectory.
6
7#include <cmath>
8#include <iostream>
9#include <vector>
10
11#include <copp/copp.hpp>
12
13int main()
14{
15 constexpr std::size_t dim = 3;
16 constexpr std::size_t n = 1001;
17
18 // Step 1: Provide a smooth path with analytic derivatives up to second
19 // order. TOPP2 does not need third derivatives.
21 dim,
22 0.0,
23 1.0,
28 {
29 const double pi_local = 3.14159265358979323846;
30 const double freq[3] = {2.0 * pi_local, 3.0 * pi_local, 5.0 * pi_local};
31 const double phase[3] = {0.0, 0.3, 0.7};
32
33 for (std::size_t j = 0; j < s.size(); ++j)
34 {
35 for (std::size_t axis = 0; axis < 3; ++axis)
36 {
37 const double x = freq[axis] * s[j] + phase[axis];
38 q(axis, j) = std::sin(x);
39 dq(axis, j) = freq[axis] * std::cos(x);
40 ddq(axis, j) = -freq[axis] * freq[axis] * std::sin(x);
41 }
42 }
43 });
44
45 // Step 2: Build a uniform station grid on the path parameter interval.
46 std::vector<double> s(n);
47 for (std::size_t k = 0; k < n; ++k)
48 {
49 s[k] = static_cast<double>(k) / static_cast<double>(n - 1);
50 }
51
52 const std::vector<double> upper(dim, 1.0);
53 const std::vector<double> lower(dim, -1.0);
54
55 // Step 3: Sample path derivatives into the robot constraint buffer and add
56 // physical velocity/acceleration limits.
57 copp::Robot robot(dim, n);
58 robot.append_s(s)
59 .set_q_from_path_2nd(path, 0, n)
60 .add_velocity_limits(upper, lower, 0, n)
61 .add_acceleration_limits(upper, lower, 0, n);
62
63 namespace topp2 = copp::solver::topp2_ra;
64
65 // Step 4: The problem borrows `robot.constraints()`. The robot must outlive
66 // the problem and solver call.
67 topp2::Problem problem{
68 robot.constraints(),
69 copp::IndexInterval{0, n - 1},
70 copp::Boundary2{0.0, 0.0},
71 };
72
73 // Step 5: Solve for the node profile `a`, integrate time, and create a
74 // uniform `s(t)` sampling.
75 auto a = topp2::solve(problem);
76 auto time = copp::interpolation::s_to_t_topp2(s, a, 0.0);
77 auto s_t = copp::interpolation::t_to_s_topp2_uniform(s, a, time.t_s, 1.0e-3);
78
79 std::cout << "TOPP2-RA done.\n";
80 std::cout << "dim = " << dim << ", N = " << n << "\n";
81 std::cout << "t_final = " << time.t_final << " s\n";
82 std::cout << "a_profile.len() = " << a.size() << "\n";
83 std::cout << "s(t) samples = " << s_t.size() << "\n";
84
85 return 0;
86}
Mutable column-major matrix view used by callback APIs.
Definition core.hpp:288
static Path from_evaluator_2nd(std::size_t dim, double s_min, double s_max, PathEvaluator2nd evaluator)
Build a path from a batch evaluator with explicit derivatives up to second order.
Robot facade backed by Rust Robot<CppRobotModel>.
Definition robot.hpp:318
ConstraintsRef constraints() noexcept
Return a borrowed raw constraint-buffer facade.
Robot & set_q_from_path_2nd(const Path &path, std::size_t idx_s_from, std::size_t idx_s_to)
Sample a path and store derivatives up to second order.
Robot & add_acceleration_limits(Span< const double > upper, Span< const double > lower, std::size_t start_idx_s, std::size_t length=0)
Add acceleration limits from broadcast vectors.
Robot & append_s(Span< const double > s)
Append strictly increasing path station samples.
Robot & add_velocity_limits(Span< const double > upper, Span< const double > lower, std::size_t start_idx_s, std::size_t length=0)
Add velocity limits from broadcast vectors.
Non-owning view of a contiguous one-dimensional array.
Definition core.hpp:139
constexpr std::size_t size() const noexcept
Definition core.hpp:169
COPP_API std::vector< double > t_to_s_topp2_uniform(Span< const double > s, Span< const double > a, Span< const double > t_s, double dt, TimeGridOptions options={})
Sample the inverse TOPP2 mapping s(t) on a uniform time grid.
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).
Second-order time-optimal reachability-analysis solver plus second-order reachable-set artifacts and ...
Second-order endpoint boundary values.
Definition core.hpp:77
Closed station-index interval used by TOPP/COPP problem descriptors.
Definition core.hpp:67
int main()
Definition topp2_ra.cpp:13