From 1fa5136e303fd3e07d05570d32323f35d7712f68 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Mon, 3 Jul 2023 17:22:45 +0200 Subject: [PATCH] RCUpdateTest: add unit test for return switch --- src/modules/rc_update/RCUpdateTest.cpp | 65 +++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/modules/rc_update/RCUpdateTest.cpp b/src/modules/rc_update/RCUpdateTest.cpp index 8c57d8d650..a58a01d3dc 100644 --- a/src/modules/rc_update/RCUpdateTest.cpp +++ b/src/modules/rc_update/RCUpdateTest.cpp @@ -110,11 +110,37 @@ public: _rc_update.setChannel(channel - 1, 0.f); } + void checkReturnSwitch(float channel_value, float threshold, uint8_t expected_position) + { + // GIVEN: First channel is configured as return switch + _param_rc_map_return_sw.set(1); + _param_rc_map_return_sw.commit(); + _param_rc_return_th.set(threshold); + _param_rc_return_th.commit(); + _rc_update.updateParams(); + EXPECT_EQ(_param_rc_map_return_sw.get(), 1); + EXPECT_FLOAT_EQ(_param_rc_return_th.get(), threshold); + // 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().return_switch, expected_position); + } + TestRCUpdate _rc_update; DEFINE_PARAMETERS( (ParamInt) _param_rc_map_fltmode, - (ParamInt) _param_rc_map_fltm_btn + (ParamInt) _param_rc_map_fltm_btn, + (ParamInt) _param_rc_map_return_sw, + (ParamFloat) _param_rc_return_th ) }; @@ -160,3 +186,40 @@ TEST_F(RCUpdateTest, ModeSlotButtonAllValues) checkModeSlotButton(31, 6, 1.f, 0); // button 6 pressed but not configured -> manual_control_switches_s::MODE_SLOT_NONE checkModeSlotButton(63, 6, 1.f, 6); // button 6 pressed -> manual_control_switches_s::MODE_SLOT_6 } + +TEST_F(RCUpdateTest, ReturnSwitchUnassigned) +{ + // GIVEN: Default configuration with no assigned return switch + EXPECT_EQ(_param_rc_map_return_sw.get(), 0); + + // WHEN: we update the switches two times to pass the simple outlier protection + _rc_update.UpdateManualSwitches(0); + _rc_update.UpdateManualSwitches(0); + + // THEN: we receive an unmapped return switch state + uORB::SubscriptionData manual_control_switches_sub{ORB_ID(manual_control_switches)}; + manual_control_switches_sub.update(); + + EXPECT_EQ(manual_control_switches_sub.get().return_switch, 0); // manual_control_switches_s::SWITCH_POS_NONE +} + +TEST_F(RCUpdateTest, ReturnSwitchPositiveThresholds) +{ + checkReturnSwitch(-1.f, 0.5f, 3); // Below threshold -> SWITCH_POS_OFF + checkReturnSwitch(0.f, 0.5f, 3); // On threshold -> SWITCH_POS_OFF + checkReturnSwitch(.001f, 0.5f, 1); // Slightly above threshold -> SWITCH_POS_ON + checkReturnSwitch(1.f, 0.5f, 1); // Above threshold -> SWITCH_POS_ON + + checkReturnSwitch(-1.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF + checkReturnSwitch(0.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF + checkReturnSwitch(.5f, 0.75f, 3); // On threshold -> SWITCH_POS_OFF + checkReturnSwitch(.501f, 0.75f, 1); // Slightly above threshold -> SWITCH_POS_ON + checkReturnSwitch(1.f, 0.75f, 1); // Above threshold -> SWITCH_POS_ON + + checkReturnSwitch(-1.f, 0.f, 3); // On minimum threshold -> SWITCH_POS_OFF + checkReturnSwitch(-.999f, 0.f, 1); // Slightly above minimum threshold -> SWITCH_POS_ON + checkReturnSwitch(1.f, 0.f, 1); // Above minimum threshold -> SWITCH_POS_ON + + checkReturnSwitch(-1.f, 1.f, 3); // Below maximum threshold -> SWITCH_POS_OFF + checkReturnSwitch(1.f, 1.f, 3); // On maximum threshold -> SWITCH_POS_OFF +}