Skip to content

Commit

Permalink
get all the logic right
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanthecoder committed Feb 6, 2025
1 parent e3844e6 commit 51b9332
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,29 @@ class MotorInterruptHandler {
hardware.unstep();
} else {
if (!_has_active_move && !has_move_messages()) {
printf("no move\n");
if (stall_detected()) {
printf("stall\n");
if (!stall_handled) {
printf("stall not handled\n");
if (stall_expected) {
printf("It's ok its expected");
} else {
if (!hardware.position_flags.check_flag(MotorPositionStatus::Flags::encoder_position_ok)) {
printf("all good we dont know where we are\n");
} else {
printf("steps %d encoder %d\n", hardware.get_step_tracker(), hardware.get_encoder_pulses());
handle_stall_without_movement();
}
}
}
if (check_for_idle_stall()) {
handle_stall_without_movement();
}
}
}
}

auto check_for_idle_stall() -> bool {
// if were expecting to be in a good state check for a stall
// good state means:
// 1. We have an encoder position to check
// 2. We haven't already sent a stall detection
// 3. A previous move didn't already trigger a error by replying with
// the wrong stop condition
// 4. The previous move wasn't a "ignore stalls" move
if (in_good_state && !stall_expected && !stall_handled &&
hardware.position_flags.check_flag(
MotorPositionStatus::Flags::encoder_position_ok)) {
return stall_detected();
}
return false;
}

auto check_for_stall() -> bool {
if (stall_detected()) {
hardware.position_flags.clear_flag(
Expand All @@ -122,11 +124,9 @@ class MotorInterruptHandler {
}

auto handle_stall_without_movement() -> void {
printf("sending error\n");
hardware.position_flags.clear_flag(
MotorPositionStatus::Flags::stepper_position_ok);
MotorPositionStatus::Flags::stepper_position_ok);
cancel_and_clear_moves(can::ids::ErrorCode::collision_detected);
printf("stall handled\n");
stall_handled = true;
}

Expand Down Expand Up @@ -546,15 +546,17 @@ class MotorInterruptHandler {
_has_active_move = false;
tick_count = 0x0;
stall_handled = false;
printf("move finished stall not handled\n");
if (buffered_move.stop_condition != 0 &&
ack_msg_id == AckMessageId::complete_without_condition) {
in_good_state = false;
}
build_and_send_ack(ack_msg_id);
set_buffered_move(MotorMoveMessage{});
// update the stall check ideal encoder counts based on
// last known location
if (!has_move_messages()) {
stall_checker.reset_itr_counts(hardware.get_step_tracker());
}
printf("move done \n");
}

void reset() {
Expand All @@ -572,7 +574,7 @@ class MotorInterruptHandler {
stall_checker.reset_itr_counts(0);
stall_handled = false;
stall_expected = false;
printf("stall not handled reset\n");
in_good_state = true;
}

[[nodiscard]] static auto overflow(q31_31 current, q31_31 future) -> bool {
Expand Down Expand Up @@ -729,6 +731,7 @@ class MotorInterruptHandler {
bool clear_queue_until_empty = false;
bool stall_handled = false;
bool stall_expected = false;
bool in_good_state = true;
bool in_estop = false;
std::atomic_bool _has_active_move = false;
};
Expand Down
2 changes: 0 additions & 2 deletions motor-control/tests/test_limit_switch_backoff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ SCENARIO(
REQUIRE(test_objs.handler.has_active_move());

AND_WHEN("the limit switch has not been triggered") {
printf("running intterupt\n");
for (int i = 0; i < (int)msg1.duration; ++i) {
test_objs.handler.run_interrupt();
}
Expand All @@ -222,7 +221,6 @@ SCENARIO(
THEN(
"the move should be stopped with ack id = stopped without "
"condition") {
printf("here's where we check\n");
REQUIRE(test_objs.reporter.messages.size() == 1);
Ack read_ack =
std::get<Ack>(test_objs.reporter.messages.front());
Expand Down
1 change: 0 additions & 1 deletion motor-control/tests/test_motor_stall_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ SCENARIO("motor handler stall detection") {
REQUIRE(ack_msg.message_index == 13);
test_objs.reporter.messages.clear();
WHEN("the interrupt runs again") {
printf("running interrupt");
for (int i = 0; i < (int)msg2.duration; ++i) {
test_objs.handler.run_interrupt();
}
Expand Down

0 comments on commit 51b9332

Please sign in to comment.