diff --git a/msg/WheelEncoders.msg b/msg/WheelEncoders.msg index a4f3955dcb3..8646828e8e0 100644 --- a/msg/WheelEncoders.msg +++ b/msg/WheelEncoders.msg @@ -1,5 +1,23 @@ -uint64 timestamp # time since system start (microseconds) +# Wheel encoder readings +# +# Angular speed, unwrapped angle and raw encoder counts for up to four wheels. +# Published by the Roboclaw driver (one instance per controller, two encoder channels each) +# and by the Gazebo bridge in simulation. +# Streamed to MAVLink as WHEEL_DISTANCE, logged, and exported over uXRCE-DDS. +# +# Wheel order follows the PX4 motor index: entry i is the wheel driven by Motor(i+1) in the +# rover's actuator configuration. +# - Mecanum: 0 = front right, 1 = front left, 2 = rear right, 3 = rear left +# - Differential: 0 = right, 1 = left (2 and 3 unused) +# - Ackermann: 0 = drivetrain (motor or drive shaft, reported as wheel rotation), 1-3 unused +# +# Unused entries have counts_per_rev = 0. +# +# The WHEEL_DISTANCE stream supports a single publisher: with several instances (e.g. two Roboclaw +# controllers) they all fill the same slots and only the last updated instance is streamed. -# Two wheels: 0 right, 1 left -float32[2] wheel_speed # [rad/s] -float32[2] wheel_angle # [rad] +uint64 timestamp # time since system start (microseconds) +float32[4] wheel_speed # [rad/s] Wheel angular speed. Positive is forward rotation +float32[4] wheel_angle # [rad] Unwrapped wheel angle since boot, 2 * pi * encoder_counts / counts_per_rev +uint32[4] counts_per_rev # [counts/rev] Encoder counts per wheel revolution +int64[4] encoder_counts # The wheel position, in encoder counts since boot. Positive is forward rotation, negative is reverse rotation diff --git a/src/drivers/roboclaw/Roboclaw.cpp b/src/drivers/roboclaw/Roboclaw.cpp index 0c7ac9b38e6..e157cddef56 100644 --- a/src/drivers/roboclaw/Roboclaw.cpp +++ b/src/drivers/roboclaw/Roboclaw.cpp @@ -233,6 +233,10 @@ int Roboclaw::readEncoder(unsigned controller) int32_t position_left = swapBytesInt32(&buffer_position[4]); wheel_encoders_s wheel_encoders{}; + wheel_encoders.counts_per_rev[0] = _param_rbclw_counts_rev.get(); + wheel_encoders.counts_per_rev[1] = _param_rbclw_counts_rev.get(); + wheel_encoders.encoder_counts[0] = position_right; + wheel_encoders.encoder_counts[1] = position_left; wheel_encoders.wheel_speed[0] = static_cast(speed_right) / math::max(_param_rbclw_counts_rev.get(), int32_t{1}) * M_TWOPI_F; wheel_encoders.wheel_speed[1] = static_cast(speed_left) / math::max(_param_rbclw_counts_rev.get(), int32_t{1}) * M_TWOPI_F; wheel_encoders.wheel_angle[0] = static_cast(position_right) / math::max(_param_rbclw_counts_rev.get(), int32_t{1}) * M_TWOPI_F; diff --git a/src/lib/rover_control/rovercontrol_params.yaml b/src/lib/rover_control/rovercontrol_params.yaml index 50aa11b659a..739bfdf365a 100644 --- a/src/lib/rover_control/rovercontrol_params.yaml +++ b/src/lib/rover_control/rovercontrol_params.yaml @@ -263,3 +263,17 @@ parameters: max: 100 increment: 0.01 decimal: 2 + RO_WHEEL_RAD: + description: + short: Wheel radius in meters + long: |- + Used to convert wheel encoder counts to the distance travelled by each wheel + for the MAVLink WHEEL_DISTANCE message: + distance = 2 * pi * RO_WHEEL_RAD * encoder_counts / counts_per_rev + type: float + default: 0.1 + unit: m + min: 0.01 + max: 10 + increment: 0.001 + decimal: 3 diff --git a/src/modules/logger/logged_topics.cpp b/src/modules/logger/logged_topics.cpp index d8984e2bae6..20b6e81787c 100644 --- a/src/modules/logger/logged_topics.cpp +++ b/src/modules/logger/logged_topics.cpp @@ -429,6 +429,7 @@ void LoggedTopics::add_high_rate_sensors_topics() add_topic("vehicle_air_data", 10); add_topic("vehicle_magnetometer", 10); add_topic("vehicle_thrust_setpoint", 10); + add_topic("wheel_encoders", 10); } void LoggedTopics::add_mavlink_tunnel() diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index cfa5d1c4567..bb2de514880 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -1814,6 +1814,9 @@ constexpr StreamRateDefault streams_mode_normal[] = { {"VFR_HUD", 4.0f}, {"VIBRATION", 0.1f}, {"WIND_COV", 0.5f}, +#if defined(CONFIG_MODULES_ROVER_ACKERMANN) || defined(CONFIG_MODULES_ROVER_DIFFERENTIAL) || defined(CONFIG_MODULES_ROVER_MECANUM) + {"WHEEL_DISTANCE", 1.0f}, +#endif #if !defined(CONSTRAINED_FLASH) {"DEBUG", 1.0f}, @@ -1897,6 +1900,9 @@ constexpr StreamRateDefault streams_mode_onboard[] = { {"VFR_HUD", 10.0f}, {"VIBRATION", 0.5f}, {"WIND_COV", 10.0f}, +#if defined(CONFIG_MODULES_ROVER_ACKERMANN) || defined(CONFIG_MODULES_ROVER_DIFFERENTIAL) || defined(CONFIG_MODULES_ROVER_MECANUM) + {"WHEEL_DISTANCE", 1.0f}, +#endif #if !defined(CONSTRAINED_FLASH) {"DEBUG", 10.0f}, @@ -2072,6 +2078,9 @@ constexpr StreamRateDefault streams_mode_config[] = { {"VFR_HUD", 20.0f}, {"VIBRATION", 2.5f}, {"WIND_COV", 10.0f}, +#if defined(CONFIG_MODULES_ROVER_ACKERMANN) || defined(CONFIG_MODULES_ROVER_DIFFERENTIAL) || defined(CONFIG_MODULES_ROVER_MECANUM) + {"WHEEL_DISTANCE", 1.0f}, +#endif #if !defined(CONSTRAINED_FLASH) {"DEBUG", 50.0f}, diff --git a/src/modules/mavlink/mavlink_messages.cpp b/src/modules/mavlink/mavlink_messages.cpp index 1f1beedd3ae..399974798e1 100644 --- a/src/modules/mavlink/mavlink_messages.cpp +++ b/src/modules/mavlink/mavlink_messages.cpp @@ -169,6 +169,7 @@ # include "streams/SCALED_PRESSURE3.hpp" # include "streams/UAVIONIX_ADSB_OUT_CFG.hpp" # include "streams/UAVIONIX_ADSB_OUT_DYNAMIC.hpp" +# include "streams/WHEEL_DISTANCE.hpp" #endif // !CONSTRAINED_FLASH // ensure PX4 rotation enum and MAV_SENSOR_ROTATION align @@ -543,6 +544,10 @@ static constexpr StreamListItem streams_list[] = { #if defined(GLOBAL_POSITION_SENSOR_HPP) create_stream_list_item(), #endif // GLOBAL_POSITION_SENSOR_HPP +#if defined(WHEEL_DISTANCE_HPP) + create_stream_list_item(), +#endif // WHEEL_DISTANCE_HPP + }; const char *get_stream_name(const uint16_t msg_id) diff --git a/src/modules/mavlink/streams/WHEEL_DISTANCE.hpp b/src/modules/mavlink/streams/WHEEL_DISTANCE.hpp new file mode 100644 index 00000000000..7c24d0dc9bb --- /dev/null +++ b/src/modules/mavlink/streams/WHEEL_DISTANCE.hpp @@ -0,0 +1,72 @@ +#ifndef WHEEL_DISTANCE_HPP +#define WHEEL_DISTANCE_HPP + +#include +#include + +class MavlinkStreamWheelDistance : public MavlinkStream +{ +public: + static MavlinkStream *new_instance(Mavlink *mavlink) { return new MavlinkStreamWheelDistance(mavlink); } + + static constexpr const char *get_name_static() { return "WHEEL_DISTANCE"; } + static constexpr uint16_t get_id_static() { return MAVLINK_MSG_ID_WHEEL_DISTANCE; } + + const char *get_name() const override { return get_name_static(); } + uint16_t get_id() override { return get_id_static(); } + + unsigned get_size() override + { + return _wheel_encoders_subs.advertised() ? (MAVLINK_MSG_ID_WHEEL_DISTANCE_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES) : 0; + } +private: + explicit MavlinkStreamWheelDistance(Mavlink *mavlink) : MavlinkStream(mavlink) + { + _param_ro_wheel_rad = param_find("RO_WHEEL_RAD"); + } + + static constexpr int NUM_WHEELS = sizeof(wheel_encoders_s::counts_per_rev) / sizeof(wheel_encoders_s::counts_per_rev[0]); + + uORB::SubscriptionMultiArray _wheel_encoders_subs{ORB_ID::wheel_encoders}; + param_t _param_ro_wheel_rad{PARAM_INVALID}; + + // Only one publisher is supported: every instance fills the same slots, so the last updated one wins + bool send() override + { + bool updated = false; + + mavlink_wheel_distance_t msg{}; + float wheel_radius = 0.f; // Wheel radius in meters + param_get(_param_ro_wheel_rad, &wheel_radius); + int count = 0; + + for (int i = 0; i < _wheel_encoders_subs.size(); i++) { + wheel_encoders_s wheel_encoders; + + if (_wheel_encoders_subs[i].update(&wheel_encoders)) { + + msg.time_usec = wheel_encoders.timestamp; + + for (int j = 0; j < NUM_WHEELS; j++) { + if (wheel_encoders.counts_per_rev[j] > 0) { + msg.distance[j] = (double)2.0 * (double)M_PI_F * (double)wheel_radius * wheel_encoders.encoder_counts[j] / + (double)wheel_encoders.counts_per_rev[j]; + count = j + 1; + } + } + } + } + + if (count > 0) { + msg.count = count; + mavlink_msg_wheel_distance_send_struct(_mavlink->get_channel(), &msg); + + updated = true; + } + + return updated; + + } +}; + +#endif // WHEEL_DISTANCE_HPP diff --git a/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.cpp b/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.cpp index 3c1af2633c4..dba0aebdd81 100644 --- a/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.cpp +++ b/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.cpp @@ -32,6 +32,19 @@ ****************************************************************************/ #include "GZMixingInterfaceWheel.hpp" +#include + +int GZMixingInterfaceWheel::wheelSlot(int channel) const +{ + if (!_mixing_output.isMotor(channel)) { + return -1; + } + + // WheelEncoders entry i is the wheel driven by Motor(i+1), whichever channel it is assigned to + const int motor = static_cast(_mixing_output.outputFunction(channel)) - static_cast(OutputFunction::Motor1); + + return (motor < MAX_WHEELS) ? motor : -1; +} bool GZMixingInterfaceWheel::init(const std::string &model_name) { @@ -112,14 +125,46 @@ void GZMixingInterfaceWheel::wheelSpeedCallback(const gz::msgs::Actuators &actua return; } + hrt_abstime current_t = hrt_absolute_time(); + pthread_mutex_lock(&_node_mutex); wheel_encoders_s wheel_encoders{}; - for (int i = 0; i < actuators.velocity_size(); i++) { - wheel_encoders.wheel_speed[i] = (float)actuators.velocity(i); + // Velocity i belongs to output channel i, see updateOutputs() + const int num_channels = math::min(actuators.velocity_size(), MAX_ACTUATORS); + + // No integration on the first callback, there is no previous speed yet + const float dt = (_prev_t == 0) ? 0.f : (current_t - _prev_t) * 1e-6f; + + for (int i = 0; i < num_channels; i++) { + const int slot = wheelSlot(i); + + if (slot < 0) { + continue; + } + + const float wheel_speed = (float)actuators.velocity(i); + + if (_prev_t == 0) { + _prev_wheel_speed[slot] = wheel_speed; + } + + // Trapezoidal integration of the wheel speed to the unwrapped wheel angle + _wheel_angle[slot] += 0.5f * (wheel_speed + _prev_wheel_speed[slot]) * dt; + _prev_wheel_speed[slot] = wheel_speed; + + // Quantize like a real encoder + const int64_t encoder_counts = static_cast(floorf(_wheel_angle[slot] * SIM_COUNTS_PER_REV / M_TWOPI_F)); + + wheel_encoders.wheel_speed[slot] = wheel_speed; + wheel_encoders.counts_per_rev[slot] = SIM_COUNTS_PER_REV; + wheel_encoders.encoder_counts[slot] = encoder_counts; + wheel_encoders.wheel_angle[slot] = encoder_counts * M_TWOPI_F / SIM_COUNTS_PER_REV; } + _prev_t = current_t; + if (actuators.velocity_size() > 0) { wheel_encoders.timestamp = hrt_absolute_time(); _wheel_encoders_pub.publish(wheel_encoders); diff --git a/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.hpp b/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.hpp index 7b408894884..98347d62c74 100644 --- a/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.hpp +++ b/src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.hpp @@ -78,9 +78,23 @@ private: gz::transport::Node &_node; pthread_mutex_t _node_mutex; + // wheel_encoders slot for the wheel motor on output channel `channel`, following the wheel + // order in WheelEncoders.msg. -1 if the channel is not a wheel motor or has no slot. + int wheelSlot(int channel) const; + MixingOutput _mixing_output{"SIM_GZ_WH", MAX_ACTUATORS, *this, MixingOutput::SchedulingPolicy::Auto, false, false}; gz::transport::Node::Publisher _actuators_pub; + hrt_abstime _prev_t{0}; + + // One entry per wheel_encoders slot, independent of which rover modules are built + static constexpr int MAX_WHEELS = sizeof(wheel_encoders_s::wheel_speed) / sizeof(wheel_encoders_s::wheel_speed[0]); + + // Resolution of the simulated encoders, in the range of typical quadrature encoders + static constexpr uint32_t SIM_COUNTS_PER_REV = 1024; + + float _prev_wheel_speed[MAX_WHEELS] {}; // [rad/s] + float _wheel_angle[MAX_WHEELS] {}; // [rad] Unwrapped wheel angle since the first callback uORB::Publication _wheel_encoders_pub{ORB_ID(wheel_encoders)}; }; diff --git a/src/modules/uxrce_dds_client/dds_topics.yaml b/src/modules/uxrce_dds_client/dds_topics.yaml index d70c0b97c34..f57a19b7d71 100644 --- a/src/modules/uxrce_dds_client/dds_topics.yaml +++ b/src/modules/uxrce_dds_client/dds_topics.yaml @@ -123,6 +123,10 @@ publications: type: px4_msgs::msg::InputRc rate_limit: 10. + - topic: /fmu/out/wheel_encoders + type: px4_msgs::msg::WheelEncoders + rate_limit: 10. + # Create uORB::Publication subscriptions: - topic: /fmu/in/register_ext_component_request