2020-12-27 22:04:13 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
2023-10-11 04:41:52 -03:00
|
|
|
support for serial connected AHRS systems
|
2020-12-27 22:04:13 -04:00
|
|
|
*/
|
|
|
|
|
2023-04-24 06:54:51 -03:00
|
|
|
#include "AP_ExternalAHRS_config.h"
|
|
|
|
|
|
|
|
#if HAL_EXTERNAL_AHRS_ENABLED
|
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
#include "AP_ExternalAHRS.h"
|
2023-04-24 06:54:51 -03:00
|
|
|
#include "AP_ExternalAHRS_backend.h"
|
2021-03-24 03:24:01 -03:00
|
|
|
#include "AP_ExternalAHRS_VectorNav.h"
|
2023-08-18 16:14:40 -03:00
|
|
|
#include "AP_ExternalAHRS_MicroStrain5.h"
|
2023-09-23 16:42:35 -03:00
|
|
|
#include "AP_ExternalAHRS_MicroStrain7.h"
|
2023-11-30 18:00:30 -04:00
|
|
|
#include "AP_ExternalAHRS_InertialLabs.h"
|
2020-12-27 22:04:13 -04:00
|
|
|
|
2021-03-24 03:24:01 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2023-12-01 17:59:27 -04:00
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2023-12-02 04:32:26 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2020-12-27 22:04:13 -04:00
|
|
|
|
2021-03-24 03:24:01 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2020-12-27 22:04:13 -04:00
|
|
|
|
|
|
|
AP_ExternalAHRS *AP_ExternalAHRS::_singleton;
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
AP_ExternalAHRS::AP_ExternalAHRS()
|
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
_singleton = this;
|
2021-02-06 02:24:39 -04:00
|
|
|
if (rate.get() < 50) {
|
|
|
|
// min 50Hz
|
|
|
|
rate.set(50);
|
|
|
|
}
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:08:05 -04:00
|
|
|
#ifndef HAL_EXTERNAL_AHRS_DEFAULT
|
|
|
|
#define HAL_EXTERNAL_AHRS_DEFAULT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
// table of user settable parameters
|
|
|
|
const AP_Param::GroupInfo AP_ExternalAHRS::var_info[] = {
|
|
|
|
|
|
|
|
// @Param: _TYPE
|
|
|
|
// @DisplayName: AHRS type
|
|
|
|
// @Description: Type of AHRS device
|
2023-12-24 09:13:13 -04:00
|
|
|
// @Values: 0:None,1:VectorNav,2:MicroStrain5,5:InertialLabs,7:MicroStrain7
|
2020-12-27 22:04:13 -04:00
|
|
|
// @User: Standard
|
2021-01-13 17:08:05 -04:00
|
|
|
AP_GROUPINFO_FLAGS("_TYPE", 1, AP_ExternalAHRS, devtype, HAL_EXTERNAL_AHRS_DEFAULT, AP_PARAM_FLAG_ENABLE),
|
2020-12-27 22:04:13 -04:00
|
|
|
|
2021-02-06 02:24:39 -04:00
|
|
|
// @Param: _RATE
|
|
|
|
// @DisplayName: AHRS data rate
|
|
|
|
// @Description: Requested rate for AHRS device
|
|
|
|
// @Units: Hz
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("_RATE", 2, AP_ExternalAHRS, rate, 50),
|
2022-12-22 17:16:10 -04:00
|
|
|
|
|
|
|
// @Param: _OPTIONS
|
|
|
|
// @DisplayName: External AHRS options
|
|
|
|
// @Description: External AHRS options bitmask
|
|
|
|
// @Bitmask: 0:Vector Nav use uncompensated values for accel gyro and mag.
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("_OPTIONS", 3, AP_ExternalAHRS, options, 0),
|
|
|
|
|
2023-01-24 00:39:49 -04:00
|
|
|
// @Param: _SENSORS
|
|
|
|
// @DisplayName: External AHRS sensors
|
|
|
|
// @Description: External AHRS sensors bitmask
|
|
|
|
// @Bitmask: 0:GPS,1:IMU,2:Baro,3:Compass
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("_SENSORS", 4, AP_ExternalAHRS, sensors, 0xF),
|
2023-12-02 04:32:26 -04:00
|
|
|
|
|
|
|
// @Param: _LOG_RATE
|
|
|
|
// @DisplayName: AHRS logging rate
|
|
|
|
// @Description: Logging rate for EARHS devices
|
|
|
|
// @Units: Hz
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("_LOG_RATE", 5, AP_ExternalAHRS, log_rate, 10),
|
2023-01-24 00:39:49 -04:00
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void AP_ExternalAHRS::init(void)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (rate.get() < 50) {
|
|
|
|
// min 50Hz
|
|
|
|
rate.set(50);
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 03:24:01 -03:00
|
|
|
switch (DevType(devtype)) {
|
2021-04-13 23:29:34 -03:00
|
|
|
case DevType::None:
|
|
|
|
// nothing to do
|
2023-08-26 00:07:54 -03:00
|
|
|
return;
|
2023-11-30 18:00:30 -04:00
|
|
|
|
2023-04-24 06:54:51 -03:00
|
|
|
#if AP_EXTERNAL_AHRS_VECTORNAV_ENABLED
|
2021-03-24 03:24:01 -03:00
|
|
|
case DevType::VecNav:
|
|
|
|
backend = new AP_ExternalAHRS_VectorNav(this, state);
|
2023-08-26 00:07:54 -03:00
|
|
|
return;
|
2023-04-24 06:54:51 -03:00
|
|
|
#endif
|
2023-11-30 18:00:30 -04:00
|
|
|
|
2023-08-18 16:14:40 -03:00
|
|
|
#if AP_EXTERNAL_AHRS_MICROSTRAIN5_ENABLED
|
|
|
|
case DevType::MicroStrain5:
|
|
|
|
backend = new AP_ExternalAHRS_MicroStrain5(this, state);
|
2023-08-26 00:07:54 -03:00
|
|
|
return;
|
2023-09-23 16:42:35 -03:00
|
|
|
#endif
|
2023-11-30 18:00:30 -04:00
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
#if AP_EXTERNAL_AHRS_MICROSTRAIN7_ENABLED
|
|
|
|
case DevType::MicroStrain7:
|
|
|
|
backend = new AP_ExternalAHRS_MicroStrain7(this, state);
|
|
|
|
return;
|
2023-04-24 06:54:51 -03:00
|
|
|
#endif
|
2023-11-30 18:00:30 -04:00
|
|
|
|
|
|
|
#if AP_EXTERNAL_AHRS_INERTIAL_LABS_ENABLED
|
|
|
|
case DevType::InertialLabs:
|
|
|
|
backend = new AP_ExternalAHRS_InertialLabs(this, state);
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
2023-08-26 00:07:54 -03:00
|
|
|
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Unsupported ExternalAHRS type %u", unsigned(devtype));
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2022-12-21 09:25:05 -04:00
|
|
|
bool AP_ExternalAHRS::enabled() const
|
|
|
|
{
|
|
|
|
return DevType(devtype) != DevType::None;
|
|
|
|
}
|
|
|
|
|
2021-03-24 03:24:01 -03:00
|
|
|
// get serial port number for the uart, or -1 if not applicable
|
2023-01-24 00:39:49 -04:00
|
|
|
int8_t AP_ExternalAHRS::get_port(AvailableSensor sensor) const
|
2020-12-27 22:04:13 -04:00
|
|
|
{
|
2023-01-24 00:39:49 -04:00
|
|
|
if (!backend || !has_sensor(sensor)) {
|
2020-12-27 22:04:13 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
return backend->get_port();
|
2020-12-27 22:04:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// accessors for AP_AHRS
|
|
|
|
bool AP_ExternalAHRS::healthy(void) const
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
return backend && backend->healthy();
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:26:27 -04:00
|
|
|
bool AP_ExternalAHRS::initialised(void) const
|
2020-12-27 22:04:13 -04:00
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
return backend && backend->initialised();
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::get_quaternion(Quaternion &quat)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (state.have_quaternion) {
|
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
quat = state.quat;
|
|
|
|
return true;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
return false;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::get_origin(Location &loc)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (state.have_origin) {
|
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
loc = state.origin;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::get_location(Location &loc)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (!state.have_location) {
|
2020-12-27 22:04:13 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
loc = state.location;
|
2023-12-01 21:10:08 -04:00
|
|
|
|
|
|
|
if (state.last_location_update_us != 0 &&
|
|
|
|
state.have_velocity) {
|
|
|
|
// extrapolate position based on velocity to cope with slow backends
|
|
|
|
const float dt = (AP_HAL::micros() - state.last_location_update_us)*1.0e-6;
|
|
|
|
if (dt < 1) {
|
|
|
|
// only extrapolate for 1s max
|
|
|
|
Vector3p ofs = state.velocity.topostype();
|
|
|
|
ofs *= dt;
|
|
|
|
loc.offset(ofs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2f AP_ExternalAHRS::get_groundspeed_vector()
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
Vector2f vec{state.velocity.x, state.velocity.y};
|
|
|
|
return vec;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::get_velocity_NED(Vector3f &vel)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (!state.have_velocity) {
|
2020-12-27 22:04:13 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
vel = state.velocity;
|
2020-12-27 22:04:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::get_speed_down(float &speedD)
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (!state.have_velocity) {
|
2020-12-27 22:04:13 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
speedD = state.velocity.z;
|
2020-12-27 22:04:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS::pre_arm_check(char *failure_msg, uint8_t failure_msg_len) const
|
|
|
|
{
|
2023-09-24 00:25:06 -03:00
|
|
|
if (backend == nullptr) {
|
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "ExternalAHRS: Invalid backend");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:38:55 -04:00
|
|
|
if (!state.have_origin) {
|
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "ExternalAHRS: No origin");
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-24 00:25:06 -03:00
|
|
|
return backend->pre_arm_check(failure_msg, failure_msg_len);
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-03-24 03:24:01 -03:00
|
|
|
get filter status
|
2020-12-27 22:04:13 -04:00
|
|
|
*/
|
|
|
|
void AP_ExternalAHRS::get_filter_status(nav_filter_status &status) const
|
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
status = {};
|
|
|
|
if (backend) {
|
|
|
|
backend->get_filter_status(status);
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:20:39 -04:00
|
|
|
bool AP_ExternalAHRS::get_gyro(Vector3f &gyro)
|
2020-12-27 22:04:13 -04:00
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
2024-02-26 18:20:39 -04:00
|
|
|
if (!has_sensor(AvailableSensor::IMU)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gyro = state.gyro;
|
|
|
|
return true;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2024-02-26 18:20:39 -04:00
|
|
|
bool AP_ExternalAHRS::get_accel(Vector3f &accel)
|
2020-12-27 22:04:13 -04:00
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
2024-02-26 18:20:39 -04:00
|
|
|
if (!has_sensor(AvailableSensor::IMU)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
accel = state.accel;
|
|
|
|
return true;
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send an EKF_STATUS message to GCS
|
2022-07-08 01:15:35 -03:00
|
|
|
void AP_ExternalAHRS::send_status_report(GCS_MAVLINK &link) const
|
2020-12-27 22:04:13 -04:00
|
|
|
{
|
2021-03-24 03:24:01 -03:00
|
|
|
if (backend) {
|
2022-07-08 01:15:35 -03:00
|
|
|
backend->send_status_report(link);
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
2021-03-24 03:24:01 -03:00
|
|
|
}
|
2020-12-27 22:04:13 -04:00
|
|
|
|
2021-03-24 03:24:01 -03:00
|
|
|
void AP_ExternalAHRS::update(void)
|
|
|
|
{
|
|
|
|
if (backend) {
|
|
|
|
backend->update();
|
|
|
|
}
|
2023-12-01 17:59:27 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
if backend has not supplied an origin and AHRS has an origin
|
|
|
|
then use that origin so we get a common origin for minimum
|
|
|
|
disturbance when switching backends
|
|
|
|
*/
|
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
if (!state.have_origin) {
|
|
|
|
Location origin;
|
|
|
|
if (AP::ahrs().get_origin(origin)) {
|
|
|
|
state.origin = origin;
|
|
|
|
state.have_origin = true;
|
|
|
|
}
|
|
|
|
}
|
2024-03-03 08:56:08 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2023-12-02 04:32:26 -04:00
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
if (log_rate.get() > 0 && now_ms - last_log_ms >= uint32_t(1000U/log_rate.get())) {
|
|
|
|
last_log_ms = now_ms;
|
|
|
|
|
|
|
|
// @LoggerMessage: EAHR
|
|
|
|
// @Description: External AHRS data
|
|
|
|
// @Field: TimeUS: Time since system startup
|
|
|
|
// @Field: Roll: euler roll
|
|
|
|
// @Field: Pitch: euler pitch
|
|
|
|
// @Field: Yaw: euler yaw
|
|
|
|
// @Field: VN: velocity north
|
|
|
|
// @Field: VE: velocity east
|
|
|
|
// @Field: VD: velocity down
|
|
|
|
// @Field: Lat: latitude
|
|
|
|
// @Field: Lon: longitude
|
|
|
|
// @Field: Alt: altitude AMSL
|
|
|
|
// @Field: Flg: nav status flags
|
|
|
|
|
|
|
|
float roll, pitch, yaw;
|
|
|
|
state.quat.to_euler(roll, pitch, yaw);
|
|
|
|
nav_filter_status filterStatus {};
|
|
|
|
get_filter_status(filterStatus);
|
|
|
|
|
|
|
|
AP::logger().WriteStreaming("EAHR", "TimeUS,Roll,Pitch,Yaw,VN,VE,VD,Lat,Lon,Alt,Flg",
|
|
|
|
"sdddnnnDUm-",
|
|
|
|
"F000000GG0-",
|
|
|
|
"QffffffLLfI",
|
|
|
|
AP_HAL::micros64(),
|
|
|
|
degrees(roll), degrees(pitch), degrees(yaw),
|
|
|
|
state.velocity.x, state.velocity.y, state.velocity.z,
|
|
|
|
state.location.lat, state.location.lng, state.location.alt*0.01,
|
|
|
|
filterStatus.value);
|
|
|
|
}
|
2024-03-03 08:56:08 -04:00
|
|
|
#endif // HAL_LOGGING_ENABLED
|
2020-12-27 22:04:13 -04:00
|
|
|
}
|
|
|
|
|
2022-12-21 11:51:42 -04:00
|
|
|
// Get model/type name
|
|
|
|
const char* AP_ExternalAHRS::get_name() const
|
|
|
|
{
|
|
|
|
if (backend) {
|
|
|
|
return backend->get_name();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:04:13 -04:00
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_ExternalAHRS &externalAHRS()
|
|
|
|
{
|
|
|
|
return *AP_ExternalAHRS::get_singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // HAL_EXTERNAL_AHRS_ENABLED
|
|
|
|
|