plcopen 0.22.0
载入中...
搜索中...
未找到
quintic_fast_path.h
浏览该文件的文档.
1#pragma once
2
3// T24 quintic Hermite fast path (algorithm contract §4).
4//
5// Closed-form quintic from (p,v,a) to (p_aim,v_aim,0) over h cycles.
6// Analytical extrema check: velocity (cubic roots), acceleration
7// (quadratic roots), jerk (linear root) + endpoints. No sampling,
8// no margins — the check IS the envelope proof.
9//
10// Accept → ~0.5µs per joint. Reject → fall back to full OTG (~8.6µs).
11
12#include <algorithm>
13#include <cmath>
14#include <cstdint>
15
16#include "otg/profile1d.h"
17#include "rt/error.h"
18
20{
21
23{
24 double d[6] = {};
25 std::int64_t h = 0;
26};
27
29{
30
31inline int solve_quadratic(double a, double b, double c, double roots[3])
32{
33 if(std::fabs(a) < 1e-30) {
34 if(std::fabs(b) < 1e-30) {
35 return 0;
36 }
37 roots[0] = -c / b;
38 return 1;
39 }
40 const double disc = b * b - 4.0 * a * c;
41 if(disc < 0.0) {
42 return 0;
43 }
44 const double sqrt_disc = std::sqrt(disc);
45 roots[0] = (-b - sqrt_disc) / (2.0 * a);
46 roots[1] = (-b + sqrt_disc) / (2.0 * a);
47 return disc > 1e-30 ? 2 : 1;
48}
49
50inline int solve_cubic(double a3, double a2, double a1, double a0, double roots[3])
51{
52 if(std::fabs(a3) < 1e-30) {
53 return solve_quadratic(a2, a1, a0, roots);
54 }
55
56 const double B = a2 / a3;
57 const double C = a1 / a3;
58 const double D = a0 / a3;
59 const double p = C - B * B / 3.0;
60 const double q = 2.0 * B * B * B / 27.0 - B * C / 3.0 + D;
61 const double offset = -B / 3.0;
62
63 if(std::fabs(p) < 1e-30) {
64 roots[0] = -std::cbrt(q) + offset;
65 return 1;
66 }
67
68 const double disc = -4.0 * p * p * p - 27.0 * q * q;
69
70 if(disc > 1e-20) {
71 const double m = 2.0 * std::sqrt(-p / 3.0);
72 const double cos_arg =
73 std::max(-1.0, std::min(1.0, 3.0 * q / (p * m)));
74 const double theta = std::acos(cos_arg) / 3.0;
75 constexpr double two_pi_3 = 2.09439510239319526;
76 roots[0] = m * std::cos(theta) + offset;
77 roots[1] = m * std::cos(theta - two_pi_3) + offset;
78 roots[2] = m * std::cos(theta - 2.0 * two_pi_3) + offset;
79 return 3;
80 }
81
82 const double half_q = q / 2.0;
83 const double r = half_q * half_q + p * p * p / 27.0;
84 const double scale =
85 std::fabs(half_q * half_q) + std::fabs(p * p * p / 27.0) + 1e-30;
86 if(std::fabs(r) < scale * 1e-8) {
87 const double u = std::cbrt(-half_q);
88 roots[0] = 2.0 * u + offset;
89 roots[1] = -u + offset;
90 return 2;
91 }
92 const double sqrt_r = std::sqrt(std::max(r, 0.0));
93 roots[0] =
94 std::cbrt(-half_q + sqrt_r) + std::cbrt(-half_q - sqrt_r) + offset;
95 return 1;
96}
97
98} // namespace quintic_detail
99
100inline bool solve_quintic(otg::State1D from, double target_position,
101 double target_velocity, std::int64_t h,
102 QuinticProfile &out)
103{
104 if(h <= 0) {
105 return false;
106 }
107 const double hd = static_cast<double>(h);
108
109 out.h = h;
110 out.d[0] = from.position;
111 out.d[1] = from.velocity * hd;
112 out.d[2] = from.acceleration * hd * hd / 2.0;
113
114 const double R0 = target_position - out.d[0] - out.d[1] - out.d[2];
115 const double R1 = target_velocity * hd - out.d[1] - 2.0 * out.d[2];
116 const double R2 = -2.0 * out.d[2];
117
118 out.d[3] = (20.0 * R0 - 8.0 * R1 + R2) / 2.0;
119 out.d[4] = (-30.0 * R0 + 14.0 * R1 - 2.0 * R2) / 2.0;
120 out.d[5] = (12.0 * R0 - 6.0 * R1 + R2) / 2.0;
121 return true;
122}
123
124inline otg::State1D sample_quintic(const QuinticProfile &q, std::int64_t tick)
125{
126 const double h = static_cast<double>(q.h);
127 const double s = static_cast<double>(tick) / h;
128
129 const double p = q.d[0] + s * (q.d[1] + s * (q.d[2] +
130 s * (q.d[3] + s * (q.d[4] + s * q.d[5]))));
131 const double v = (q.d[1] + s * (2.0 * q.d[2] + s * (3.0 * q.d[3] +
132 s * (4.0 * q.d[4] + s * 5.0 * q.d[5])))) / h;
133 const double a = (2.0 * q.d[2] + s * (6.0 * q.d[3] +
134 s * (12.0 * q.d[4] + s * 20.0 * q.d[5]))) / (h * h);
135 return {p, v, a};
136}
137
139 const otg::Limits1D &limits)
140{
141 const double h = static_cast<double>(qp.h);
142 if(h <= 0.0) {
143 return false;
144 }
145
146 constexpr double tol = 1.0 + 1e-9;
147
148 // --- Velocity extrema: q''(σ) = 0 roots (cubic) + endpoints ---
149 {
150 double roots[3];
151 const int n = quintic_detail::solve_cubic(
152 20.0 * qp.d[5], 12.0 * qp.d[4],
153 6.0 * qp.d[3], 2.0 * qp.d[2], roots);
154 for(int i = -2; i < n; ++i) {
155 double sigma;
156 if(i == -2) {
157 sigma = 0.0;
158 } else if(i == -1) {
159 sigma = 1.0;
160 } else {
161 sigma = roots[i];
162 if(sigma < 0.0 || sigma > 1.0) {
163 continue;
164 }
165 }
166 const double v_norm =
167 qp.d[1] +
168 sigma * (2.0 * qp.d[2] +
169 sigma * (3.0 * qp.d[3] +
170 sigma * (4.0 * qp.d[4] +
171 sigma * 5.0 * qp.d[5])));
172 if(std::fabs(v_norm / h) > limits.max_velocity * tol) {
173 return false;
174 }
175 }
176 }
177
178 // --- Acceleration extrema: q'''(σ) = 0 roots (quadratic) + endpoints ---
179 {
180 double roots[3];
182 60.0 * qp.d[5], 24.0 * qp.d[4], 6.0 * qp.d[3], roots);
183 for(int i = -2; i < n; ++i) {
184 double sigma;
185 if(i == -2) {
186 sigma = 0.0;
187 } else if(i == -1) {
188 sigma = 1.0;
189 } else {
190 sigma = roots[i];
191 if(sigma < 0.0 || sigma > 1.0) {
192 continue;
193 }
194 }
195 const double a_norm =
196 2.0 * qp.d[2] +
197 sigma * (6.0 * qp.d[3] +
198 sigma * (12.0 * qp.d[4] + sigma * 20.0 * qp.d[5]));
199 const double a_phys = a_norm / (h * h);
200 if(a_phys > limits.max_acceleration * tol) {
201 return false;
202 }
203 if(a_phys < -limits.max_deceleration * tol) {
204 return false;
205 }
206 }
207 }
208
209 // --- Jerk extrema: q''''(σ) = 0 root (linear) + endpoints ---
210 {
211 double sigma_crit = -1.0;
212 if(std::fabs(qp.d[5]) > 1e-30) {
213 sigma_crit = -qp.d[4] / (5.0 * qp.d[5]);
214 }
215 for(int i = -2; i < 1; ++i) {
216 double sigma;
217 if(i == -2) {
218 sigma = 0.0;
219 } else if(i == -1) {
220 sigma = 1.0;
221 } else {
222 sigma = sigma_crit;
223 if(sigma < 0.0 || sigma > 1.0) {
224 continue;
225 }
226 }
227 const double j_norm =
228 6.0 * qp.d[3] +
229 sigma * (24.0 * qp.d[4] + sigma * 60.0 * qp.d[5]);
230 if(std::fabs(j_norm / (h * h * h)) > limits.max_jerk * tol) {
231 return false;
232 }
233 }
234 }
235 return true;
236}
237
238} // namespace plcopen::core::stream
int solve_quadratic(double a, double b, double c, double roots[3])
int solve_cubic(double a3, double a2, double a1, double a0, double roots[3])
bool solve_quintic(otg::State1D from, double target_position, double target_velocity, std::int64_t h, QuinticProfile &out)
otg::State1D sample_quintic(const QuinticProfile &q, std::int64_t tick)
bool check_quintic_limits(const QuinticProfile &qp, const otg::Limits1D &limits)