Skip to content

Commit

Permalink
add the unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanthecoder committed Feb 6, 2025
1 parent 51b9332 commit a28e57d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class MotorInterruptHandler {
can::messages::ErrorMessage{.message_index = message_index,
.severity = severity,
.error_code = err_code});
if (err_code == can::ids::ErrorCode::collision_detected) {
if (err_code == can::ids::ErrorCode::collision_detected && _has_active_move) {
build_and_send_ack(AckMessageId::position_error);
}
status_queue_client.send_move_status_reporter_queue(
Expand Down
2 changes: 1 addition & 1 deletion motor-control/core/stall_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ auto StallCheck::reset_itr_counts(int32_t stepper_steps) -> void {
return 0;
}
return 1.0F / _stepper_tick_per_um;
}
}
41 changes: 40 additions & 1 deletion motor-control/tests/test_motor_stall_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ SCENARIO("motor handler stall detection") {
REQUIRE(!test_objs.hw.position_flags.check_flag(
Flags::stepper_position_ok));
THEN("move completed and no error was raised") {
REQUIRE(test_objs.reporter.messages.size() == 6);
REQUIRE(test_objs.reporter.messages.size() == 5);
for (auto& element : test_objs.reporter.messages) {
printf("%ld\n", element.index());
}
Expand Down Expand Up @@ -353,4 +353,43 @@ SCENARIO("motor handler stall detection") {
}
}
}

GIVEN("A stall when a motor is not in motion.") {
auto velocity = 5;
auto cond = GENERATE(Stops::none, Stops::sync_line);
auto msg1 = Move{.message_index = 101,
.duration = 23,
.velocity = velocity,
.stop_condition = static_cast<uint8_t>(cond)};
test_objs.hw.sim_set_encoder_pulses(0);
test_objs.handler.set_current_position(0);


WHEN("A move is commanded") {
test_objs.queue.try_write(msg1);
test_objs.handler.update_move();
for (int i = 0; i < (int)msg1.duration; ++i) {
test_objs.handler.run_interrupt();
test_objs.hw.sim_set_encoder_pulses(i*velocity);
test_objs.handler.set_current_position(static_cast<uint64_t>(i*velocity) << 31);
}
THEN("Move ends successfully.") {
REQUIRE(test_objs.hw.position_flags.check_flag(
Flags::stepper_position_ok));
REQUIRE(test_objs.reporter.messages.size() == 1);
}
AND_WHEN("Motion happens after the move.") {
test_objs.hw.sim_set_encoder_pulses(0);
test_objs.reporter.messages.clear();
for (int i = 0; i < 10; ++i) {
test_objs.handler.run_interrupt();
}
THEN("an error is sent.") {
// should be 2 but idk why this runs more than once
REQUIRE(test_objs.reporter.messages.size() == 3);
REQUIRE(!test_objs.hw.position_flags.check_flag(Flags::stepper_position_ok));
}
}
}
}
}

0 comments on commit a28e57d

Please sign in to comment.