194 const bool falling = !
execute && last_execute_;
195 const bool rising =
execute && !last_execute_;
202 if (!
execute && terminal_low_cycle_)
205 terminal_low_cycle_ =
false;
210 terminal_low_cycle_ =
false;
236 if (
axis_ref->command_pending(phase_command_id_))
260 if (
axis_ref->command_pending(phase_command_id_))
274 start_position_ =
axis_ref->snapshot().command_position;
286 start_position_ =
axis_ref->snapshot().command_position;
293 return level.
error();
295 last_signal_level_ = level.
value();
306 phase_command_id_ = 0;
307 return armed.
error();
309 probe_command_id_ = armed.
value();
331 if (
axis_ref->command_pending(phase_command_id_))
360 if (limit_exceeded(snap))
365 if (level && level.
value() != escape_level_)
382 if (limit_exceeded(snap))
386 if (reverse_on_limit_ && (
axis_ref->axis_info_inputs().limit_switch_pos ||
387 axis_ref->axis_info_inputs().limit_switch_neg))
403 if (search_condition_met())
407 release_owned_probe();
430 cmd.
value = captured_position_;
442 phase_command_id_ = moved.
value();
470 phase_command_id_ = 0;
471 terminal_low_cycle_ = !
execute;
482 release_owned_probe();
490 phase_command_id_ = 0;
491 terminal_low_cycle_ = !
execute;
494 void command_aborted()
496 release_owned_probe();
509 phase_command_id_ = 0;
510 terminal_low_cycle_ = !
execute;
513 void release_owned_probe()
515 if (
axis_ref !=
nullptr && probe_command_id_ != 0 &&
520 probe_command_id_ = 0;
523 bool search_condition_met()
534 const bool current = level.value();
535 const bool rising = !last_signal_level_ &&
current;
536 const bool falling = last_signal_level_ && !
current;
538 switch (switch_mode_)
549 return direction_sign_ > 0.0 ? rising : falling;
551 return direction_sign_ < 0.0 ? rising : falling;
558 axis::AxisCommand cmd{};
562 cmd.acceleration =
axis_ref->motion_limits().max_acceleration;
563 cmd.deceleration =
axis_ref->motion_limits().max_deceleration;
564 cmd.jerk =
axis_ref->motion_limits().max_jerk;
568 const rt::Result<std::uint32_t> result =
axis_ref->submit(cmd);
571 phase_command_id_ = result.value();
602 use_probe_ = use_probe;
615 switch (switch_mode_)
637 direction_sign_ = 1.0;
640 direction_sign_ = -1.0;
643 if (!allow_switch_modes)
645 direction_sign_ = initial_level ? -1.0 : 1.0;
648 if (!allow_switch_modes)
650 direction_sign_ = initial_level ? 1.0 : -1.0;
673 axis::AxisCommand cmd{};
683 return result.error();
685 phase_command_id_ = result.value();
689 double start_position_ = 0.0;
690 double captured_position_ = 0.0;
691 std::int64_t search_cycles_ = 0;
692 std::uint32_t phase_command_id_ = 0;
693 std::uint32_t probe_command_id_ = 0;
694 bool last_execute_ =
false;
695 bool terminal_low_cycle_ =
false;
696 double direction_sign_ = 1.0;
697 axis::ReferenceSignalRef reference_signal_{};
700 bool use_probe_ =
true;
701 bool last_signal_level_ =
false;
702 bool escape_level_ =
false;
703 bool reverse_on_limit_ =
false;
886 const bool falling = !
execute && last_execute_;
887 const bool rising =
execute && !last_execute_;
889 if (falling && phase_ == Phase::idle &&
894 if (!
execute && terminal_low_cycle_)
897 terminal_low_cycle_ =
false;
917 terminal_low_cycle_ =
false;
935 axis::AxisCommand command{};
945 const rt::Result<std::uint32_t> submitted =
axis_ref->submit(command);
948 fail(submitted.error());
951 command_id_ = submitted.value();
953 detected_cycles_ = 0;
955 if (
axis_ref->command_pending(command_id_))
958 phase_ = Phase::queued;
968 if (
axis_ref ==
nullptr || phase_ == Phase::idle)
970 const axis::AxisSnapshot &snapshot =
axis_ref->snapshot();
971 if (phase_ == Phase::queued)
973 if (
axis_ref->command_pending(command_id_))
977 if (snapshot.active_command_id == command_id_)
993 if (phase_ == Phase::searching)
995 if (snapshot.active_command_id != command_id_)
1003 std::fabs(snapshot.command_position - start_position_) >
distance_limit))
1009 const bool torque_reached =
1011 const bool velocity_reached =
1013 detected_cycles_ = torque_reached && velocity_reached ? detected_cycles_ + 1 : 0;
1016 if (detected_cycles_ < required)
1018 axis::AxisCommand
halt{};
1021 halt.acceleration =
axis_ref->motion_limits().max_acceleration;
1022 halt.deceleration =
axis_ref->motion_limits().max_deceleration;
1025 const rt::Result<std::uint32_t> submitted =
axis_ref->submit(halt);
1028 fail(submitted.error());
1031 command_id_ = submitted.value();
1032 phase_ = Phase::halting;
1035 if (snapshot.active_command_id == command_id_)
1037 if (snapshot.active_command_id != 0 || snapshot.last_completed_command_id != command_id_)
1049 phase_ = Phase::positioning;
1054 phase_ = Phase::idle;
1055 terminal_low_cycle_ = !
execute;
1058 void activate_search()
1061 start_position_ =
axis_ref->snapshot().command_position;
1062 elapsed_cycles_ = 0;
1063 detected_cycles_ = 0;
1065 phase_ = Phase::searching;
1073 phase_ = Phase::idle;
1074 terminal_low_cycle_ = !
execute;
1080 outputs.command_aborted =
true;
1081 phase_ = Phase::idle;
1082 terminal_low_cycle_ = !
execute;
1085 Phase phase_ = Phase::idle;
1086 bool last_execute_ =
false;
1087 bool terminal_low_cycle_ =
false;
1088 std::uint32_t command_id_ = 0;
1089 double start_position_ = 0.0;
1090 std::int64_t elapsed_cycles_ = 0;
1091 std::int64_t detected_cycles_ = 0;
1139 reference_signal_ = reference;
1145 const bool falling = !
execute && last_execute_;
1146 const bool rising =
execute && !last_execute_;
1148 if (falling && phase_ == Phase::idle &&
1153 if (!
execute && terminal_low_cycle_)
1156 terminal_low_cycle_ =
false;
1173 bool valid_map()
const
1175 if (code_map_ ==
nullptr || code_map_->
count == 0 ||
1181 for (std::size_t i = 0; i < code_map_->
count; ++i)
1183 const DistanceCodeEntry &entry = code_map_->
entries[i];
1184 if (!std::isfinite(entry.signed_distance) ||
1185 !std::isfinite(entry.second_mark_position) || entry.signed_distance == 0.0)
1187 for (std::size_t j = i + 1; j < code_map_->
count; ++j)
1200 terminal_low_cycle_ =
false;
1227 const rt::Result<std::uint32_t> submitted =
axis_ref->submit(command);
1230 fail(submitted.error());
1233 command_id_ = submitted.value();
1234 elapsed_cycles_ = 0;
1236 if (
axis_ref->command_pending(command_id_))
1239 phase_ = Phase::queued;
1249 if (
axis_ref ==
nullptr || phase_ == Phase::idle)
1251 const axis::AxisSnapshot &snapshot =
axis_ref->snapshot();
1252 if (phase_ == Phase::queued)
1254 if (
axis_ref->command_pending(command_id_))
1256 if (snapshot.active_command_id == command_id_)
1266 if (phase_ == Phase::first_mark || phase_ == Phase::second_mark)
1268 if (snapshot.active_command_id != command_id_)
1276 std::fabs(snapshot.command_position - start_position_) >
distance_limit))
1282 if (!
axis_ref->probe_captured(reference_signal_.input))
1284 const double mark =
axis_ref->probe_recorded_position(reference_signal_.input);
1286 if (phase_ == Phase::first_mark)
1289 const rt::Result<std::uint32_t> armed =
1290 axis_ref->arm_touch_probe(reference_signal_.input,
false, 0.0, 0.0);
1293 fail(armed.error());
1296 probe_id_ = armed.value();
1297 phase_ = Phase::second_mark;
1300 const double distance = mark - first_mark_;
1301 std::size_t matches = 0;
1302 double position = 0.0;
1303 for (std::size_t i = 0; i < code_map_->count; ++i)
1305 if (std::fabs(distance - code_map_->entries[i].signed_distance) <=
1306 code_map_->tolerance)
1309 position = code_map_->entries[i].second_mark_position;
1317 resolved_position_ = position;
1318 axis::AxisCommand
halt{};
1321 halt.acceleration =
axis_ref->motion_limits().max_acceleration;
1322 halt.deceleration =
axis_ref->motion_limits().max_deceleration;
1325 const rt::Result<std::uint32_t> submitted =
axis_ref->submit(halt);
1328 fail(submitted.error());
1331 command_id_ = submitted.value();
1332 phase_ = Phase::halting;
1335 if (snapshot.active_command_id == command_id_)
1337 if (snapshot.active_command_id != 0 || snapshot.last_completed_command_id != command_id_)
1350 phase_ = Phase::idle;
1351 terminal_low_cycle_ = !
execute;
1356 const rt::Result<std::uint32_t> armed =
1357 axis_ref->arm_touch_probe(reference_signal_.input,
false, 0.0, 0.0);
1360 fail(armed.error());
1364 probe_id_ = armed.value();
1365 start_position_ =
axis_ref->snapshot().command_position;
1366 elapsed_cycles_ = 0;
1368 phase_ = Phase::first_mark;
1371 void release_probe()
1373 if (
axis_ref !=
nullptr && probe_id_ != 0 &&
1374 axis_ref->probe_command_id(reference_signal_.input) == probe_id_)
1376 axis_ref->abort_trigger(reference_signal_.input);
1387 phase_ = Phase::idle;
1388 terminal_low_cycle_ = !
execute;
1395 outputs.command_aborted =
true;
1396 phase_ = Phase::idle;
1397 terminal_low_cycle_ = !
execute;
1400 Phase phase_ = Phase::idle;
1401 bool last_execute_ =
false;
1402 bool terminal_low_cycle_ =
false;
1403 std::uint32_t command_id_ = 0;
1404 std::uint32_t probe_id_ = 0;
1405 double start_position_ = 0.0;
1406 double first_mark_ = 0.0;
1407 double resolved_position_ = 0.0;
1408 std::int64_t elapsed_cycles_ = 0;
1409 const DistanceCodeMap *code_map_ =
nullptr;
1410 axis::ReferenceSignalRef reference_signal_{0};