From 1bccd5557add1618d53d20793900d662fb90ac5a Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 6 May 2025 14:56:42 +0200 Subject: [PATCH] rc_update: add unit tests for two way payload power switch with the intention to extend it to an optional 3 way switch. --- src/modules/rc_update/RCUpdateTest.cpp | 69 +++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/modules/rc_update/RCUpdateTest.cpp b/src/modules/rc_update/RCUpdateTest.cpp index d0df1c28be..8925b24d89 100644 --- a/src/modules/rc_update/RCUpdateTest.cpp +++ b/src/modules/rc_update/RCUpdateTest.cpp @@ -128,13 +128,39 @@ public: EXPECT_EQ(manual_control_switches_sub.get().return_switch, expected_position); } + void checkPayloadPowerSwitch(float channel_value, float threshold, uint8_t expected_position) + { + // GIVEN: First channel is configured as payload power switch + _param_rc_map_pay_sw.set(1); + _param_rc_map_pay_sw.commit(); + _param_rc_payload_th.set(threshold); + _param_rc_payload_th.commit(); + _rc_update.updateParams(); + EXPECT_EQ(_param_rc_map_pay_sw.get(), 1); + EXPECT_FLOAT_EQ(_param_rc_payload_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)}; + EXPECT_EQ(manual_control_switches_sub.get().payload_power_switch, expected_position); + } + TestRCUpdate _rc_update; DEFINE_PARAMETERS( (ParamInt) _param_rc_map_fltmode, (ParamInt) _param_rc_map_fltm_btn, + (ParamInt) _param_rc_map_return_sw, - (ParamFloat) _param_rc_return_th + (ParamFloat) _param_rc_return_th, + + (ParamInt) _param_rc_map_pay_sw, + (ParamFloat) _param_rc_payload_th ) }; @@ -233,3 +259,44 @@ TEST_F(RCUpdateTest, ReturnSwitchNegativeThresholds) checkReturnSwitch(1.f, -.001f, 3); // Above minimum threshold -> SWITCH_POS_OFF checkReturnSwitch(-1.f, -.001f, 1); // Slightly below minimum threshold -> SWITCH_POS_OFF } + +TEST_F(RCUpdateTest, PayloadPowerSwitchPositiveThresholds) +{ + checkPayloadPowerSwitch(-1.f, 0.5f, 3); // Below threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(0.f, 0.5f, 3); // On threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.001f, 0.5f, 1); // Slightly above threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(1.f, 0.5f, 1); // Above threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(-1.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(0.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.5f, 0.75f, 3); // On threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.501f, 0.75f, 1); // Slightly above threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(1.f, 0.75f, 1); // Above threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(-1.f, 0.f, 3); // On minimum threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(-.999f, 0.f, 1); // Slightly above minimum threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(1.f, 0.f, 1); // Above minimum threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(-1.f, 1.f, 3); // Below maximum threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(1.f, 1.f, 3); // On maximum threshold -> SWITCH_POS_OFF +} + +TEST_F(RCUpdateTest, PayloadPowerSwitchNegativeThresholds) +{ + checkPayloadPowerSwitch(1.f, -0.5f, 3); // Above threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(0.f, -0.5f, 3); // On threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(-.001f, -0.5f, 1); // Slightly below threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(-1.f, -0.5f, 1); // Below threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(1.f, -0.75f, 3); // Above threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.5f, -0.75f, 3); // On threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.499f, -0.75f, 1); // Slightly below threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(-1.f, -0.75f, 1); // Below threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(1.f, -1.f, 3); // On maximum threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(.999f, -1.f, 1); // Slighly below maximum threshold -> SWITCH_POS_ON + checkPayloadPowerSwitch(-1.f, -1.f, 1); // Below minimum threshold -> SWITCH_POS_ON + + checkPayloadPowerSwitch(1.f, -.001f, 3); // Above minimum threshold -> SWITCH_POS_OFF + checkPayloadPowerSwitch(-1.f, -.001f, 1); // Slightly below minimum threshold -> SWITCH_POS_OFF +}