mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-17 06:10:35 +08:00
ManualControlSelector: revamp logic to switch when configuration is changed
This commit is contained in:
@@ -39,7 +39,7 @@ namespace manual_control
|
||||
|
||||
void ManualControlSelector::update_time_only(uint64_t now)
|
||||
{
|
||||
if (now - _setpoint.chosen_input.timestamp_sample > _timeout) {
|
||||
if (!isInputValid(_setpoint.chosen_input, now)) {
|
||||
_setpoint.valid = false;
|
||||
_instance = -1;
|
||||
}
|
||||
@@ -47,16 +47,27 @@ void ManualControlSelector::update_time_only(uint64_t now)
|
||||
|
||||
void ManualControlSelector::update_manual_control_input(uint64_t now, const manual_control_input_s &input, int instance)
|
||||
{
|
||||
// This method requires the current timestamp explicitly in order to prevent the case
|
||||
// where the timestamp_sample of some source is already outdated.
|
||||
|
||||
if (now >= input.timestamp_sample && now - input.timestamp_sample > _timeout) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If previous setpoint is timed out, set it invalid, so it can get replaced below.
|
||||
// First check if the setpoint in use got invalid, so it can get replaced below.
|
||||
update_time_only(now);
|
||||
|
||||
const bool update_existing_input = _setpoint.valid == true && input.data_source == _setpoint.chosen_input.data_source;
|
||||
const bool start_using_new_input = _setpoint.valid == false;
|
||||
|
||||
// Switch to new input if it's valid and we don't already have a valid one
|
||||
if (isInputValid(input, now) && (update_existing_input || start_using_new_input)) {
|
||||
_setpoint = setpoint_from_input(input);
|
||||
_setpoint.valid = true;
|
||||
_instance = instance;
|
||||
}
|
||||
}
|
||||
|
||||
bool ManualControlSelector::isInputValid(const manual_control_input_s &input, uint64_t now) const
|
||||
{
|
||||
// Check for timeout
|
||||
const bool sample_from_the_past = now >= input.timestamp_sample;
|
||||
const bool sample_newer_than_timeout = now - input.timestamp_sample < _timeout;
|
||||
|
||||
// Check if source matches the configuration
|
||||
const bool source_rc_matched = (_rc_in_mode == 0) && (input.data_source == manual_control_input_s::SOURCE_RC);
|
||||
const bool source_mavlink_matched = (_rc_in_mode == 1) &&
|
||||
(input.data_source == manual_control_input_s::SOURCE_MAVLINK_0
|
||||
@@ -66,20 +77,9 @@ void ManualControlSelector::update_manual_control_input(uint64_t now, const manu
|
||||
|| input.data_source == manual_control_input_s::SOURCE_MAVLINK_4
|
||||
|| input.data_source == manual_control_input_s::SOURCE_MAVLINK_5);
|
||||
const bool source_any_matched = (_rc_in_mode == 3);
|
||||
const bool not_overriding_existing_source = !_setpoint.valid
|
||||
|| (_setpoint.chosen_input.data_source == input.data_source);
|
||||
|
||||
if (source_rc_matched || source_mavlink_matched || source_any_matched) {
|
||||
if (not_overriding_existing_source) {
|
||||
_setpoint = setpoint_from_input(input);
|
||||
_setpoint.valid = true;
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
} else {
|
||||
_setpoint.valid = false;
|
||||
_instance = -1;
|
||||
}
|
||||
return sample_from_the_past && sample_newer_than_timeout
|
||||
&& (source_rc_matched || source_mavlink_matched || source_any_matched);
|
||||
}
|
||||
|
||||
manual_control_setpoint_s ManualControlSelector::setpoint_from_input(const manual_control_input_s &input)
|
||||
|
||||
@@ -51,6 +51,7 @@ public:
|
||||
int instance() const { return _instance; };
|
||||
|
||||
private:
|
||||
bool isInputValid(const manual_control_input_s &input, uint64_t now) const;
|
||||
static manual_control_setpoint_s setpoint_from_input(const manual_control_input_s &input);
|
||||
|
||||
manual_control_setpoint_s _setpoint{};
|
||||
|
||||
@@ -40,6 +40,30 @@ using namespace manual_control;
|
||||
|
||||
static constexpr uint64_t some_time = 12345678;
|
||||
|
||||
TEST(ManualControlSelector, RcInputContinuous)
|
||||
{
|
||||
ManualControlSelector selector;
|
||||
selector.set_rc_in_mode(0);
|
||||
selector.set_timeout(500_ms);
|
||||
|
||||
uint64_t timestamp = some_time;
|
||||
|
||||
// Now provide input with the correct source.
|
||||
manual_control_input_s input {};
|
||||
input.data_source = manual_control_input_s::SOURCE_RC;
|
||||
input.timestamp_sample = timestamp;
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
selector.update_manual_control_input(timestamp, input, 1);
|
||||
EXPECT_TRUE(selector.setpoint().valid);
|
||||
EXPECT_EQ(selector.setpoint().chosen_input.timestamp_sample, timestamp);
|
||||
EXPECT_EQ(selector.instance(), 1);
|
||||
EXPECT_TRUE(selector.setpoint().chosen_input.data_source == manual_control_input_s::SOURCE_RC);
|
||||
timestamp += 100_ms;
|
||||
input.timestamp_sample = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ManualControlSelector, RcInputOnly)
|
||||
{
|
||||
ManualControlSelector selector;
|
||||
|
||||
Reference in New Issue
Block a user