38 return active_ ? r_tube_ : 0.0;
43 return active_ && vector_mode_;
47 std::array<double, MaxAxes> &velocities,
48 std::size_t axis_count)
const
55 for(std::size_t i = 0; i < axis_count; ++i) {
64 for(std::size_t i = 0; i < axis_count; ++i) {
65 velocities[i] = lateral_dir_[i] * v_lat;
76 return captured_vector_state_;
81 return captured_output_history_only_;
86 return output_velocity_;
91 return output_acceleration_;
101 return acceleration_;
107 vector_mode_ =
false;
112 has_velocity_ =
false;
113 captured_vector_state_ =
false;
114 captured_output_history_only_ =
false;
122 const bool had = has_velocity_;
123 has_velocity_ =
false;
133 const std::array<double, MaxAxes> &velocity,
134 const std::array<double, MaxAxes> &acceleration,
135 std::size_t axis_count)
140 for(std::size_t i = 0; i < axis_count; ++i) {
141 output_velocity_[i] = velocity[i];
142 output_acceleration_[i] = acceleration[i];
153 const std::array<double, MaxAxes> &velocity,
154 const std::array<double, MaxAxes> &acceleration,
155 std::size_t axis_count)
157 has_velocity_ =
false;
158 captured_vector_state_ =
false;
163 acceleration_.fill(0.0);
164 output_velocity_.fill(0.0);
165 output_acceleration_.fill(0.0);
166 for(std::size_t i = 0; i < axis_count; ++i) {
167 velocity_[i] = velocity[i];
168 acceleration_[i] = acceleration[i];
169 output_velocity_[i] = velocity[i];
170 output_acceleration_[i] = acceleration[i];
172 captured_vector_state_ =
true;
173 captured_output_history_only_ =
true;
174 has_velocity_ =
true;
183 void capture(
bool motion_active,
bool linear_path,
double path_length,
185 const std::array<double, MaxAxes> &start,
186 const std::array<double, MaxAxes> &finish,
187 std::size_t axis_count)
189 has_velocity_ =
false;
190 captured_vector_state_ =
false;
191 if(!motion_active || !linear_path || path_length <= 0.0) {
196 std::array<double, MaxAxes> q_s{};
197 std::array<double, MaxAxes> q_ss{};
198 for(std::size_t i = 0; i < axis_count; ++i) {
199 q_s[i] = (finish[i] - start[i]) / path_length;
201 captured_vector_state_ = active_ && vector_mode_;
202 captured_output_history_only_ =
false;
203 capture_state(along, q_s, q_ss, tick, axis_count);
213 const std::array<double, MaxAxes> &start,
214 const std::array<double, MaxAxes> &finish,
215 std::size_t axis_count)
217 has_velocity_ =
false;
218 captured_vector_state_ =
false;
219 if(!motion_active || path_length <= 0.0 || axis_count < 2) {
224 std::array<double, MaxAxes> q_s{};
225 std::array<double, MaxAxes> q_ss{};
226 circular_derivatives(arc, along.
position, path_length, start, finish,
227 axis_count, q_s, q_ss,
nullptr);
228 captured_vector_state_ =
true;
229 captured_output_history_only_ =
false;
230 capture_state(along, q_s, q_ss, tick, axis_count);
235 const std::array<double, MaxAxes> &q_s,
236 const std::array<double, MaxAxes> &q_ss,
237 std::int64_t tick, std::size_t axis_count)
239 for(std::size_t i = 0; i < axis_count; ++i) {
240 velocity_[i] = along.
velocity * q_s[i];
244 if(active_ && vector_mode_) {
245 for(std::size_t i = 0; i < axis_count; ++i) {
254 for(std::size_t i = 0; i < axis_count; ++i) {
255 velocity_[i] += lat.velocity * lateral_dir_[i];
256 acceleration_[i] += lat.acceleration * lateral_dir_[i];
259 output_velocity_ = velocity_;
260 output_acceleration_ = acceleration_;
261 has_velocity_ =
true;
265 const otg::Limits1D &limits,
266 const std::array<otg::State1D, MaxAxes> &residuals,
267 std::size_t axis_count,
double state_threshold,
268 std::array<otg::Profile1D, MaxAxes> &profiles,
269 std::int64_t &duration)
const
272 std::array<bool, MaxAxes> active_residuals{};
273 for(std::size_t i = 0; i < axis_count; ++i) {
274 const otg::State1D residual = residuals[i];
275 if(std::fabs(residual.position) <= state_threshold &&
276 std::fabs(residual.velocity) <= state_threshold &&
277 std::fabs(residual.acceleration) <= state_threshold) {
280 active_residuals[i] =
true;
281 const rt::Result<otg::Profile1D> optimal =
284 return optimal.error();
286 duration = std::max(duration, optimal.value().duration_cycles());
293 std::array<otg::Profile1D, MaxAxes> planned{};
294 for(std::size_t i = 0; i < axis_count; ++i) {
295 if(!active_residuals[i]) {
298 const rt::Result<otg::Profile1D>
fixed =
302 return fixed.error();
304 planned[i] =
fixed.value();
319 std::size_t axis_count,
320 const std::array<double, MaxAxes> &start,
321 const std::array<double, MaxAxes> &finish,
323 std::int64_t &active_duration)
325 constexpr double kBeta = 0.5;
326 constexpr double kAlignedThreshold = 1e-9;
331 std::array<double, MaxAxes> q_s{};
333 for(std::size_t i = 0; i < axis_count; ++i) {
334 q_s[i] = (finish[i] - start[i]) / longest;
340 double q_s_norm_sq = 0.0;
341 double s_dot_0 = 0.0;
343 for(std::size_t i = 0; i < axis_count; ++i) {
344 q_s_norm_sq += q_s[i] * q_s[i];
345 s_dot_0 += velocity_[i] * q_s[i];
346 a_s0 += acceleration_[i] * q_s[i];
348 if(q_s_norm_sq > 0.0) {
349 s_dot_0 /= q_s_norm_sq;
354 double lat_speed_sq = 0.0;
355 std::array<double, MaxAxes> lat_vel{};
356 for(std::size_t i = 0; i < axis_count; ++i) {
357 lat_vel[i] = velocity_[i] - s_dot_0 * q_s[i];
358 lat_speed_sq += lat_vel[i] * lat_vel[i];
360 const double lat_speed = std::sqrt(lat_speed_sq);
362 if(lat_speed <= kAlignedThreshold) {
368 {0.0, s_dot_0, a_s0}, {longest, 0.0, 0.0}, limits);
370 return along.
error();
372 active_profile = along.
value();
382 for(std::size_t i = 0; i < axis_count; ++i) {
383 lateral_dir_[i] = lat_vel[i] / lat_speed;
400 lateral_profile_ = lat.
value();
401 duration_ = lateral_profile_.duration_cycles();
404 double max_disp = 0.0;
405 for(std::int64_t t = 1; t <= duration_; ++t) {
408 const double d = std::fabs(s.position);
437 const double stop_dist =
439 if(stop_dist > longest * 1.5) {
443 {0.0, s_dot_0, a_s0}, {longest, 0.0, 0.0}, along_limits);
445 return along.
error();
447 active_profile = along.
value();
453 active_duration = duration_;
465 std::size_t axis_count,
466 const std::array<double, MaxAxes> &start,
467 const std::array<double, MaxAxes> &finish,
469 std::int64_t &active_duration,
470 bool euclidean_metric =
false)
472 constexpr double kBeta = 0.5;
473 constexpr double kStateThreshold = 1e-12;
479 for(std::size_t i = 0; i < axis_count; ++i) {
482 if(path_length <= 0.0) {
483 vector_mode_ =
false;
487 std::array<double, MaxAxes> q_s{};
488 double q_s_norm_sq = 0.0;
489 double s_dot_0 = 0.0;
491 for(std::size_t i = 0; i < axis_count; ++i) {
492 q_s[i] = (finish[i] - start[i]) / path_length;
493 q_s_norm_sq += q_s[i] * q_s[i];
494 s_dot_0 += velocity_[i] * q_s[i];
495 a_s0 += acceleration_[i] * q_s[i];
497 if(q_s_norm_sq <= 1e-18) {
498 vector_mode_ =
false;
501 s_dot_0 /= q_s_norm_sq;
504 double entry_analytic_l2_sq = 0.0;
505 double entry_actual_l2_sq = 0.0;
506 for(std::size_t i = 0; i < axis_count; ++i) {
507 entry_analytic_l2_sq += velocity_[i] * velocity_[i];
508 entry_actual_l2_sq += output_velocity_[i] * output_velocity_[i];
510 const double entry_analytic_l2 = std::sqrt(entry_analytic_l2_sq);
511 const double entry_actual_l2 = std::sqrt(entry_actual_l2_sq);
513 bool has_residual =
false;
514 for(std::size_t i = 0; i < axis_count; ++i) {
515 const double residual_velocity =
516 velocity_[i] - q_s[i] * s_dot_0;
517 const double residual_acceleration =
518 acceleration_[i] - q_s[i] * a_s0;
519 if(std::fabs(residual_velocity) > kStateThreshold ||
520 std::fabs(residual_acceleration) > kStateThreshold) {
527 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, limits);
529 vector_mode_ =
false;
530 return along.
error();
532 const std::int64_t along_duration =
533 along.
value().duration_cycles();
534 if(!validate_linear_profiles(
535 limits, path_length, q_s, axis_count, along.
value(),
536 lateral_profiles_, output_velocity_, output_acceleration_,
537 along_duration, euclidean_metric, entry_analytic_l2,
539 vector_mode_ =
false;
542 active_profile = along.
value();
543 active_duration = along_duration;
544 vector_mode_ =
false;
559 const double stop_dist =
561 if(stop_dist > path_length) {
562 vector_mode_ =
false;
566 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, along_limits);
568 vector_mode_ =
false;
569 return along.
error();
571 active_profile = along.
value();
574 const double lateral_v_limit =
575 euclidean_metric && axis_count > 1
577 std::sqrt(
static_cast<double>(axis_count))
585 std::array<otg::State1D, MaxAxes> residuals{};
586 for(std::size_t i = 0; i < axis_count; ++i) {
589 velocity_[i] - q_s[i] * s_dot_0,
590 acceleration_[i] - q_s[i] * a_s0,
593 std::array<otg::Profile1D, MaxAxes> planned_lateral{};
594 std::int64_t planned_duration = 0;
595 const rt::ErrorCode lateral_error = plan_synchronized_residual_profiles(
596 lateral_limits, residuals, axis_count, kStateThreshold,
597 planned_lateral, planned_duration);
599 vector_mode_ =
false;
600 return lateral_error;
602 lateral_profiles_ = planned_lateral;
603 duration_ = planned_duration;
604 active_duration = std::max(active_duration, duration_);
605 if(!validate_linear_profiles(limits, path_length, q_s, axis_count,
606 active_profile, lateral_profiles_,
607 output_velocity_, output_acceleration_,
608 active_duration, euclidean_metric,
609 entry_analytic_l2, entry_actual_l2)) {
615 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
616 bounded_limits, bounded_profile);
617 std::int64_t bounded_duration = std::max(
621 validate_linear_profiles(
622 limits, path_length, q_s, axis_count, bounded_profile,
623 lateral_profiles_, output_velocity_, output_acceleration_,
624 bounded_duration, euclidean_metric, entry_analytic_l2,
627 bounded = make_bounded_stop_then_go(
628 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
629 bounded_limits, bounded_profile);
630 bounded_duration = std::max(
634 validate_linear_profiles(
635 limits, path_length, q_s, axis_count,
636 bounded_profile, lateral_profiles_, output_velocity_,
637 output_acceleration_, bounded_duration,
638 euclidean_metric, entry_analytic_l2, entry_actual_l2);
641 vector_mode_ =
false;
644 active_profile = bounded_profile;
645 active_duration = bounded_duration;
647 for(std::int64_t tick = 1; tick <= duration_; ++tick) {
648 double radius_sq = 0.0;
649 for(std::size_t i = 0; i < axis_count; ++i) {
654 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
656 active_ = duration_ > 0;
666 std::size_t axis_count,
667 const std::array<double, MaxAxes> &start,
668 const std::array<double, MaxAxes> &finish,
671 std::int64_t &active_duration)
673 constexpr double kBeta = 0.5;
674 constexpr double kStateThreshold = 1e-12;
680 for(std::size_t i = 0; i < axis_count; ++i) {
683 if(path_length <= 0.0 || axis_count < 2) {
684 vector_mode_ =
false;
688 std::array<double, MaxAxes> q_s{};
689 std::array<double, MaxAxes> q_ss{};
690 circular_derivatives(arc, 0.0, path_length, start, finish, axis_count,
692 double q_s_norm_sq = 0.0;
693 double s_dot_0 = 0.0;
694 for(std::size_t i = 0; i < axis_count; ++i) {
695 q_s_norm_sq += q_s[i] * q_s[i];
696 s_dot_0 += velocity_[i] * q_s[i];
698 if(q_s_norm_sq <= 1e-18) {
699 vector_mode_ =
false;
702 s_dot_0 /= q_s_norm_sq;
710 for(std::size_t i = 0; i < axis_count; ++i) {
711 a_s0 += (acceleration_[i] -
712 q_ss[i] * s_dot_0 * s_dot_0) * q_s[i];
715 if(s_dot_0 <= kStateThreshold && a_s0 < 0.0) {
723 limits, kBeta, arc, path_length, start, finish, axis_count);
728 vector_mode_ =
false;
737 const double stop_dist =
739 if(stop_dist > path_length) {
740 vector_mode_ =
false;
744 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, along_limits);
746 vector_mode_ =
false;
747 return along.
error();
749 active_profile = along.
value();
758 std::array<otg::State1D, MaxAxes> residuals{};
759 for(std::size_t i = 0; i < axis_count; ++i) {
760 const double base_acceleration =
761 q_ss[i] * s_dot_0 * s_dot_0 + q_s[i] * a_s0;
764 velocity_[i] - q_s[i] * s_dot_0,
765 acceleration_[i] - base_acceleration,
768 std::array<otg::Profile1D, MaxAxes> planned_lateral{};
769 std::int64_t planned_duration = 0;
770 const rt::ErrorCode lateral_error = plan_synchronized_residual_profiles(
771 lateral_limits, residuals, axis_count, kStateThreshold,
772 planned_lateral, planned_duration);
774 vector_mode_ =
false;
775 return lateral_error;
777 lateral_profiles_ = planned_lateral;
778 duration_ = planned_duration;
779 active_duration = std::max(active_duration, duration_);
781 if(!validate_circular_profiles(limits, path_length, axis_count,
782 start, finish, arc, active_profile,
783 lateral_profiles_, output_velocity_,
784 output_acceleration_, active_duration)) {
797 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
798 bounded_limits, bounded_profile);
799 std::int64_t bounded_duration = std::max(
803 validate_circular_profiles(
804 limits, path_length, axis_count, start, finish, arc,
805 bounded_profile, lateral_profiles_, output_velocity_,
806 output_acceleration_, bounded_duration);
808 bounded = make_bounded_stop_then_go(
809 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
810 bounded_limits, bounded_profile);
811 bounded_duration = std::max(
815 validate_circular_profiles(
816 limits, path_length, axis_count, start, finish, arc,
817 bounded_profile, lateral_profiles_, output_velocity_,
818 output_acceleration_, bounded_duration);
821 vector_mode_ =
false;
824 active_profile = bounded_profile;
825 active_duration = bounded_duration;
828 for(std::int64_t tick = 1; tick <= duration_; ++tick) {
829 double radius_sq = 0.0;
830 for(std::size_t i = 0; i < axis_count; ++i) {
835 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
837 active_ = duration_ > 0;
847 std::size_t axis_count,
848 const std::array<double, MaxAxes> &start,
849 const std::array<double, MaxAxes> &finish,
851 std::int64_t current_tick,
852 const std::array<double, MaxAxes> &initial_output_velocity,
853 const std::array<double, MaxAxes> &initial_output_acceleration,
855 std::int64_t &halt_duration,
856 bool euclidean_metric =
false)
858 constexpr double kBeta = 0.5;
859 constexpr double kStateThreshold = 1e-12;
860 if(!active_ || !vector_mode_ || path_length <= 0.0) {
864 std::array<double, MaxAxes> q_s{};
865 for(std::size_t i = 0; i < axis_count; ++i) {
866 q_s[i] = (finish[i] - start[i]) / path_length;
876 std::array<otg::State1D, MaxAxes> live_lateral{};
877 for(std::size_t i = 0; i < axis_count; ++i) {
885 stopped_along, reduced_along, along_limits.
max_jerk);
889 double brake_velocity = reduced_along.
velocity;
890 if(brake_velocity < 0.0) {
891 brake_velocity = 0.0;
893 const double stop_position =
897 const double path_tolerance =
898 1e-12 * std::max(1.0, path_length);
899 if(stop_position > path_length + path_tolerance) {
903 reduced_along, {stop_position, 0.0, 0.0}, along_limits);
905 return along_tail.
error();
907 reduced = append_profile(stopped_along, along_tail.
value());
915 const double lateral_acceleration_limit =
918 const double lateral_v_limit =
919 euclidean_metric && axis_count > 1
921 std::sqrt(
static_cast<double>(axis_count))
925 lateral_acceleration_limit,
926 lateral_acceleration_limit,
929 std::array<otg::Profile1D, MaxAxes> stopped_lateral{};
930 std::int64_t lateral_duration = 0;
931 for(std::size_t i = 0; i < axis_count; ++i) {
933 if(std::fabs(live.
position) <= kStateThreshold &&
934 std::fabs(live.
velocity) <= kStateThreshold &&
939 reduced = append_acceleration_zeroing(
940 stopped_lateral[i], reduced_lateral,
947 reduced_lateral, {0.0, 0.0, 0.0}, lateral_limits);
949 return lateral_tail.
error();
951 reduced = append_profile(stopped_lateral[i], lateral_tail.
value());
955 lateral_duration = std::max(
956 lateral_duration, stopped_lateral[i].duration_cycles());
959 const std::int64_t total_duration = std::max(
962 for(std::size_t i = 0; i < axis_count; ++i) {
963 const double analytic_acceleration =
965 live_lateral[i].acceleration;
968 std::max(initial_output_acceleration[i],
969 analytic_acceleration));
972 std::max(-initial_output_acceleration[i],
973 -analytic_acceleration));
975 double entry_analytic_l2_sq = 0.0;
976 double entry_actual_l2_sq = 0.0;
977 for(std::size_t i = 0; i < axis_count; ++i) {
978 const double analytic_v =
979 q_s[i] * along_state.
velocity + live_lateral[i].velocity;
980 entry_analytic_l2_sq += analytic_v * analytic_v;
981 entry_actual_l2_sq +=
982 initial_output_velocity[i] * initial_output_velocity[i];
984 const double entry_analytic_l2 = std::sqrt(entry_analytic_l2_sq);
985 const double entry_actual_l2 = std::sqrt(entry_actual_l2_sq);
986 if(!validate_linear_profiles(
987 validation_limits, path_length, q_s, axis_count,
988 stopped_along, stopped_lateral, initial_output_velocity,
989 initial_output_acceleration, total_duration, euclidean_metric,
990 entry_analytic_l2, entry_actual_l2)) {
994 halt_profile = stopped_along;
995 halt_duration = total_duration;
996 lateral_profiles_ = stopped_lateral;
997 duration_ = lateral_duration;
999 for(std::int64_t tick = 0; tick <= duration_; ++tick) {
1000 double radius_sq = 0.0;
1001 for(std::size_t i = 0; i < axis_count; ++i) {
1006 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
1008 active_ = duration_ > 0;
1009 vector_mode_ = active_;
1019 std::size_t axis_count,
1020 const std::array<double, MaxAxes> &start,
1021 const std::array<double, MaxAxes> &finish,
1024 std::int64_t current_tick,
1025 const std::array<double, MaxAxes> &initial_output_velocity,
1026 const std::array<double, MaxAxes> &initial_output_acceleration,
1028 std::int64_t &halt_duration)
1030 constexpr double kBeta = 0.5;
1031 constexpr double kStateThreshold = 1e-12;
1032 if(!active_ || !vector_mode_ || path_length <= 0.0 || axis_count < 2) {
1039 limits, kBeta, arc, path_length, start, finish, axis_count);
1047 std::array<otg::State1D, MaxAxes> live_lateral{};
1048 for(std::size_t i = 0; i < axis_count; ++i) {
1056 stopped_along, reduced_along, along_limits.
max_jerk);
1060 double brake_velocity = reduced_along.
velocity;
1061 if(brake_velocity < 0.0) {
1062 brake_velocity = 0.0;
1064 const double stop_position =
1068 if(stop_position < -1e-9 || stop_position > path_length + 1e-9) {
1072 reduced_along, {stop_position, 0.0, 0.0}, along_limits);
1074 return along_tail.
error();
1076 reduced = append_profile(stopped_along, along_tail.
value());
1087 std::array<otg::Profile1D, MaxAxes> stopped_lateral{};
1088 std::int64_t lateral_duration = 0;
1089 for(std::size_t i = 0; i < axis_count; ++i) {
1091 if(std::fabs(live.
position) <= kStateThreshold &&
1092 std::fabs(live.
velocity) <= kStateThreshold &&
1097 reduced = append_acceleration_zeroing(
1098 stopped_lateral[i], reduced_lateral,
1105 reduced_lateral, {0.0, 0.0, 0.0}, lateral_limits);
1107 return lateral_tail.
error();
1109 reduced = append_profile(stopped_lateral[i], lateral_tail.
value());
1113 lateral_duration = std::max(
1114 lateral_duration, stopped_lateral[i].duration_cycles());
1117 const std::int64_t total_duration = std::max(
1120 std::array<double, MaxAxes> q_s{};
1121 std::array<double, MaxAxes> q_ss{};
1122 circular_derivatives(arc, along_state.
position, path_length, start,
1123 finish, axis_count, q_s, q_ss,
nullptr);
1124 for(std::size_t i = 0; i < axis_count; ++i) {
1125 const double analytic_acceleration =
1128 live_lateral[i].acceleration;
1131 std::max(initial_output_acceleration[i],
1132 analytic_acceleration));
1135 std::max(-initial_output_acceleration[i],
1136 -analytic_acceleration));
1138 if(!validate_circular_profiles(
1139 validation_limits, path_length, axis_count, start, finish, arc,
1140 stopped_along, stopped_lateral, initial_output_velocity,
1141 initial_output_acceleration, total_duration)) {
1145 halt_profile = stopped_along;
1146 halt_duration = total_duration;
1147 lateral_profiles_ = stopped_lateral;
1148 duration_ = lateral_duration;
1150 for(std::int64_t tick = 0; tick <= duration_; ++tick) {
1151 double radius_sq = 0.0;
1152 for(std::size_t i = 0; i < axis_count; ++i) {
1157 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
1159 active_ = duration_ > 0;
1160 vector_mode_ = active_;
1165 std::array<double, MaxAxes> &offsets,
1166 std::size_t axis_count)
1169 if(!active_ || !vector_mode_) {
1172 for(std::size_t i = 0; i < axis_count; ++i) {
1176 if(tick >= duration_) {
1192 if(tick >= duration_) {
1200 return lateral_dir_[index];
1210 profile, start, finish, limits);
1214 const otg::State1D &start,
const otg::Target1D &finish,
1215 const otg::Limits1D &limits, otg::Profile1D &profile)
1217 constexpr double kTolerance = 1e-12;
1218 if(finish.velocity != 0.0 || finish.acceleration != 0.0) {
1222 profile = otg::Profile1D{};
1225 profile, stopped, limits.max_jerk);
1230 profile, stopped, 0.0, limits,
1236 const double position_tolerance =
1237 kTolerance * std::max(1.0, std::fabs(
finish.position));
1238 if(
stopped.position >
finish.position + position_tolerance) {
1242 profile, stopped, finish, limits);
1245 static rt::ErrorCode append_profile(otg::Profile1D &destination,
1246 const otg::Profile1D &source)
1248 for(std::size_t i = 0; i < source.segment_count(); ++i) {
1249 const rt::ErrorCode added = destination.add_segment(source.segment(i));
1258 otg::Profile1D &profile, otg::State1D &state,
double max_jerk)
1260 if(state.acceleration == 0.0) {
1263 const double cycles =
1264 std::ceil(std::fabs(state.acceleration) / max_jerk);
1266 profile, state, -state.acceleration / cycles, cycles);
1269 static bool validate_linear_profiles(
1270 const otg::Limits1D &limits,
1272 const std::array<double, MaxAxes> &q_s,
1273 std::size_t axis_count,
1274 const otg::Profile1D &along_profile,
1275 const std::array<otg::Profile1D, MaxAxes> &lateral_profiles,
1276 const std::array<double, MaxAxes> &initial_velocity,
1277 const std::array<double, MaxAxes> &initial_acceleration,
1278 std::int64_t total_duration,
1279 bool euclidean_metric =
false,
1280 double entry_analytic_l2 = 0.0,
1281 double entry_actual_l2 = 0.0)
1283 constexpr double kTolerance = 1e-9;
1284 const double path_tolerance =
1285 1e-12 * std::max(1.0, path_length);
1286 const double analytic_l2_cap =
1287 entry_analytic_l2 > limits.max_velocity ? entry_analytic_l2
1288 : limits.max_velocity;
1289 const double actual_l2_cap =
1290 entry_actual_l2 > limits.max_velocity ? entry_actual_l2
1291 : limits.max_velocity;
1292 bool analytic_in_l2_budget =
1293 entry_analytic_l2 <= limits.max_velocity + kTolerance;
1294 bool actual_in_l2_budget =
1295 entry_actual_l2 <= limits.max_velocity + kTolerance;
1296 std::array<double, MaxAxes> previous_lateral_acceleration{};
1297 std::array<double, MaxAxes> previous_position{};
1298 std::array<double, MaxAxes> previous_velocity = initial_velocity;
1299 std::array<double, MaxAxes> previous_acceleration =
1300 initial_acceleration;
1301 otg::State1D previous_along{};
1302 bool terminal_clamp_entered =
false;
1303 for(std::int64_t tick = 0; tick <= total_duration; ++tick) {
1306 if(terminal_clamp_entered &&
1307 along.position < path_length - path_tolerance) {
1310 if(along.position >= path_length - path_tolerance) {
1311 terminal_clamp_entered =
true;
1313 const double sampled_path =
1314 std::min(along.position, path_length);
1315 const double scalar_jerk =
1318 : along.acceleration - previous_along.acceleration;
1319 double analytic_v_sq = 0.0;
1320 double actual_v_sq = 0.0;
1321 for(std::size_t i = 0; i < axis_count; ++i) {
1324 const double lateral_jerk =
1327 : lateral.acceleration -
1328 previous_lateral_acceleration[i];
1329 const double velocity =
1330 q_s[i] * along.velocity + lateral.velocity;
1331 const double acceleration =
1332 q_s[i] * along.acceleration + lateral.acceleration;
1334 q_s[i] * scalar_jerk + lateral_jerk;
1335 analytic_v_sq += velocity * velocity;
1336 if(along.position <= path_length + path_tolerance) {
1337 if(std::fabs(velocity) >
1338 limits.max_velocity + kTolerance ||
1340 limits.max_acceleration + kTolerance ||
1342 -limits.max_deceleration - kTolerance ||
1345 limits.max_jerk + kTolerance)) {
1350 const double position =
1351 q_s[i] * sampled_path + lateral.position;
1354 position - previous_position[i];
1355 const double actual_acceleration =
1357 const double actual_jerk =
1358 actual_acceleration - previous_acceleration[i];
1361 limits.max_velocity + kTolerance ||
1362 actual_acceleration >
1363 limits.max_acceleration + kTolerance ||
1364 actual_acceleration <
1365 -limits.max_deceleration - kTolerance ||
1366 std::fabs(actual_jerk) >
1367 limits.max_jerk + kTolerance) {
1371 previous_acceleration[i] = actual_acceleration;
1373 previous_position[i] = position;
1374 previous_lateral_acceleration[i] = lateral.acceleration;
1376 if(euclidean_metric) {
1377 if(along.position <= path_length + path_tolerance) {
1378 const double analytic_l2 = std::sqrt(analytic_v_sq);
1379 const double cap_now = analytic_in_l2_budget
1380 ? limits.max_velocity
1382 if(analytic_l2 > cap_now + kTolerance) {
1385 if(analytic_l2 <= limits.max_velocity + kTolerance) {
1386 analytic_in_l2_budget =
true;
1390 const double actual_l2 = std::sqrt(actual_v_sq);
1391 const double cap_now = actual_in_l2_budget
1392 ? limits.max_velocity
1394 if(actual_l2 > cap_now + kTolerance) {
1397 if(actual_l2 <= limits.max_velocity + kTolerance) {
1398 actual_in_l2_budget =
true;
1402 previous_along = along;
1407 static void circular_derivatives(
1408 const geom::ArcSegment &arc,
double arclength,
double path_length,
1409 const std::array<double, MaxAxes> &start,
1410 const std::array<double, MaxAxes> &finish,
1411 std::size_t axis_count,
1412 std::array<double, MaxAxes> &q_s,
1413 std::array<double, MaxAxes> &q_ss,
1414 std::array<double, MaxAxes> *q_sss)
1418 if(q_sss !=
nullptr) {
1427 if(q_sss !=
nullptr) {
1429 (*q_sss)[0] = third.x;
1430 (*q_sss)[1] = third.y;
1432 for(std::size_t i = 2; i < axis_count; ++i) {
1433 q_s[i] = (
finish[i] - start[i]) / path_length;
1437 static otg::Limits1D circular_along_limits(
1438 const otg::Limits1D &limits,
double beta,
1439 const geom::ArcSegment &arc,
double path_length,
1440 const std::array<double, MaxAxes> &start,
1441 const std::array<double, MaxAxes> &finish,
1442 std::size_t axis_count)
1444 double max_qs = 1.0;
1445 for(std::size_t i = 2; i < axis_count; ++i) {
1446 max_qs = std::max(max_qs,
1447 std::fabs((finish[i] - start[i]) / path_length));
1449 const double max_qss = 1.0 /
arc.radius;
1450 const double max_qsss = max_qss * max_qss;
1451 const double curvature_acceleration =
1452 0.5 * beta * std::min(limits.max_acceleration,
1453 limits.max_deceleration);
1454 const double jerk_budget = beta * limits.max_jerk;
1456 otg::Limits1D result{
1457 beta * limits.max_velocity / max_qs,
1458 0.5 * beta * limits.max_acceleration / max_qs,
1459 0.5 * beta * limits.max_deceleration / max_qs,
1460 jerk_budget / (3.0 * max_qs),
1462 result.max_velocity = std::min(
1463 result.max_velocity,
1464 std::sqrt(curvature_acceleration / max_qss));
1465 result.max_velocity = std::min(
1466 result.max_velocity,
1467 std::cbrt(jerk_budget / (3.0 * max_qsss)));
1468 if(result.max_velocity > 1e-15) {
1469 const double cross_term_cap =
1470 jerk_budget / (9.0 * max_qss * result.max_velocity);
1471 result.max_acceleration =
1472 std::min(result.max_acceleration, cross_term_cap);
1473 result.max_deceleration =
1474 std::min(result.max_deceleration, cross_term_cap);
1479 bool validate_circular_profiles(
1480 const otg::Limits1D &limits,
double path_length,
1481 std::size_t axis_count,
1482 const std::array<double, MaxAxes> &start,
1483 const std::array<double, MaxAxes> &finish,
1484 const geom::ArcSegment &arc,
1485 const otg::Profile1D &along_profile,
1486 const std::array<otg::Profile1D, MaxAxes> &lateral_profiles,
1487 const std::array<double, MaxAxes> &initial_velocity,
1488 const std::array<double, MaxAxes> &initial_acceleration,
1489 std::int64_t total_duration)
const
1491 constexpr double kTolerance = 1e-9;
1492 const double path_tolerance =
1493 1e-12 * std::max(1.0, path_length);
1494 std::array<double, MaxAxes> previous_lateral_acceleration{};
1495 std::array<double, MaxAxes> previous_position{};
1496 std::array<double, MaxAxes> previous_velocity = initial_velocity;
1497 std::array<double, MaxAxes> previous_acceleration =
1498 initial_acceleration;
1499 otg::State1D previous_along{};
1500 bool terminal_clamp_entered =
false;
1501 for(std::int64_t tick = 0; tick <= total_duration; ++tick) {
1504 if(along.position < -path_tolerance ||
1505 (terminal_clamp_entered &&
1506 along.position < path_length - path_tolerance)) {
1509 if(along.position >= path_length - path_tolerance) {
1510 terminal_clamp_entered =
true;
1512 const double sampled_path =
1513 std::min(std::max(along.position, 0.0), path_length);
1514 const geom::Vec3 point =
geom::sample(arc, sampled_path);
1515 std::array<double, MaxAxes> q_s{};
1516 std::array<double, MaxAxes> q_ss{};
1517 std::array<double, MaxAxes> q_sss{};
1518 circular_derivatives(arc, sampled_path, path_length, start,
1519 finish, axis_count, q_s, q_ss, &q_sss);
1520 const double scalar_jerk =
1521 tick == 0 ? 0.0 : along.acceleration - previous_along.acceleration;
1523 for(std::size_t i = 0; i < axis_count; ++i) {
1526 const double lateral_jerk =
1528 : lateral.acceleration -
1529 previous_lateral_acceleration[i];
1530 const double velocity = q_s[i] * along.velocity +
1532 const double acceleration =
1533 q_ss[i] * along.velocity * along.velocity +
1534 q_s[i] * along.acceleration + lateral.acceleration;
1536 q_sss[i] * along.velocity * along.velocity * along.velocity +
1537 3.0 * q_ss[i] * along.velocity * along.acceleration +
1538 q_s[i] * scalar_jerk + lateral_jerk;
1539 if(along.position <= path_length + path_tolerance) {
1540 if(std::fabs(velocity) >
1541 limits.max_velocity + kTolerance ||
1543 limits.max_acceleration + kTolerance ||
1545 -limits.max_deceleration - kTolerance ||
1548 limits.max_jerk + kTolerance)) {
1553 double position = 0.0;
1559 position = start[i] +
1561 (sampled_path / path_length);
1563 position += lateral.position;
1566 position - previous_position[i];
1567 const double actual_acceleration =
1569 const double actual_jerk =
1570 actual_acceleration - previous_acceleration[i];
1572 limits.max_velocity + kTolerance ||
1573 actual_acceleration >
1574 limits.max_acceleration + kTolerance ||
1575 actual_acceleration <
1576 -limits.max_deceleration - kTolerance ||
1577 std::fabs(actual_jerk) >
1578 limits.max_jerk + kTolerance) {
1582 previous_acceleration[i] = actual_acceleration;
1584 previous_position[i] = position;
1585 previous_lateral_acceleration[i] = lateral.acceleration;
1587 previous_along = along;
1592 std::int64_t duration_ = 0;
1593 double r_tube_ = 0.0;
1594 std::array<double, MaxAxes> lateral_dir_{};
1595 std::array<double, MaxAxes> velocity_{};
1596 std::array<double, MaxAxes> acceleration_{};
1597 std::array<double, MaxAxes> output_velocity_{};
1598 std::array<double, MaxAxes> output_acceleration_{};
1599 std::array<otg::Profile1D, MaxAxes> lateral_profiles_{};
1600 otg::Profile1D lateral_profile_{};
1601 bool active_ =
false;
1602 bool has_velocity_ =
false;
1603 bool vector_mode_ =
false;
1604 bool captured_vector_state_ =
false;
1605 bool captured_output_history_only_ =
false;