mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 08:40:30 +08:00
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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> * 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 <frimi999@gmail.com> --------- Signed-off-by: Michael Fritsche <frimi999@gmail.com>
This commit is contained in:
1 parent
92b18d17f3
commit
d34142bc4e
40 files changed
+679
-96
No files matched your search
@@ -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)
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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 <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
#include <uORB/SubscriptionCallback.hpp>
|
||||
|
||||
/**
|
||||
* @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};
|
||||
};
|
||||
@@ -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 <gtest/gtest.h>
|
||||
|
||||
#include "RoverControllerWorkItem.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <hrt_work.h>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/topics/vehicle_angular_velocity.h>
|
||||
|
||||
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<int> run_sequence{0};
|
||||
|
||||
class TestController : public RoverControllerWorkItem
|
||||
{
|
||||
public:
|
||||
TestController(const char *name) : RoverControllerWorkItem(name, ORB_ID(vehicle_angular_velocity)) {}
|
||||
|
||||
std::atomic<int> runs{0};
|
||||
std::atomic<int> enables{0};
|
||||
std::atomic<int> 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<vehicle_angular_velocity_s> _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);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
|
||||
@@ -45,6 +46,7 @@
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/topics/actuator_motors.h>
|
||||
#include <uORB/topics/vehicle_angular_velocity.h>
|
||||
#include <uORB/topics/actuator_servos.h>
|
||||
#include <uORB/topics/surface_vehicle_steering_setpoint.h>
|
||||
#include <uORB/topics/rover_throttle_setpoint.h>
|
||||
@@ -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)};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
// Library includes
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRateYaw.hpp>
|
||||
#include <math.h>
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/pure_pursuit/PurePursuit.hpp>
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <math.h>
|
||||
|
||||
// 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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Library includes
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
};
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
|
||||
@@ -45,6 +46,7 @@
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/topics/actuator_motors.h>
|
||||
#include <uORB/topics/vehicle_angular_velocity.h>
|
||||
#include <uORB/topics/surface_vehicle_steering_setpoint.h>
|
||||
#include <uORB/topics/rover_throttle_setpoint.h>
|
||||
#include <uORB/topics/manual_control_setpoint.h>
|
||||
@@ -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].
|
||||
|
||||
@@ -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{};
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRateYaw.hpp>
|
||||
#include <math.h>
|
||||
@@ -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)};
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/pure_pursuit/PurePursuit.hpp>
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <math.h>
|
||||
|
||||
// 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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
@@ -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)};
|
||||
|
||||
+5
-2
@@ -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)) {
|
||||
|
||||
+5
-1
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
};
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
|
||||
@@ -45,13 +46,14 @@
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/topics/actuator_motors.h>
|
||||
#include <uORB/topics/vehicle_angular_velocity.h>
|
||||
#include <uORB/topics/surface_vehicle_steering_setpoint.h>
|
||||
#include <uORB/topics/rover_throttle_setpoint.h>
|
||||
|
||||
/**
|
||||
* @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].
|
||||
|
||||
@@ -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{};
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRateYaw.hpp>
|
||||
#include <math.h>
|
||||
@@ -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)};
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
// Libraries
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/pure_pursuit/PurePursuit.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/geo/geo.h>
|
||||
#include <math.h>
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
#include <math.h>
|
||||
@@ -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)};
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
// Libraries
|
||||
#include <lib/rover_control/RoverControl.hpp>
|
||||
#include <lib/rover_control/RoverControllerWorkItem.hpp>
|
||||
#include <lib/pid/PID.hpp>
|
||||
#include <matrix/matrix/math.hpp>
|
||||
#include <lib/slew_rate/SlewRate.hpp>
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
};
|
||||
Reference in new issue
Block a user