101 if(session_started_ || mode_ !=
Mode::idle || have_target_ || have_profile_ ||
126 session_started_ =
true;
130 have_target_ =
false;
131 have_profile_ =
false;
132 quintic_active_ =
false;
134 pending_dirty_ =
false;
136 stream_interval_ = 1;
137 rejected_targets_ = 0;
140 if(
state.velocity != 0.0 ||
state.acceleration != 0.0) {
151 session_started_ =
false;
153 have_target_ =
false;
154 have_profile_ =
false;
155 pending_dirty_ =
false;
156 quintic_active_ =
false;
166 config_.limits.max_velocity <= 0.0) {
171 have_target_ =
false;
172 have_profile_ =
false;
173 quintic_active_ =
false;
175 pending_dirty_ =
false;
177 if(config_.extrapolation_cycles > 0 &&
state.velocity != 0.0) {
179 extrapolation_tick_ = 0;
180 extrapolation_start_velocity_ =
state.velocity;
181 synthetic_position_ =
state.position;
182 }
else if(
state.velocity != 0.0 ||
state.acceleration != 0.0) {
195 if(!std::isfinite(target.
position) ||
203 const std::uint64_t timestamp_delta =
204 have_target_ ? positive_cycle_delta(target.
timestamp_cycles, latest_timestamp_) : 0;
208 if(config_.position_envelope_enabled) {
209 if(position < config_.min_position) {
210 position = config_.min_position;
212 }
else if(position > config_.max_position) {
213 position = config_.max_position;
218 double velocity = 0.0;
220 velocity = clamp_velocity(target.
velocity);
221 }
else if(have_target_) {
222 const double span =
static_cast<double>(timestamp_delta);
223 velocity = clamp_velocity((position - latest_position_) / span);
227 stream_interval_ = timestamp_delta > 256
229 :
static_cast<std::int64_t
>(timestamp_delta);
232 const bool repeated_rest_target =
234 position == latest_position_ && velocity == 0.0 &&
235 latest_velocity_ == 0.0;
237 latest_position_ = position;
238 latest_velocity_ = velocity;
246 if(!repeated_rest_target) {
247 pending_position_ = position;
248 pending_velocity_ = velocity;
249 pending_dirty_ =
true;
270 if(now_ < INT64_MAX) {
275 positive_cycle_delta(now_, latest_timestamp_) >
276 static_cast<std::uint64_t
>(config_.timeout_cycles)) {
278 if(config_.extrapolation_cycles > 0 && state_.velocity != 0.0) {
280 extrapolation_tick_ = 0;
281 extrapolation_start_velocity_ = state_.velocity;
282 synthetic_position_ = state_.position;
289 ++extrapolation_tick_;
290 const double fraction =
291 1.0 -
static_cast<double>(extrapolation_tick_) /
292 static_cast<double>(config_.extrapolation_cycles);
293 if(fraction <= 0.0) {
296 const double velocity = extrapolation_start_velocity_ * fraction;
297 synthetic_position_ += velocity;
298 pending_position_ = synthetic_position_;
299 pending_velocity_ = velocity;
300 pending_dirty_ =
true;
302 }
else if(mode_ ==
Mode::tracking && pending_velocity_ != 0.0 && have_target_) {
312 pending_position_ = latest_position_;
313 if(now_ > latest_timestamp_) {
314 const double displacement =
315 pending_velocity_ *
static_cast<double>(
316 positive_cycle_delta(now_, latest_timestamp_));
317 const double projected_position = latest_position_ + displacement;
318 if(std::isfinite(displacement) && std::isfinite(projected_position)) {
319 pending_position_ = projected_position;
323 pending_velocity_ = 0.0;
324 pending_dirty_ =
true;
327 if(config_.position_envelope_enabled) {
328 if(pending_position_ < config_.min_position) {
329 pending_position_ = config_.min_position;
330 }
else if(pending_position_ > config_.max_position) {
331 pending_position_ = config_.max_position;
337 config_.position_envelope_enabled && state_.velocity != 0.0) {
338 const std::int64_t profile_duration =
339 quintic_active_ ? quintic_profile_.h : profile_.duration_cycles();
340 if(profile_tick_ >= profile_duration) {
341 const double stopping_reach =
343 const double next_position = state_.position + state_.velocity;
344 if((state_.velocity > 0.0 &&
345 next_position + stopping_reach >= config_.max_position) ||
346 (state_.velocity < 0.0 &&
347 next_position + stopping_reach <= config_.min_position)) {
348 pending_position_ = state_.velocity > 0.0 ? config_.max_position
349 : config_.min_position;
350 pending_velocity_ = 0.0;
351 pending_dirty_ =
true;
357 replan_result = replan(allow_slow_replan);
363 const std::int64_t profile_duration =
364 quintic_active_ ? quintic_profile_.h : profile_.duration_cycles();
366 profile_tick_ > profile_duration &&
367 std::fabs(state_.position - pending_position_) >
368 1e-9 * (1.0 + std::fabs(pending_position_))) {
369 pending_dirty_ =
true;
373 profile_tick_ >= profile_.duration_cycles() && state_.velocity == 0.0) {
381 return pending_dirty_;
409 return rejected_targets_;
414 return dropout_count_;
419 return filter_faults_;
423 static std::uint64_t positive_cycle_delta(std::int64_t later, std::int64_t earlier)
428 return static_cast<std::uint64_t
>(later) -
static_cast<std::uint64_t
>(earlier);
431 static bool representable_cycle_count(
double cycles, std::int64_t &result)
435 constexpr double Int64UpperExclusive = 9223372036854775808.0;
436 if(!std::isfinite(cycles) || cycles < 1.0 || cycles >= Int64UpperExclusive) {
439 result =
static_cast<std::int64_t
>(cycles);
443 double clamp_to_envelope(
double position)
const
448 if(position < config_.min_position) {
449 return config_.min_position;
451 if(position > config_.max_position) {
452 return config_.max_position;
457 double clamp_velocity(
double velocity)
const
459 if(velocity > config_.limits.max_velocity) {
460 return config_.limits.max_velocity;
462 if(velocity < -config_.limits.max_velocity) {
463 return -config_.limits.max_velocity;
468 bool position_in_envelope(
double position)
const
470 return !config_.position_envelope_enabled ||
471 (position >= config_.min_position && position <= config_.max_position);
474 static bool normalize_segment(
const otg::Segment1D &segment,
double coefficients[6])
476 const double duration =
static_cast<double>(segment.duration_cycles);
477 if(duration <= 0.0 || !std::isfinite(duration)) {
481 coefficients[0] = segment.c0;
482 coefficients[1] = segment.c1;
483 coefficients[2] = segment.c2;
484 coefficients[3] = segment.c3;
485 coefficients[4] = segment.c4;
486 coefficients[5] = segment.c5;
487 for(
int degree = 1; degree <= 5; ++degree) {
489 coefficients[degree] *= duration;
492 for(
int degree = 0; degree <= 5; ++degree) {
493 if(!std::isfinite(coefficients[degree])) {
500 static double normalized_position(
const double coefficients[6],
double sigma)
502 return coefficients[0] +
503 sigma * (coefficients[1] +
504 sigma * (coefficients[2] +
505 sigma * (coefficients[3] +
506 sigma * (coefficients[4] +
507 sigma * coefficients[5]))));
510 static double normalized_velocity(
const double coefficients[6],
double sigma)
512 return coefficients[1] +
513 sigma * (2.0 * coefficients[2] +
514 sigma * (3.0 * coefficients[3] +
515 sigma * (4.0 * coefficients[4] +
516 sigma * 5.0 * coefficients[5])));
519 static bool solve_quadratic_for_proof(
double a,
double b,
double c,
520 double roots[3],
int &count)
522 if(!std::isfinite(a) || !std::isfinite(b) || !std::isfinite(c)) {
532 return std::isfinite(roots[0]);
535 const double scale = std::max(std::fabs(a), std::max(std::fabs(b), std::fabs(c)));
542 if(std::fabs(a) < 1e-30) {
546 for(
int i = 0; i <
count; ++i) {
547 if(!std::isfinite(roots[i])) {
554 static bool solve_cubic_for_proof(
double a3,
double a2,
double a1,
double a0,
555 double roots[3],
int &count)
557 if(!std::isfinite(a3) || !std::isfinite(a2) || !std::isfinite(a1) ||
558 !std::isfinite(a0)) {
562 return solve_quadratic_for_proof(a2, a1, a0, roots, count);
566 std::max(std::fabs(a3),
567 std::max(std::fabs(a2), std::max(std::fabs(a1), std::fabs(a0))));
572 if(std::fabs(a3) < 1e-30) {
576 for(
int i = 0; i <
count; ++i) {
577 if(!std::isfinite(roots[i])) {
584 bool segment_in_envelope(
const otg::Segment1D &segment)
const
586 double coefficients[6];
587 if(!normalize_segment(segment, coefficients)) {
590 if(!position_in_envelope(segment.start.position) ||
591 !position_in_envelope(segment.finish.position) ||
592 !position_in_envelope(normalized_position(coefficients, 0.0)) ||
593 !position_in_envelope(normalized_position(coefficients, 1.0))) {
598 if(coefficients[4] == 0.0 && coefficients[5] == 0.0) {
602 if(!solve_quadratic_for_proof(3.0 * coefficients[3],
603 2.0 * coefficients[2], coefficients[1],
607 for(
int i = 0; i <
count; ++i) {
608 if(roots[i] > 0.0 && roots[i] < 1.0 &&
609 !position_in_envelope(normalized_position(coefficients, roots[i]))) {
619 double minimum_velocity = segment.start.velocity;
620 if(!std::isfinite(minimum_velocity) ||
621 !std::isfinite(segment.finish.velocity)) {
624 double maximum_velocity = minimum_velocity;
625 if(segment.finish.velocity < minimum_velocity) {
626 minimum_velocity = segment.finish.velocity;
628 if(segment.finish.velocity > maximum_velocity) {
629 maximum_velocity = segment.finish.velocity;
632 if(!solve_cubic_for_proof(20.0 * coefficients[5], 12.0 * coefficients[4],
633 6.0 * coefficients[3], 2.0 * coefficients[2],
637 for(
int i = 0; i <
count; ++i) {
638 const double sigma = roots[i];
639 if(sigma < 0.0 || sigma > 1.0) {
642 const double velocity =
643 sigma == 0.0 ? segment.start.velocity
645 ? segment.finish.velocity
646 : normalized_velocity(coefficients, sigma));
647 if(!std::isfinite(velocity)) {
650 if(velocity < minimum_velocity) {
651 minimum_velocity = velocity;
653 if(velocity > maximum_velocity) {
654 maximum_velocity = velocity;
660 return minimum_velocity >= 0.0 || maximum_velocity <= 0.0;
663 bool profile_in_envelope(
const otg::Profile1D &profile)
const
665 if(!config_.position_envelope_enabled) {
668 for(std::size_t i = 0; i < profile.segment_count(); ++i) {
669 if(!segment_in_envelope(profile.segment(i))) {
676 bool quintic_in_envelope(
const QuinticProfile &profile)
const
678 if(!config_.position_envelope_enabled) {
683 otg::Segment1D segment{};
684 segment.duration_cycles = 1;
685 segment.c0 = profile.d[0];
686 segment.c1 = profile.d[1];
687 segment.c2 = profile.d[2];
688 segment.c3 = profile.d[3];
689 segment.c4 = profile.d[4];
690 segment.c5 = profile.d[5];
691 segment.start.position = profile.d[0];
692 segment.finish.position = profile.d[0] + profile.d[1] + profile.d[2] +
693 profile.d[3] + profile.d[4] + profile.d[5];
694 return segment_in_envelope(segment);
697 void enter_stopping()
703 pending_velocity_ = 0.0;
704 pending_dirty_ =
true;
707 rt::Result<otg::Profile1D> plan_controlled_stop(otg::State1D from)
const
709 otg::Profile1D profile{};
710 otg::State1D
state = from;
711 if(
state.acceleration != 0.0) {
712 const double cycles =
713 std::ceil(std::fabs(
state.acceleration) /
714 config_.limits.max_jerk);
715 const double finish_velocity =
716 state.velocity + 0.5 *
state.acceleration * cycles;
718 profile,
state, -
state.acceleration / cycles, cycles,
true,
719 finish_velocity, 0.0);
724 if(
state.velocity != 0.0) {
726 profile,
state, 0.0, config_.limits,
732 if(profile.segment_count() == 0 ||
state.velocity != 0.0 ||
733 state.acceleration != 0.0) {
737 for(std::size_t i = 0; i < profile.segment_count(); ++i) {
751 otg::State1D from = state_;
752 from.velocity = clamp_velocity(from.velocity);
753 if(from.acceleration > config_.limits.max_acceleration) {
754 from.acceleration = config_.limits.max_acceleration;
755 }
else if(from.acceleration < -config_.limits.max_deceleration) {
756 from.acceleration = -config_.limits.max_deceleration;
759 !std::isfinite(pending_velocity_)) {
760 pending_dirty_ =
false;
772 double through_velocity = pending_velocity_;
773 double aim = pending_position_;
774 std::int64_t rendezvous_cycles = 0;
775 if(through_velocity != 0.0) {
782 const double quantum = 1.2 * std::fabs(through_velocity);
783 const double jerk_depth = std::cbrt(60.0 * quantum / config_.limits.max_jerk);
784 const double accel_depth =
785 std::sqrt(5.77 * quantum / config_.limits.max_acceleration);
786 double horizon =
static_cast<double>(stream_interval_);
787 if(jerk_depth > horizon) {
788 horizon = jerk_depth;
790 if(accel_depth > horizon) {
791 horizon = accel_depth;
793 const double horizon_ceil = std::ceil(horizon);
794 double horizon_displacement = through_velocity * horizon_ceil;
795 double horizon_aim = pending_position_ + horizon_displacement;
797 representable_cycle_count(horizon_ceil, rendezvous_cycles) &&
798 std::isfinite(horizon_displacement) && std::isfinite(horizon_aim);
804 const double needed = from.position + reach;
805 representable = std::isfinite(reach) && std::isfinite(needed);
807 ((through_velocity > 0.0 && needed > aim) ||
808 (through_velocity < 0.0 && needed < aim))) {
809 const double merge_span = needed - pending_position_;
810 const double merge_cycles = std::ceil(merge_span / through_velocity);
811 std::int64_t merge_count = 0;
812 const double merge_displacement = through_velocity * merge_cycles;
813 const double merge_aim = pending_position_ + merge_displacement;
814 representable = std::isfinite(merge_span) &&
815 representable_cycle_count(merge_cycles, merge_count) &&
816 std::isfinite(merge_displacement) &&
817 std::isfinite(merge_aim);
820 rendezvous_cycles = merge_count;
828 pending_velocity_ = 0.0;
829 through_velocity = 0.0;
830 aim = pending_position_;
831 rendezvous_cycles = 0;
832 }
else if((through_velocity > 0.0 && aim < from.position) ||
833 (through_velocity < 0.0 && aim > from.position)) {
834 through_velocity = 0.0;
835 aim = pending_position_;
836 rendezvous_cycles = 0;
840 double constrained_aim = clamp_to_envelope(aim);
841 if(config_.position_envelope_enabled && through_velocity != 0.0) {
842 const double stopping_reach =
844 const double stopping_position = constrained_aim + stopping_reach;
845 if(!std::isfinite(stopping_reach) || !std::isfinite(stopping_position)) {
847 through_velocity > 0.0 ? config_.max_position : config_.min_position;
848 through_velocity = 0.0;
849 rendezvous_cycles = 0;
850 }
else if(through_velocity > 0.0 &&
851 stopping_position >= config_.max_position) {
852 constrained_aim = config_.max_position;
853 through_velocity = 0.0;
854 rendezvous_cycles = 0;
855 }
else if(through_velocity < 0.0 &&
856 stopping_position <= config_.min_position) {
857 constrained_aim = config_.min_position;
858 through_velocity = 0.0;
859 rendezvous_cycles = 0;
862 otg::Target1D to{constrained_aim, through_velocity, 0.0};
866 if(config_.quintic_fast_path && rendezvous_cycles > 0) {
868 if(
solve_quintic(from, to.position, to.velocity, rendezvous_cycles, qp) &&
870 quintic_profile_ = qp;
871 quintic_active_ =
true;
873 have_profile_ =
true;
874 pending_dirty_ =
false;
879 if(!allow_slow_replan) {
882 pending_dirty_ =
false;
884 rt::Result<otg::Profile1D> planned =
885 rendezvous_cycles > 0
888 : rt::Result<otg::Profile1D>::failure(
890 if(planned && !profile_in_envelope(planned.value())) {
891 planned = rt::Result<otg::Profile1D>::failure(rt::ErrorCode::infeasible);
894 planned = otg::plan_time_optimal(from, to, config_.limits);
895 if(planned && !profile_in_envelope(planned.value())) {
896 planned = rt::Result<otg::Profile1D>::failure(rt::ErrorCode::infeasible);
906 if(planned && !profile_in_envelope(planned.value())) {
911 planned =
otg::plan(from, to, config_.limits);
912 if(planned && !profile_in_envelope(planned.value())) {
917 planned = plan_controlled_stop(from);
918 if(planned && !profile_in_envelope(planned.value())) {
928 profile_ = planned.value();
929 quintic_active_ =
false;
931 have_profile_ =
true;
942 if(quintic_active_) {
943 if(profile_tick_ <= quintic_profile_.h) {
947 const otg::State1D
finish =
949 if(
finish.velocity != 0.0) {
951 {state_.position +
finish.velocity,
finish.velocity, 0.0});
953 commit_sample(finish);
958 if(profile_tick_ <= profile_.duration_cycles()) {
965 const otg::State1D
finish =
966 profile_.segment(profile_.segment_count() - 1).finish;
967 if(
finish.velocity != 0.0) {
968 commit_sample({state_.position +
finish.velocity,
finish.velocity, 0.0});
970 commit_sample(finish);
974 void commit_sample(otg::State1D candidate)
985 have_profile_ =
false;
986 quintic_active_ =
false;
989 std::int64_t now_ = 0;
990 double latest_position_ = 0.0;
991 double latest_velocity_ = 0.0;
992 std::int64_t latest_timestamp_ = 0;
993 double pending_position_ = 0.0;
994 double pending_velocity_ = 0.0;
995 std::int64_t profile_tick_ = 0;
996 std::int64_t extrapolation_tick_ = 0;
997 double extrapolation_start_velocity_ = 0.0;
998 double synthetic_position_ = 0.0;
999 std::int64_t stream_interval_ = 1;
1000 otg::State1D state_{};
1003 otg::Profile1D profile_{};
1004 Mode mode_ = Mode::idle;
1005 std::uint32_t rejected_targets_ = 0;
1006 std::uint32_t dropout_count_ = 0;
1007 std::uint32_t filter_faults_ = 0;
1008 bool have_target_ =
false;
1009 bool session_started_ =
false;
1010 bool pending_dirty_ =
false;
1011 bool quintic_active_ =
false;
1012 bool have_profile_ =
false;
1013 bool clamped_ =
false;