diff --git a/src/modules/rc_update/RCUpdateTest.cpp b/src/modules/rc_update/RCUpdateTest.cpp index 393ffdae12..f07b119131 100644 --- a/src/modules/rc_update/RCUpdateTest.cpp +++ b/src/modules/rc_update/RCUpdateTest.cpp @@ -38,15 +38,87 @@ using namespace rc_update; -TEST(RCUpdateTest, ModeSlotUnassigned) +class TestRCUpdate : public RCUpdate +{ +public: + void UpdateManualSwitches(const hrt_abstime ×tamp_sample) { RCUpdate::UpdateManualSwitches(timestamp_sample); } + void update_rc_functions() { RCUpdate::update_rc_functions(); } + void setChannel(size_t index, float channel_value) { _rc.channels[index] = channel_value; } +}; + +class RCUpdateTest : public ::testing::Test, ModuleParams +{ +public: + RCUpdateTest() : ModuleParams(nullptr) {} + + void SetUp() override + { + // Disable autosaving parameters to avoid busy loop in param_set() + param_control_autosave(false); + } + + void checkModeSlotSwitch(float channel_value, uint8_t expected_slot) + { + // GIVEN: First channel is configured as mode switch + _param_rc_map_fltmode.set(1); + EXPECT_EQ(_param_rc_map_fltmode.get(), 1); + // GIVEN: First channel has some value + _rc_update.setChannel(0, channel_value); + + // WHEN: we update the switches two times to pass the simple outlier protection + _rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(0); + + // THEN: we receive the expected mode slot + uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; + manual_control_switches_sub.update(); + + EXPECT_EQ(manual_control_switches_sub.get().mode_slot, expected_slot); + } + + void checkModeSlotButton(uint8_t button_configuration, uint8_t channel, float channel_value, uint8_t expected_slot) + { + // GIVEN: Buttons are configured + _param_rc_map_fltm_btn.set(button_configuration); + EXPECT_EQ(_param_rc_map_fltm_btn.get(), button_configuration); + // GIVEN: buttons are mapped + _rc_update.update_rc_functions(); + // GIVEN: First channel has some value + _rc_update.setChannel(channel - 1, channel_value); + + // WHEN: we update the switches 4 times: + // - initiate the button press + // - keep the same button pressed + // - hold the button for 50ms + // - pass the simple outlier protection + _rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(51_ms); + _rc_update.UpdateManualSwitches(51_ms); + + // THEN: we receive the expected mode slot + uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; + manual_control_switches_sub.update(); + + EXPECT_EQ(manual_control_switches_sub.get().mode_slot, expected_slot); + } + + TestRCUpdate _rc_update; + + DEFINE_PARAMETERS( + (ParamInt) _param_rc_map_fltmode, + (ParamInt) _param_rc_map_fltm_btn + ) +}; + +TEST_F(RCUpdateTest, ModeSlotUnassigned) { - RCUpdate rc_update; // GIVEN: Default configuration with no assigned mode switch - EXPECT_EQ(rc_update._param_rc_map_fltmode.get(), 0); + EXPECT_EQ(_param_rc_map_fltmode.get(), 0); // WHEN: we update the switches two times to pass the simple outlier protection - rc_update.UpdateManualSwitches(0); - rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(0); // THEN: we receive no mode slot uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; @@ -55,28 +127,7 @@ TEST(RCUpdateTest, ModeSlotUnassigned) EXPECT_EQ(manual_control_switches_sub.get().mode_slot, 0); // manual_control_switches_s::MODE_SLOT_NONE } -void checkModeSlotSwitch(float channel_value, uint8_t expected_slot) -{ - RCUpdate rc_update; - - // GIVEN: First channel is configured as mode switch - rc_update._param_rc_map_fltmode.set(1); - EXPECT_EQ(rc_update._param_rc_map_fltmode.get(), 1); - // GIVEN: First channel has some value - rc_update._rc.channels[0] = channel_value; - - // WHEN: we update the switches two times to pass the simple outlier protection - rc_update.UpdateManualSwitches(0); - rc_update.UpdateManualSwitches(0); - - // THEN: we receive the expected mode slot - uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; - manual_control_switches_sub.update(); - - EXPECT_EQ(manual_control_switches_sub.get().mode_slot, expected_slot); -} - -TEST(RCUpdateTest, ModeSlotSwitchAllValues) +TEST_F(RCUpdateTest, ModeSlotSwitchAllValues) { checkModeSlotSwitch(-1.f, 1); // manual_control_switches_s::MODE_SLOT_1 checkModeSlotSwitch(-.5f, 2); // manual_control_switches_s::MODE_SLOT_2 @@ -86,36 +137,7 @@ TEST(RCUpdateTest, ModeSlotSwitchAllValues) checkModeSlotSwitch(1.f, 6); // manual_control_switches_s::MODE_SLOT_6 } -void checkModeSlotButton(uint8_t button_configuration, uint8_t channel, float channel_value, uint8_t expected_slot) -{ - RCUpdate rc_update; - - // GIVEN: Buttons are configured - rc_update._param_rc_map_fltm_btn.set(button_configuration); - EXPECT_EQ(rc_update._param_rc_map_fltm_btn.get(), button_configuration); - // GIVEN: buttons are mapped - rc_update.update_rc_functions(); - // GIVEN: First channel has some value - rc_update._rc.channels[channel - 1] = channel_value; - - // WHEN: we update the switches 4 times: - // - initiate the button press - // - keep the same button pressed - // - hold the button for 50ms - // - pass the simple outlier protection - rc_update.UpdateManualSwitches(0); - rc_update.UpdateManualSwitches(0); - rc_update.UpdateManualSwitches(51_ms); - rc_update.UpdateManualSwitches(51_ms); - - // THEN: we receive the expected mode slot - uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; - manual_control_switches_sub.update(); - - EXPECT_EQ(manual_control_switches_sub.get().mode_slot, expected_slot); -} - -TEST(RCUpdateTest, ModeSlotButtonAllValues) +TEST_F(RCUpdateTest, ModeSlotButtonAllValues) { checkModeSlotButton(1, 1, -1.f, 0); // button not pressed -> manual_control_switches_s::MODE_SLOT_NONE checkModeSlotButton(1, 1, 0.f, 0); // button not pressed over threshold -> manual_control_switches_s::MODE_SLOT_NONE diff --git a/src/modules/rc_update/rc_update.h b/src/modules/rc_update/rc_update.h index 76ff66833e..104e2cb492 100644 --- a/src/modules/rc_update/rc_update.h +++ b/src/modules/rc_update/rc_update.h @@ -89,6 +89,7 @@ public: int print_status() override; +protected: static constexpr uint64_t VALID_DATA_MIN_INTERVAL_US{1_s / 3}; // assume valid RC input is at least 3 Hz void Run() override;