mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-29 20:20:56 +08:00
feat(rover): add high level encoder support (#28848)
* feat(rover): add high level encoder support Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * fix(rover): add gz mixing interface to output encoder position Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * fix(rover): add wheel encoder to logged topics, high rate sensors Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * fix(rover): fix wheel encoder division by zero and stream iteration Corrected Roboclaw driver to initialize counts_per_rev, fixed wheel encoder stream to iterate over multiple wheels, and added trapezoidal integration to GZMixingInterfaceWheel. Removed unused RO_WHEEL_RAD from position controllers and restricted the wheel encoder stream to rover targets. Assisted-by: Qwen-Code Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com> Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * fix(rover): adjust wheel radius default for SITL Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * Update src/lib/rover_control/rovercontrol_params.yaml Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * Update src/lib/rover_control/rovercontrol_params.yaml Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * Update msg/WheelEncoders.msg Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * chore(rover): fixes to decimal and constrained flash mavlink message Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * chore(gz): update encoder counts reference Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * chore(rover): fix to outdated encoder pos reference * fix(rover): update uorb message to account for 4 wheel encoders Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * Update msg/WheelEncoders.msg Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * fix(roboclaw): add in encoder position, counts per rev. Msg desc update Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * chore(wheel_encoders): add description to top of msg file for docs Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * chore(sim): fixes to initialize wheel pos m, rename wheel_encoder to wheel_distance Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * Update src/lib/rover_control/rovercontrol_params.yaml Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * Update src/modules/mavlink/streams/WHEEL_DISTANCE.hpp Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * Update src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.cpp Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * Update src/modules/simulation/gz_bridge/GZMixingInterfaceWheel.hpp Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> * chore(rover): update wheel encoder msg, fixes to gz bridge Signed-off-by: dirksavage88 <dirksavage88@gmail.com> * chore(rover): Apply batched suggestions from code review Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com> --------- Signed-off-by: dirksavage88 <dirksavage88@gmail.com> Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com> Co-authored-by: Michael Fritsche <64464652+Frigiii@users.noreply.github.com>
This commit is contained in:
10 files changed
+192
-6
No files matched your search
@@ -32,6 +32,19 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "GZMixingInterfaceWheel.hpp"
|
||||
#include <lib/mathlib/mathlib.h>
|
||||
|
||||
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<int>(_mixing_output.outputFunction(channel)) - static_cast<int>(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<int64_t>(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);
|
||||
|
||||
@@ -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_s> _wheel_encoders_pub{ORB_ID(wheel_encoders)};
|
||||
};
|
||||
Reference in new issue
Block a user