mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-14 13:21:36 +08:00
mavlink: Templated SCALED_IMU mavlink stream
This commit is contained in:
@@ -125,6 +125,7 @@ using matrix::wrap_2pi;
|
||||
#include "streams/PROTOCOL_VERSION.hpp"
|
||||
#include "streams/RAW_RPM.hpp"
|
||||
#include "streams/RC_CHANNELS.hpp"
|
||||
#include "streams/SCALED_IMU.hpp"
|
||||
#include "streams/STATUSTEXT.hpp"
|
||||
#include "streams/STORAGE_INFORMATION.hpp"
|
||||
#include "streams/TRAJECTORY_REPRESENTATION_WAYPOINTS.hpp"
|
||||
@@ -1193,263 +1194,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class MavlinkStreamScaledIMU : public MavlinkStream
|
||||
{
|
||||
public:
|
||||
const char *get_name() const override
|
||||
{
|
||||
return MavlinkStreamScaledIMU::get_name_static();
|
||||
}
|
||||
|
||||
static const char *get_name_static()
|
||||
{
|
||||
return "SCALED_IMU";
|
||||
}
|
||||
|
||||
static uint16_t get_id_static()
|
||||
{
|
||||
return MAVLINK_MSG_ID_SCALED_IMU;
|
||||
}
|
||||
|
||||
uint16_t get_id() override
|
||||
{
|
||||
return get_id_static();
|
||||
}
|
||||
|
||||
static MavlinkStream *new_instance(Mavlink *mavlink)
|
||||
{
|
||||
return new MavlinkStreamScaledIMU(mavlink);
|
||||
}
|
||||
|
||||
unsigned get_size() override
|
||||
{
|
||||
return _raw_imu_sub.advertised() ? (MAVLINK_MSG_ID_SCALED_IMU_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES) : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uORB::Subscription _raw_imu_sub{ORB_ID(vehicle_imu), 0};
|
||||
uORB::Subscription _raw_mag_sub{ORB_ID(sensor_mag), 0};
|
||||
|
||||
// do not allow top copy this class
|
||||
MavlinkStreamScaledIMU(MavlinkStreamScaledIMU &) = delete;
|
||||
MavlinkStreamScaledIMU &operator = (const MavlinkStreamScaledIMU &) = delete;
|
||||
|
||||
protected:
|
||||
explicit MavlinkStreamScaledIMU(Mavlink *mavlink) : MavlinkStream(mavlink)
|
||||
{}
|
||||
|
||||
bool send() override
|
||||
{
|
||||
if (_raw_imu_sub.updated() || _raw_mag_sub.updated()) {
|
||||
|
||||
vehicle_imu_s imu{};
|
||||
_raw_imu_sub.copy(&imu);
|
||||
|
||||
sensor_mag_s sensor_mag{};
|
||||
_raw_mag_sub.copy(&sensor_mag);
|
||||
|
||||
mavlink_scaled_imu_t msg{};
|
||||
|
||||
msg.time_boot_ms = imu.timestamp / 1000;
|
||||
|
||||
// Accelerometer in mG
|
||||
const float accel_dt_inv = 1.e6f / (float)imu.delta_velocity_dt;
|
||||
const Vector3f accel = Vector3f{imu.delta_velocity} * accel_dt_inv * 1000.0f / CONSTANTS_ONE_G;
|
||||
|
||||
// Gyroscope in mrad/s
|
||||
const float gyro_dt_inv = 1.e6f / (float)imu.delta_angle_dt;
|
||||
const Vector3f gyro = Vector3f{imu.delta_angle} * gyro_dt_inv * 1000.0f;
|
||||
|
||||
msg.xacc = (int16_t)accel(0);
|
||||
msg.yacc = (int16_t)accel(1);
|
||||
msg.zacc = (int16_t)accel(2);
|
||||
msg.xgyro = gyro(0);
|
||||
msg.ygyro = gyro(1);
|
||||
msg.zgyro = gyro(2);
|
||||
msg.xmag = sensor_mag.x * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.ymag = sensor_mag.y * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.zmag = sensor_mag.z * 1000.0f; // Gauss -> MilliGauss
|
||||
|
||||
mavlink_msg_scaled_imu_send_struct(_mavlink->get_channel(), &msg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class MavlinkStreamScaledIMU2 : public MavlinkStream
|
||||
{
|
||||
public:
|
||||
const char *get_name() const override
|
||||
{
|
||||
return MavlinkStreamScaledIMU2::get_name_static();
|
||||
}
|
||||
|
||||
static constexpr const char *get_name_static()
|
||||
{
|
||||
return "SCALED_IMU2";
|
||||
}
|
||||
|
||||
static constexpr uint16_t get_id_static()
|
||||
{
|
||||
return MAVLINK_MSG_ID_SCALED_IMU2;
|
||||
}
|
||||
|
||||
uint16_t get_id() override
|
||||
{
|
||||
return get_id_static();
|
||||
}
|
||||
|
||||
static MavlinkStream *new_instance(Mavlink *mavlink)
|
||||
{
|
||||
return new MavlinkStreamScaledIMU2(mavlink);
|
||||
}
|
||||
|
||||
unsigned get_size() override
|
||||
{
|
||||
return _raw_imu_sub.advertised() ? (MAVLINK_MSG_ID_SCALED_IMU2_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES) : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uORB::Subscription _raw_imu_sub{ORB_ID(vehicle_imu), 1};
|
||||
uORB::Subscription _raw_mag_sub{ORB_ID(sensor_mag), 1};
|
||||
|
||||
// do not allow top copy this class
|
||||
MavlinkStreamScaledIMU2(MavlinkStreamScaledIMU2 &) = delete;
|
||||
MavlinkStreamScaledIMU2 &operator = (const MavlinkStreamScaledIMU2 &) = delete;
|
||||
|
||||
protected:
|
||||
explicit MavlinkStreamScaledIMU2(Mavlink *mavlink) : MavlinkStream(mavlink)
|
||||
{}
|
||||
|
||||
bool send() override
|
||||
{
|
||||
if (_raw_imu_sub.updated() || _raw_mag_sub.updated()) {
|
||||
|
||||
vehicle_imu_s imu{};
|
||||
_raw_imu_sub.copy(&imu);
|
||||
|
||||
sensor_mag_s sensor_mag{};
|
||||
_raw_mag_sub.copy(&sensor_mag);
|
||||
|
||||
mavlink_scaled_imu2_t msg{};
|
||||
|
||||
msg.time_boot_ms = imu.timestamp / 1000;
|
||||
|
||||
// Accelerometer in mG
|
||||
const float accel_dt_inv = 1.e6f / (float)imu.delta_velocity_dt;
|
||||
const Vector3f accel = Vector3f{imu.delta_velocity} * accel_dt_inv * 1000.0f / CONSTANTS_ONE_G;
|
||||
|
||||
// Gyroscope in mrad/s
|
||||
const float gyro_dt_inv = 1.e6f / (float)imu.delta_angle_dt;
|
||||
const Vector3f gyro = Vector3f{imu.delta_angle} * gyro_dt_inv * 1000.0f;
|
||||
|
||||
msg.xacc = (int16_t)accel(0);
|
||||
msg.yacc = (int16_t)accel(1);
|
||||
msg.zacc = (int16_t)accel(2);
|
||||
msg.xgyro = gyro(0);
|
||||
msg.ygyro = gyro(1);
|
||||
msg.zgyro = gyro(2);
|
||||
msg.xmag = sensor_mag.x * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.ymag = sensor_mag.y * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.zmag = sensor_mag.z * 1000.0f; // Gauss -> MilliGauss
|
||||
|
||||
mavlink_msg_scaled_imu2_send_struct(_mavlink->get_channel(), &msg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class MavlinkStreamScaledIMU3 : public MavlinkStream
|
||||
{
|
||||
public:
|
||||
const char *get_name() const override
|
||||
{
|
||||
return MavlinkStreamScaledIMU3::get_name_static();
|
||||
}
|
||||
static constexpr const char *get_name_static()
|
||||
{
|
||||
return "SCALED_IMU3";
|
||||
}
|
||||
|
||||
static constexpr uint16_t get_id_static()
|
||||
{
|
||||
return MAVLINK_MSG_ID_SCALED_IMU3;
|
||||
}
|
||||
|
||||
uint16_t get_id() override
|
||||
{
|
||||
return get_id_static();
|
||||
}
|
||||
|
||||
static MavlinkStream *new_instance(Mavlink *mavlink)
|
||||
{
|
||||
return new MavlinkStreamScaledIMU3(mavlink);
|
||||
}
|
||||
|
||||
unsigned get_size() override
|
||||
{
|
||||
return _raw_imu_sub.advertised() ? (MAVLINK_MSG_ID_SCALED_IMU3_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES) : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uORB::Subscription _raw_imu_sub{ORB_ID(vehicle_imu), 2};
|
||||
uORB::Subscription _raw_mag_sub{ORB_ID(sensor_mag), 2};
|
||||
|
||||
// do not allow top copy this class
|
||||
MavlinkStreamScaledIMU3(MavlinkStreamScaledIMU3 &) = delete;
|
||||
MavlinkStreamScaledIMU3 &operator = (const MavlinkStreamScaledIMU3 &) = delete;
|
||||
|
||||
protected:
|
||||
explicit MavlinkStreamScaledIMU3(Mavlink *mavlink) : MavlinkStream(mavlink)
|
||||
{}
|
||||
|
||||
bool send() override
|
||||
{
|
||||
if (_raw_imu_sub.updated() || _raw_mag_sub.updated()) {
|
||||
|
||||
vehicle_imu_s imu{};
|
||||
_raw_imu_sub.copy(&imu);
|
||||
|
||||
sensor_mag_s sensor_mag{};
|
||||
_raw_mag_sub.copy(&sensor_mag);
|
||||
|
||||
mavlink_scaled_imu3_t msg{};
|
||||
|
||||
msg.time_boot_ms = imu.timestamp / 1000;
|
||||
|
||||
// Accelerometer in mG
|
||||
const float accel_dt_inv = 1.e6f / (float)imu.delta_velocity_dt;
|
||||
const Vector3f accel = Vector3f{imu.delta_velocity} * accel_dt_inv * 1000.0f / CONSTANTS_ONE_G;
|
||||
|
||||
// Gyroscope in mrad/s
|
||||
const float gyro_dt_inv = 1.e6f / (float)imu.delta_angle_dt;
|
||||
const Vector3f gyro = Vector3f{imu.delta_angle} * gyro_dt_inv * 1000.0f;
|
||||
|
||||
msg.xacc = (int16_t)accel(0);
|
||||
msg.yacc = (int16_t)accel(1);
|
||||
msg.zacc = (int16_t)accel(2);
|
||||
msg.xgyro = gyro(0);
|
||||
msg.ygyro = gyro(1);
|
||||
msg.zgyro = gyro(2);
|
||||
msg.xmag = sensor_mag.x * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.ymag = sensor_mag.y * 1000.0f; // Gauss -> MilliGauss
|
||||
msg.zmag = sensor_mag.z * 1000.0f; // Gauss -> MilliGauss
|
||||
|
||||
mavlink_msg_scaled_imu3_send_struct(_mavlink->get_channel(), &msg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class MavlinkStreamVFRHUD : public MavlinkStream
|
||||
{
|
||||
public:
|
||||
@@ -3254,9 +2998,11 @@ static const StreamListItem streams_list[] = {
|
||||
create_stream_list_item<MavlinkStreamBatteryStatus>(),
|
||||
create_stream_list_item<MavlinkStreamSmartBatteryInfo>(),
|
||||
create_stream_list_item<MavlinkStreamHighresIMU>(),
|
||||
create_stream_list_item<MavlinkStreamScaledIMU>(),
|
||||
create_stream_list_item<MavlinkStreamScaledIMU2>(),
|
||||
create_stream_list_item<MavlinkStreamScaledIMU3>(),
|
||||
#if defined(SCALED_IMU_HPP)
|
||||
create_stream_list_item<MavlinkStreamScaledIMU<0> >(),
|
||||
create_stream_list_item<MavlinkStreamScaledIMU<1> >(),
|
||||
create_stream_list_item<MavlinkStreamScaledIMU<2> >(),
|
||||
#endif // SCALED_IMU_HPP
|
||||
create_stream_list_item<MavlinkStreamScaledPressure<0> >(),
|
||||
// create_stream_list_item<MavlinkStreamScaledPressure<1> >(),
|
||||
// create_stream_list_item<MavlinkStreamScaledPressure<2> >(),
|
||||
|
||||
Reference in New Issue
Block a user