427 bool powered()
const {
return snapshot_.powered; }
433 if(
cycle.period_ns() <= 0) {
452 return active_ || !queue_.empty() || sync_kind_ !=
SyncKind::none || superimposed_active_ ||
461 (group_owner_ !=
nullptr &&
471 if (group_owner_ !=
nullptr)
473 if (snapshot_.powered || !std::isfinite(limits.
max_velocity) ||
488 if(!limits)
return limits.
error();
493 bool enable_negative =
true)
499 if (enabled && !power_feedback_)
503 const bool positive_changed = positive_enabled_ != (enabled && enable_positive);
504 const bool negative_changed = negative_enabled_ != (enabled && enable_negative);
505 positive_enabled_ = enabled && enable_positive;
506 negative_enabled_ = enabled && enable_negative;
511 if (enabled == snapshot_.powered)
513 if ((positive_changed || negative_changed) &&
514 !active_direction_enabled())
523 snapshot_.powered = enabled;
533 power_feedback_ = available;
534 if (available || !snapshot_.powered)
538 snapshot_.powered =
false;
544 if (!std::isfinite(position) || is_moving() || !target_inside_limits(position,
true))
548 snapshot_.command_position = position;
549 snapshot_.actual_position = position;
550 snapshot_.command_velocity = 0.0;
551 snapshot_.actual_velocity = 0.0;
561 value = snapshot_.command_position;
564 value = limits_.max_position;
567 value = limits_.min_position;
570 value = limits_.max_position_enabled ? 1.0 : 0.0;
573 value = limits_.min_position_enabled ? 1.0 : 0.0;
577 value = limits_.max_velocity;
580 value = snapshot_.actual_velocity;
583 value = snapshot_.command_velocity;
587 value = limits_.max_acceleration;
591 value = limits_.max_deceleration;
595 value = limits_.max_jerk;
607 const bool has_position_profile = active_ &&
611 if (!std::isfinite(delta) || sync_kind_ !=
SyncKind::none || stream_active_ ||
612 superimposed_active_ || group_blocks_standalone_motion())
616 if (!std::isfinite(snapshot_.command_position + delta) ||
617 !std::isfinite(snapshot_.actual_position + delta) ||
618 !target_inside_limits(snapshot_.command_position + delta,
true) ||
619 !target_inside_limits(snapshot_.actual_position + delta,
true))
623 if (has_position_profile && (!std::isfinite(active_target_ + delta) ||
624 !target_inside_limits(active_target_ + delta,
true)))
628 for (std::size_t i = 0; i < queue_.size(); ++i)
632 (!std::isfinite(queue_[i].value + delta) ||
633 !target_inside_limits(queue_[i].value + delta,
true)))
638 if (has_position_profile && active_profile_.translate(delta) !=
rt::ErrorCode::ok)
642 snapshot_.command_position += delta;
643 snapshot_.actual_position += delta;
646 if (has_position_profile)
648 active_target_ += delta;
649 active_last_sample_ += delta;
654 active_command_.value += delta;
657 for (std::size_t i = 0; i < queue_.size(); ++i)
662 queue_[i].value += delta;
665 for (ProbeSlot &probe : probes_)
668 probe.recorded_position += delta;
669 if (probe.window_only)
671 probe.first_position += delta;
672 probe.last_position += delta;
682 if (!active_ || !std::isfinite(delta) || sync_kind_ !=
SyncKind::none || stream_active_ ||
683 superimposed_active_ || group_blocks_standalone_motion())
688 const double position = snapshot_.command_position + delta;
689 const double actual_position = snapshot_.actual_position + delta;
690 if (!std::isfinite(position) || !std::isfinite(actual_position) ||
691 !target_inside_limits(position,
true) || !target_inside_limits(actual_position,
true))
699 !continuous_holding_ && !override_paused_;
703 if (override_braking_)
705 updated_profile = active_profile_;
714 ? active_command_.end_velocity * override_
718 {position, snapshot_.command_velocity, snapshot_.command_acceleration},
719 {active_target_, end_velocity, 0.0}, limits);
722 return replanned.
error();
724 updated_profile = replanned.
value();
728 snapshot_.command_position = position;
729 snapshot_.actual_position = actual_position;
732 active_profile_ = updated_profile;
733 if (override_braking_)
735 active_last_sample_ += delta;
740 active_last_sample_ = position;
741 snapshot_.active_command_reached_target =
false;
744 for (ProbeSlot &probe : probes_)
747 probe.recorded_position += delta;
748 if (probe.window_only)
750 probe.first_position += delta;
751 probe.last_position += delta;
763 value = limits_.max_position_enabled;
766 value = limits_.min_position_enabled;
776 if (!std::isfinite(value))
783 if (limits_.min_position_enabled && limits_.max_position_enabled &&
784 value < limits_.min_position)
788 limits_.max_position = value;
791 if (limits_.min_position_enabled && limits_.max_position_enabled &&
792 value > limits_.max_position)
796 limits_.min_position = value;
804 limits_.max_velocity = value;
812 limits_.max_acceleration = value;
820 limits_.max_deceleration = value;
828 limits_.max_jerk = value;
840 limits_.max_position_enabled = value;
843 limits_.min_position_enabled = value;
861 double value,
bool queued)
898 return set_override(factor, acceleration_override_, jerk_override_);
902 double acceleration_factor,
905 if (group_blocks_standalone_motion())
909 if (!std::isfinite(velocity_factor) || velocity_factor < 0.0 ||
910 velocity_factor > 1.0 || !std::isfinite(acceleration_factor) ||
911 acceleration_factor <= 0.0 || acceleration_factor > 1.0 ||
912 !std::isfinite(jerk_factor) || jerk_factor <= 0.0 ||
917 const double previous_velocity = override_;
918 const double previous_acceleration = acceleration_override_;
919 const double previous_jerk = jerk_override_;
920 override_ = velocity_factor;
921 acceleration_override_ = acceleration_factor;
922 jerk_override_ = jerk_factor;
923 if (previous_velocity == velocity_factor &&
924 previous_acceleration == acceleration_factor &&
925 previous_jerk == jerk_factor)
930 const auto restore_previous = [&]() {
931 override_ = previous_velocity;
932 acceleration_override_ = previous_acceleration;
933 jerk_override_ = previous_jerk;
936 if (velocity_factor == 0.0 && active_ && active_command_.kind !=
CommandKind::halt &&
939 const double v0 = snapshot_.command_velocity;
940 const double a0 = snapshot_.command_acceleration;
941 const double scaled_acceleration =
942 active_command_.acceleration * acceleration_override_;
943 const double scaled_deceleration =
944 active_command_.deceleration * acceleration_override_;
945 const double scaled_jerk = active_command_.jerk * jerk_override_;
946 const otg::Limits1D halt_limits{std::fabs(v0) + active_command_.velocity + 1e-9,
947 std::fmax(std::fabs(a0), scaled_acceleration),
948 scaled_deceleration, scaled_jerk};
949 double brake_velocity = v0;
950 double brake_shift = 0.0;
953 const double zero_cycles = std::ceil(std::fabs(a0) / scaled_jerk);
954 brake_velocity += 0.5 * a0 * zero_cycles;
955 brake_shift += v0 * zero_cycles + a0 * zero_cycles * zero_cycles / 3.0;
957 const double stop_position =
958 snapshot_.command_position + brake_shift +
961 {snapshot_.command_position, v0, a0}, {stop_position, 0.0, 0.0}, halt_limits);
965 return profile.
error();
967 active_profile_ = profile.
value();
968 active_last_sample_ = snapshot_.command_position;
970 override_braking_ =
true;
971 override_paused_ =
false;
975 if (previous_velocity == 0.0 && velocity_factor > 0.0 && override_paused_)
977 override_paused_ =
false;
983 const double direction = active_target_ >= snapshot_.command_position ? 1.0 : -1.0;
985 ? direction * active_command_.end_velocity * velocity_factor
989 {active_target_, end_velocity, 0.0}, limits);
993 override_paused_ =
true;
994 return profile.
error();
996 active_profile_ = profile.
value();
997 active_last_sample_ = snapshot_.command_position;
1005 if (!profile_driven)
1011 ? active_command_.end_velocity * override_
1013 if (continuous_holding_)
1015 continuous_hold_velocity_ = end_velocity;
1019 const otg::Limits1D limits = scaled_limits(active_command_);
1022 snapshot_.command_acceleration},
1023 {active_target_, end_velocity, 0.0}, limits);
1027 return profile.
error();
1029 active_profile_ = profile.
value();
1031 active_last_sample_ = snapshot_.command_position;
1032 continuous_hold_velocity_ = end_velocity;
1033 snapshot_.active_command_reached_target =
false;
1039 if (snapshot_.active_command_id != 0)
1041 record_command_error(snapshot_.active_command_id, code);
1043 for (std::size_t i = 0; i < queue_.size(); ++i)
1045 record_command_error(queue_[i].command_id, code);
1048 record_command_error(sync_id_, code);
1049 if (superimposed_id_ != 0)
1050 record_command_error(superimposed_id_, code);
1051 if (stream_id_ != 0)
1052 record_command_error(stream_id_, code);
1053 for (std::size_t i = 0; i < management_queue_.size(); ++i)
1055 remember_management_result(management_queue_[i].command_id, code);
1057 management_queue_.clear();
1059 snapshot_.error =
true;
1060 snapshot_.error_id = code;
1071 snapshot_.error =
false;
1079 if (group_blocks_standalone_motion())
1087 std::size_t segment_count,
1088 double acceleration_scale,
1089 double acceleration_offset,
1092 if (group_blocks_standalone_motion() || !snapshot_.powered ||
1095 !std::isfinite(acceleration_scale) || !std::isfinite(acceleration_offset) ||
1096 !std::isfinite(time_scale) || time_scale <= 0.0)
1100 for (std::size_t i = 0; i < segment_count; ++i)
1102 const double acceleration =
1103 segments[i].
target * acceleration_scale + acceleration_offset;
1104 const double duration =
static_cast<double>(segments[i].
duration_cycles) * time_scale;
1105 if (!std::isfinite(acceleration) || segments[i].
relative ||
1106 segments[i].duration_cycles <= 0 || !std::isfinite(duration) ||
1107 std::round(duration) < 1.0 || std::round(duration) >= std::ldexp(1.0, 63))
1114 acceleration_segment_count_ = segment_count;
1115 acceleration_segment_index_ = 0;
1116 acceleration_segment_tick_ = 0;
1117 acceleration_profile_completed_ =
false;
1118 for (std::size_t i = 0; i < segment_count; ++i)
1122 storage.
value = segments[i].
target * acceleration_scale + acceleration_offset;
1124 std::llround(
static_cast<double>(segments[i].duration_cycles) * time_scale));
1125 queue_.push_back(storage);
1128 active_command_ = {};
1130 active_command_.command_id = next_command_id_++;
1131 snapshot_.active_command_id = active_command_.command_id;
1132 snapshot_.active_command_reached_target =
false;
1141 std::size_t command_count,
1142 bool enforce_soft_limits =
false)
const
1144 if (commands ==
nullptr || command_count == 0 || command_count >
QueueCapacity ||
1145 group_blocks_standalone_motion() || !snapshot_.powered ||
1162 ? snapshot_.command_position
1163 : queued_endpoint();
1165 ? snapshot_.command_velocity
1168 ? snapshot_.command_acceleration
1170 for (std::size_t i = 0; i < command_count; ++i)
1174 if (
command.buffer_mode != expected_mode ||
1181 command.min_duration_cycles < 0)
1187 ? from_position +
command.value
1189 if (!std::isfinite(target))
1193 if (!target_inside_limits(target, enforce_soft_limits))
1200 {from_position, from_velocity, from_acceleration}, {target, 0.0, 0.0}, limits);
1203 return profile.
error();
1206 from_position = target;
1207 from_velocity = 0.0;
1208 from_acceleration = 0.0;
1220 if(
cycle.period_ns() <= 0) {
1231 if(!std::isfinite(base.max_velocity) || !std::isfinite(base.max_acceleration) ||
1232 !std::isfinite(base.max_deceleration) || !std::isfinite(base.max_jerk) ||
1237 base.max_velocity <= 0.0 || base.max_acceleration <= 0.0 ||
1238 base.max_deceleration <= 0.0 || base.max_jerk <= 0.0) {
1246 group_owner_ = owner;
1247 group_status_ = owner ==
nullptr ? nullptr :
status;
1251 rt::Result<std::uint32_t> submit_group_owned(AxisCommand
command)
1269 if (!target_inside_limits(
command.value))
1273 const otg::Limits1D limits = scaled_limits(
command);
1274 const rt::Result<otg::Profile1D> profile =
1276 snapshot_.command_acceleration},
1277 {
command.value, 0.0, 0.0}, limits);
1281 void abort_group_owned_motion()
1292 bool group_blocks_standalone_motion()
const
1294 return group_owner_ !=
nullptr &&
1351 const double direction_reference =
1353 ? snapshot_.command_position
1354 : queued_endpoint();
1355 if (!command_direction_enabled(
command, direction_reference))
1362 rt::Result<std::uint32_t> submit_impl(AxisCommand
command)
1372 command.command_id = next_command_id_++;
1377 const double prior_torque = snapshot_.command_torque;
1381 torque_command_id_ =
command.command_id;
1382 snapshot_.active_command_id =
command.command_id;
1383 snapshot_.command_torque =
command.torque_ramp == 0.0
1386 snapshot_.torque_velocity_limit =
command.velocity;
1387 snapshot_.torque_acceleration_limit =
command.acceleration;
1388 snapshot_.torque_deceleration_limit =
command.deceleration;
1389 snapshot_.torque_jerk_limit =
command.jerk;
1390 snapshot_.torque_direction =
command.direction;
1391 snapshot_.torque_mode =
true;
1403 const double takeover_endpoint = queued_endpoint();
1404 const AxisStatus takeover_status = snapshot_.status;
1408 const double takeover_velocity = snapshot_.command_velocity;
1409 const double takeover_acceleration = snapshot_.command_acceleration;
1413 snapshot_.command_velocity = takeover_velocity;
1414 snapshot_.command_acceleration = takeover_acceleration;
1415 snapshot_.actual_velocity = takeover_velocity;
1416 snapshot_.actual_acceleration = takeover_acceleration;
1420 const double relative_base = snapshot_.command_position;
1423 : snapshot_.command_position;
1431 !target_inside_limits(
command.value))
1458 bool master_absolute,
1459 bool slave_absolute,
1460 std::uint32_t replace_id = 0)
1463 if(master ==
nullptr || master ==
this || !table.
valid() ||
1464 (!master_absolute && !table.
periodic &&
1470 if(replace_id != 0) {
1472 if(selection.id == replace_id) {
1478 if(slot ==
nullptr) {
1480 if(selection.id == 0) {
1486 if(slot ==
nullptr) {
1490 const std::uint32_t
id = slot->
id == 0 ? next_command_id_++ : slot->
id;
1496 master->snapshot().command_position,
1497 snapshot_.command_position};
1507 if(selection.id ==
id) {
1517 !std::isfinite(
command.ratio_numerator) || !std::isfinite(
command.ratio_denominator) ||
1518 command.ratio_denominator == 0.0 || !std::isfinite(
command.master_sync_position) ||
1519 !std::isfinite(
command.slave_sync_position) ||
1520 !std::isfinite(
command.master_start_distance) ||
1521 !std::isfinite(
command.approach_velocity) ||
command.approach_velocity < 0.0 ||
1522 !std::isfinite(
command.acceleration) ||
command.acceleration < 0.0 ||
1523 !std::isfinite(
command.deceleration) ||
command.deceleration < 0.0 ||
1545 phase_offset_ = 0.0;
1546 abort_phasing_commands();
1547 sync_profile_ready_ =
false;
1551 sync_phase_ = sync_entry_phase_;
1559 !std::isfinite(
command.master_offset) || !std::isfinite(
command.master_scaling) ||
1560 command.master_scaling == 0.0 || !std::isfinite(
command.slave_offset) ||
1561 !std::isfinite(
command.slave_scaling) || !std::isfinite(
command.master_sync_position) ||
1562 !std::isfinite(
command.master_start_distance) ||
command.master_start_distance < 0.0 ||
1563 !std::isfinite(
command.approach_velocity) ||
command.approach_velocity < 0.0)
1590 sync_phase_ = sync_entry_phase_;
1598 command.master2 ==
this || !std::isfinite(
command.ratio_numerator_m1) ||
1599 !std::isfinite(
command.ratio_denominator_m1) ||
command.ratio_denominator_m1 == 0.0 ||
1600 !std::isfinite(
command.ratio_numerator_m2) ||
1601 !std::isfinite(
command.ratio_denominator_m2) ||
command.ratio_denominator_m2 == 0.0)
1615 sync_phase_ = sync_entry_phase_;
1629 !std::isfinite(tolerance) || tolerance < 0.0 ||
command.master_start_distance != 0.0 ||
1630 !std::isfinite(
command.master_offset) || !std::isfinite(
command.master_scaling) ||
1631 command.master_scaling == 0.0 || !std::isfinite(
command.slave_offset) ||
1632 !std::isfinite(
command.slave_scaling))
1647 const double master_position = master_value(*sync_cam_.master, sync_cam_.source);
1648 const double table_input =
1649 (master_position -
command.master_offset) /
command.master_scaling;
1651 ? replacement.
sample(table_input)
1652 :
command.table.sample(table_input);
1655 return sampled.
error();
1657 const double replacement_slave =
1664 if (std::fabs(replacement_slave -
current.value()) > tolerance)
1669 sync_cam_.table =
command.table;
1670 sync_cam_.interpolation =
command.interpolation;
1671 sync_cam_.master_offset =
command.master_offset;
1672 sync_cam_.master_scaling =
command.master_scaling;
1673 sync_cam_.slave_offset =
command.slave_offset;
1674 sync_cam_.slave_scaling =
command.slave_scaling;
1677 cam_spline_ = replacement;
1684 if (sync_kind_ !=
SyncKind::gear || !std::isfinite(ratio_numerator) ||
1685 !std::isfinite(ratio_denominator) || ratio_denominator == 0.0)
1689 sync_gear_.ratio_numerator = ratio_numerator;
1690 sync_gear_.ratio_denominator = ratio_denominator;
1695 double slave_scaling)
1697 if (sync_kind_ !=
SyncKind::cam || !std::isfinite(master_offset) ||
1698 !std::isfinite(master_scaling) || master_scaling == 0.0 ||
1699 !std::isfinite(slave_offset) || !std::isfinite(slave_scaling))
1703 sync_cam_.master_offset = master_offset;
1704 sync_cam_.master_scaling = master_scaling;
1705 sync_cam_.slave_offset = slave_offset;
1706 sync_cam_.slave_scaling = slave_scaling;
1711 double ratio_denominator_m1,
double ratio_numerator_m2,
1712 double ratio_denominator_m2)
1715 !std::isfinite(ratio_denominator_m1) || ratio_denominator_m1 == 0.0 ||
1716 !std::isfinite(ratio_numerator_m2) || !std::isfinite(ratio_denominator_m2) ||
1717 ratio_denominator_m2 == 0.0)
1721 sync_combine_.mode = mode;
1722 sync_combine_.ratio_numerator_m1 = ratio_numerator_m1;
1723 sync_combine_.ratio_denominator_m1 = ratio_denominator_m1;
1724 sync_combine_.ratio_numerator_m2 = ratio_numerator_m2;
1725 sync_combine_.ratio_denominator_m2 = ratio_denominator_m2;
1735 const double velocity = snapshot_.command_velocity;
1737 snapshot_.active_command_id = 0;
1738 if (velocity != 0.0 && snapshot_.powered)
1742 hold.
value = velocity < 0.0 ? -1.0 : 1.0;
1743 hold.
velocity = std::fabs(velocity);
1750 snapshot_.command_velocity = 0.0;
1751 snapshot_.actual_velocity = 0.0;
1767 sync_kind_ !=
SyncKind::none || stream_active_ || group_owner_ !=
nullptr)
1771 const rt::ErrorCode configured = stream_filter_.configure(config);
1780 const double takeover_velocity = snapshot_.command_velocity;
1781 const double takeover_acceleration = snapshot_.command_acceleration;
1783 snapshot_.command_velocity = takeover_velocity;
1784 snapshot_.actual_velocity = takeover_velocity;
1785 snapshot_.command_acceleration = takeover_acceleration;
1786 snapshot_.actual_acceleration = takeover_acceleration;
1789 {snapshot_.command_position, takeover_velocity, takeover_acceleration});
1792 snapshot_.command_velocity = 0.0;
1793 snapshot_.actual_velocity = 0.0;
1794 snapshot_.command_acceleration = 0.0;
1795 snapshot_.actual_acceleration = 0.0;
1798 stream_active_ =
true;
1799 stream_id_ = next_command_id_++;
1806 if (!stream_active_)
1810 return stream_filter_.push_target(target);
1817 if (!stream_active_)
1821 if (snapshot_.command_velocity != 0.0 || snapshot_.command_acceleration != 0.0)
1825 stream_filter_.end_session();
1826 stream_active_ =
false;
1848 for (std::size_t i = 0; i < phasing_result_count_; ++i)
1850 const std::size_t index =
1851 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
1852 if (phasing_results_[index].command_id == command_id)
1854 return phasing_results_[index].state;
1862 for (std::size_t i = 0; i < phasing_result_count_; ++i)
1864 const std::size_t index =
1865 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
1866 if (phasing_results_[index].command_id == command_id)
1868 return phasing_results_[index].covered_shift;
1883 !std::isfinite(
command.phase_shift) || !std::isfinite(
command.velocity) ||
1884 !std::isfinite(
command.acceleration) || !std::isfinite(
command.deceleration) ||
1910 const double target =
command.relative ? phase_offset_ +
command.phase_shift
1912 if (target != phase_offset_)
1917 {phase_offset_, phasing_velocity_, phasing_acceleration_},
1918 {target, 0.0, 0.0}, limits);
1926 command.command_id = next_command_id_++;
1930 abort_phasing_commands(
true);
1933 if (phasing_active_)
1957 if (!std::isfinite(position) || !std::isfinite(velocity) || !std::isfinite(acceleration) ||
1962 snapshot_.actual_position = position;
1963 snapshot_.actual_velocity = velocity;
1964 snapshot_.actual_acceleration = acceleration;
1965 snapshot_.actual_torque =
torque;
1983 snapshot_.homed =
true;
1984 homing_limits_suspended_ =
false;
1991 snapshot_.homed =
false;
1992 homing_limits_suspended_ =
true;
1997 snapshot_.homed =
true;
1998 homing_limits_suspended_ =
false;
2037 digital_input_[input] = level;
2056 digital_output_[output] = level;
2086 double first_position,
double last_position)
2092 if (window_only && (!std::isfinite(first_position) || !std::isfinite(last_position) ||
2093 first_position > last_position))
2097 ProbeSlot &slot = probes_[input];
2099 slot.captured =
false;
2100 slot.window_only = window_only;
2101 slot.first_position = first_position;
2102 slot.last_position = last_position;
2103 slot.recorded_position = 0.0;
2104 slot.last_level = digital_input_[input];
2105 slot.command_id = next_command_id_++;
2116 probes_[input].armed =
false;
2117 probes_[input].captured =
false;
2118 probes_[input].command_id = 0;
2130 stream_active_ || superimposed_active_ || group_blocks_standalone_motion())
2134 if (passive_homing_id_ != 0)
2136 passive_homing_aborted_id_ = passive_homing_id_;
2142 passive_homing_id_ = armed.
value();
2143 passive_homing_input_ = input;
2149 if (owner == 0 || passive_homing_id_ != owner)
2153 passive_homing_id_ = 0;
2159 if (passive_homing_id_ == 0)
2163 const std::uint32_t owner = passive_homing_id_;
2165 passive_homing_aborted_id_ = owner;
2166 passive_homing_id_ = 0;
2203 cycle_base_motion();
2204 cycle_superimposed();
2210 double acceleration,
double deceleration,
2213 if (group_blocks_standalone_motion() || !snapshot_.powered ||
2215 stream_active_ || !std::isfinite(distance) || !std::isfinite(velocity) ||
2216 velocity <= 0.0 || !std::isfinite(acceleration) || acceleration <= 0.0 ||
2217 !std::isfinite(deceleration) || deceleration <= 0.0 || !std::isfinite(jerk) ||
2218 jerk <= 0.0 || !direction_enabled(distance))
2224 scaled_limits(velocity, acceleration, deceleration, jerk);
2232 superimposed_profile_ = profile.
value();
2233 superimposed_tick_ = 0;
2234 superimposed_last_ = 0.0;
2235 superimposed_active_ =
true;
2236 superimposed_halting_ =
false;
2237 superimposed_id_ = next_command_id_++;
2238 superimposed_completed_id_ = 0;
2247 !std::isfinite(deceleration) || deceleration <= 0.0 ||
2248 !std::isfinite(jerk) || jerk <= 0.0)
2252 if (!superimposed_active_)
2261 superimposed_active_ =
false;
2262 superimposed_id_ = 0;
2263 superimposed_halting_ =
false;
2264 superimposed_completed_id_ = 0;
2268 double brake_velocity =
current.velocity;
2269 double brake_shift = 0.0;
2270 if (
current.acceleration != 0.0)
2272 const double zero_cycles =
2273 std::ceil(std::fabs(
current.acceleration) / jerk);
2274 brake_velocity += 0.5 *
current.acceleration * zero_cycles;
2275 brake_shift +=
current.velocity * zero_cycles +
2276 current.acceleration * zero_cycles * zero_cycles / 3.0;
2279 std::fabs(
current.velocity) + std::fabs(brake_velocity) + 1e-9,
2280 std::fmax(std::fabs(
current.acceleration), deceleration),
2281 deceleration, jerk};
2282 const double stop_position =
current.position + brake_shift +
2285 current, {stop_position, 0.0, 0.0}, halt_limits);
2288 return halt.error();
2291 superimposed_tick_ = 0;
2292 superimposed_last_ =
current.position;
2293 superimposed_halting_ =
true;
2294 superimposed_completed_id_ = 0;
2300 superimposed_active_ =
false;
2301 superimposed_id_ = 0;
2302 superimposed_completed_id_ = 0;
2303 superimposed_halting_ =
false;
2317 double acceleration,
2318 double deceleration,
2321 if(!superimposed_active_ || superimposed_halting_ || command_id == 0 ||
2322 command_id != superimposed_id_ || !std::isfinite(distance) ||
2323 !std::isfinite(velocity) || velocity <= 0.0 ||
2324 !std::isfinite(acceleration) || acceleration <= 0.0 ||
2325 !std::isfinite(deceleration) || deceleration <= 0.0 ||
2326 !std::isfinite(jerk) || jerk <= 0.0 ||
2327 !direction_enabled(distance - superimposed_last_))
2334 current, {distance, 0.0, 0.0},
2335 scaled_limits(velocity, acceleration, deceleration, jerk));
2338 return profile.
error();
2340 superimposed_profile_ = profile.
value();
2341 superimposed_tick_ = 0;
2342 superimposed_last_ =
current.position;
2350 if (command_id == 0)
2354 for (std::size_t i = 0; i < queue_.size(); ++i)
2356 if (queue_[i].command_id == command_id)
2366 if(command_id == 0)
return false;
2367 for(std::size_t i = 0; i < management_queue_.size(); ++i) {
2368 if(management_queue_[i].command_id == command_id)
return true;
2375 return command_id != 0 && active_management_id_ == command_id;
2380 for(std::size_t i = 0; i < management_results_.size(); ++i) {
2381 if(management_results_[i].command_id == command_id) {
2390 for(std::size_t i = 0; i < management_results_.size(); ++i) {
2391 if(management_results_[i].command_id == command_id) {
2392 return management_results_[i].error;
2400 if (command_id == 0)
2404 for (std::size_t offset = 0; offset < command_error_count_; ++offset)
2406 const std::size_t index =
2407 (command_error_cursor_ + CommandErrorCapacity - 1 - offset) % CommandErrorCapacity;
2408 if (command_errors_[index].command_id == command_id)
2410 return command_errors_[index].error;
2421 snapshot_.active_command_id != command_id)
2425 record_command_error(command_id, code);
2427 continuous_holding_ =
false;
2428 override_braking_ =
false;
2429 override_paused_ =
false;
2430 blend_armed_ =
false;
2431 snapshot_.active_command_id = 0;
2432 snapshot_.active_command_reached_target =
false;
2433 snapshot_.command_acceleration = 0.0;
2434 snapshot_.actual_acceleration = 0.0;
2438 start_next_queued();
2441 base_velocity_ = 0.0;
2442 snapshot_.command_velocity = 0.0;
2443 snapshot_.actual_velocity = 0.0;
2452 return command_id != 0 && active_ && snapshot_.active_command_id == command_id &&
2454 std::fabs(snapshot_.command_velocity - signed_velocity(active_command_)) <= 1e-12;
2459 return command_id != 0 && active_ && snapshot_.active_command_id == command_id &&
2461 snapshot_.active_command_reached_target &&
2462 std::fabs(snapshot_.command_velocity - continuous_hold_velocity_) <= 1e-12;
2469 if (command_id == 0 || stop_lock_id_ != command_id)
2473 if (active_ && snapshot_.active_command_id == command_id)
2475 stop_release_requested_ =
true;
2479 stop_release_requested_ =
false;
2493 if (!active_ || snapshot_.active_command_id != command_id || command_id == 0 ||
2495 !std::isfinite(velocity) || velocity <= 0.0 ||
2496 !direction_enabled(direction_value))
2500 active_command_.value = direction_value;
2501 active_command_.velocity = velocity;
2508 if(!active_ || snapshot_.active_command_id != command_id ||
2518 active_command_.value =
command.value;
2519 active_command_.torque_ramp =
command.torque_ramp;
2520 active_command_.velocity =
command.velocity;
2521 active_command_.acceleration =
command.acceleration;
2522 active_command_.deceleration =
command.deceleration;
2523 active_command_.jerk =
command.jerk;
2524 active_command_.direction =
command.direction;
2525 snapshot_.torque_velocity_limit =
command.velocity;
2526 snapshot_.torque_acceleration_limit =
command.acceleration;
2527 snapshot_.torque_deceleration_limit =
command.deceleration;
2528 snapshot_.torque_jerk_limit =
command.jerk;
2529 snapshot_.torque_direction =
command.direction;
2537 if (group_blocks_standalone_motion() || !active_ ||
2538 snapshot_.active_command_id != command_id || command_id == 0 ||
2539 !std::isfinite(target) ||
2547 if (!target_inside_limits(target))
2551 if (!direction_enabled(target - snapshot_.command_position))
2556 double end_velocity = 0.0;
2559 end_velocity = active_command_.end_velocity * override_;
2561 const otg::Limits1D limits = scaled_limits(active_command_);
2564 snapshot_.command_acceleration},
2565 {target, end_velocity, 0.0}, limits);
2568 return profile.
error();
2571 active_profile_ = profile.
value();
2573 active_last_sample_ = snapshot_.command_position;
2574 active_target_ = target;
2575 active_command_.value = target;
2576 continuous_holding_ =
false;
2577 continuous_hold_velocity_ = end_velocity;
2578 snapshot_.active_command_reached_target =
false;
2583 void set_synchronized_position(
double position) { set_synchronized_state(position, 0.0, 0.0); }
2585 void set_synchronized_state(
double position,
double velocity,
double acceleration)
2596 void clear_synchronized()
2609 if (group_blocks_standalone_motion() || !snapshot_.powered ||
2625 reset_superimposed();
2627 sync_id_ = next_command_id_++;
2634 abort_phasing_commands();
2639 phase_offset_ = 0.0;
2640 sync_profile_ready_ =
false;
2645 const AxisSnapshot &
snapshot = master.snapshot();
2652 const AxisSnapshot &
snapshot = master.snapshot();
2657 double sync_master_sync_position()
const
2659 return sync_kind_ ==
SyncKind::gear ? sync_gear_.master_sync_position
2660 : sync_cam_.master_sync_position;
2663 double sync_master_start_distance()
const
2665 return sync_kind_ ==
SyncKind::gear ? sync_gear_.master_start_distance
2666 : sync_cam_.master_start_distance;
2669 double sync_approach_velocity()
const
2671 return sync_kind_ ==
SyncKind::gear ? sync_gear_.approach_velocity
2672 : sync_cam_.approach_velocity;
2676 rt::Result<double> sync_target_at_sync()
const
2682 return cam_slave_value(sync_master_sync_position());
2685 rt::Result<double> cam_slave_value(
double master_position)
const
2687 const double table_input =
2688 (master_position - sync_cam_.master_offset) / sync_cam_.master_scaling;
2690 ? cam_spline_.sample(table_input)
2691 : sync_cam_.table.sample(table_input);
2697 sync_cam_.slave_scaling * sampled.value());
2701 double covered_shift)
2703 for (std::size_t i = 0; i < phasing_result_count_; ++i)
2705 const std::size_t index =
2706 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
2707 if (phasing_results_[index].command_id == command_id)
2709 phasing_results_[index].state = state;
2710 phasing_results_[index].covered_shift = covered_shift;
2714 phasing_results_[phasing_result_cursor_] = {command_id, state, covered_shift};
2715 phasing_result_cursor_ = (phasing_result_cursor_ + 1) % PhasingResultCapacity;
2716 if (phasing_result_count_ < PhasingResultCapacity)
2718 ++phasing_result_count_;
2722 void abort_phasing_commands(
bool preserve_kinematics =
false)
2724 if (phasing_active_)
2727 phase_offset_ - phasing_start_offset_);
2729 for (std::size_t i = 0; i < phasing_queue_.size(); ++i)
2733 phasing_queue_.clear();
2734 phasing_active_ =
false;
2736 if (!preserve_kinematics)
2738 phasing_velocity_ = 0.0;
2739 phasing_acceleration_ = 0.0;
2746 const double target =
command.relative ? phase_offset_ +
command.phase_shift
2748 phasing_start_offset_ = phase_offset_;
2749 phasing_target_ = target;
2750 phasing_id_ =
command.command_id;
2751 if (target == phase_offset_ ||
command.velocity == 0.0)
2753 phase_offset_ = target;
2754 phasing_velocity_ = 0.0;
2755 phasing_acceleration_ = 0.0;
2756 phasing_active_ =
false;
2758 phase_offset_ - phasing_start_offset_);
2763 const otg::Limits1D limits{
command.velocity,
command.acceleration,
2766 {phase_offset_, phasing_velocity_, phasing_acceleration_}, {target, 0.0, 0.0}, limits);
2770 return planned.error();
2772 phasing_profile_ = planned.value();
2774 phasing_active_ =
true;
2779 void start_next_phasing()
2781 while (!phasing_active_ && !phasing_queue_.empty())
2783 const PhasingCommand next = phasing_queue_[0];
2784 for (std::size_t i = 1; i < phasing_queue_.size(); ++i)
2786 phasing_queue_[i - 1] = phasing_queue_[i];
2788 phasing_queue_.pop_back();
2797 void advance_phasing()
2799 if (!phasing_active_)
2801 start_next_phasing();
2805 const otg::State1D state =
2807 phase_offset_ = state.position;
2808 phasing_velocity_ = state.velocity;
2809 phasing_acceleration_ = state.acceleration;
2811 phase_offset_ - phasing_start_offset_);
2812 if (phasing_tick_ >= phasing_profile_.duration_cycles())
2814 phase_offset_ = phasing_target_;
2815 phasing_velocity_ = 0.0;
2816 phasing_acceleration_ = 0.0;
2818 phase_offset_ - phasing_start_offset_);
2819 phasing_active_ =
false;
2821 start_next_phasing();
2825 rt::Result<double> sync_engaged_position()
2830 const double master = master_value(*sync_gear_.master, sync_gear_.source);
2831 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2836 return cam_slave_value(master_value(*sync_cam_.master, sync_cam_.source));
2838 const double master1 =
2839 master_value(*sync_combine_.master1, sync_combine_.source_m1) *
2840 (sync_combine_.ratio_numerator_m1 / sync_combine_.ratio_denominator_m1);
2841 const double master2 =
2842 master_value(*sync_combine_.master2, sync_combine_.source_m2) *
2843 (sync_combine_.ratio_numerator_m2 / sync_combine_.ratio_denominator_m2);
2848 double sync_engaged_velocity(
double next_position)
const
2852 return master_velocity(*sync_gear_.master, sync_gear_.source) *
2853 (sync_gear_.ratio_numerator / sync_gear_.ratio_denominator);
2857 const double master1 =
2858 master_velocity(*sync_combine_.master1, sync_combine_.source_m1) *
2859 (sync_combine_.ratio_numerator_m1 / sync_combine_.ratio_denominator_m1);
2860 const double master2 =
2861 master_velocity(*sync_combine_.master2, sync_combine_.source_m2) *
2862 (sync_combine_.ratio_numerator_m2 / sync_combine_.ratio_denominator_m2);
2864 : master1 + master2;
2866 return next_position - snapshot_.command_position;
2869 otg::Limits1D gear_limits()
const
2871 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2872 const double target_velocity =
2873 master_velocity(*sync_gear_.master, sync_gear_.source) * ratio;
2874 const double velocity = sync_gear_.position_sync
2875 ? (sync_gear_.approach_velocity > 0.0
2876 ? sync_gear_.approach_velocity
2877 : limits_.max_velocity)
2878 : std::fmax(std::fabs(snapshot_.command_velocity),
2879 std::fabs(target_velocity)) + 1e-9;
2881 sync_gear_.acceleration > 0.0 ? sync_gear_.acceleration
2882 : limits_.max_acceleration,
2883 sync_gear_.deceleration > 0.0 ? sync_gear_.deceleration
2884 : limits_.max_deceleration,
2885 sync_gear_.jerk > 0.0 ? sync_gear_.jerk : limits_.max_jerk};
2890 if (sync_gear_.acceleration == 0.0 && sync_gear_.deceleration == 0.0 &&
2891 sync_gear_.jerk == 0.0)
2896 sync_profile_ready_ =
false;
2899 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2900 const double target_velocity =
2901 master_velocity(*sync_gear_.master, sync_gear_.source) * ratio;
2902 if (std::fabs(snapshot_.command_velocity - target_velocity) <= 1e-12 &&
2903 std::fabs(snapshot_.command_acceleration) <= 1e-12)
2905 sync_profile_ready_ =
false;
2908 const otg::Limits1D limits = gear_limits();
2909 const double ramp_distance =
2912 {snapshot_.command_position, snapshot_.command_velocity,
2913 snapshot_.command_acceleration},
2914 {snapshot_.command_position + ramp_distance, target_velocity, 0.0}, limits);
2917 return planned.error();
2919 sync_profile_ = planned.value();
2920 sync_profile_tick_ = 0;
2921 sync_profile_target_velocity_ = target_velocity;
2922 sync_profile_ready_ =
true;
2926 int sync_window_direction()
const
2928 const double distance = sync_master_start_distance();
2934 ? master_velocity(*sync_gear_.master, sync_gear_.source)
2935 : master_velocity(*sync_cam_.master, sync_cam_.source);
2940 return sync_window_master_value() <= sync_master_sync_position() ? 1 : -1;
2943 bool sync_reached(
double master,
double boundary)
const
2945 return sync_window_direction() > 0 ? master >= boundary : master <= boundary;
2950 const double master = sync_window_master_value();
2952 ? master_velocity(*sync_gear_.master, sync_gear_.source)
2953 : master_velocity(*sync_cam_.master, sync_cam_.source);
2954 const double remaining = sync_master_sync_position() - master;
2955 if (remaining * master_speed <= 0.0)
2962 const std::int64_t cycles =
static_cast<std::int64_t
>(
2963 std::ceil(std::fabs(remaining / master_speed))) + 1;
2964 const rt::Result<double> target = sync_target_at_sync();
2967 return target.error();
2969 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2970 const double target_velocity = master_speed * ratio;
2972 {snapshot_.command_position, snapshot_.command_velocity,
2973 snapshot_.command_acceleration},
2974 {target.value(), target_velocity, 0.0}, gear_limits(), cycles > 0 ? cycles : 1);
2977 return planned.error();
2979 sync_profile_ = planned.value();
2980 sync_profile_tick_ = 0;
2981 sync_profile_ready_ =
true;
2982 approach_window_begin_ = sync_master_sync_position() - sync_master_start_distance();
2983 approach_start_slave_ = snapshot_.command_position;
2989 const std::uint32_t failed_id = sync_id_;
2991 record_command_error(failed_id, code);
2992 snapshot_.active_command_id = 0;
2993 snapshot_.command_velocity = 0.0;
2994 snapshot_.actual_velocity = 0.0;
2995 snapshot_.command_acceleration = 0.0;
2996 snapshot_.actual_acceleration = 0.0;
3000 void enter_engaged()
3004 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
3005 phase_offset_ = sync_gear_.position_sync
3006 ? sync_gear_.slave_sync_position -
3007 sync_gear_.master_sync_position * ratio
3008 : snapshot_.command_position -
3009 master_value(*sync_gear_.master, sync_gear_.source) * ratio;
3011 sync_profile_ready_ =
false;
3013 snapshot_.active_command_id = sync_id_;
3022 if (!stream_active_)
3026 const otg::State1D state = stream_filter_.cycle();
3027 snapshot_.command_position = state.position;
3028 snapshot_.actual_position = state.position;
3029 snapshot_.command_velocity = state.velocity;
3030 snapshot_.actual_velocity = state.velocity;
3031 snapshot_.command_acceleration = state.acceleration;
3032 snapshot_.actual_acceleration = state.acceleration;
3053 sync_phase_ = sync_entry_phase_;
3058 const double master = sync_window_master_value();
3059 const double window_begin = sync_master_sync_position() - sync_master_start_distance();
3060 if (!sync_reached(master, window_begin))
3064 if (!sync_reached(master, sync_master_sync_position()))
3071 fail_sync_command(planned);
3077 approach_window_begin_ = window_begin;
3078 approach_start_slave_ = snapshot_.command_position;
3092 if (!sync_profile_ready_)
3094 const rt::ErrorCode planned = plan_gear_velocity_transition();
3097 fail_sync_command(planned);
3100 if (!sync_profile_ready_)
3106 ++sync_profile_tick_;
3107 const otg::State1D state =
3109 snapshot_.active_command_id = sync_id_;
3110 set_synchronized_state(state.position, state.velocity, state.acceleration);
3111 if (sync_profile_tick_ >= sync_profile_.duration_cycles())
3113 const double live_target =
3114 master_velocity(*sync_gear_.master, sync_gear_.source) *
3115 (sync_gear_.ratio_numerator / sync_gear_.ratio_denominator);
3116 sync_profile_ready_ =
false;
3117 if (std::fabs(live_target - sync_profile_target_velocity_) <= 1e-12)
3125 const double master = sync_window_master_value();
3128 if (sync_profile_tick_ < sync_profile_.duration_cycles())
3130 ++sync_profile_tick_;
3133 snapshot_.active_command_id = sync_id_;
3134 set_synchronized_state(state.position, state.velocity, state.acceleration);
3135 if (sync_profile_tick_ >= sync_profile_.duration_cycles())
3137 if (!sync_reached(master, sync_master_sync_position()))
3148 if (!sync_reached(master, sync_master_sync_position()))
3155 else if (sync_reached(master, sync_master_sync_position()))
3161 const rt::Result<double> target_at_sync = sync_target_at_sync();
3162 if (!target_at_sync)
3167 const double span = sync_master_sync_position() - approach_window_begin_;
3168 double progress =
span > 0.0 ? (master - approach_window_begin_) / span : 1.0;
3169 progress = progress < 0.0 ? 0.0 : (progress > 1.0 ? 1.0 : progress);
3170 double target = approach_start_slave_ +
3171 (target_at_sync.value() - approach_start_slave_) * progress;
3172 target = cap_approach_step(target);
3173 snapshot_.active_command_id = sync_id_;
3174 set_synchronized_position(target);
3181 const rt::Result<double> position = sync_engaged_position();
3187 snapshot_.active_command_id = sync_id_;
3188 const double velocity = sync_engaged_velocity(position.value());
3189 set_synchronized_state(position.value(), velocity,
3190 velocity - snapshot_.command_velocity);
3196 double sync_window_master_value()
const
3198 return sync_kind_ ==
SyncKind::gear ? master_value(*sync_gear_.master, sync_gear_.source)
3199 : master_value(*sync_cam_.master, sync_cam_.source);
3202 double cap_approach_step(
double target)
const
3204 const double cap = sync_approach_velocity();
3209 const double step = target - snapshot_.command_position;
3212 return snapshot_.command_position + cap;
3216 return snapshot_.command_position - cap;
3221 bool is_moving()
const
3229 double queued_endpoint()
const
3231 double endpoint = active_ ? active_target_ : snapshot_.command_position;
3232 for (std::size_t i = 0; i < queue_.size(); ++i)
3234 const AxisCommand &
queued = queue_[i];
3245 AxisCommand normalize(AxisCommand
command,
double takeover_endpoint)
const
3261 double signed_velocity(AxisCommand
command)
const
3263 const double scaled =
command.velocity * override_;
3264 return command.value < 0.0 ? -scaled : scaled;
3267 otg::Limits1D scaled_limits(
double velocity,
double acceleration,
3268 double deceleration,
double jerk)
const
3270 return {velocity * override_, acceleration * acceleration_override_,
3271 deceleration * acceleration_override_, jerk * jerk_override_};
3274 otg::Limits1D scaled_limits(
const AxisCommand &
command)
const
3280 bool direction_enabled(
double delta)
const
3282 return delta == 0.0 || (delta > 0.0 ? positive_enabled_ : negative_enabled_);
3285 bool command_direction_enabled(
const AxisCommand &
command,
3286 double reference_position)
const
3295 return direction_enabled(
command.value);
3299 return direction_enabled(
command.value - reference_position);
3305 bool active_direction_enabled()
const
3311 if (snapshot_.command_velocity != 0.0)
3313 return direction_enabled(snapshot_.command_velocity);
3315 return !active_ || command_direction_enabled(active_command_,
3316 snapshot_.command_position);
3319 bool target_inside_limits(
double target,
bool enforce_soft_limits =
false)
const
3321 if (homing_limits_suspended_ && !enforce_soft_limits)
3325 if (limits_.min_position_enabled && target < limits_.min_position)
3329 if (limits_.max_position_enabled && target > limits_.max_position)
3346 continuous_holding_ =
false;
3347 blend_armed_ =
false;
3348 snapshot_.active_command_reached_target =
false;
3349 snapshot_.active_command_id =
command.command_id;
3350 snapshot_.command_torque_limit =
command.torque_limit;
3363 stop_lock_id_ =
command.command_id;
3364 stop_release_requested_ =
false;
3371 halt_profiled_ =
false;
3372 const double v0 = snapshot_.command_velocity;
3373 const double a0 = snapshot_.command_acceleration;
3374 if (v0 != 0.0 || a0 != 0.0)
3376 double brake_velocity = v0;
3377 double brake_shift = 0.0;
3380 const double zero_cycles = std::ceil(std::fabs(a0) /
command.jerk);
3381 brake_velocity += 0.5 * a0 * zero_cycles;
3382 brake_shift += v0 * zero_cycles + a0 * zero_cycles * zero_cycles / 3.0;
3384 const otg::Limits1D halt_limits{
3385 std::fabs(v0) + std::fabs(brake_velocity) + 1e-9,
3386 std::fabs(a0) >
command.acceleration ? std::fabs(a0) :
command.acceleration,
3388 const double stop_position =
3389 snapshot_.command_position + brake_shift +
3392 {snapshot_.command_position, v0, a0}, {stop_position, 0.0, 0.0}, halt_limits);
3395 active_profile_ =
halt.value();
3396 active_last_sample_ = snapshot_.command_position;
3397 active_target_ = stop_position;
3398 halt_profiled_ =
true;
3406 const double target =
command.value;
3407 if (!target_inside_limits(target))
3412 double target_velocity = 0.0;
3415 target_velocity =
command.end_velocity * override_;
3418 active_target_ = target;
3419 const otg::Limits1D limits = scaled_limits(
command);
3420 const rt::Result<otg::Profile1D> profile =
3422 snapshot_.command_acceleration},
3423 {target, target_velocity, 0.0}, limits);
3426 return profile.error();
3429 active_profile_ = profile.value();
3430 active_last_sample_ = snapshot_.command_position;
3431 continuous_hold_velocity_ = target_velocity;
3433 snapshot_.status =
command.homing
3440 void cycle_base_motion()
3447 if (override_paused_)
3454 if (!acceleration_profile_completed_)
3456 const AxisCommand &segment = queue_[acceleration_segment_index_];
3457 const double acceleration = segment.value;
3458 snapshot_.command_acceleration = acceleration;
3459 snapshot_.actual_acceleration = acceleration;
3460 snapshot_.command_velocity += acceleration;
3461 snapshot_.actual_velocity = snapshot_.command_velocity;
3462 snapshot_.command_position += snapshot_.command_velocity;
3463 snapshot_.actual_position = snapshot_.command_position;
3464 base_velocity_ = snapshot_.command_velocity;
3465 ++acceleration_segment_tick_;
3466 if (acceleration_segment_tick_ >= segment.min_duration_cycles)
3468 acceleration_segment_tick_ = 0;
3469 ++acceleration_segment_index_;
3470 if (acceleration_segment_index_ >= acceleration_segment_count_)
3472 acceleration_profile_completed_ =
true;
3473 snapshot_.active_command_reached_target =
true;
3474 snapshot_.command_acceleration = 0.0;
3475 snapshot_.actual_acceleration = 0.0;
3481 snapshot_.command_position += snapshot_.command_velocity;
3482 snapshot_.actual_position = snapshot_.command_position;
3487 if (!override_braking_ &&
3493 const double delta =
3494 active_command_.value - snapshot_.command_torque;
3495 if(active_command_.torque_ramp == 0.0 ||
3496 std::fabs(delta) <= active_command_.torque_ramp) {
3497 snapshot_.command_torque = active_command_.value;
3499 snapshot_.command_torque +=
3500 delta < 0.0 ? -active_command_.torque_ramp
3501 : active_command_.torque_ramp;
3503 snapshot_.command_velocity = 0.0;
3504 snapshot_.command_acceleration = 0.0;
3507 const double velocity =
3508 continuous_holding_ ? continuous_hold_velocity_ : signed_velocity(active_command_);
3509 base_velocity_ = velocity;
3510 snapshot_.command_velocity = velocity;
3511 snapshot_.actual_velocity = velocity;
3512 snapshot_.command_acceleration = 0.0;
3513 snapshot_.actual_acceleration = 0.0;
3514 snapshot_.command_position += velocity;
3515 snapshot_.actual_position = snapshot_.command_position;
3516 if (!continuous_holding_ && active_command_.min_duration_cycles > 0)
3519 if (active_tick_ >= active_command_.min_duration_cycles)
3536 const otg::State1D state =
3540 const double step = state.position - active_last_sample_;
3541 active_last_sample_ = state.position;
3542 snapshot_.command_position +=
step;
3543 snapshot_.command_velocity = state.velocity;
3544 snapshot_.command_acceleration = state.acceleration;
3545 snapshot_.actual_position = snapshot_.command_position;
3546 snapshot_.actual_velocity = state.velocity;
3547 snapshot_.actual_acceleration = state.acceleration;
3548 base_velocity_ = state.velocity;
3560 const double nominal = active_command_.velocity * override_;
3561 const double threshold =
3563 const double speed = std::fabs(state.velocity);
3564 if (speed > threshold)
3566 blend_armed_ =
true;
3568 else if (blend_armed_)
3576 blend_armed_ =
false;
3579 if (active_tick_ >= active_profile_.duration_cycles())
3581 if (override_braking_)
3583 override_braking_ =
false;
3584 override_paused_ =
true;
3585 snapshot_.command_velocity = 0.0;
3586 snapshot_.actual_velocity = 0.0;
3587 snapshot_.command_acceleration = 0.0;
3588 snapshot_.actual_acceleration = 0.0;
3593 if (active_tick_ < active_command_.min_duration_cycles)
3599 snapshot_.homed =
true;
3603 snapshot_.active_command_reached_target =
true;
3604 continuous_holding_ =
true;
3612 void cycle_superimposed()
3614 if (!superimposed_active_)
3623 ++superimposed_tick_;
3624 const otg::State1D state =
3626 const double step = state.position - superimposed_last_;
3627 superimposed_last_ = state.position;
3628 snapshot_.command_position +=
step;
3629 snapshot_.actual_position = snapshot_.command_position;
3630 snapshot_.command_velocity = base_velocity_ + state.velocity;
3631 snapshot_.actual_velocity = snapshot_.command_velocity;
3633 if (superimposed_tick_ >= superimposed_profile_.duration_cycles())
3635 superimposed_active_ =
false;
3636 superimposed_completed_id_ =
3637 superimposed_halting_ ? 0 : superimposed_id_;
3638 superimposed_id_ = 0;
3639 superimposed_halting_ =
false;
3640 snapshot_.command_velocity = base_velocity_;
3641 snapshot_.actual_velocity = base_velocity_;
3649 void finish_active()
3651 const bool locked_stop =
3653 stop_lock_id_ == snapshot_.active_command_id && !stop_release_requested_;
3655 continuous_holding_ =
false;
3656 base_velocity_ = 0.0;
3657 snapshot_.last_completed_command_id = snapshot_.active_command_id;
3658 snapshot_.active_command_id = 0;
3659 snapshot_.active_command_reached_target =
false;
3660 snapshot_.command_velocity = 0.0;
3661 snapshot_.actual_velocity = 0.0;
3662 snapshot_.command_acceleration = 0.0;
3663 snapshot_.actual_acceleration = 0.0;
3664 snapshot_.command_torque_limit = 0.0;
3668 : (active_command_.homing
3671 if (active_command_.kind ==
CommandKind::stop && active_command_.lock_stopping &&
3675 stop_release_requested_ =
false;
3681 start_next_queued();
3687 void blend_into_next()
3689 snapshot_.last_completed_command_id = snapshot_.active_command_id;
3690 blend_armed_ =
false;
3691 start_next_queued();
3694 void start_next_queued()
3701 const AxisCommand next = queue_[0];
3702 for (std::size_t i = 1; i < queue_.size(); ++i)
3704 queue_[i - 1] = queue_[i];
3714 continuous_holding_ =
false;
3715 acceleration_profile_completed_ =
false;
3716 acceleration_segment_count_ = 0;
3717 acceleration_segment_index_ = 0;
3718 acceleration_segment_tick_ = 0;
3719 override_braking_ =
false;
3720 override_paused_ =
false;
3721 base_velocity_ = 0.0;
3724 reset_superimposed();
3727 stream_filter_.end_session();
3729 stream_active_ =
false;
3731 torque_command_id_ = 0;
3733 stop_release_requested_ =
false;
3734 snapshot_.command_torque = 0.0;
3735 snapshot_.command_torque_limit = 0.0;
3736 snapshot_.torque_velocity_limit = 0.0;
3737 snapshot_.torque_acceleration_limit = 0.0;
3738 snapshot_.torque_deceleration_limit = 0.0;
3739 snapshot_.torque_jerk_limit = 0.0;
3741 snapshot_.torque_mode =
false;
3746 snapshot_.active_command_id = 0;
3747 snapshot_.active_command_reached_target =
false;
3748 snapshot_.command_velocity = 0.0;
3749 snapshot_.actual_velocity = 0.0;
3750 snapshot_.command_acceleration = 0.0;
3751 snapshot_.actual_acceleration = 0.0;
3754 void reset_superimposed()
3756 superimposed_active_ =
false;
3757 superimposed_id_ = 0;
3758 superimposed_completed_id_ = 0;
3759 superimposed_tick_ = 0;
3760 superimposed_last_ = 0.0;
3761 superimposed_halting_ =
false;
3768 ProbeSlot &slot = probes_[
input];
3769 const bool level = digital_input_[
input];
3770 const bool rising = level && !slot.last_level;
3771 slot.last_level = level;
3772 if (!slot.armed || !rising)
3776 const double position = snapshot_.actual_position;
3777 if (slot.window_only &&
3778 (position < slot.first_position || position > slot.last_position))
3783 slot.captured =
true;
3784 slot.recorded_position = position;
3791 bool captured =
false;
3792 bool window_only =
false;
3793 bool last_level =
false;
3794 double first_position = 0.0;
3795 double last_position = 0.0;
3796 double recorded_position = 0.0;
3797 std::uint32_t command_id = 0;
3802 std::uint32_t command_id = 0;
3806 struct AxisManagementResult
3808 std::uint32_t command_id = 0;
3812 rt::Result<std::uint32_t> submit_management(AxisManagementCommand
command,
3816 command.command_id = next_command_id_++;
3820 : rt::Result<std::uint32_t>::failure(added);
3831 if(
command.command_id == 0)
command.command_id = next_command_id_++;
3832 active_management_id_ =
command.command_id;
3836 const AxisSnapshot before = snapshot_;
3838 :
command.value - before.actual_position;
3856 remember_management_result(
command.command_id, result);
3857 last_management_command_id_ =
command.command_id;
3858 active_management_id_ = 0;
3862 void cycle_management()
3864 if(management_queue_.empty())
return;
3865 const AxisManagementCommand
command = management_queue_[0];
3866 for(std::size_t i = 1; i < management_queue_.size(); ++i) {
3867 management_queue_[i - 1] = management_queue_[i];
3869 management_queue_.pop_back();
3873 void remember_management_result(std::uint32_t command_id,
rt::ErrorCode error)
3876 for(std::size_t i = 1; i < management_results_.size(); ++i) {
3877 management_results_[i - 1] = management_results_[i];
3879 management_results_.pop_back();
3881 management_results_.push_back({command_id,
error});
3886 static constexpr std::size_t CommandErrorCapacity =
QueueCapacity + 4;
3888 void record_command_error(std::uint32_t command_id,
rt::ErrorCode code)
3894 command_errors_[command_error_cursor_] = {command_id, code};
3895 command_error_cursor_ = (command_error_cursor_ + 1) % CommandErrorCapacity;
3896 if (command_error_count_ < CommandErrorCapacity)
3898 ++command_error_count_;
3902 void *group_owner_ =
nullptr;
3904 double active_target_ = 0.0;
3905 double active_last_sample_ = 0.0;
3906 double base_velocity_ = 0.0;
3907 double continuous_hold_velocity_ = 0.0;
3908 double override_ = 1.0;
3909 double acceleration_override_ = 1.0;
3910 double jerk_override_ = 1.0;
3911 std::int64_t active_tick_ = 0;
3912 std::int64_t superimposed_tick_ = 0;
3913 double superimposed_last_ = 0.0;
3914 static constexpr std::size_t PhasingResultCapacity = 16;
3915 double phase_offset_ = 0.0;
3916 double phasing_target_ = 0.0;
3917 double phasing_start_offset_ = 0.0;
3918 double phasing_velocity_ = 0.0;
3919 double phasing_acceleration_ = 0.0;
3920 double approach_window_begin_ = 0.0;
3921 double approach_start_slave_ = 0.0;
3922 double sync_profile_target_velocity_ = 0.0;
3923 std::int64_t sync_profile_tick_ = 0;
3924 std::int64_t phasing_tick_ = 0;
3925 std::size_t command_error_cursor_ = 0;
3926 std::size_t command_error_count_ = 0;
3927 std::size_t acceleration_segment_count_ = 0;
3928 std::size_t acceleration_segment_index_ = 0;
3929 std::int64_t acceleration_segment_tick_ = 0;
3930 std::size_t passive_homing_input_ = 0;
3931 MotionLimits limits_{};
3932 GearInCommand sync_gear_{};
3933 CombineAxesCommand sync_combine_{};
3934 AxisSnapshot snapshot_{};
3935 AxisCommand active_command_{};
3936 CamInCommand sync_cam_{};
3937 std::array<CamTableSelection, QueueCapacity> cam_table_selections_{};
3938 std::array<ProbeSlot, DigitalInputCount> probes_{};
3939 rt::StaticVector<AxisCommand, QueueCapacity> queue_{};
3940 rt::StaticVector<AxisManagementCommand, QueueCapacity> management_queue_{};
3941 rt::StaticVector<AxisManagementResult, QueueCapacity> management_results_{};
3942 rt::StaticVector<PhasingCommand, QueueCapacity> phasing_queue_{};
3943 exec::CamSpline cam_spline_{};
3944 otg::Profile1D active_profile_{};
3945 otg::Profile1D superimposed_profile_{};
3946 otg::Profile1D sync_profile_{};
3947 otg::Profile1D phasing_profile_{};
3948 stream::StreamFilter1D stream_filter_{};
3950 std::uint32_t next_command_id_ = 1;
3951 std::uint32_t active_management_id_ = 0;
3952 std::uint32_t last_management_command_id_ = 0;
3953 std::uint32_t superimposed_id_ = 0;
3954 std::uint32_t superimposed_completed_id_ = 0;
3958 std::uint32_t sync_id_ = 0;
3959 std::uint32_t phasing_id_ = 0;
3960 std::uint32_t stream_id_ = 0;
3961 std::uint32_t torque_command_id_ = 0;
3962 std::uint32_t stop_lock_id_ = 0;
3963 std::uint32_t passive_homing_id_ = 0;
3964 std::uint32_t passive_homing_aborted_id_ = 0;
3965 std::array<CommandError, CommandErrorCapacity> command_errors_{};
3966 std::array<PhasingCommandResult, PhasingResultCapacity> phasing_results_{};
3967 std::size_t phasing_result_cursor_ = 0;
3968 std::size_t phasing_result_count_ = 0;
3969 bool active_ =
false;
3970 bool continuous_holding_ =
false;
3971 bool override_braking_ =
false;
3972 bool override_paused_ =
false;
3973 bool halt_profiled_ =
false;
3974 bool blend_armed_ =
false;
3975 bool superimposed_active_ =
false;
3976 bool superimposed_halting_ =
false;
3977 bool phasing_active_ =
false;
3978 bool sync_profile_ready_ =
false;
3979 bool stream_active_ =
false;
3980 bool homing_limits_suspended_ =
false;
3981 bool stop_release_requested_ =
false;
3982 bool power_feedback_ =
true;
3983 bool positive_enabled_ =
false;
3984 bool negative_enabled_ =
false;
3985 bool acceleration_profile_completed_ =
false;
3986 std::array<bool, DigitalInputCount> digital_input_{};
3987 std::array<bool, DigitalOutputCount> digital_output_{};