diff --git a/msg/SurfaceVehicleCourseSetpoint.msg b/msg/SurfaceVehicleCourseSetpoint.msg index 55b587c6e4f..6f87d0b0665 100644 --- a/msg/SurfaceVehicleCourseSetpoint.msg +++ b/msg/SurfaceVehicleCourseSetpoint.msg @@ -1,17 +1,16 @@ # Surface Vehicle Course Setpoint # # Course setpoint for surface vehicles, on land or on water (rovers, boats). -# Consumed by the position controllers of rover_ackermann, rover_differential and -# rover_mecanum, which turn it into speed and attitude setpoints. -# Published by the Guided Course mode of rover_ackermann, rover_differential and rover_mecanum, -# and can be sent from ROS 2 over uXRCE-DDS (/fmu/in/surface_vehicle_course_setpoint). +# Published by the position controllers of rover_ackermann, rover_differential and rover_mecanum (pure +# pursuit towards the target) and by their Guided Course mode. Can be sent from ROS 2 over uXRCE-DDS +# (/fmu/in/surface_vehicle_course_setpoint). +# Consumed by the course controllers of these modules, which turn it into attitude and speed setpoints. # The vehicle tries to track the course over ground, as with the fixed-wing course hold: vehicles # that steer by heading correct their heading by the estimated drift angle; omnidirectional vehicles drive # along the course setpoint directly. There is no desired track, so a sideways offset is not # corrected. To follow a desired track, publish surface_vehicle_position_setpoint instead. -# The position controller follows whichever of the two was published most recently. uint64 timestamp # [us] Time since system start -float32 course # [rad] [@range -pi, pi] [@frame NED] [@invalid NaN No course; the position controller stays idle] Course over ground to hold -float32 speed # [m/s] [@range -inf, inf] [@invalid NaN Defaults to maximum speed] Cruising speed; a negative value drives the vehicle in reverse (rear first) along the course +float32 course # [rad] [@range -pi, pi] [@frame NED] [@invalid NaN No course. The last valid course is controlled until a NaN course is received. With a speed, the speed is still commanded (rover_ackermann and rover_differential keep the current heading, rover_mecanum drives along its body x axis); with NaN speed too, the course controller stops] Course over ground to hold +float32 speed # [m/s] [@range -inf, inf] [@invalid NaN Speed not controlled: the speed controller is deactivated, and the heading is turned so that an external thrust commanded (surface_vehicle_thrust_setpoint) drives along the course: the sign of x gives forwards or reverse, rover_mecanum also aligns any x/y thrust direction with the course] Cruising speed; a negative value drives the vehicle in reverse (rear first) along the course float32 yaw # [rad] [@range -pi, pi] [@frame NED] [@invalid NaN Not considered] Currently unused. Only for omnidirectional vehicles: vehicle yaw while holding the course. diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp new file mode 100644 index 00000000000..d4ed34c424b --- /dev/null +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp @@ -0,0 +1,153 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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 "AckermannCourseControl.hpp" + +using namespace time_literals; + +AckermannCourseControl::AckermannCourseControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_ackermann_course_control", ORB_ID(vehicle_local_position)) +{ + _surface_vehicle_attitude_setpoint_pub.advertise(); + _surface_vehicle_speed_setpoint_pub.advertise(); + + updateParams(); +} + +void AckermannCourseControl::updateParams() +{ + ModuleParams::updateParams(); + _max_yaw_rate = _param_ro_yaw_rate_lim.get() * M_DEG_TO_RAD_F; + _min_speed = _param_ra_wheel_base.get() * _max_yaw_rate / tanf(_param_ra_max_str_ang.get()); +} + +void AckermannCourseControl::updateCourseControl() +{ + updateSubscriptions(); + + // The last valid course setpoint is controlled until a NaN course is received. A NaN course with a speed (e.g. the + // position controller stopping at its target) keeps the current heading, so the controllers below keep getting setpoints. + // Only a NaN course without speed (e.g. Position mode while steering) stops the course controller. + if (!PX4_ISFINITE(_course_setpoint) && !PX4_ISFINITE(_speed_setpoint)) { + return; + } + + const hrt_abstime timestamp = hrt_absolute_time(); + float speed_setpoint = _speed_setpoint; + + // Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel + const bool reverse = PX4_ISFINITE(speed_setpoint) ? speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON; + + float yaw_setpoint = _vehicle_yaw; // No course: keep the current heading + + if (PX4_ISFINITE(_course_setpoint)) { + if (reverse) { + // Driving in reverse, the rover points away from its direction of travel. + // The drift angle is only estimated driving forwards, so it is not compensated here. + yaw_setpoint = matrix::wrap_pi(_course_setpoint + M_PI_F); + + } else { + // Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind + yaw_setpoint = matrix::wrap_pi(_course_setpoint - _drift_angle_estimator.getDriftAngle()); + } + } + + surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; + surface_vehicle_attitude_setpoint.timestamp = timestamp; + surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; + _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); + + if (PX4_ISFINITE(speed_setpoint)) { + // Slow down for large heading errors, but not below the speed needed to turn at the yaw rate limit + if (_param_ro_speed_red.get() > FLT_EPSILON) { + const float heading_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw)); + const float speed_reduction = math::constrain(_param_ro_speed_red.get() * math::interpolate(heading_error, + 0.f, M_PI_F, 0.f, 1.f), 0.f, 1.f); + const float max_speed = math::constrain(_param_ro_max_thr_speed.get() * (1.f - speed_reduction), _min_speed, + _param_ro_max_thr_speed.get()); + speed_setpoint = math::constrain(speed_setpoint, -max_speed, max_speed); + } + } + + // No speed setpoint: NaN deactivates the speed controller, the thrust then comes from elsewhere (thrust-only course hold) + surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; + surface_vehicle_speed_setpoint.timestamp = timestamp; + surface_vehicle_speed_setpoint.x = speed_setpoint; + surface_vehicle_speed_setpoint.y = NAN; + _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); +} + +void AckermannCourseControl::updateSubscriptions() +{ + if (_vehicle_attitude_sub.updated()) { + vehicle_attitude_s vehicle_attitude{}; + _vehicle_attitude_sub.copy(&vehicle_attitude); + _vehicle_yaw = matrix::Eulerf(matrix::Quatf(vehicle_attitude.q)).psi(); + } + + if (_vehicle_local_position_sub.updated()) { + vehicle_local_position_s vehicle_local_position{}; + _vehicle_local_position_sub.copy(&vehicle_local_position); + _drift_angle_estimator.update(vehicle_local_position); + } + + if (_surface_vehicle_thrust_setpoint_sub.updated()) { + surface_vehicle_thrust_setpoint_s surface_vehicle_thrust_setpoint{}; + _surface_vehicle_thrust_setpoint_sub.copy(&surface_vehicle_thrust_setpoint); + _thrust_setpoint_x = surface_vehicle_thrust_setpoint.x; + } + + if (_surface_vehicle_course_setpoint_sub.updated()) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); + _course_setpoint = surface_vehicle_course_setpoint.course; + _speed_setpoint = surface_vehicle_course_setpoint.speed; + } +} + +bool AckermannCourseControl::runSanityChecks() +{ + bool ret = true; + + if (_param_ra_wheel_base.get() < FLT_EPSILON || _param_ra_max_str_ang.get() < FLT_EPSILON + || _param_ro_yaw_rate_lim.get() < FLT_EPSILON) { + ret = false; + } + + if (_param_ro_speed_red.get() > FLT_EPSILON && _param_ro_max_thr_speed.get() < FLT_EPSILON) { + ret = false; + } + + return ret; +} diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp new file mode 100644 index 00000000000..896e8767095 --- /dev/null +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp @@ -0,0 +1,131 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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. + * + ****************************************************************************/ + +#pragma once + +// PX4 includes +#include + +// Library includes +#include +#include +#include +#include + +// uORB includes +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief Class for ackermann course control. + * + * Sits between the position controller and the attitude and speed controllers: turns a course over ground into a heading + * setpoint (drift compensation forwards, pointing away from the course in reverse) and passes the speed on, reduced + * for large heading errors. Without a speed setpoint it commands the heading for the thrust + * commanded elsewhere and deactivates the speed controller (NaN speed setpoint). + * The last valid course setpoint is controlled until a NaN course is received. With a NaN course and a speed the current + * heading is kept, with NaN course and speed the course controller stops publishing. + */ +class AckermannCourseControl : public ModuleParams, public RoverControllerWorkItem +{ +public: + /** + * @brief Constructor for AckermannCourseControl. + * @param parent The parent ModuleParams object. + */ + AckermannCourseControl(ModuleParams *parent); + ~AckermannCourseControl() = default; + + /** + * @brief Generate and publish surfaceVehicleAttitudeSetpoint and surfaceVehicleSpeedSetpoint from the last surfaceVehicleCourseSetpoint. + */ + void updateCourseControl(); + + /** + * @brief Check if the necessary parameters are set. + * @return True if all checks pass. + */ + bool runSanityChecks(); + + /** + * @brief Reset course controller. + */ + void reset() { _course_setpoint = NAN; _speed_setpoint = NAN; _drift_angle_estimator.reset(); } + +protected: + /** + * @brief Update the parameters of the module. + */ + void updateParams() override; + +private: + void runController() override { updateCourseControl(); } + + /** + * @brief Update uORB subscriptions used in course controller. + */ + void updateSubscriptions(); + + // uORB subscriptions + uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; + uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; + uORB::Subscription _surface_vehicle_thrust_setpoint_sub{ORB_ID(surface_vehicle_thrust_setpoint)}; + + // uORB publications + uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; + + // Variables + RoverControl::DriftAngleEstimator _drift_angle_estimator{}; + float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received + float _speed_setpoint{NAN}; // Last received speed setpoint, passed on if finite + float _vehicle_yaw{0.f}; + float _thrust_setpoint_x{0.f}; + float _max_yaw_rate{0.f}; + float _min_speed{0.f}; + + DEFINE_PARAMETERS( + (ParamFloat) _param_ro_max_thr_speed, + (ParamFloat) _param_ro_speed_red, + (ParamFloat) _param_ro_yaw_rate_lim, + (ParamFloat) _param_ra_wheel_base, + (ParamFloat) _param_ra_max_str_ang + ) +}; diff --git a/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt b/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt new file mode 100644 index 00000000000..30cc4f89fe4 --- /dev/null +++ b/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt @@ -0,0 +1,39 @@ +############################################################################ +# +# Copyright (c) 2025 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. +# +############################################################################ + +px4_add_library(AckermannCourseControl + AckermannCourseControl.cpp +) + +target_link_libraries(AckermannCourseControl PUBLIC rover_control) +target_include_directories(AckermannCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_ackermann/AckermannDriveModes/AckermannAutoMode/AckermannAutoMode.cpp b/src/modules/rover_ackermann/AckermannDriveModes/AckermannAutoMode/AckermannAutoMode.cpp index c743f8dffbb..ad6cd016fd0 100644 --- a/src/modules/rover_ackermann/AckermannDriveModes/AckermannAutoMode/AckermannAutoMode.cpp +++ b/src/modules/rover_ackermann/AckermannDriveModes/AckermannAutoMode/AckermannAutoMode.cpp @@ -176,7 +176,7 @@ void AckermannAutoMode::guidedCourse() heading) + vehicle_local_position.vy * sinf(heading), 0.f) : 0.f; } - // Streamed, so the course is held again after the position controller was reset on a control mode change + // Streamed straight to the course controller, so the course is held again after it was reset on a mode change surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; surface_vehicle_course_setpoint.timestamp = hrt_absolute_time(); // The navigator publishes the course in [0, 2pi) diff --git a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp index 22401969489..ba6ee6e14fb 100644 --- a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp +++ b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp @@ -40,8 +40,7 @@ AckermannPosControl::AckermannPosControl(ModuleParams *parent) : RoverControllerWorkItem("rover_ackermann_pos_control", ORB_ID(vehicle_local_position)) { _pure_pursuit_status_pub.advertise(); - _surface_vehicle_speed_setpoint_pub.advertise(); - _surface_vehicle_attitude_setpoint_pub.advertise(); + _surface_vehicle_course_setpoint_pub.advertise(); updateParams(); } @@ -49,96 +48,77 @@ AckermannPosControl::AckermannPosControl(ModuleParams *parent) : void AckermannPosControl::updateParams() { ModuleParams::updateParams(); - _max_yaw_rate = _param_ro_yaw_rate_lim.get() * M_DEG_TO_RAD_F; - _min_speed = _param_ra_wheel_base.get() * _max_yaw_rate / tanf(_param_ra_max_str_ang.get()); - } void AckermannPosControl::updatePosControl() { updateSubscriptions(); - hrt_abstime timestamp = hrt_absolute_time(); + const hrt_abstime timestamp = hrt_absolute_time(); - // Course control: like the fixed wing course hold, only the course over ground is controlled, there is no desired track to return to after a sideways offset - const bool course_control = PX4_ISFINITE(_course); - - if (course_control || _target_waypoint_ned.isAllFinite()) { - float distance_to_target = course_control ? 0.f : (_target_waypoint_ned - _curr_pos_ned).norm(); - - if (_arrival_speed > FLT_EPSILON) { - distance_to_target -= _acceptance_radius; // shift target to the edge of the acceptance radius if arrival speed not zero + if (!_target_waypoint_ned.isAllFinite()) { + // Idle. Only stop the course controller when a position setpoint without target was received (e.g. Position + // mode while steering), not on every cycle, so a mode commanding the course controller directly (Guided Course) + // is not overridden. + if (_stop_course_control) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = NAN; + surface_vehicle_course_setpoint.yaw = NAN; + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); + _stop_course_control = false; } - if (course_control || distance_to_target > _acceptance_radius || _arrival_speed > FLT_EPSILON) { + return; + } - float speed_setpoint{_cruising_speed}; + float distance_to_target = (_target_waypoint_ned - _curr_pos_ned).norm(); - if (!course_control && _param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { - speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), - _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); - } + if (_arrival_speed > FLT_EPSILON) { + distance_to_target -= _acceptance_radius; // shift target to the edge of the acceptance radius if arrival speed not zero + } - float bearing_setpoint{NAN}; + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.yaw = NAN; - if (course_control) { - // Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind - bearing_setpoint = speed_setpoint > -FLT_EPSILON ? matrix::wrap_pi(_course - _drift_angle_estimator.getDriftAngle()) : - _course; + if (distance_to_target > _acceptance_radius || _arrival_speed > FLT_EPSILON) { + float speed_setpoint{_cruising_speed}; - } else { - pure_pursuit_status_s pure_pursuit_status{}; - pure_pursuit_status.timestamp = timestamp; + if (_param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { + speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), + _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); + } - bearing_setpoint = PurePursuit::calcTargetBearing(pure_pursuit_status, _param_pp_lookahd_gain.get(), - _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, _start_ned, - _curr_pos_ned, fabsf(speed_setpoint)); - _pure_pursuit_status_pub.publish(pure_pursuit_status); - } + pure_pursuit_status_s pure_pursuit_status{}; + pure_pursuit_status.timestamp = timestamp; - // Driving in reverse, the rover points away from its direction of travel - const float yaw_setpoint = speed_setpoint > -FLT_EPSILON ? bearing_setpoint : matrix::wrap_pi( - bearing_setpoint + M_PI_F); + // Pure pursuit returns the direction of travel towards the lookahead point, which is the course setpoint. + // The course controller turns it into a heading (drift compensation, reversing) and slows down for large course errors. + surface_vehicle_course_setpoint.course = PurePursuit::calcTargetBearing(pure_pursuit_status, + _param_pp_lookahd_gain.get(), _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, + _start_ned, _curr_pos_ned, fabsf(speed_setpoint)); + surface_vehicle_course_setpoint.speed = speed_setpoint; + _pure_pursuit_status_pub.publish(pure_pursuit_status); - if (_param_ro_speed_red.get() > FLT_EPSILON) { - const float course_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw)); - const float speed_reduction = math::constrain(_param_ro_speed_red.get() * math::interpolate(course_error, - 0.f, M_PI_F, 0.f, 1.f), 0.f, 1.f); - const float max_speed = math::constrain(_param_ro_max_thr_speed.get() * (1.f - speed_reduction), _min_speed, - _param_ro_max_thr_speed.get()); - speed_setpoint = math::constrain(speed_setpoint, -max_speed, max_speed); - } + } else { + // Stop: no course to control anymore, only the speed + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = 0.f; - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = speed_setpoint; - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); - surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; - surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; - _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); + if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { + _stopped = true; + _target_waypoint_ned = _curr_pos_ned; + } - } else { - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = 0.f; - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); - surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; - surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = _vehicle_yaw; - _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); - - if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { - _stopped = true; - _target_waypoint_ned = _curr_pos_ned; - } - - if (_stopped && _updated_reset_counter != _reset_counter) { - _target_waypoint_ned = _curr_pos_ned; - _reset_counter = _updated_reset_counter; - } + if (_stopped && _updated_reset_counter != _reset_counter) { + _target_waypoint_ned = _curr_pos_ned; + _reset_counter = _updated_reset_counter; } } + + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); } void AckermannPosControl::updateSubscriptions() @@ -153,14 +133,12 @@ void AckermannPosControl::updateSubscriptions() vehicle_attitude_s vehicle_attitude{}; _vehicle_attitude_sub.copy(&vehicle_attitude); _vehicle_attitude_quaternion = matrix::Quatf(vehicle_attitude.q); - _vehicle_yaw = matrix::Eulerf(_vehicle_attitude_quaternion).psi(); } if (_vehicle_local_position_sub.updated()) { vehicle_local_position_s vehicle_local_position{}; _vehicle_local_position_sub.copy(&vehicle_local_position); _updated_reset_counter = vehicle_local_position.xy_reset_counter; - _drift_angle_estimator.update(vehicle_local_position); _curr_pos_ned = Vector2f(vehicle_local_position.x, vehicle_local_position.y); Vector3f velocity_ned(vehicle_local_position.vx, vehicle_local_position.vy, vehicle_local_position.vz); Vector3f velocity_xyz = _vehicle_attitude_quaternion.rotateVectorInverse(velocity_ned); @@ -177,22 +155,9 @@ void AckermannPosControl::updateSubscriptions() _cruising_speed = PX4_ISFINITE(surface_vehicle_position_setpoint.cruising_speed) ? surface_vehicle_position_setpoint.cruising_speed : _param_ro_speed_lim.get(); _target_waypoint_ned = Vector2f(surface_vehicle_position_setpoint.target_ned[0], surface_vehicle_position_setpoint.target_ned[1]); - _course = NAN; + _stop_course_control = !_target_waypoint_ned.isAllFinite(); _stopped = false; } - - if (_surface_vehicle_course_setpoint_sub.updated()) { - surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint; - _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); - _start_ned = Vector2f(NAN, NAN); - _arrival_speed = 0.f; - _cruising_speed = PX4_ISFINITE(surface_vehicle_course_setpoint.speed) ? - surface_vehicle_course_setpoint.speed : _param_ro_speed_lim.get(); - _target_waypoint_ned = Vector2f(NAN, NAN); - _course = surface_vehicle_course_setpoint.course; - _stopped = false; - } - } bool AckermannPosControl::runSanityChecks() diff --git a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp index a17c1c86062..7c3d06f0549 100644 --- a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp +++ b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.hpp @@ -47,10 +47,8 @@ // uORB includes #include #include -#include #include -#include -#include +#include #include #include #include @@ -72,7 +70,7 @@ public: ~AckermannPosControl() = default; /** - * @brief Generate and publish surfaceVehicleSpeedSetpoint and surfaceVehicleAttitudeSetpoint from surfaceVehiclePositionSetpoint or surfaceVehicleCourseSetpoint. + * @brief Generate and publish surfaceVehicleCourseSetpoint (course and speed) from surfaceVehiclePositionSetpoint. */ void updatePosControl(); @@ -85,7 +83,7 @@ public: /** * @brief Reset position controller. */ - void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _course = NAN; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false; _drift_angle_estimator.reset();}; + void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false; _stop_course_control = false;}; protected: /** @@ -105,12 +103,10 @@ private: uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; uORB::Subscription _surface_vehicle_position_setpoint_sub{ORB_ID(surface_vehicle_position_setpoint)}; - uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; uORB::Subscription _position_controller_status_sub{ORB_ID(position_controller_status)}; // uORB publications - uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; - uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + uORB::Publication _surface_vehicle_course_setpoint_pub{ORB_ID(surface_vehicle_course_setpoint)}; uORB::Publication _pure_pursuit_status_pub{ORB_ID(pure_pursuit_status)}; // Variables @@ -118,31 +114,22 @@ private: Vector2f _curr_pos_ned{}; Vector2f _start_ned{}; Vector2f _target_waypoint_ned{}; - float _course{NAN}; - RoverControl::DriftAngleEstimator _drift_angle_estimator{}; float _arrival_speed{0.f}; - float _vehicle_yaw{0.f}; - float _max_yaw_rate{0.f}; float _acceptance_radius{0.f}; // Acceptance radius for the waypoint. - float _min_speed{NAN}; float _vehicle_speed{0.f}; float _cruising_speed{NAN}; bool _stopped{false}; + bool _stop_course_control{false}; // Publish a NaN course setpoint once, on a position setpoint without target uint8_t _reset_counter{0}; /**< counter for estimator resets in xy-direction */ uint8_t _updated_reset_counter{0}; /**< counter for estimator resets in xy-direction */ DEFINE_PARAMETERS( - (ParamFloat) _param_ro_max_thr_speed, - (ParamFloat) _param_ro_speed_red, (ParamFloat) _param_ro_decel_lim, (ParamFloat) _param_ro_jerk_lim, (ParamFloat) _param_ro_speed_lim, (ParamFloat) _param_pp_lookahd_gain, (ParamFloat) _param_pp_lookahd_max, (ParamFloat) _param_pp_lookahd_min, - (ParamFloat) _param_ro_yaw_rate_lim, - (ParamFloat) _param_ra_wheel_base, - (ParamFloat) _param_ra_max_str_ang, (ParamFloat) _param_ro_speed_th ) }; diff --git a/src/modules/rover_ackermann/CMakeLists.txt b/src/modules/rover_ackermann/CMakeLists.txt index 79dce06543e..2af450e0c0f 100644 --- a/src/modules/rover_ackermann/CMakeLists.txt +++ b/src/modules/rover_ackermann/CMakeLists.txt @@ -35,6 +35,7 @@ add_subdirectory(AckermannActControl) add_subdirectory(AckermannRateControl) add_subdirectory(AckermannAttControl) add_subdirectory(AckermannSpeedControl) +add_subdirectory(AckermannCourseControl) add_subdirectory(AckermannPosControl) add_subdirectory(AckermannDriveModes) @@ -49,6 +50,7 @@ px4_add_module( AckermannRateControl AckermannAttControl AckermannSpeedControl + AckermannCourseControl AckermannPosControl AckermannAutoMode AckermannManualMode diff --git a/src/modules/rover_ackermann/RoverAckermann.cpp b/src/modules/rover_ackermann/RoverAckermann.cpp index 841f418a1af..c011a598002 100644 --- a/src/modules/rover_ackermann/RoverAckermann.cpp +++ b/src/modules/rover_ackermann/RoverAckermann.cpp @@ -50,6 +50,7 @@ bool RoverAckermann::init() // 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_course_control.init() || !_ackermann_speed_control.init() || !_ackermann_att_control.init() || !_ackermann_rate_control.init() @@ -129,10 +130,14 @@ void RoverAckermann::generateSetpoints() vehicle_status_s vehicle_status{}; _vehicle_status_sub.copy(&vehicle_status); - // Each entry into Guided Course takes the speed the rover is driving at + // Every mode starts from scratch if (vehicle_status.nav_state != _nav_state) { _nav_state = vehicle_status.nav_state; + // Each entry into Guided Course takes the speed the rover is driving at _auto_mode.reset(); + // The position and course controllers hold their last target and course until a new setpoint or a reset + _ackermann_pos_control.reset(); + _ackermann_course_control.reset(); } // Navigator stores and acks the speed, Guided Course applies it right away @@ -192,20 +197,31 @@ void RoverAckermann::generateSetpoints() void RoverAckermann::enableControllers(const bool enabled) { const bool pos_was_enabled = _ackermann_pos_control.enabled(); + const bool course_was_enabled = _ackermann_course_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_course_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled) + && registered; 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()) { + // Callbacks sharing a trigger topic run in registration order: keep position -> course -> speed controller + // (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity). + // A controller enabled after one below it moves all already enabled controllers below it behind itself. + const bool pos_newly_enabled = !pos_was_enabled && _ackermann_pos_control.enabled(); + const bool course_newly_enabled = !course_was_enabled && _ackermann_course_control.enabled(); + + if (pos_newly_enabled && course_was_enabled && _ackermann_course_control.enabled()) { + registered = _ackermann_course_control.reregisterTrigger() && registered; + } + + if ((pos_newly_enabled || course_newly_enabled) && speed_was_enabled && _ackermann_speed_control.enabled()) { registered = _ackermann_speed_control.reregisterTrigger() && registered; } @@ -238,7 +254,8 @@ void RoverAckermann::runSanityChecks() return; } - if (_vehicle_control_mode.flag_control_position_enabled && !_ackermann_pos_control.runSanityChecks()) { + if (_vehicle_control_mode.flag_control_position_enabled && (!_ackermann_course_control.runSanityChecks() + || !_ackermann_pos_control.runSanityChecks())) { _sanity_checks_passed = false; return; } @@ -249,6 +266,7 @@ void RoverAckermann::runSanityChecks() void RoverAckermann::reset() { _ackermann_pos_control.reset(); + _ackermann_course_control.reset(); _ackermann_speed_control.reset(); _ackermann_att_control.reset(); _ackermann_rate_control.reset(); diff --git a/src/modules/rover_ackermann/RoverAckermann.hpp b/src/modules/rover_ackermann/RoverAckermann.hpp index dca932f0e96..d1fc6a48db8 100644 --- a/src/modules/rover_ackermann/RoverAckermann.hpp +++ b/src/modules/rover_ackermann/RoverAckermann.hpp @@ -58,6 +58,7 @@ #include "AckermannRateControl/AckermannRateControl.hpp" #include "AckermannAttControl/AckermannAttControl.hpp" #include "AckermannSpeedControl/AckermannSpeedControl.hpp" +#include "AckermannCourseControl/AckermannCourseControl.hpp" #include "AckermannPosControl/AckermannPosControl.hpp" #include "AckermannDriveModes/AckermannAutoMode/AckermannAutoMode.hpp" #include "AckermannDriveModes/AckermannManualMode/AckermannManualMode.hpp" @@ -137,6 +138,7 @@ private: AckermannRateControl _ackermann_rate_control{this}; AckermannAttControl _ackermann_att_control{this}; AckermannSpeedControl _ackermann_speed_control{this}; + AckermannCourseControl _ackermann_course_control{this}; AckermannPosControl _ackermann_pos_control{this}; AckermannAutoMode _auto_mode{this}; AckermannManualMode _manual_mode{this}; diff --git a/src/modules/rover_differential/CMakeLists.txt b/src/modules/rover_differential/CMakeLists.txt index 10d7d551a13..a3fb091a1fe 100644 --- a/src/modules/rover_differential/CMakeLists.txt +++ b/src/modules/rover_differential/CMakeLists.txt @@ -35,6 +35,7 @@ add_subdirectory(DifferentialActControl) add_subdirectory(DifferentialRateControl) add_subdirectory(DifferentialAttControl) add_subdirectory(DifferentialSpeedControl) +add_subdirectory(DifferentialCourseControl) add_subdirectory(DifferentialPosControl) add_subdirectory(DifferentialDriveModes) @@ -49,6 +50,7 @@ px4_add_module( DifferentialRateControl DifferentialAttControl DifferentialSpeedControl + DifferentialCourseControl DifferentialPosControl DifferentialAutoMode DifferentialManualMode diff --git a/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt b/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt new file mode 100644 index 00000000000..ab76f56fe0a --- /dev/null +++ b/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt @@ -0,0 +1,39 @@ +############################################################################ +# +# Copyright (c) 2025 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. +# +############################################################################ + +px4_add_library(DifferentialCourseControl + DifferentialCourseControl.cpp +) + +target_link_libraries(DifferentialCourseControl PUBLIC rover_control) +target_include_directories(DifferentialCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp new file mode 100644 index 00000000000..e2a9d531a10 --- /dev/null +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp @@ -0,0 +1,160 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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 "DifferentialCourseControl.hpp" + +using namespace time_literals; + +DifferentialCourseControl::DifferentialCourseControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_differential_course_control", ORB_ID(vehicle_local_position)) +{ + _surface_vehicle_attitude_setpoint_pub.advertise(); + _surface_vehicle_speed_setpoint_pub.advertise(); + + updateParams(); +} + +void DifferentialCourseControl::updateParams() +{ + ModuleParams::updateParams(); +} + +void DifferentialCourseControl::updateCourseControl() +{ + updateSubscriptions(); + + // The last valid course setpoint is controlled until a NaN course is received. A NaN course with a speed (e.g. the + // position controller stopping at its target) keeps the current heading, so the controllers below keep getting setpoints. + // Only a NaN course without speed (e.g. Position mode while steering) stops the course controller. + if (!PX4_ISFINITE(_course_setpoint) && !PX4_ISFINITE(_speed_setpoint)) { + return; + } + + const hrt_abstime timestamp = hrt_absolute_time(); + float speed_setpoint = _speed_setpoint; + + // Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel + const bool reverse = PX4_ISFINITE(speed_setpoint) ? speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON; + + float yaw_setpoint = _vehicle_yaw; // No course: keep the current heading + + if (PX4_ISFINITE(_course_setpoint)) { + if (reverse) { + // Driving in reverse, the rover points away from its direction of travel. + // The drift angle is only estimated driving forwards, so it is not compensated here. + yaw_setpoint = matrix::wrap_pi(_course_setpoint + M_PI_F); + + } else { + // Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind + yaw_setpoint = matrix::wrap_pi(_course_setpoint - _drift_angle_estimator.getDriftAngle()); + } + + const float heading_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw)); + + if (_current_state == DrivingState::DRIVING && heading_error > _param_rd_trans_drv_trn.get()) { + _current_state = DrivingState::SPOT_TURNING; + + } else if (_current_state == DrivingState::SPOT_TURNING && heading_error < _param_rd_trans_trn_drv.get()) { + _current_state = DrivingState::DRIVING; + } + + // Slow down for large heading errors and stop to turn on the spot + if (PX4_ISFINITE(speed_setpoint)) { + if (_current_state == DrivingState::SPOT_TURNING) { + speed_setpoint = 0.f; + + } else if (_param_ro_speed_red.get() > FLT_EPSILON) { + const float speed_reduction = math::constrain(_param_ro_speed_red.get() * math::interpolate(heading_error, + 0.f, M_PI_F, 0.f, 1.f), 0.f, 1.f); + const float max_speed = math::constrain(_param_ro_max_thr_speed.get() * (1.f - speed_reduction), 0.f, + _param_ro_max_thr_speed.get()); + speed_setpoint = math::constrain(speed_setpoint, -max_speed, max_speed); + } + } + + } else { + _current_state = DrivingState::DRIVING; + } + + surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; + surface_vehicle_attitude_setpoint.timestamp = timestamp; + surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; + _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); + + // No speed setpoint: NaN deactivates the speed controller, the thrust then comes from elsewhere (thrust-only course hold) + surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; + surface_vehicle_speed_setpoint.timestamp = timestamp; + surface_vehicle_speed_setpoint.x = speed_setpoint; + surface_vehicle_speed_setpoint.y = NAN; + _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); +} + +void DifferentialCourseControl::updateSubscriptions() +{ + if (_vehicle_attitude_sub.updated()) { + vehicle_attitude_s vehicle_attitude{}; + _vehicle_attitude_sub.copy(&vehicle_attitude); + _vehicle_yaw = matrix::Eulerf(matrix::Quatf(vehicle_attitude.q)).psi(); + } + + if (_vehicle_local_position_sub.updated()) { + vehicle_local_position_s vehicle_local_position{}; + _vehicle_local_position_sub.copy(&vehicle_local_position); + _drift_angle_estimator.update(vehicle_local_position); + } + + if (_surface_vehicle_thrust_setpoint_sub.updated()) { + surface_vehicle_thrust_setpoint_s surface_vehicle_thrust_setpoint{}; + _surface_vehicle_thrust_setpoint_sub.copy(&surface_vehicle_thrust_setpoint); + _thrust_setpoint_x = surface_vehicle_thrust_setpoint.x; + } + + if (_surface_vehicle_course_setpoint_sub.updated()) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); + _course_setpoint = surface_vehicle_course_setpoint.course; + _speed_setpoint = surface_vehicle_course_setpoint.speed; + } +} + +bool DifferentialCourseControl::runSanityChecks() +{ + bool ret = true; + + if (_param_ro_speed_red.get() > FLT_EPSILON && _param_ro_max_thr_speed.get() < FLT_EPSILON) { + ret = false; + } + + return ret; +} diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp new file mode 100644 index 00000000000..f8464d3fded --- /dev/null +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp @@ -0,0 +1,135 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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. + * + ****************************************************************************/ + +#pragma once + +// PX4 includes +#include + +// Library includes +#include +#include +#include +#include + +// uORB includes +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief Enum class for the different states of driving. + */ +enum class DrivingState { + SPOT_TURNING, // The vehicle is currently turning on the spot. + DRIVING // The vehicle is currently driving. +}; + +/** + * @brief Class for differential course control. + * + * Sits between the position controller and the attitude and speed controllers: turns a course over ground into a heading + * setpoint (drift compensation forwards, pointing away from the course in reverse) and passes the speed on, reduced + * for large heading errors and zero while turning on the spot. Without a speed setpoint it commands the heading for the thrust + * commanded elsewhere and deactivates the speed controller (NaN speed setpoint). The last valid course setpoint is controlled until a NaN course is received. + */ +class DifferentialCourseControl : public ModuleParams, public RoverControllerWorkItem +{ +public: + /** + * @brief Constructor for DifferentialCourseControl. + * @param parent The parent ModuleParams object. + */ + DifferentialCourseControl(ModuleParams *parent); + ~DifferentialCourseControl() = default; + + /** + * @brief Generate and publish surfaceVehicleAttitudeSetpoint and surfaceVehicleSpeedSetpoint from the last surfaceVehicleCourseSetpoint. + */ + void updateCourseControl(); + + /** + * @brief Check if the necessary parameters are set. + * @return True if all checks pass. + */ + bool runSanityChecks(); + + /** + * @brief Reset course controller. + */ + void reset() { _course_setpoint = NAN; _speed_setpoint = NAN; _current_state = DrivingState::DRIVING; _drift_angle_estimator.reset(); } + +protected: + /** + * @brief Update the parameters of the module. + */ + void updateParams() override; + +private: + void runController() override { updateCourseControl(); } + + /** + * @brief Update uORB subscriptions used in course controller. + */ + void updateSubscriptions(); + + // uORB subscriptions + uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; + uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; + uORB::Subscription _surface_vehicle_thrust_setpoint_sub{ORB_ID(surface_vehicle_thrust_setpoint)}; + + // uORB publications + uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; + + // Variables + RoverControl::DriftAngleEstimator _drift_angle_estimator{}; + DrivingState _current_state{DrivingState::DRIVING}; + float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received + float _speed_setpoint{NAN}; // Last received speed setpoint, passed on if finite + float _vehicle_yaw{0.f}; + float _thrust_setpoint_x{0.f}; + + DEFINE_PARAMETERS( + (ParamFloat) _param_rd_trans_trn_drv, + (ParamFloat) _param_rd_trans_drv_trn, + (ParamFloat) _param_ro_max_thr_speed, + (ParamFloat) _param_ro_speed_red + ) +}; diff --git a/src/modules/rover_differential/DifferentialDriveModes/DifferentialAutoMode/DifferentialAutoMode.cpp b/src/modules/rover_differential/DifferentialDriveModes/DifferentialAutoMode/DifferentialAutoMode.cpp index 4328b789403..b6b470f4ef7 100644 --- a/src/modules/rover_differential/DifferentialDriveModes/DifferentialAutoMode/DifferentialAutoMode.cpp +++ b/src/modules/rover_differential/DifferentialDriveModes/DifferentialAutoMode/DifferentialAutoMode.cpp @@ -134,7 +134,7 @@ void DifferentialAutoMode::guidedCourse() heading) + vehicle_local_position.vy * sinf(heading), 0.f) : 0.f; } - // Streamed, so the course is held again after the position controller was reset on a control mode change + // Streamed straight to the course controller, so the course is held again after it was reset on a mode change surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; surface_vehicle_course_setpoint.timestamp = hrt_absolute_time(); // The navigator publishes the course in [0, 2pi) diff --git a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp index d528d483d26..3cc220d35ac 100644 --- a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp +++ b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp @@ -40,8 +40,7 @@ DifferentialPosControl::DifferentialPosControl(ModuleParams *parent) : RoverControllerWorkItem("rover_differential_pos_control", ORB_ID(vehicle_local_position)) { _pure_pursuit_status_pub.advertise(); - _surface_vehicle_speed_setpoint_pub.advertise(); - _surface_vehicle_attitude_setpoint_pub.advertise(); + _surface_vehicle_course_setpoint_pub.advertise(); updateParams(); } @@ -55,101 +54,70 @@ void DifferentialPosControl::updatePosControl() { updateSubscriptions(); - hrt_abstime timestamp = hrt_absolute_time(); + const hrt_abstime timestamp = hrt_absolute_time(); - - // Course control: like the fixed wing course hold, only the course over ground is controlled, there is no desired track to return to after a sideways offset - const bool course_control = PX4_ISFINITE(_course); - - if (course_control || _target_waypoint_ned.isAllFinite()) { - float distance_to_target = course_control ? 0.f : (_target_waypoint_ned - _curr_pos_ned).norm(); - - if (_arrival_speed > FLT_EPSILON) { - distance_to_target -= - _param_nav_acc_rad.get(); // shift target to the edge of the acceptance radius if arrival speed not zero + if (!_target_waypoint_ned.isAllFinite()) { + // Idle. Only stop the course controller when a position setpoint without target was received (e.g. Position + // mode while steering), not on every cycle, so a mode commanding the course controller directly (Guided Course) + // is not overridden. + if (_stop_course_control) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = NAN; + surface_vehicle_course_setpoint.yaw = NAN; + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); + _stop_course_control = false; } - if (course_control || distance_to_target > _param_nav_acc_rad.get() || _arrival_speed > FLT_EPSILON) { - float speed_setpoint{_cruising_speed}; + return; + } - if (!course_control && _param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { - speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), - _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); - } + float distance_to_target = (_target_waypoint_ned - _curr_pos_ned).norm(); - float yaw_setpoint{NAN}; + if (_arrival_speed > FLT_EPSILON) { + distance_to_target -= _param_nav_acc_rad.get(); // shift target to the edge of the acceptance radius if arrival speed not zero + } - if (course_control) { - // Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind - yaw_setpoint = speed_setpoint > -FLT_EPSILON ? matrix::wrap_pi(_course - _drift_angle_estimator.getDriftAngle()) : - _course; + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.yaw = NAN; - } else { - pure_pursuit_status_s pure_pursuit_status{}; - pure_pursuit_status.timestamp = timestamp; + if (distance_to_target > _param_nav_acc_rad.get() || _arrival_speed > FLT_EPSILON) { + float speed_setpoint{_cruising_speed}; - yaw_setpoint = PurePursuit::calcTargetBearing(pure_pursuit_status, _param_pp_lookahd_gain.get(), - _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, _start_ned, - _curr_pos_ned, fabsf(speed_setpoint)); - _pure_pursuit_status_pub.publish(pure_pursuit_status); - } + if (_param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { + speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), + _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); + } - // Driving in reverse, the rover points away from its direction of travel - if (speed_setpoint < -FLT_EPSILON) { - yaw_setpoint = matrix::wrap_pi(yaw_setpoint + M_PI_F); - } + pure_pursuit_status_s pure_pursuit_status{}; + pure_pursuit_status.timestamp = timestamp; - const float heading_error = matrix::wrap_pi(yaw_setpoint - _vehicle_yaw); + // Pure pursuit returns the direction of travel towards the lookahead point, which is the course setpoint + surface_vehicle_course_setpoint.course = PurePursuit::calcTargetBearing(pure_pursuit_status, + _param_pp_lookahd_gain.get(), _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, + _start_ned, _curr_pos_ned, fabsf(speed_setpoint)); + surface_vehicle_course_setpoint.speed = speed_setpoint; + _pure_pursuit_status_pub.publish(pure_pursuit_status); - if (_current_state == DrivingState::DRIVING && fabsf(heading_error) > _param_rd_trans_drv_trn.get()) { - _current_state = DrivingState::SPOT_TURNING; + } else { + // Stop: no course to control anymore, only the speed + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = 0.f; - } else if (_current_state == DrivingState::SPOT_TURNING && fabsf(heading_error) < _param_rd_trans_trn_drv.get()) { - _current_state = DrivingState::DRIVING; - } + if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { + _stopped = true; + _target_waypoint_ned = _curr_pos_ned; + } - if (_current_state == DrivingState::SPOT_TURNING) { - speed_setpoint = 0.f; // stop during spot turning - - } else if (_param_ro_speed_red.get() > FLT_EPSILON) { - const float speed_reduction = math::constrain(_param_ro_speed_red.get() * math::interpolate(fabsf(heading_error), - 0.f, M_PI_F, 0.f, 1.f), 0.f, 1.f); - const float max_speed = math::constrain(_param_ro_max_thr_speed.get() * (1.f - speed_reduction), 0.f, - _param_ro_max_thr_speed.get()); - speed_setpoint = math::constrain(speed_setpoint, -max_speed, max_speed); - } - - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = speed_setpoint; - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); - surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; - surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; - _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); - - } else { - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = 0.f; - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); - surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; - surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = _vehicle_yaw; - _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); - - if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { - _stopped = true; - _target_waypoint_ned = _curr_pos_ned; - } - - if (_stopped && _updated_reset_counter != _reset_counter) { - _target_waypoint_ned = _curr_pos_ned; - _reset_counter = _updated_reset_counter; - } + if (_stopped && _updated_reset_counter != _reset_counter) { + _target_waypoint_ned = _curr_pos_ned; + _reset_counter = _updated_reset_counter; } } + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); } void DifferentialPosControl::updateSubscriptions() @@ -158,14 +126,12 @@ void DifferentialPosControl::updateSubscriptions() vehicle_attitude_s vehicle_attitude{}; _vehicle_attitude_sub.copy(&vehicle_attitude); _vehicle_attitude_quaternion = matrix::Quatf(vehicle_attitude.q); - _vehicle_yaw = matrix::Eulerf(_vehicle_attitude_quaternion).psi(); } if (_vehicle_local_position_sub.updated()) { vehicle_local_position_s vehicle_local_position{}; _vehicle_local_position_sub.copy(&vehicle_local_position); _updated_reset_counter = vehicle_local_position.xy_reset_counter; - _drift_angle_estimator.update(vehicle_local_position); _curr_pos_ned = Vector2f(vehicle_local_position.x, vehicle_local_position.y); @@ -184,19 +150,7 @@ void DifferentialPosControl::updateSubscriptions() _cruising_speed = PX4_ISFINITE(surface_vehicle_position_setpoint.cruising_speed) ? surface_vehicle_position_setpoint.cruising_speed : _param_ro_speed_lim.get(); _target_waypoint_ned = Vector2f(surface_vehicle_position_setpoint.target_ned[0], surface_vehicle_position_setpoint.target_ned[1]); - _course = NAN; - _stopped = false; - } - - if (_surface_vehicle_course_setpoint_sub.updated()) { - surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint; - _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); - _start_ned = Vector2f(NAN, NAN); - _arrival_speed = 0.f; - _cruising_speed = PX4_ISFINITE(surface_vehicle_course_setpoint.speed) ? - surface_vehicle_course_setpoint.speed : _param_ro_speed_lim.get(); - _target_waypoint_ned = Vector2f(NAN, NAN); - _course = surface_vehicle_course_setpoint.course; + _stop_course_control = !_target_waypoint_ned.isAllFinite(); _stopped = false; } } diff --git a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp index e25560cedc9..a328a1b2aea 100644 --- a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp +++ b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.hpp @@ -47,8 +47,6 @@ // uORB includes #include #include -#include -#include #include #include #include @@ -57,14 +55,6 @@ using namespace matrix; -/** - * @brief Enum class for the different states of driving. - */ -enum class DrivingState { - SPOT_TURNING, // The vehicle is currently turning on the spot. - DRIVING // The vehicle is currently driving. -}; - /** * @brief Class for differential position control. */ @@ -79,7 +69,7 @@ public: ~DifferentialPosControl() = default; /** - * @brief Generate and publish surfaceVehicleSpeedSetpoint and surfaceVehicleAttitudeSetpoint from surfaceVehiclePositionSetpoint or surfaceVehicleCourseSetpoint. + * @brief Generate and publish surfaceVehicleCourseSetpoint (course and speed) from surfaceVehiclePositionSetpoint. */ void updatePosControl(); @@ -92,7 +82,7 @@ public: /** * @brief Reset position controller. */ - void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _course = NAN; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false; _drift_angle_estimator.reset();}; + void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false; _stop_course_control = false;}; protected: /** @@ -112,33 +102,25 @@ private: uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; uORB::Subscription _surface_vehicle_position_setpoint_sub{ORB_ID(surface_vehicle_position_setpoint)}; - uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; // uORB publications - uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; uORB::Publication _pure_pursuit_status_pub{ORB_ID(pure_pursuit_status)}; - uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + uORB::Publication _surface_vehicle_course_setpoint_pub{ORB_ID(surface_vehicle_course_setpoint)}; // Variables Quatf _vehicle_attitude_quaternion{}; Vector2f _curr_pos_ned{}; Vector2f _start_ned{}; Vector2f _target_waypoint_ned{}; - float _course{NAN}; - RoverControl::DriftAngleEstimator _drift_angle_estimator{}; float _arrival_speed{0.f}; - float _vehicle_yaw{0.f}; float _vehicle_speed{0.f}; float _cruising_speed{NAN}; bool _stopped{false}; + bool _stop_course_control{false}; // Publish a NaN course setpoint once, on a position setpoint without target uint8_t _reset_counter{0}; /**< counter for estimator resets in xy-direction */ uint8_t _updated_reset_counter{0}; /**< counter for estimator resets in xy-direction */ - DrivingState _current_state{DrivingState::DRIVING}; DEFINE_PARAMETERS( - (ParamFloat) _param_rd_trans_trn_drv, - (ParamFloat) _param_rd_trans_drv_trn, - (ParamFloat) _param_ro_max_thr_speed, (ParamFloat) _param_ro_decel_lim, (ParamFloat) _param_ro_jerk_lim, (ParamFloat) _param_ro_speed_lim, @@ -146,7 +128,6 @@ private: (ParamFloat) _param_pp_lookahd_max, (ParamFloat) _param_pp_lookahd_min, (ParamFloat) _param_nav_acc_rad, - (ParamFloat) _param_ro_speed_red, (ParamFloat) _param_ro_speed_th ) }; diff --git a/src/modules/rover_differential/RoverDifferential.cpp b/src/modules/rover_differential/RoverDifferential.cpp index f58d3733057..3ebe4a729a7 100644 --- a/src/modules/rover_differential/RoverDifferential.cpp +++ b/src/modules/rover_differential/RoverDifferential.cpp @@ -50,6 +50,7 @@ bool RoverDifferential::init() // 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_course_control.init() || !_differential_speed_control.init() || !_differential_att_control.init() || !_differential_rate_control.init() @@ -130,10 +131,14 @@ void RoverDifferential::generateSetpoints() vehicle_status_s vehicle_status{}; _vehicle_status_sub.copy(&vehicle_status); - // Each entry into Guided Course takes the speed the rover is driving at + // Every mode starts from scratch if (vehicle_status.nav_state != _nav_state) { _nav_state = vehicle_status.nav_state; + // Each entry into Guided Course takes the speed the rover is driving at _auto_mode.reset(); + // The position and course controllers hold their last target and course until a new setpoint or a reset + _differential_pos_control.reset(); + _differential_course_control.reset(); } // Navigator stores and acks the speed, Guided Course applies it right away @@ -193,20 +198,31 @@ void RoverDifferential::generateSetpoints() void RoverDifferential::enableControllers(const bool enabled) { const bool pos_was_enabled = _differential_pos_control.enabled(); + const bool course_was_enabled = _differential_course_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_course_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled) + && registered; 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()) { + // Callbacks sharing a trigger topic run in registration order: keep position -> course -> speed controller + // (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity). + // A controller enabled after one below it moves all already enabled controllers below it behind itself. + const bool pos_newly_enabled = !pos_was_enabled && _differential_pos_control.enabled(); + const bool course_newly_enabled = !course_was_enabled && _differential_course_control.enabled(); + + if (pos_newly_enabled && course_was_enabled && _differential_course_control.enabled()) { + registered = _differential_course_control.reregisterTrigger() && registered; + } + + if ((pos_newly_enabled || course_newly_enabled) && speed_was_enabled && _differential_speed_control.enabled()) { registered = _differential_speed_control.reregisterTrigger() && registered; } @@ -239,7 +255,8 @@ void RoverDifferential::runSanityChecks() return; } - if (_vehicle_control_mode.flag_control_position_enabled && !_differential_pos_control.runSanityChecks()) { + if (_vehicle_control_mode.flag_control_position_enabled && (!_differential_course_control.runSanityChecks() + || !_differential_pos_control.runSanityChecks())) { _sanity_checks_passed = false; return; } @@ -250,6 +267,7 @@ void RoverDifferential::runSanityChecks() void RoverDifferential::reset() { _differential_pos_control.reset(); + _differential_course_control.reset(); _differential_speed_control.reset(); _differential_att_control.reset(); _differential_rate_control.reset(); diff --git a/src/modules/rover_differential/RoverDifferential.hpp b/src/modules/rover_differential/RoverDifferential.hpp index 683c86dd701..8e9727ef566 100644 --- a/src/modules/rover_differential/RoverDifferential.hpp +++ b/src/modules/rover_differential/RoverDifferential.hpp @@ -57,6 +57,7 @@ #include "DifferentialRateControl/DifferentialRateControl.hpp" #include "DifferentialAttControl/DifferentialAttControl.hpp" #include "DifferentialSpeedControl/DifferentialSpeedControl.hpp" +#include "DifferentialCourseControl/DifferentialCourseControl.hpp" #include "DifferentialPosControl/DifferentialPosControl.hpp" #include "DifferentialDriveModes/DifferentialAutoMode/DifferentialAutoMode.hpp" #include "DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.hpp" @@ -136,6 +137,7 @@ private: DifferentialRateControl _differential_rate_control{this}; DifferentialAttControl _differential_att_control{this}; DifferentialSpeedControl _differential_speed_control{this}; + DifferentialCourseControl _differential_course_control{this}; DifferentialPosControl _differential_pos_control{this}; DifferentialAutoMode _auto_mode{this}; DifferentialManualMode _manual_mode{this}; diff --git a/src/modules/rover_mecanum/CMakeLists.txt b/src/modules/rover_mecanum/CMakeLists.txt index 295910c7d4d..d149d9bd9fd 100644 --- a/src/modules/rover_mecanum/CMakeLists.txt +++ b/src/modules/rover_mecanum/CMakeLists.txt @@ -35,6 +35,7 @@ add_subdirectory(MecanumActControl) add_subdirectory(MecanumRateControl) add_subdirectory(MecanumAttControl) add_subdirectory(MecanumSpeedControl) +add_subdirectory(MecanumCourseControl) add_subdirectory(MecanumPosControl) add_subdirectory(MecanumDriveModes) @@ -49,6 +50,7 @@ px4_add_module( MecanumRateControl MecanumAttControl MecanumSpeedControl + MecanumCourseControl MecanumPosControl MecanumAutoMode MecanumManualMode diff --git a/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt b/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt new file mode 100644 index 00000000000..77149a5908b --- /dev/null +++ b/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt @@ -0,0 +1,39 @@ +############################################################################ +# +# Copyright (c) 2025 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. +# +############################################################################ + +px4_add_library(MecanumCourseControl + MecanumCourseControl.cpp +) + +target_link_libraries(MecanumCourseControl PUBLIC rover_control) +target_include_directories(MecanumCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp new file mode 100644 index 00000000000..3b55a8eea06 --- /dev/null +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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 "MecanumCourseControl.hpp" + +using namespace time_literals; + +MecanumCourseControl::MecanumCourseControl(ModuleParams *parent) : + ModuleParams(parent), + RoverControllerWorkItem("rover_mecanum_course_control", ORB_ID(vehicle_local_position)) +{ + _surface_vehicle_speed_setpoint_pub.advertise(); + _surface_vehicle_attitude_setpoint_pub.advertise(); + + updateParams(); +} + +void MecanumCourseControl::updateParams() +{ + ModuleParams::updateParams(); +} + +void MecanumCourseControl::updateCourseControl() +{ + updateSubscriptions(); + + // The last valid course setpoint is controlled until a NaN course is received + if (!PX4_ISFINITE(_course_setpoint) && !PX4_ISFINITE(_speed_setpoint)) { + return; + } + + const hrt_abstime timestamp = hrt_absolute_time(); + surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; + surface_vehicle_speed_setpoint.timestamp = timestamp; + + if (PX4_ISFINITE(_course_setpoint) && PX4_ISFINITE(_speed_setpoint)) { + // The rover drives along the course independent of its heading. + // The speed controller tracks the ground velocity in body frame, so a velocity along the course setpoint needs no drift angle correction. + const Vector3f velocity_in_local_frame(_speed_setpoint * cosf(_course_setpoint), _speed_setpoint * sinf(_course_setpoint), + 0.f); + const Vector3f velocity_in_body_frame = _vehicle_attitude_quaternion.rotateVectorInverse(velocity_in_local_frame); + surface_vehicle_speed_setpoint.x = velocity_in_body_frame(0); + surface_vehicle_speed_setpoint.y = velocity_in_body_frame(1); + + } else if (PX4_ISFINITE(_course_setpoint)) { + // No speed setpoint: the course is held with whatever thrust is commanded. + // The heading is turned so that the commanded thrust direction in body frame points along the course, + // e.g. thrust to the right drives along the course with the course on the left of the heading. + surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; + surface_vehicle_attitude_setpoint.timestamp = timestamp; + surface_vehicle_attitude_setpoint.yaw_setpoint = matrix::wrap_pi(_course_setpoint - _thrust_direction); + _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); + + // NaN deactivates the speed controller, the thrust then comes from elsewhere + surface_vehicle_speed_setpoint.x = NAN; + surface_vehicle_speed_setpoint.y = NAN; + + } else { + // Without a course the speed is still passed on along the body x axis, e.g. the position controller stopping at its target + surface_vehicle_speed_setpoint.x = _speed_setpoint; + surface_vehicle_speed_setpoint.y = 0.f; + } + + _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); +} + +void MecanumCourseControl::updateSubscriptions() +{ + if (_vehicle_attitude_sub.updated()) { + vehicle_attitude_s vehicle_attitude{}; + _vehicle_attitude_sub.copy(&vehicle_attitude); + _vehicle_attitude_quaternion = matrix::Quatf(vehicle_attitude.q); + } + + if (_surface_vehicle_thrust_setpoint_sub.updated()) { + surface_vehicle_thrust_setpoint_s surface_vehicle_thrust_setpoint{}; + _surface_vehicle_thrust_setpoint_sub.copy(&surface_vehicle_thrust_setpoint); + const float thrust_x = PX4_ISFINITE(surface_vehicle_thrust_setpoint.x) ? surface_vehicle_thrust_setpoint.x : 0.f; + const float thrust_y = PX4_ISFINITE(surface_vehicle_thrust_setpoint.y) ? surface_vehicle_thrust_setpoint.y : 0.f; + + // Without thrust the direction is kept, so the vehicle does not turn around when the thrust passes through zero + if (fabsf(thrust_x) > FLT_EPSILON || fabsf(thrust_y) > FLT_EPSILON) { + _thrust_direction = atan2f(thrust_y, thrust_x); + } + } + + if (_surface_vehicle_course_setpoint_sub.updated()) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); + _course_setpoint = surface_vehicle_course_setpoint.course; + _speed_setpoint = surface_vehicle_course_setpoint.speed; + } +} + +bool MecanumCourseControl::runSanityChecks() +{ + return true; +} diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp new file mode 100644 index 00000000000..9afcc61ba0d --- /dev/null +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp @@ -0,0 +1,121 @@ +/**************************************************************************** + * + * Copyright (c) 2025 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. + * + ****************************************************************************/ + +#pragma once + +// PX4 includes +#include + +// Library includes +#include +#include +#include +#include + +// uORB includes +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace matrix; + +/** + * @brief Class for mecanum course control. + * + * Sits between the position controller and the speed and attitude controllers. With a speed it turns the course over + * ground into a body frame velocity setpoint, independent of the heading. Without a speed it turns the heading so that + * the direction of the thrust commanded elsewhere (body x and y) points along the course, and deactivates the speed + * controller (NaN speed setpoint). The last valid course setpoint is controlled until a + * NaN course is received. With a NaN course the speed is passed on along the body x axis, with NaN course and speed + * the course controller stops publishing. + */ +class MecanumCourseControl : public ModuleParams, public RoverControllerWorkItem +{ +public: + /** + * @brief Constructor for MecanumCourseControl. + * @param parent The parent ModuleParams object. + */ + MecanumCourseControl(ModuleParams *parent); + ~MecanumCourseControl() = default; + + /** + * @brief Generate and publish surfaceVehicleSpeedSetpoint and, without a speed, surfaceVehicleAttitudeSetpoint from the last surfaceVehicleCourseSetpoint. + */ + void updateCourseControl(); + + /** + * @brief Check if the necessary parameters are set. + * @return True if all checks pass. + */ + bool runSanityChecks(); + + /** + * @brief Reset course controller. + */ + void reset() { _course_setpoint = NAN; _speed_setpoint = NAN; _thrust_direction = 0.f; } + +protected: + /** + * @brief Update the parameters of the module. + */ + void updateParams() override; + +private: + void runController() override { updateCourseControl(); } + + /** + * @brief Update uORB subscriptions used in course controller. + */ + void updateSubscriptions(); + + // uORB subscriptions + uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; + uORB::Subscription _surface_vehicle_thrust_setpoint_sub{ORB_ID(surface_vehicle_thrust_setpoint)}; + + // uORB publications + uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; + uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + + // Variables + Quatf _vehicle_attitude_quaternion{}; + float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received + float _speed_setpoint{NAN}; // Last received speed setpoint + float _thrust_direction{0.f}; // [rad] Direction of the last nonzero thrust setpoint in body frame, 0 forwards, pi/2 right +}; diff --git a/src/modules/rover_mecanum/MecanumDriveModes/MecanumAutoMode/MecanumAutoMode.cpp b/src/modules/rover_mecanum/MecanumDriveModes/MecanumAutoMode/MecanumAutoMode.cpp index b68c781893d..947f0d56e9b 100644 --- a/src/modules/rover_mecanum/MecanumDriveModes/MecanumAutoMode/MecanumAutoMode.cpp +++ b/src/modules/rover_mecanum/MecanumDriveModes/MecanumAutoMode/MecanumAutoMode.cpp @@ -135,7 +135,7 @@ void MecanumAutoMode::guidedCourse() heading) + vehicle_local_position.vy * sinf(heading), 0.f) : 0.f; } - // Streamed, so the course is held again after the position controller was reset on a control mode change + // Streamed straight to the course controller, so the course is held again after it was reset on a mode change surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; surface_vehicle_course_setpoint.timestamp = hrt_absolute_time(); // The navigator publishes the course in [0, 2pi) diff --git a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp index c7add4409f0..35bb2caadff 100644 --- a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp +++ b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.cpp @@ -39,9 +39,8 @@ 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(); - _surface_vehicle_attitude_setpoint_pub.advertise(); + _surface_vehicle_course_setpoint_pub.advertise(); updateParams(); } @@ -55,69 +54,70 @@ void MecanumPosControl::updatePosControl() { updateSubscriptions(); - hrt_abstime timestamp = hrt_absolute_time(); + const hrt_abstime timestamp = hrt_absolute_time(); - // Course control: like the fixed wing course hold, only the course over ground is controlled, there is no desired track to return to after a sideways offset - const bool course_control = PX4_ISFINITE(_course); - - if (course_control || _target_waypoint_ned.isAllFinite()) { - float distance_to_target = course_control ? 0.f : (_target_waypoint_ned - _curr_pos_ned).norm(); - - if (_arrival_speed > FLT_EPSILON) { - distance_to_target -= - _param_nav_acc_rad.get(); // shift target to the edge of the acceptance radius if arrival speed not zero + if (!_target_waypoint_ned.isAllFinite()) { + // Idle. Only stop the course controller when a position setpoint without target was received (e.g. Position + // mode while steering), not on every cycle, so a mode commanding the course controller directly (Guided Course) + // is not overridden. + if (_stop_course_control) { + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = NAN; + surface_vehicle_course_setpoint.yaw = NAN; + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); + _stop_course_control = false; } - if (course_control || distance_to_target > _param_nav_acc_rad.get() || _arrival_speed > FLT_EPSILON) { + return; + } - float speed_setpoint{_cruising_speed}; + float distance_to_target = (_target_waypoint_ned - _curr_pos_ned).norm(); - if (!course_control && _param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { - speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), - _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); - } + if (_arrival_speed > FLT_EPSILON) { + distance_to_target -= _param_nav_acc_rad.get(); // shift target to the edge of the acceptance radius if arrival speed not zero + } - // The speed controller tracks the ground velocity in body frame, so a velocity along the course setpoint needs no drift angle correction - float bearing_setpoint{_course}; + surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; + surface_vehicle_course_setpoint.timestamp = timestamp; + surface_vehicle_course_setpoint.yaw = NAN; - if (!course_control) { - pure_pursuit_status_s pure_pursuit_status{}; - pure_pursuit_status.timestamp = timestamp; + if (distance_to_target > _param_nav_acc_rad.get() || _arrival_speed > FLT_EPSILON) { + float speed_setpoint{_cruising_speed}; - bearing_setpoint = PurePursuit::calcTargetBearing(pure_pursuit_status, _param_pp_lookahd_gain.get(), - _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, _start_ned, - _curr_pos_ned, fabsf(speed_setpoint)); - _pure_pursuit_status_pub.publish(pure_pursuit_status); - } + if (_param_ro_decel_lim.get() > FLT_EPSILON && _param_ro_jerk_lim.get() > FLT_EPSILON) { + speed_setpoint = math::min(math::trajectory::computeMaxSpeedFromDistance(_param_ro_jerk_lim.get(), + _param_ro_decel_lim.get(), distance_to_target, fabsf(_arrival_speed)), _cruising_speed); + } - const Vector3f velocity_in_local_frame(speed_setpoint * cosf(bearing_setpoint), - speed_setpoint * sinf(bearing_setpoint), 0.f); - const Vector3f velocity_in_body_frame = _vehicle_attitude_quaternion.rotateVectorInverse(velocity_in_local_frame); + pure_pursuit_status_s pure_pursuit_status{}; + pure_pursuit_status.timestamp = timestamp; - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = velocity_in_body_frame(0); - surface_vehicle_speed_setpoint.y = velocity_in_body_frame(1); - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); + // Pure pursuit returns the direction of travel towards the lookahead point, which is the course setpoint + surface_vehicle_course_setpoint.course = PurePursuit::calcTargetBearing(pure_pursuit_status, + _param_pp_lookahd_gain.get(), _param_pp_lookahd_max.get(), _param_pp_lookahd_min.get(), _target_waypoint_ned, + _start_ned, _curr_pos_ned, fabsf(speed_setpoint)); + surface_vehicle_course_setpoint.speed = speed_setpoint; + _pure_pursuit_status_pub.publish(pure_pursuit_status); - } else { - surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; - surface_vehicle_speed_setpoint.timestamp = timestamp; - surface_vehicle_speed_setpoint.x = 0.f; - surface_vehicle_speed_setpoint.y = 0.f; - _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); + } else { + // Stop: no course to control anymore, only the speed + surface_vehicle_course_setpoint.course = NAN; + surface_vehicle_course_setpoint.speed = 0.f; - if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { - _stopped = true; - _target_waypoint_ned = _curr_pos_ned; - } + if (!_stopped && fabsf(_vehicle_speed) < FLT_EPSILON) { + _stopped = true; + _target_waypoint_ned = _curr_pos_ned; + } - if (_stopped && _updated_reset_counter != _reset_counter) { - _target_waypoint_ned = _curr_pos_ned; - _reset_counter = _updated_reset_counter; - } + if (_stopped && _updated_reset_counter != _reset_counter) { + _target_waypoint_ned = _curr_pos_ned; + _reset_counter = _updated_reset_counter; } } + + _surface_vehicle_course_setpoint_pub.publish(surface_vehicle_course_setpoint); } void MecanumPosControl::updateSubscriptions() @@ -148,19 +148,7 @@ void MecanumPosControl::updateSubscriptions() _cruising_speed = PX4_ISFINITE(surface_vehicle_position_setpoint.cruising_speed) ? surface_vehicle_position_setpoint.cruising_speed : _param_ro_speed_lim.get(); _target_waypoint_ned = Vector2f(surface_vehicle_position_setpoint.target_ned[0], surface_vehicle_position_setpoint.target_ned[1]); - _course = NAN; - _stopped = false; - } - - if (_surface_vehicle_course_setpoint_sub.updated()) { - surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint; - _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); - _start_ned = Vector2f(NAN, NAN); - _arrival_speed = 0.f; - _cruising_speed = PX4_ISFINITE(surface_vehicle_course_setpoint.speed) ? - surface_vehicle_course_setpoint.speed : _param_ro_speed_lim.get(); - _target_waypoint_ned = Vector2f(NAN, NAN); - _course = surface_vehicle_course_setpoint.course; + _stop_course_control = !_target_waypoint_ned.isAllFinite(); _stopped = false; } } diff --git a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp index 9aaeaac1027..12c43c401de 100644 --- a/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp +++ b/src/modules/rover_mecanum/MecanumPosControl/MecanumPosControl.hpp @@ -47,8 +47,6 @@ // uORB includes #include #include -#include -#include #include #include #include @@ -71,7 +69,7 @@ public: ~MecanumPosControl() = default; /** - * @brief Generate and publish surfaceVehicleSpeedSetpoint and surfaceVehicleAttitudeSetpoint from surfaceVehiclePositionSetpoint or surfaceVehicleCourseSetpoint. + * @brief Generate and publish surfaceVehicleCourseSetpoint (course and speed) from surfaceVehiclePositionSetpoint. */ void updatePosControl(); @@ -84,7 +82,7 @@ public: /** * @brief Reset position controller. */ - void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _course = NAN; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false;}; + void reset() {_start_ned = Vector2f{NAN, NAN}; _target_waypoint_ned = Vector2f{NAN, NAN}; _arrival_speed = 0.f; _cruising_speed = _param_ro_speed_lim.get(); _stopped = false; _stop_course_control = false;}; protected: @@ -106,23 +104,21 @@ private: uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; uORB::Subscription _surface_vehicle_position_setpoint_sub{ORB_ID(surface_vehicle_position_setpoint)}; - uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; // uORB publications - uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; uORB::Publication _pure_pursuit_status_pub{ORB_ID(pure_pursuit_status)}; - uORB::Publication _surface_vehicle_attitude_setpoint_pub{ORB_ID(surface_vehicle_attitude_setpoint)}; + uORB::Publication _surface_vehicle_course_setpoint_pub{ORB_ID(surface_vehicle_course_setpoint)}; // Variables Quatf _vehicle_attitude_quaternion{}; Vector2f _curr_pos_ned{}; Vector2f _start_ned{}; Vector2f _target_waypoint_ned{}; - float _course{NAN}; float _arrival_speed{0.f}; float _vehicle_speed{0.f}; float _cruising_speed{NAN}; bool _stopped{false}; + bool _stop_course_control{false}; // Publish a NaN course setpoint once, on a position setpoint without target uint8_t _reset_counter{0}; /**< counter for estimator resets in xy-direction */ uint8_t _updated_reset_counter{0}; /**< counter for estimator resets in xy-direction */ diff --git a/src/modules/rover_mecanum/RoverMecanum.cpp b/src/modules/rover_mecanum/RoverMecanum.cpp index db7f2967704..f3e951d328a 100644 --- a/src/modules/rover_mecanum/RoverMecanum.cpp +++ b/src/modules/rover_mecanum/RoverMecanum.cpp @@ -50,6 +50,7 @@ bool RoverMecanum::init() // 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_course_control.init() || !_mecanum_speed_control.init() || !_mecanum_att_control.init() || !_mecanum_rate_control.init() @@ -130,10 +131,14 @@ void RoverMecanum::generateSetpoints() vehicle_status_s vehicle_status{}; _vehicle_status_sub.copy(&vehicle_status); - // Each entry into Guided Course takes the speed the rover is driving at + // Every mode starts from scratch if (vehicle_status.nav_state != _nav_state) { _nav_state = vehicle_status.nav_state; + // Each entry into Guided Course takes the speed the rover is driving at _auto_mode.reset(); + // The position and course controllers hold their last target and course until a new setpoint or a reset + _mecanum_pos_control.reset(); + _mecanum_course_control.reset(); } // Navigator stores and acks the speed, Guided Course applies it right away @@ -193,20 +198,31 @@ void RoverMecanum::generateSetpoints() void RoverMecanum::enableControllers(const bool enabled) { const bool pos_was_enabled = _mecanum_pos_control.enabled(); + const bool course_was_enabled = _mecanum_course_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_course_control.setEnabled(enabled && _vehicle_control_mode.flag_control_position_enabled) + && registered; 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()) { + // Callbacks sharing a trigger topic run in registration order: keep position -> course -> speed controller + // (vehicle_local_position) and the rate controller ahead of the actuator controller (vehicle_angular_velocity). + // A controller enabled after one below it moves all already enabled controllers below it behind itself. + const bool pos_newly_enabled = !pos_was_enabled && _mecanum_pos_control.enabled(); + const bool course_newly_enabled = !course_was_enabled && _mecanum_course_control.enabled(); + + if (pos_newly_enabled && course_was_enabled && _mecanum_course_control.enabled()) { + registered = _mecanum_course_control.reregisterTrigger() && registered; + } + + if ((pos_newly_enabled || course_newly_enabled) && speed_was_enabled && _mecanum_speed_control.enabled()) { registered = _mecanum_speed_control.reregisterTrigger() && registered; } @@ -239,7 +255,8 @@ void RoverMecanum::runSanityChecks() return; } - if (_vehicle_control_mode.flag_control_position_enabled && !_mecanum_pos_control.runSanityChecks()) { + if (_vehicle_control_mode.flag_control_position_enabled && (!_mecanum_course_control.runSanityChecks() + || !_mecanum_pos_control.runSanityChecks())) { _sanity_checks_passed = false; return; } @@ -250,6 +267,7 @@ void RoverMecanum::runSanityChecks() void RoverMecanum::reset() { _mecanum_pos_control.reset(); + _mecanum_course_control.reset(); _mecanum_speed_control.reset(); _mecanum_att_control.reset(); _mecanum_rate_control.reset(); diff --git a/src/modules/rover_mecanum/RoverMecanum.hpp b/src/modules/rover_mecanum/RoverMecanum.hpp index 88de9d0615e..e1d10e31803 100644 --- a/src/modules/rover_mecanum/RoverMecanum.hpp +++ b/src/modules/rover_mecanum/RoverMecanum.hpp @@ -57,6 +57,7 @@ #include "MecanumRateControl/MecanumRateControl.hpp" #include "MecanumAttControl/MecanumAttControl.hpp" #include "MecanumSpeedControl/MecanumSpeedControl.hpp" +#include "MecanumCourseControl/MecanumCourseControl.hpp" #include "MecanumPosControl/MecanumPosControl.hpp" #include "MecanumDriveModes/MecanumAutoMode/MecanumAutoMode.hpp" #include "MecanumDriveModes/MecanumManualMode/MecanumManualMode.hpp" @@ -136,6 +137,7 @@ private: MecanumRateControl _mecanum_rate_control{this}; MecanumAttControl _mecanum_att_control{this}; MecanumSpeedControl _mecanum_speed_control{this}; + MecanumCourseControl _mecanum_course_control{this}; MecanumPosControl _mecanum_pos_control{this}; MecanumAutoMode _auto_mode{this}; MecanumManualMode _manual_mode{this};