From d34142bc4e5ee7fc3cafd5c285d438fde9918009 Mon Sep 17 00:00:00 2001 From: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:23:12 +0100 Subject: [PATCH] refactor(rover): run each rover controller on its own trigger topic (#29630) * refactor(rover): run each rover controller on its own trigger topic The rover modules ran the whole controller cascade from the gyro callback, so the position and speed controllers ran several times per EKF output on stale estimates. Each controller is now a work item triggered by the topic it consumes (local position, attitude, angular velocity, steering setpoint), and the module only generates setpoints and enables the controllers the active control mode needs. All work items share the rate_ctrl queue, so enabling and resetting never overlaps a controller run. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): register controller callbacks top-down Position and speed control share the vehicle_local_position trigger and uORB schedules callbacks in registration order. Speed was registered first, so it always consumed the previous cycle's position-control setpoint, adding one EKF period of latency. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * docs(rover): move controller update descriptions back to their functions The init() and setEnabled() declarations were inserted between the update*Control() doc comments and their functions, leaving two stacked @brief blocks. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): widen the dt clamp of the EKF-triggered controllers Attitude and speed control now run on vehicle_attitude and vehicle_local_position instead of the gyro. Their period is the EKF2 output period, nominally exactly the old 10 ms upper bound, so jitter or a larger EKF2_PREDICT_US clipped dt and slowed the slew-rate limiters and PID integrators below real time. Allow up to 50 ms. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * docs(rover): correct the attitude controller dt comment vehicle_attitude is published on every EKF2 output predictor update at IMU_INTEG_RATE, not at the EKF2_PREDICT_US filter update rate. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): restart the controller dt when a controller is re-enabled A controller keeps its last run timestamp while it is disabled, so the first run after a mode switch computed dt over the whole disabled time and hit the upper clamp. With the speed and attitude clamps at 50 ms that step was six EKF periods long for the slew limiters and integrators. Restart the timestamp on enabling, so the first dt is the lower bound. SIH rover (10045), position -> manual -> position while driving, speed PID integral after the first re-enabled run: dt 50 ms before, 1 ms after (normal runs 8 ms). Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): pace the actuator controllers by the gyro The actuator controllers ran only when a steering setpoint arrived. A mode change resets the rate controller's setpoint to NaN, so the next cycle published no steering setpoint and therefore no actuator setpoint. SIH lockstep waits for an actuator output every step and froze there: switching to stabilized or offboard velocity stalled the simulation. Trigger the actuator controllers on vehicle_angular_velocity, registered after the rate controller, so they publish every cycle with the steering setpoint of the same cycle. SIH rover (10045), manual/acro/stab/position/offboard velocity cycle, 3 runs each: before all 3 runs froze (offboard switch in all, stab switch in one), after no lockstep timeouts, actuator outputs every cycle (max gap 8 ms) and >= 99.7 % of them use the steering setpoint of the same cycle. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * perf(rover): only wake the enabled controllers All controllers stayed registered on their trigger topics, so in every mode and while disarmed the disabled ones were scheduled at up to gyro rate just to return. Register a controller's callback when it gets enabled and unregister it when it gets disabled. A newly enabled controller runs once right away, because it registered after the trigger of the current cycle fired; without that the lockstep simulation misses the cycle's actuator output and stalls. When an upper controller is enabled while the lower one sharing its trigger topic keeps running (position/speed on vehicle_local_position, rate/actuator on vehicle_angular_velocity), the lower one is re-registered behind it to keep the cascade order. SIH rover (10045), 3 runs each: - work_queue status: disarmed only the module runs, manual adds the actuator controller, acro rate + actuator, stab + attitude, position all five, offboard velocity all but position; no lockstep timeouts. - Value-based order check (servo command vs steering setpoint, speed controller setpoint vs position controller setpoint): every consumer uses the setpoint of the same cycle after manual -> acro, manual -> offboard velocity and offboard velocity -> Go-To. With the re-registration disabled the actuator controller used the previous cycle's steering setpoint after acro / offboard, and the speed controller the previous cycle's speed setpoint in Go-To (153 of 153). - Re-enable probe: first dt after position -> manual -> position is still 1-2 ms. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): assign the rover control workitems to nav_and_controllers * fix(rover): retry a failed controller callback registration setEnabled() marked a controller enabled even when registering its trigger callback failed. The next call then saw no change and never retried, so the controller ran once from ScheduleNow() and then stayed idle with a single error. A controller now only counts as enabled once its callback is registered, so the next module cycle retries. A failed re-registration for the cascade order clears it the same way. The modules report a failure once instead of every cycle. SIH rover (10045), attitude controller registration forced to fail for the first 1500 attempts: the controller stays off while it fails and starts at 250 Hz on the first successful retry, one error per failure episode. Mode cycle without the fault unchanged. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * refactor(rover): share the controller trigger logic in a base class All 15 rover controllers carried the same copy of init(), setEnabled(), enabled(), reregisterTrigger() and the enabled check in Run(). Copies of the code that decides when a controller runs and in which order can drift apart, so RoverControllerWorkItem in lib/rover_control now owns the trigger subscription, the work queue and the enable state. A controller only implements its step and, if it has a time step, the restart on enabling. The trigger only wakes the controller, so the controllers read their inputs through plain subscriptions again. The actuator controllers no longer drain the gyro topic: callbacks are scheduled on new publications, not on unread data. A functional test covers the contract the modules rely on: no runs while disabled, one run right away on enabling, a run on every trigger, registration order for controllers sharing a trigger and re-registering to restore the cascade order. SIH rover (10045), manual/acro/stab/position cycle: each mode wakes the same controllers at the same rates as before, actuator setpoints at 250 Hz, no errors. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche * fix(rover): drop the protected destructor of the controller base class clang-tidy (cppcoreguidelines-virtual-class-destructor) rejects a destructor that is both protected and virtual. WorkItem already has a public virtual destructor, so the implicit one is enough. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Michael Fritsche --------- Signed-off-by: Michael Fritsche --- src/lib/rover_control/CMakeLists.txt | 3 + .../rover_control/RoverControllerWorkItem.cpp | 83 +++++++ .../rover_control/RoverControllerWorkItem.hpp | 112 +++++++++ .../RoverControllerWorkItemTest.cpp | 215 ++++++++++++++++++ .../AckermannActControl.cpp | 6 +- .../AckermannActControl.hpp | 6 +- .../AckermannAttControl.cpp | 7 +- .../AckermannAttControl.hpp | 6 +- .../AckermannPosControl.cpp | 4 +- .../AckermannPosControl.hpp | 5 +- .../AckermannRateControl.cpp | 4 +- .../AckermannRateControl.hpp | 6 +- .../AckermannSpeedControl.cpp | 7 +- .../AckermannSpeedControl.hpp | 6 +- .../rover_ackermann/RoverAckermann.cpp | 54 +++-- .../rover_ackermann/RoverAckermann.hpp | 10 +- .../DifferentialActControl.cpp | 6 +- .../DifferentialActControl.hpp | 7 +- .../DifferentialAttControl.cpp | 7 +- .../DifferentialAttControl.hpp | 6 +- .../DifferentialPosControl.cpp | 4 +- .../DifferentialPosControl.hpp | 5 +- .../DifferentialRateControl.cpp | 4 +- .../DifferentialRateControl.hpp | 5 +- .../DifferentialSpeedControl.cpp | 7 +- .../DifferentialSpeedControl.hpp | 6 +- .../rover_differential/RoverDifferential.cpp | 54 +++-- .../rover_differential/RoverDifferential.hpp | 10 +- .../MecanumActControl/MecanumActControl.cpp | 6 +- .../MecanumActControl/MecanumActControl.hpp | 7 +- .../MecanumAttControl/MecanumAttControl.cpp | 7 +- .../MecanumAttControl/MecanumAttControl.hpp | 5 +- .../MecanumPosControl/MecanumPosControl.cpp | 4 +- .../MecanumPosControl/MecanumPosControl.hpp | 5 +- .../MecanumRateControl/MecanumRateControl.cpp | 4 +- .../MecanumRateControl/MecanumRateControl.hpp | 5 +- .../MecanumSpeedControl.cpp | 7 +- .../MecanumSpeedControl.hpp | 6 +- src/modules/rover_mecanum/RoverMecanum.cpp | 54 +++-- src/modules/rover_mecanum/RoverMecanum.hpp | 10 +- 40 files changed, 679 insertions(+), 96 deletions(-) create mode 100644 src/lib/rover_control/RoverControllerWorkItem.cpp create mode 100644 src/lib/rover_control/RoverControllerWorkItem.hpp create mode 100644 src/lib/rover_control/RoverControllerWorkItemTest.cpp diff --git a/src/lib/rover_control/CMakeLists.txt b/src/lib/rover_control/CMakeLists.txt index ddda959b080..a2246c81f40 100644 --- a/src/lib/rover_control/CMakeLists.txt +++ b/src/lib/rover_control/CMakeLists.txt @@ -34,6 +34,8 @@ px4_add_library(rover_control RoverControl.cpp RoverControl.hpp + RoverControllerWorkItem.cpp + RoverControllerWorkItem.hpp ) # The library is only linked by the rover modules, so its parameters are only needed with them if(CONFIG_MODULES_ROVER_ACKERMANN OR CONFIG_MODULES_ROVER_DIFFERENTIAL OR CONFIG_MODULES_ROVER_MECANUM OR BUILD_TESTING) @@ -43,3 +45,4 @@ endif() target_link_libraries(rover_control PUBLIC PID) target_link_libraries(rover_control PUBLIC geo) px4_add_unit_gtest(SRC RoverControlTest.cpp LINKLIBS rover_control) +px4_add_functional_gtest(SRC RoverControllerWorkItemTest.cpp LINKLIBS rover_control) diff --git a/src/lib/rover_control/RoverControllerWorkItem.cpp b/src/lib/rover_control/RoverControllerWorkItem.cpp new file mode 100644 index 00000000000..6b20861f204 --- /dev/null +++ b/src/lib/rover_control/RoverControllerWorkItem.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "RoverControllerWorkItem.hpp" + +RoverControllerWorkItem::RoverControllerWorkItem(const char *name, const orb_metadata *trigger) : + WorkItem(name, px4::wq_configurations::nav_and_controllers), + _trigger_sub{this, trigger} +{ +} + +bool RoverControllerWorkItem::init() +{ + const bool registered = _trigger_sub.registerCallback(); + _trigger_sub.unregisterCallback(); + return registered; +} + +bool RoverControllerWorkItem::setEnabled(bool enabled) +{ + if (enabled && !_enabled) { + _enabled = _trigger_sub.registerCallback(); + + if (_enabled) { + onEnable(); + ScheduleNow(); + } + + } else if (!enabled && _enabled) { + _trigger_sub.unregisterCallback(); + _enabled = false; + } + + return _enabled == enabled; +} + +bool RoverControllerWorkItem::reregisterTrigger() +{ + if (!_enabled) { + return true; // Not registered, setEnabled() registers it at the end of the list + } + + _trigger_sub.unregisterCallback(); + _enabled = _trigger_sub.registerCallback(); + return _enabled; +} + +void RoverControllerWorkItem::Run() +{ + if (_enabled) { + runController(); + } +} diff --git a/src/lib/rover_control/RoverControllerWorkItem.hpp b/src/lib/rover_control/RoverControllerWorkItem.hpp new file mode 100644 index 00000000000..613a30ba5c2 --- /dev/null +++ b/src/lib/rover_control/RoverControllerWorkItem.hpp @@ -0,0 +1,112 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file RoverControllerWorkItem.hpp + * + * Work item for a rover controller that runs on the topic it consumes, only while it is enabled. + */ + +#pragma once + +#include +#include + +/** + * @brief Base class for a rover controller that runs on its own trigger topic. + * + * Only an enabled controller is woken by its trigger topic. All rover controllers and their module run on the same + * work queue, so enabling, disabling and resetting a controller from the module never overlaps with a controller run. + * + * The trigger subscription only wakes the controller, it is never read. Derived classes read their inputs through their + * own subscriptions. Destroy a controller only from its work queue thread or while it is disabled, because the trigger + * callback is unregistered after the derived class is already destroyed. + */ +class RoverControllerWorkItem : public px4::WorkItem +{ +public: + /** + * @brief Check that the trigger callback can be registered. + * + * Registering once also creates the trigger topic, so setEnabled() can register it later without failing. + * + * @return true if the trigger callback could be registered + */ + bool init(); + + /** + * @brief Enable or disable the controller. + * + * On enabling it also runs once right away, so it does not miss the trigger that was published before it registered. + * It only counts as enabled once the trigger callback is registered, otherwise the next call retries. + * + * @param enabled true to run the controller on its trigger topic + * @return true if the operation succeeded + */ + bool setEnabled(bool enabled); + + bool enabled() const { return _enabled; } + + /** + * @brief Move the trigger callback to the end of the callback list. + * + * Callbacks sharing a topic run in registration order, so this keeps a lower controller of the cascade behind an + * upper controller that was enabled after it. On failure the controller is disabled and the next setEnabled() retries. + * + * @return true if the trigger callback could be registered again + */ + bool reregisterTrigger(); + +protected: + /** + * @param name Work item name, shown by work_queue status + * @param trigger Topic that triggers the controller + */ + RoverControllerWorkItem(const char *name, const orb_metadata *trigger); + + /** + * @brief Run one controller step, called on every trigger while enabled. + */ + virtual void runController() = 0; + + /** + * @brief Called when the controller gets enabled, e.g. to restart the time step so it does not span the time spent disabled. + */ + virtual void onEnable() {} + +private: + void Run() final; + + uORB::SubscriptionCallbackWorkItem _trigger_sub; + bool _enabled{false}; +}; diff --git a/src/lib/rover_control/RoverControllerWorkItemTest.cpp b/src/lib/rover_control/RoverControllerWorkItemTest.cpp new file mode 100644 index 00000000000..ff5dbeb8913 --- /dev/null +++ b/src/lib/rover_control/RoverControllerWorkItemTest.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * Functional test for RoverControllerWorkItem: the contract the rover modules rely on to run each controller on its own + * trigger topic, only while enabled, and in cascade order when controllers share a trigger topic. + */ + +#include + +#include "RoverControllerWorkItem.hpp" + +#include +#include +#include +#include +#include +#include +#include + +using namespace time_literals; + +namespace +{ + +// Stop the work queue manager at the end so the process exits instead of timing out +class RoverControllerWorkItemTestEnvironment : public ::testing::Environment +{ +public: + void TearDown() override { px4::WorkQueueManagerStop(); } +}; + +const auto *global_env = ::testing::AddGlobalTestEnvironment(new RoverControllerWorkItemTestEnvironment()); + +// Global run sequence, to check the order in which controllers sharing a trigger run +std::atomic run_sequence{0}; + +class TestController : public RoverControllerWorkItem +{ +public: + TestController(const char *name) : RoverControllerWorkItem(name, ORB_ID(vehicle_angular_velocity)) {} + + std::atomic runs{0}; + std::atomic enables{0}; + std::atomic last_run_sequence{0}; + +private: + void runController() override + { + last_run_sequence = ++run_sequence; + ++runs; + } + + void onEnable() override { ++enables; } +}; + +class RoverControllerWorkItemTest : public ::testing::Test +{ +public: + void SetUp() override + { + static bool wq_manager_started = false; + + if (!wq_manager_started) { + // The gtest harness starts no platform threads, the hrt callout worker must exist before the work queues run + hrt_init(); + hrt_work_queue_init(); + ASSERT_EQ(px4::WorkQueueManagerStart(), 0); + wq_manager_started = true; + } + + const hrt_abstime wq_wait_start = hrt_absolute_time(); + + while (px4::WorkQueueFindOrCreate(px4::wq_configurations::nav_and_controllers) == nullptr) { + ASSERT_LT(hrt_elapsed_time(&wq_wait_start), 10_s) << "work queue manager did not start"; + px4_usleep(10_ms); + } + + // Keep the controllers alive for the whole process: a work queue is destroyed when its last item detaches, + // which would race the next test's construction against the dying queue + static TestController *upper = new TestController("test_upper_control"); + static TestController *lower = new TestController("test_lower_control"); + _upper = upper; + _lower = lower; + _upper->setEnabled(false); + _lower->setEnabled(false); + settle(); + } + +protected: + void publishTrigger() + { + vehicle_angular_velocity_s angular_velocity{}; + angular_velocity.timestamp = hrt_absolute_time(); + _trigger_pub.publish(angular_velocity); + settle(); + } + + // Let the work queue run everything that is scheduled + static void settle() { px4_usleep(20_ms); } + + TestController *_upper{nullptr}; + TestController *_lower{nullptr}; + uORB::Publication _trigger_pub{ORB_ID(vehicle_angular_velocity)}; +}; + +} // namespace + +TEST_F(RoverControllerWorkItemTest, InitRegistersAndReleasesTrigger) +{ + EXPECT_TRUE(_upper->init()); + EXPECT_FALSE(_upper->enabled()); + + const int runs = _upper->runs; + publishTrigger(); + EXPECT_EQ(_upper->runs, runs); // init() must not leave the callback registered +} + +TEST_F(RoverControllerWorkItemTest, DisabledControllerIsNotWoken) +{ + const int runs = _upper->runs; + publishTrigger(); + publishTrigger(); + EXPECT_EQ(_upper->runs, runs); +} + +TEST_F(RoverControllerWorkItemTest, EnablingRunsOnceAndThenOnEveryTrigger) +{ + const int runs = _upper->runs; + const int enables = _upper->enables; + + EXPECT_TRUE(_upper->setEnabled(true)); + EXPECT_TRUE(_upper->enabled()); + settle(); + EXPECT_EQ(_upper->enables, enables + 1); + EXPECT_EQ(_upper->runs, runs + 1); // runs right away, without waiting for the next trigger + + // Staying enabled neither calls onEnable() again nor schedules an extra run + EXPECT_TRUE(_upper->setEnabled(true)); + settle(); + EXPECT_EQ(_upper->enables, enables + 1); + EXPECT_EQ(_upper->runs, runs + 1); + + publishTrigger(); + publishTrigger(); + EXPECT_EQ(_upper->runs, runs + 3); +} + +TEST_F(RoverControllerWorkItemTest, DisablingStopsTheController) +{ + EXPECT_TRUE(_upper->setEnabled(true)); + settle(); + EXPECT_TRUE(_upper->setEnabled(false)); + EXPECT_FALSE(_upper->enabled()); + + const int runs = _upper->runs; + publishTrigger(); + EXPECT_EQ(_upper->runs, runs); +} + +TEST_F(RoverControllerWorkItemTest, ControllersSharingTheTriggerRunInRegistrationOrder) +{ + // The lower controller is enabled first, so it runs first + EXPECT_TRUE(_lower->setEnabled(true)); + EXPECT_TRUE(_upper->setEnabled(true)); + settle(); + publishTrigger(); + EXPECT_LT(_lower->last_run_sequence, _upper->last_run_sequence); + + // Re-registering the lower controller moves it behind the upper one, as the modules do to keep the cascade order + EXPECT_TRUE(_lower->reregisterTrigger()); + EXPECT_TRUE(_lower->enabled()); + publishTrigger(); + EXPECT_LT(_upper->last_run_sequence, _lower->last_run_sequence); +} + +TEST_F(RoverControllerWorkItemTest, ReregisteringADisabledControllerKeepsItDisabled) +{ + EXPECT_TRUE(_lower->reregisterTrigger()); + EXPECT_FALSE(_lower->enabled()); + + const int runs = _lower->runs; + publishTrigger(); + EXPECT_EQ(_lower->runs, runs); +} diff --git a/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.cpp b/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.cpp index 8288c77d43b..64f67c0551c 100644 --- a/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.cpp +++ b/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.cpp @@ -35,8 +35,12 @@ using namespace time_literals; -AckermannActControl::AckermannActControl(ModuleParams *parent) : ModuleParams(parent) +AckermannActControl::AckermannActControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_act_control", ORB_ID(vehicle_angular_velocity)) { + // Paced by the gyro like the rate controller, so actuator setpoints are published every cycle, + // also when no new steering setpoint arrives (e.g. the cycle after a mode change resets the rate controller) updateParams(); } diff --git a/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.hpp b/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.hpp index 19a0671d21f..176359a0876 100644 --- a/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.hpp +++ b/src/modules/rover_ackermann/AckermannActControl/AckermannActControl.hpp @@ -38,6 +38,7 @@ // Libraries #include +#include #include #include @@ -45,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -52,7 +54,7 @@ /** * @brief Class for ackermann actuator control. */ -class AckermannActControl : public ModuleParams +class AckermannActControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -79,6 +81,8 @@ protected: void updateParams() override; private: + void runController() override { updateActControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled // uORB subscriptions uORB::Subscription _actuator_servos_sub{ORB_ID(actuator_servos)}; diff --git a/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.cpp b/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.cpp index 2e85e5f6c7f..7299ed82319 100644 --- a/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.cpp +++ b/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -AckermannAttControl::AckermannAttControl(ModuleParams *parent) : ModuleParams(parent) +AckermannAttControl::AckermannAttControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_att_control", ORB_ID(vehicle_attitude)) { _surface_vehicle_rate_setpoint_pub.advertise(); _surface_vehicle_attitude_status_pub.advertise(); @@ -65,7 +67,8 @@ void AckermannAttControl::updateAttControl() hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs on every vehicle_attitude publication (EKF2 output predictor at IMU_INTEG_RATE >= 100 Hz), allow for jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; if (PX4_ISFINITE(_yaw_setpoint)) { // Calculate yaw rate limit for slew rate diff --git a/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.hpp b/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.hpp index d9e1837eb8c..24667054310 100644 --- a/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.hpp +++ b/src/modules/rover_ackermann/AckermannAttControl/AckermannAttControl.hpp @@ -38,6 +38,7 @@ // Library includes #include +#include #include #include #include @@ -55,7 +56,7 @@ /** * @brief Class for ackermann attitude control. */ -class AckermannAttControl : public ModuleParams +class AckermannAttControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -88,6 +89,9 @@ protected: void updateParams() override; private: + void runController() override { updateAttControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Update uORB subscriptions used in attitude controller. */ diff --git a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp index e19a9c6c0cc..05046c65496 100644 --- a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp +++ b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -AckermannPosControl::AckermannPosControl(ModuleParams *parent) : ModuleParams(parent) +AckermannPosControl::AckermannPosControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_pos_control", ORB_ID(vehicle_local_position)) { _pure_pursuit_status_pub.advertise(); _surface_vehicle_speed_setpoint_pub.advertise(); diff --git a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp index 7dbd8d28635..44e8ff17360 100644 --- a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp +++ b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp @@ -41,6 +41,7 @@ #include #include #include +#include #include // uORB includes @@ -60,7 +61,7 @@ using namespace matrix; /** * @brief Class for ackermann position control. */ -class AckermannPosControl : public ModuleParams +class AckermannPosControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -93,6 +94,8 @@ protected: void updateParams() override; private: + void runController() override { updatePosControl(); } + /** * @brief Update uORB subscriptions used in position controller. */ diff --git a/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.cpp b/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.cpp index 269040c0b00..1a602cba9c4 100644 --- a/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.cpp +++ b/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -AckermannRateControl::AckermannRateControl(ModuleParams *parent) : ModuleParams(parent) +AckermannRateControl::AckermannRateControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_rate_control", ORB_ID(vehicle_angular_velocity)) { _surface_vehicle_steering_setpoint_pub.advertise(); _surface_vehicle_rate_status_pub.advertise(); diff --git a/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.hpp b/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.hpp index d18df414e66..7036bbf0b30 100644 --- a/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.hpp +++ b/src/modules/rover_ackermann/AckermannRateControl/AckermannRateControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -55,7 +56,7 @@ /** * @brief Class for ackermann rate control. */ -class AckermannRateControl : public ModuleParams +class AckermannRateControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -88,6 +89,9 @@ protected: void updateParams() override; private: + void runController() override { updateRateControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Update uORB subscriptions used in rate controller. */ diff --git a/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.cpp b/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.cpp index d770ccf05d7..a4d75badc17 100644 --- a/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.cpp +++ b/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -AckermannSpeedControl::AckermannSpeedControl(ModuleParams *parent) : ModuleParams(parent) +AckermannSpeedControl::AckermannSpeedControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_speed_control", ORB_ID(vehicle_local_position)) { _rover_throttle_setpoint_pub.advertise(); _surface_vehicle_speed_status_pub.advertise(); @@ -63,7 +65,8 @@ void AckermannSpeedControl::updateSpeedControl() const hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs at the EKF2 output rate (EKF2_PREDICT_US up to 20 ms), allow for that plus jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; // Throttle Setpoint if (PX4_ISFINITE(_speed_setpoint)) { diff --git a/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.hpp b/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.hpp index f742b61a073..d22da7eddcd 100644 --- a/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.hpp +++ b/src/modules/rover_ackermann/AckermannSpeedControl/AckermannSpeedControl.hpp @@ -39,6 +39,7 @@ // Library includes #include +#include #include #include #include @@ -58,7 +59,7 @@ using namespace matrix; /** * @brief Class for ackermann speed control. */ -class AckermannSpeedControl : public ModuleParams +class AckermannSpeedControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -91,6 +92,9 @@ protected: void updateParams() override; private: + void runController() override { updateSpeedControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Update uORB subscriptions used in speed controller. */ diff --git a/src/modules/rover_ackermann/RoverAckermann.cpp b/src/modules/rover_ackermann/RoverAckermann.cpp index 8c50dc47b6a..841f418a1af 100644 --- a/src/modules/rover_ackermann/RoverAckermann.cpp +++ b/src/modules/rover_ackermann/RoverAckermann.cpp @@ -39,14 +39,21 @@ ModuleBase::Descriptor RoverAckermann::desc{task_spawn, custom_command, print_us RoverAckermann::RoverAckermann() : ModuleParams(nullptr), - ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl) + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers) { updateParams(); } bool RoverAckermann::init() { - if (!_vehicle_angular_velocity_sub.registerCallback()) { + // The module registers its gyro callback first and keeps it, so the setpoints it generates reach the controllers in the same cycle. + // The controllers only check their trigger here, they register it while enabled, see enableControllers(). + if (!_vehicle_angular_velocity_sub.registerCallback() + || !_ackermann_pos_control.init() + || !_ackermann_speed_control.init() + || !_ackermann_att_control.init() + || !_ackermann_rate_control.init() + || !_ackermann_act_control.init()) { PX4_ERR("callback registration failed"); return false; } @@ -67,7 +74,7 @@ void RoverAckermann::Run() return; } - // Drain the trigger. The gyro paces the loop; its value is not used here. + // Drain the trigger. The gyro paces the setpoint generation; its value is not used here. vehicle_angular_velocity_s angular_velocity; _vehicle_angular_velocity_sub.update(&angular_velocity); @@ -100,14 +107,16 @@ void RoverAckermann::Run() if (_vehicle_control_mode.flag_armed && _sanity_checks_passed) { _was_armed = true; generateSetpoints(); - updateControllers(); + enableControllers(true); } else if (_was_armed) { // Reset all controllers and stop the vehicle + enableControllers(false); reset(); _ackermann_act_control.stopVehicle(); _was_armed = false; } else { + enableControllers(false); _ackermann_act_control.stopVehicle(); } @@ -180,27 +189,36 @@ void RoverAckermann::generateSetpoints() } -void RoverAckermann::updateControllers() +void RoverAckermann::enableControllers(const bool enabled) { - if (_vehicle_control_mode.flag_control_position_enabled) { - _ackermann_pos_control.updatePosControl(); + const bool pos_was_enabled = _ackermann_pos_control.enabled(); + const bool speed_was_enabled = _ackermann_speed_control.enabled(); + const bool rate_was_enabled = _ackermann_rate_control.enabled(); + const bool act_was_enabled = _ackermann_act_control.enabled(); + + // Enable top-down, so newly enabled controllers run in cascade order in this cycle + bool registered = _ackermann_pos_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled); + registered = _ackermann_speed_control.setEnabled(enabled && _vehicle_control_mode.flag_control_velocity_enabled) && registered; + registered = _ackermann_att_control.setEnabled(enabled && _vehicle_control_mode.flag_control_attitude_enabled) && registered; + registered = _ackermann_rate_control.setEnabled(enabled && _vehicle_control_mode.flag_control_rates_enabled) && registered; + registered = _ackermann_act_control.setEnabled(enabled && _vehicle_control_mode.flag_control_allocation_enabled) && registered; + + // Callbacks sharing a trigger topic run in registration order: keep the position controller ahead of the speed + // controller (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity) + if (!pos_was_enabled && _ackermann_pos_control.enabled() && speed_was_enabled && _ackermann_speed_control.enabled()) { + registered = _ackermann_speed_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_velocity_enabled) { - _ackermann_speed_control.updateSpeedControl(); + if (!rate_was_enabled && _ackermann_rate_control.enabled() && act_was_enabled && _ackermann_act_control.enabled()) { + registered = _ackermann_act_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_attitude_enabled) { - _ackermann_att_control.updateAttControl(); + // Report a failure once, the controllers retry on every cycle + if (!registered && !_controller_registration_failed) { + PX4_ERR("controller callback registration failed, retrying"); } - if (_vehicle_control_mode.flag_control_rates_enabled) { - _ackermann_rate_control.updateRateControl(); - } - - if (_vehicle_control_mode.flag_control_allocation_enabled) { - _ackermann_act_control.updateActControl(); - } + _controller_registration_failed = !registered; } void RoverAckermann::runSanityChecks() diff --git a/src/modules/rover_ackermann/RoverAckermann.hpp b/src/modules/rover_ackermann/RoverAckermann.hpp index d6cc7d81f96..dca932f0e96 100644 --- a/src/modules/rover_ackermann/RoverAckermann.hpp +++ b/src/modules/rover_ackermann/RoverAckermann.hpp @@ -101,9 +101,14 @@ private: void generateSetpoints(); /** - * @brief Update the active controllers. + * @brief Enable the controllers the active control mode needs. + * + * Each controller runs on its own trigger topic, see the controller classes, and only the enabled ones are woken by it. + * They share this module's work queue, so enabling and resetting them here never overlaps with their Run(). + * + * @param enabled false disables all controllers */ - void updateControllers(); + void enableControllers(bool enabled); /** * @brief Check proper parameter setup for the controllers @@ -141,4 +146,5 @@ private: bool _sanity_checks_passed{true}; // True if checks for all active controllers pass uint8_t _nav_state{vehicle_status_s::NAVIGATION_STATE_MAX}; // Active mode, to detect mode changes bool _was_armed{false}; // True if the vehicle was armed before the last reset + bool _controller_registration_failed{false}; // True while a controller trigger callback is not registered }; diff --git a/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.cpp b/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.cpp index 379a227df55..c489e925132 100644 --- a/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.cpp +++ b/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.cpp @@ -35,8 +35,12 @@ using namespace time_literals; -DifferentialActControl::DifferentialActControl(ModuleParams *parent) : ModuleParams(parent) +DifferentialActControl::DifferentialActControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_act_control", ORB_ID(vehicle_angular_velocity)) { + // Paced by the gyro like the rate controller, so actuator setpoints are published every cycle, + // also when no new steering setpoint arrives (e.g. the cycle after a mode change resets the rate controller) updateParams(); } diff --git a/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.hpp b/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.hpp index 201491f34a8..2e706821ac9 100644 --- a/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.hpp +++ b/src/modules/rover_differential/DifferentialActControl/DifferentialActControl.hpp @@ -38,6 +38,7 @@ // Libraries #include +#include #include #include @@ -45,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -52,7 +54,7 @@ /** * @brief Class for differential actuator control. */ -class DifferentialActControl : public ModuleParams +class DifferentialActControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -79,6 +81,9 @@ protected: void updateParams() override; private: + void runController() override { updateActControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Compute normalized motor commands based on normalized setpoints. * @param throttle Normalized speed in body x direction [-1, 1]. diff --git a/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.cpp b/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.cpp index 332c7bdd071..185fb94345c 100644 --- a/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.cpp +++ b/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -DifferentialAttControl::DifferentialAttControl(ModuleParams *parent) : ModuleParams(parent) +DifferentialAttControl::DifferentialAttControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_att_control", ORB_ID(vehicle_attitude)) { _surface_vehicle_rate_setpoint_pub.advertise(); _surface_vehicle_attitude_status_pub.advertise(); @@ -63,7 +65,8 @@ void DifferentialAttControl::updateAttControl() { hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs on every vehicle_attitude publication (EKF2 output predictor at IMU_INTEG_RATE >= 100 Hz), allow for jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; if (_vehicle_attitude_sub.updated()) { vehicle_attitude_s vehicle_attitude{}; diff --git a/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.hpp b/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.hpp index e6401d4d0a5..a48483070f4 100644 --- a/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.hpp +++ b/src/modules/rover_differential/DifferentialAttControl/DifferentialAttControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -55,7 +56,7 @@ /** * @brief Class for differential attitude control. */ -class DifferentialAttControl : public ModuleParams +class DifferentialAttControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -88,6 +89,9 @@ protected: void updateParams() override; private: + void runController() override { updateAttControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + // uORB subscriptions uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; uORB::Subscription _surface_vehicle_attitude_setpoint_sub{ORB_ID(surface_vehicle_attitude_setpoint)}; diff --git a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp index 31aa8ca7fca..160edf8d8a1 100644 --- a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp +++ b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -DifferentialPosControl::DifferentialPosControl(ModuleParams *parent) : ModuleParams(parent) +DifferentialPosControl::DifferentialPosControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_pos_control", ORB_ID(vehicle_local_position)) { _pure_pursuit_status_pub.advertise(); _surface_vehicle_speed_setpoint_pub.advertise(); diff --git a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp index e0d13104f82..e2088ca3cc2 100644 --- a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp +++ b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp @@ -41,6 +41,7 @@ #include #include #include +#include #include // uORB includes @@ -67,7 +68,7 @@ enum class DrivingState { /** * @brief Class for differential position control. */ -class DifferentialPosControl : public ModuleParams +class DifferentialPosControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -100,6 +101,8 @@ protected: void updateParams() override; private: + void runController() override { updatePosControl(); } + /** * @brief Update uORB subscriptions used in position controller. */ diff --git a/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.cpp b/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.cpp index ea2a59b427c..25bcc6193a3 100644 --- a/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.cpp +++ b/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -DifferentialRateControl::DifferentialRateControl(ModuleParams *parent) : ModuleParams(parent) +DifferentialRateControl::DifferentialRateControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_rate_control", ORB_ID(vehicle_angular_velocity)) { _surface_vehicle_steering_setpoint_pub.advertise(); _surface_vehicle_rate_status_pub.advertise(); diff --git a/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.hpp b/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.hpp index 6a1c7f6dbd9..2066d8f9d7a 100644 --- a/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.hpp +++ b/src/modules/rover_differential/DifferentialRateControl/DifferentialRateControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -54,7 +55,7 @@ /** * @brief Class for differential rate control. */ -class DifferentialRateControl : public ModuleParams +class DifferentialRateControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -87,6 +88,8 @@ protected: void updateParams() override; private: + void runController() override { updateRateControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled // uORB subscriptions uORB::Subscription _surface_vehicle_rate_setpoint_sub{ORB_ID(surface_vehicle_rate_setpoint)}; diff --git a/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.cpp b/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.cpp index 650c7620036..bafb14476e9 100644 --- a/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.cpp +++ b/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -DifferentialSpeedControl::DifferentialSpeedControl(ModuleParams *parent) : ModuleParams(parent) +DifferentialSpeedControl::DifferentialSpeedControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_speed_control", ORB_ID(vehicle_local_position)) { _rover_throttle_setpoint_pub.advertise(); _surface_vehicle_speed_status_pub.advertise(); @@ -63,7 +65,8 @@ void DifferentialSpeedControl::updateSpeedControl() const hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs at the EKF2 output rate (EKF2_PREDICT_US up to 20 ms), allow for that plus jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; // Throttle Setpoint if (PX4_ISFINITE(_speed_setpoint)) { diff --git a/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.hpp b/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.hpp index 74757448ba9..a3e701825ed 100644 --- a/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.hpp +++ b/src/modules/rover_differential/DifferentialSpeedControl/DifferentialSpeedControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -59,7 +60,7 @@ using namespace matrix; /** * @brief Class for differential speed control. */ -class DifferentialSpeedControl : public ModuleParams +class DifferentialSpeedControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -92,6 +93,9 @@ protected: void updateParams() override; private: + void runController() override { updateSpeedControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Update uORB subscriptions used in speed controller. */ diff --git a/src/modules/rover_differential/RoverDifferential.cpp b/src/modules/rover_differential/RoverDifferential.cpp index 106e4e12c7e..f58d3733057 100644 --- a/src/modules/rover_differential/RoverDifferential.cpp +++ b/src/modules/rover_differential/RoverDifferential.cpp @@ -39,14 +39,21 @@ ModuleBase::Descriptor RoverDifferential::desc{task_spawn, custom_command, print RoverDifferential::RoverDifferential() : ModuleParams(nullptr), - ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl) + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers) { updateParams(); } bool RoverDifferential::init() { - if (!_vehicle_angular_velocity_sub.registerCallback()) { + // The module registers its gyro callback first and keeps it, so the setpoints it generates reach the controllers in the same cycle. + // The controllers only check their trigger here, they register it while enabled, see enableControllers(). + if (!_vehicle_angular_velocity_sub.registerCallback() + || !_differential_pos_control.init() + || !_differential_speed_control.init() + || !_differential_att_control.init() + || !_differential_rate_control.init() + || !_differential_act_control.init()) { PX4_ERR("callback registration failed"); return false; } @@ -67,7 +74,7 @@ void RoverDifferential::Run() return; } - // Drain the trigger. The gyro paces the loop; its value is not used here. + // Drain the trigger. The gyro paces the setpoint generation; its value is not used here. vehicle_angular_velocity_s angular_velocity; _vehicle_angular_velocity_sub.update(&angular_velocity); @@ -101,14 +108,16 @@ void RoverDifferential::Run() _was_armed = true; generateSetpoints(); - updateControllers(); + enableControllers(true); } else if (_was_armed) { // Reset all controllers and stop the vehicle + enableControllers(false); reset(); _differential_act_control.stopVehicle(); _was_armed = false; } else { + enableControllers(false); _differential_act_control.stopVehicle(); } @@ -181,27 +190,36 @@ void RoverDifferential::generateSetpoints() } -void RoverDifferential::updateControllers() +void RoverDifferential::enableControllers(const bool enabled) { - if (_vehicle_control_mode.flag_control_position_enabled) { - _differential_pos_control.updatePosControl(); + const bool pos_was_enabled = _differential_pos_control.enabled(); + const bool speed_was_enabled = _differential_speed_control.enabled(); + const bool rate_was_enabled = _differential_rate_control.enabled(); + const bool act_was_enabled = _differential_act_control.enabled(); + + // Enable top-down, so newly enabled controllers run in cascade order in this cycle + bool registered = _differential_pos_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled); + registered = _differential_speed_control.setEnabled(enabled && _vehicle_control_mode.flag_control_velocity_enabled) && registered; + registered = _differential_att_control.setEnabled(enabled && _vehicle_control_mode.flag_control_attitude_enabled) && registered; + registered = _differential_rate_control.setEnabled(enabled && _vehicle_control_mode.flag_control_rates_enabled) && registered; + registered = _differential_act_control.setEnabled(enabled && _vehicle_control_mode.flag_control_allocation_enabled) && registered; + + // Callbacks sharing a trigger topic run in registration order: keep the position controller ahead of the speed + // controller (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity) + if (!pos_was_enabled && _differential_pos_control.enabled() && speed_was_enabled && _differential_speed_control.enabled()) { + registered = _differential_speed_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_attitude_enabled) { - _differential_att_control.updateAttControl(); + if (!rate_was_enabled && _differential_rate_control.enabled() && act_was_enabled && _differential_act_control.enabled()) { + registered = _differential_act_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_rates_enabled) { - _differential_rate_control.updateRateControl(); + // Report a failure once, the controllers retry on every cycle + if (!registered && !_controller_registration_failed) { + PX4_ERR("controller callback registration failed, retrying"); } - if (_vehicle_control_mode.flag_control_velocity_enabled) { - _differential_speed_control.updateSpeedControl(); - } - - if (_vehicle_control_mode.flag_control_allocation_enabled) { - _differential_act_control.updateActControl(); - } + _controller_registration_failed = !registered; } void RoverDifferential::runSanityChecks() diff --git a/src/modules/rover_differential/RoverDifferential.hpp b/src/modules/rover_differential/RoverDifferential.hpp index 4bde6f6e579..683c86dd701 100644 --- a/src/modules/rover_differential/RoverDifferential.hpp +++ b/src/modules/rover_differential/RoverDifferential.hpp @@ -100,9 +100,14 @@ private: void generateSetpoints(); /** - * @brief Update the controllers + * @brief Enable the controllers the active control mode needs. + * + * Each controller runs on its own trigger topic, see the controller classes, and only the enabled ones are woken by it. + * They share this module's work queue, so enabling and resetting them here never overlaps with their Run(). + * + * @param enabled false disables all controllers */ - void updateControllers(); + void enableControllers(bool enabled); /** * @brief Check proper parameter setup for the controllers @@ -140,4 +145,5 @@ private: bool _sanity_checks_passed{true}; // True if checks for all active controllers pass uint8_t _nav_state{vehicle_status_s::NAVIGATION_STATE_MAX}; // Active mode, to detect mode changes bool _was_armed{false}; // True if the vehicle was armed before the last reset + bool _controller_registration_failed{false}; // True while a controller trigger callback is not registered }; diff --git a/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.cpp b/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.cpp index e3dab6892af..089b5c734f1 100644 --- a/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.cpp +++ b/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.cpp @@ -35,8 +35,12 @@ using namespace time_literals; -MecanumActControl::MecanumActControl(ModuleParams *parent) : ModuleParams(parent) +MecanumActControl::MecanumActControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_act_control", ORB_ID(vehicle_angular_velocity)) { + // Paced by the gyro like the rate controller, so actuator setpoints are published every cycle, + // also when no new steering setpoint arrives (e.g. the cycle after a mode change resets the rate controller) updateParams(); } diff --git a/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.hpp b/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.hpp index ed6c5226cec..a427dc91f46 100644 --- a/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.hpp +++ b/src/modules/rover_mecanum/MecanumActControl/MecanumActControl.hpp @@ -38,6 +38,7 @@ // Libraries #include +#include #include #include @@ -45,13 +46,14 @@ #include #include #include +#include #include #include /** * @brief Class for mecanum actuator control. */ -class MecanumActControl : public ModuleParams +class MecanumActControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -78,6 +80,9 @@ protected: void updateParams() override; private: + void runController() override { updateActControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Compute normalized motor commands based on normalized setpoints. * @param throttle_body_x Normalized speed in body x direction [-1, 1]. diff --git a/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.cpp b/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.cpp index 7531faf3efd..05ef1349c9b 100644 --- a/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.cpp +++ b/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -MecanumAttControl::MecanumAttControl(ModuleParams *parent) : ModuleParams(parent) +MecanumAttControl::MecanumAttControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_att_control", ORB_ID(vehicle_attitude)) { _surface_vehicle_rate_setpoint_pub.advertise(); _surface_vehicle_attitude_status_pub.advertise(); @@ -63,7 +65,8 @@ void MecanumAttControl::updateAttControl() { hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs on every vehicle_attitude publication (EKF2 output predictor at IMU_INTEG_RATE >= 100 Hz), allow for jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; if (_vehicle_attitude_sub.updated()) { vehicle_attitude_s vehicle_attitude{}; diff --git a/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.hpp b/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.hpp index 7526eb5eda5..24ffb35b0c2 100644 --- a/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.hpp +++ b/src/modules/rover_mecanum/MecanumAttControl/MecanumAttControl.hpp @@ -40,6 +40,7 @@ // Libraries #include +#include #include #include #include @@ -58,7 +59,7 @@ using namespace time_literals; /** * @brief Class for mecanum attitude control. */ -class MecanumAttControl : public ModuleParams +class MecanumAttControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -91,6 +92,8 @@ protected: void updateParams() override; private: + void runController() override { updateAttControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled // uORB subscriptions uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; diff --git a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp index a4fb57b5824..b2b7fb3fa89 100644 --- a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp +++ b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -MecanumPosControl::MecanumPosControl(ModuleParams *parent) : ModuleParams(parent) +MecanumPosControl::MecanumPosControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_pos_control", ORB_ID(vehicle_local_position)) { _surface_vehicle_speed_setpoint_pub.advertise(); _pure_pursuit_status_pub.advertise(); diff --git a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp index afd764c3220..c931a8f0f42 100644 --- a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp +++ b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp @@ -40,6 +40,7 @@ // Libraries #include #include +#include #include #include @@ -59,7 +60,7 @@ using namespace matrix; /** * @brief Class for mecanum position control. */ -class MecanumPosControl : public ModuleParams +class MecanumPosControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -94,6 +95,8 @@ protected: void updateParams() override; private: + void runController() override { updatePosControl(); } + /** * @brief Update uORB subscriptions used in position controller. */ diff --git a/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.cpp b/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.cpp index b2e80693f28..a9df78881c7 100644 --- a/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.cpp +++ b/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -MecanumRateControl::MecanumRateControl(ModuleParams *parent) : ModuleParams(parent) +MecanumRateControl::MecanumRateControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_rate_control", ORB_ID(vehicle_angular_velocity)) { _surface_vehicle_steering_setpoint_pub.advertise(); _surface_vehicle_rate_status_pub.advertise(); diff --git a/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.hpp b/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.hpp index 24779762bd6..383e90e00fe 100644 --- a/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.hpp +++ b/src/modules/rover_mecanum/MecanumRateControl/MecanumRateControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -54,7 +55,7 @@ /** * @brief Class for mecanum rate control. */ -class MecanumRateControl : public ModuleParams +class MecanumRateControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -87,6 +88,8 @@ protected: void updateParams() override; private: + void runController() override { updateRateControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled // uORB subscriptions uORB::Subscription _surface_vehicle_rate_setpoint_sub{ORB_ID(surface_vehicle_rate_setpoint)}; diff --git a/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.cpp b/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.cpp index ba4740b66d0..00d485c8d3d 100644 --- a/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.cpp +++ b/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.cpp @@ -35,7 +35,9 @@ using namespace time_literals; -MecanumSpeedControl::MecanumSpeedControl(ModuleParams *parent) : ModuleParams(parent) +MecanumSpeedControl::MecanumSpeedControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_speed_control", ORB_ID(vehicle_local_position)) { _rover_throttle_setpoint_pub.advertise(); _surface_vehicle_speed_status_pub.advertise(); @@ -65,7 +67,8 @@ void MecanumSpeedControl::updateSpeedControl() { const hrt_abstime timestamp_prev = _timestamp; _timestamp = hrt_absolute_time(); - const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 10_ms) * 1e-6f; + // Runs at the EKF2 output rate (EKF2_PREDICT_US up to 20 ms), allow for that plus jitter + const float dt = math::constrain(_timestamp - timestamp_prev, 1_ms, 50_ms) * 1e-6f; updateSubscriptions(); diff --git a/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.hpp b/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.hpp index 6d201fd26f3..3447bd2afde 100644 --- a/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.hpp +++ b/src/modules/rover_mecanum/MecanumSpeedControl/MecanumSpeedControl.hpp @@ -39,6 +39,7 @@ // Libraries #include +#include #include #include #include @@ -59,7 +60,7 @@ using namespace matrix; /** * @brief Class for mecanum speed control. */ -class MecanumSpeedControl : public ModuleParams +class MecanumSpeedControl : public ModuleParams, public RoverControllerWorkItem { public: /** @@ -92,6 +93,9 @@ protected: void updateParams() override; private: + void runController() override { updateSpeedControl(); } + void onEnable() override { _timestamp = hrt_absolute_time(); } // The first dt after (re-)enabling must not span the time spent disabled + /** * @brief Update uORB subscriptions used in speed controller. */ diff --git a/src/modules/rover_mecanum/RoverMecanum.cpp b/src/modules/rover_mecanum/RoverMecanum.cpp index 68de83ca253..db7f2967704 100644 --- a/src/modules/rover_mecanum/RoverMecanum.cpp +++ b/src/modules/rover_mecanum/RoverMecanum.cpp @@ -39,14 +39,21 @@ ModuleBase::Descriptor RoverMecanum::desc{task_spawn, custom_command, print_usag RoverMecanum::RoverMecanum() : ModuleParams(nullptr), - ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl) + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers) { updateParams(); } bool RoverMecanum::init() { - if (!_vehicle_angular_velocity_sub.registerCallback()) { + // The module registers its gyro callback first and keeps it, so the setpoints it generates reach the controllers in the same cycle. + // The controllers only check their trigger here, they register it while enabled, see enableControllers(). + if (!_vehicle_angular_velocity_sub.registerCallback() + || !_mecanum_pos_control.init() + || !_mecanum_speed_control.init() + || !_mecanum_att_control.init() + || !_mecanum_rate_control.init() + || !_mecanum_act_control.init()) { PX4_ERR("callback registration failed"); return false; } @@ -67,7 +74,7 @@ void RoverMecanum::Run() return; } - // Drain the trigger. The gyro paces the loop; its value is not used here. + // Drain the trigger. The gyro paces the setpoint generation; its value is not used here. vehicle_angular_velocity_s angular_velocity; _vehicle_angular_velocity_sub.update(&angular_velocity); @@ -101,14 +108,16 @@ void RoverMecanum::Run() _was_armed = true; generateSetpoints(); - updateControllers(); + enableControllers(true); } else if (_was_armed) { // Reset all controllers and stop the vehicle + enableControllers(false); reset(); _mecanum_act_control.stopVehicle(); _was_armed = false; } else { + enableControllers(false); _mecanum_act_control.stopVehicle(); } @@ -181,27 +190,36 @@ void RoverMecanum::generateSetpoints() } -void RoverMecanum::updateControllers() +void RoverMecanum::enableControllers(const bool enabled) { - if (_vehicle_control_mode.flag_control_position_enabled) { - _mecanum_pos_control.updatePosControl(); + const bool pos_was_enabled = _mecanum_pos_control.enabled(); + const bool speed_was_enabled = _mecanum_speed_control.enabled(); + const bool rate_was_enabled = _mecanum_rate_control.enabled(); + const bool act_was_enabled = _mecanum_act_control.enabled(); + + // Enable top-down, so newly enabled controllers run in cascade order in this cycle + bool registered = _mecanum_pos_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled); + registered = _mecanum_speed_control.setEnabled(enabled && _vehicle_control_mode.flag_control_velocity_enabled) && registered; + registered = _mecanum_att_control.setEnabled(enabled && _vehicle_control_mode.flag_control_attitude_enabled) && registered; + registered = _mecanum_rate_control.setEnabled(enabled && _vehicle_control_mode.flag_control_rates_enabled) && registered; + registered = _mecanum_act_control.setEnabled(enabled && _vehicle_control_mode.flag_control_allocation_enabled) && registered; + + // Callbacks sharing a trigger topic run in registration order: keep the position controller ahead of the speed + // controller (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity) + if (!pos_was_enabled && _mecanum_pos_control.enabled() && speed_was_enabled && _mecanum_speed_control.enabled()) { + registered = _mecanum_speed_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_attitude_enabled) { - _mecanum_att_control.updateAttControl(); + if (!rate_was_enabled && _mecanum_rate_control.enabled() && act_was_enabled && _mecanum_act_control.enabled()) { + registered = _mecanum_act_control.reregisterTrigger() && registered; } - if (_vehicle_control_mode.flag_control_rates_enabled) { - _mecanum_rate_control.updateRateControl(); + // Report a failure once, the controllers retry on every cycle + if (!registered && !_controller_registration_failed) { + PX4_ERR("controller callback registration failed, retrying"); } - if (_vehicle_control_mode.flag_control_velocity_enabled) { - _mecanum_speed_control.updateSpeedControl(); - } - - if (_vehicle_control_mode.flag_control_allocation_enabled) { - _mecanum_act_control.updateActControl(); - } + _controller_registration_failed = !registered; } void RoverMecanum::runSanityChecks() diff --git a/src/modules/rover_mecanum/RoverMecanum.hpp b/src/modules/rover_mecanum/RoverMecanum.hpp index 82d666fce4b..88de9d0615e 100644 --- a/src/modules/rover_mecanum/RoverMecanum.hpp +++ b/src/modules/rover_mecanum/RoverMecanum.hpp @@ -100,9 +100,14 @@ private: void generateSetpoints(); /** - * @brief Update the controllers + * @brief Enable the controllers the active control mode needs. + * + * Each controller runs on its own trigger topic, see the controller classes, and only the enabled ones are woken by it. + * They share this module's work queue, so enabling and resetting them here never overlaps with their Run(). + * + * @param enabled false disables all controllers */ - void updateControllers(); + void enableControllers(bool enabled); /** * @brief Check proper parameter setup for the controllers @@ -140,4 +145,5 @@ private: bool _sanity_checks_passed{true}; // True if checks for all active controllers pass uint8_t _nav_state{vehicle_status_s::NAVIGATION_STATE_MAX}; // Active mode, to detect mode changes bool _was_armed{false}; // True if the vehicle was armed before the last reset + bool _controller_registration_failed{false}; // True while a controller trigger callback is not registered };