2023-08-18 16:14:40 -03: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 MicroStrain CX5/GX5-45 serially connected AHRS Systems
|
2023-08-18 16:14:40 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define ALLOW_DOUBLE_MATH_FUNCTIONS
|
|
|
|
|
|
|
|
#include "AP_ExternalAHRS_config.h"
|
|
|
|
|
|
|
|
#if AP_EXTERNAL_AHRS_MICROSTRAIN5_ENABLED
|
|
|
|
|
|
|
|
#include "AP_ExternalAHRS_MicroStrain5.h"
|
|
|
|
#include "AP_Compass/AP_Compass_config.h"
|
|
|
|
#include <AP_Baro/AP_Baro.h>
|
|
|
|
#include <AP_Compass/AP_Compass.h>
|
|
|
|
#include <AP_GPS/AP_GPS.h>
|
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
#include <AP_InertialSensor/AP_InertialSensor.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
#include <AP_Logger/AP_Logger.h>
|
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2023-08-28 23:26:09 -03:00
|
|
|
#include <AP_SerialManager/AP_SerialManager.h>
|
2023-08-18 16:14:40 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
static constexpr uint8_t gnss_instance = 0;
|
|
|
|
|
2023-08-18 16:14:40 -03:00
|
|
|
AP_ExternalAHRS_MicroStrain5::AP_ExternalAHRS_MicroStrain5(AP_ExternalAHRS *_frontend,
|
|
|
|
AP_ExternalAHRS::state_t &_state): AP_ExternalAHRS_backend(_frontend, _state)
|
|
|
|
{
|
|
|
|
auto &sm = AP::serialmanager();
|
|
|
|
uart = sm.find_serial(AP_SerialManager::SerialProtocol_AHRS, 0);
|
|
|
|
|
|
|
|
baudrate = sm.find_baudrate(AP_SerialManager::SerialProtocol_AHRS, 0);
|
|
|
|
port_num = sm.find_portnum(AP_SerialManager::SerialProtocol_AHRS, 0);
|
|
|
|
|
|
|
|
if (!uart) {
|
2023-09-23 16:42:35 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "MicroStrain5 ExternalAHRS no UART");
|
2023-08-18 16:14:40 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_ExternalAHRS_MicroStrain5::update_thread, void), "AHRS", 2048, AP_HAL::Scheduler::PRIORITY_SPI, 0)) {
|
2023-09-23 16:42:35 -03:00
|
|
|
AP_BoardConfig::allocation_error("MicroStrain5 failed to allocate ExternalAHRS update thread");
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
hal.scheduler->delay(5000);
|
2023-09-23 16:42:35 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "MicroStrain5 ExternalAHRS initialised");
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_ExternalAHRS_MicroStrain5::update_thread(void)
|
|
|
|
{
|
|
|
|
if (!port_open) {
|
|
|
|
port_open = true;
|
|
|
|
uart->begin(baudrate);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
build_packet();
|
|
|
|
hal.scheduler->delay_microseconds(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Builds packets by looking at each individual byte, once a full packet has been read in it checks the checksum then handles the packet.
|
|
|
|
void AP_ExternalAHRS_MicroStrain5::build_packet()
|
|
|
|
{
|
2023-08-28 01:27:46 -03:00
|
|
|
if (uart == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-18 16:14:40 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
uint32_t nbytes = MIN(uart->available(), 2048u);
|
|
|
|
while (nbytes--> 0) {
|
|
|
|
uint8_t b;
|
|
|
|
if (!uart->read(b)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DescriptorSet descriptor;
|
|
|
|
if (handle_byte(b, descriptor)) {
|
|
|
|
switch (descriptor) {
|
|
|
|
case DescriptorSet::IMUData:
|
|
|
|
post_imu();
|
|
|
|
break;
|
|
|
|
case DescriptorSet::GNSSData:
|
2023-09-23 16:42:35 -03:00
|
|
|
case DescriptorSet::GNSSRecv1:
|
|
|
|
case DescriptorSet::GNSSRecv2:
|
2023-08-18 16:14:40 -03:00
|
|
|
break;
|
2023-09-23 16:42:35 -03:00
|
|
|
case DescriptorSet::FilterData:
|
2023-08-18 16:14:40 -03:00
|
|
|
post_filter();
|
|
|
|
break;
|
|
|
|
case DescriptorSet::BaseCommand:
|
|
|
|
case DescriptorSet::DMCommand:
|
|
|
|
case DescriptorSet::SystemCommand:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Posts data from an imu packet to `state` and `handle_external` methods
|
|
|
|
void AP_ExternalAHRS_MicroStrain5::post_imu() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
state.accel = imu_data.accel;
|
|
|
|
state.gyro = imu_data.gyro;
|
|
|
|
|
|
|
|
state.quat = imu_data.quat;
|
|
|
|
state.have_quaternion = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
AP_ExternalAHRS::ins_data_message_t ins {
|
|
|
|
accel: imu_data.accel,
|
|
|
|
gyro: imu_data.gyro,
|
|
|
|
temperature: -300
|
|
|
|
};
|
|
|
|
AP::ins().handle_external(ins);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if AP_COMPASS_EXTERNALAHRS_ENABLED
|
|
|
|
{
|
|
|
|
AP_ExternalAHRS::mag_data_message_t mag {
|
|
|
|
field: imu_data.mag
|
|
|
|
};
|
|
|
|
AP::compass().handle_external(mag);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if AP_BARO_EXTERNALAHRS_ENABLED
|
|
|
|
{
|
|
|
|
const AP_ExternalAHRS::baro_data_message_t baro {
|
|
|
|
instance: 0,
|
|
|
|
pressure_pa: imu_data.pressure,
|
|
|
|
// setting temp to 25 effectively disables barometer temperature calibrations - these are already performed by MicroStrain
|
|
|
|
temperature: 25,
|
|
|
|
};
|
|
|
|
AP::baro().handle_external(baro);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_ExternalAHRS_MicroStrain5::post_filter() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
state.velocity = Vector3f{filter_data.ned_velocity_north, filter_data.ned_velocity_east, filter_data.ned_velocity_down};
|
|
|
|
state.have_velocity = true;
|
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
state.location = Location{filter_data.lat, filter_data.lon, gnss_data[gnss_instance].msl_altitude, Location::AltFrame::ABSOLUTE};
|
2023-08-18 16:14:40 -03:00
|
|
|
state.have_location = true;
|
2023-12-01 21:10:08 -04:00
|
|
|
state.last_location_update_us = AP_HAL::micros();
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
AP_ExternalAHRS::gps_data_message_t gps {
|
|
|
|
gps_week: filter_data.week,
|
|
|
|
ms_tow: filter_data.tow_ms,
|
2024-09-28 04:27:44 -03:00
|
|
|
fix_type: AP_GPS_FixType(gnss_data[gnss_instance].fix_type),
|
2023-09-23 16:42:35 -03:00
|
|
|
satellites_in_view: gnss_data[gnss_instance].satellites,
|
2023-08-18 16:14:40 -03:00
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
horizontal_pos_accuracy: gnss_data[gnss_instance].horizontal_position_accuracy,
|
|
|
|
vertical_pos_accuracy: gnss_data[gnss_instance].vertical_position_accuracy,
|
|
|
|
horizontal_vel_accuracy: gnss_data[gnss_instance].speed_accuracy,
|
2023-08-18 16:14:40 -03:00
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
hdop: gnss_data[gnss_instance].hdop,
|
|
|
|
vdop: gnss_data[gnss_instance].vdop,
|
2023-08-18 16:14:40 -03:00
|
|
|
|
|
|
|
longitude: filter_data.lon,
|
|
|
|
latitude: filter_data.lat,
|
2023-09-23 16:42:35 -03:00
|
|
|
msl_altitude: gnss_data[gnss_instance].msl_altitude,
|
2023-08-18 16:14:40 -03:00
|
|
|
|
|
|
|
ned_vel_north: filter_data.ned_velocity_north,
|
|
|
|
ned_vel_east: filter_data.ned_velocity_east,
|
|
|
|
ned_vel_down: filter_data.ned_velocity_down,
|
|
|
|
};
|
|
|
|
|
2024-09-28 04:27:44 -03:00
|
|
|
if (gps.fix_type >= AP_GPS_FixType::FIX_3D && !state.have_origin) {
|
2023-08-18 16:14:40 -03:00
|
|
|
WITH_SEMAPHORE(state.sem);
|
|
|
|
state.origin = Location{int32_t(filter_data.lat),
|
|
|
|
int32_t(filter_data.lon),
|
2023-09-23 16:42:35 -03:00
|
|
|
int32_t(gnss_data[gnss_instance].msl_altitude),
|
2023-08-18 16:14:40 -03:00
|
|
|
Location::AltFrame::ABSOLUTE};
|
|
|
|
state.have_origin = true;
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
uint8_t gps_instance;
|
|
|
|
if (AP::gps().get_first_external_instance(gps_instance)) {
|
|
|
|
AP::gps().handle_external(gps, gps_instance);
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t AP_ExternalAHRS_MicroStrain5::get_port(void) const
|
|
|
|
{
|
|
|
|
if (!uart) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return port_num;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get model/type name
|
|
|
|
const char* AP_ExternalAHRS_MicroStrain5::get_name() const
|
|
|
|
{
|
|
|
|
return "MicroStrain5";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS_MicroStrain5::healthy(void) const
|
|
|
|
{
|
|
|
|
uint32_t now = AP_HAL::millis();
|
2023-09-23 16:42:35 -03:00
|
|
|
return (now - last_imu_pkt < 40 && now - last_gps_pkt < 500 && now - last_filter_pkt < 500);
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS_MicroStrain5::initialised(void) const
|
|
|
|
{
|
2023-09-23 16:42:35 -03:00
|
|
|
return last_imu_pkt != 0 && last_gps_pkt != 0 && last_filter_pkt != 0;
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_ExternalAHRS_MicroStrain5::pre_arm_check(char *failure_msg, uint8_t failure_msg_len) const
|
|
|
|
{
|
|
|
|
if (!healthy()) {
|
2023-09-23 16:42:35 -03:00
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "MicroStrain5 unhealthy");
|
2023-08-18 16:14:40 -03:00
|
|
|
return false;
|
|
|
|
}
|
2023-09-23 16:42:35 -03:00
|
|
|
if (gnss_data[gnss_instance].fix_type < 3) {
|
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "MicroStrain5 no GPS lock");
|
2023-08-18 16:14:40 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (filter_status.state != 0x02) {
|
2023-09-23 16:42:35 -03:00
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "MicroStrain5 filter not running");
|
2023-08-18 16:14:40 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_ExternalAHRS_MicroStrain5::get_filter_status(nav_filter_status &status) const
|
|
|
|
{
|
|
|
|
memset(&status, 0, sizeof(status));
|
2023-09-23 16:42:35 -03:00
|
|
|
if (last_imu_pkt != 0 && last_gps_pkt != 0) {
|
2023-09-24 06:00:11 -03:00
|
|
|
status.flags.initalized = true;
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
2023-09-23 16:42:35 -03:00
|
|
|
if (healthy() && last_imu_pkt != 0) {
|
2023-09-24 06:00:11 -03:00
|
|
|
status.flags.attitude = true;
|
|
|
|
status.flags.vert_vel = true;
|
|
|
|
status.flags.vert_pos = true;
|
2023-08-18 16:14:40 -03:00
|
|
|
|
2023-09-23 16:42:35 -03:00
|
|
|
if (gnss_data[gnss_instance].fix_type >= 3) {
|
2023-09-24 06:00:11 -03:00
|
|
|
status.flags.horiz_vel = true;
|
|
|
|
status.flags.horiz_pos_rel = true;
|
|
|
|
status.flags.horiz_pos_abs = true;
|
|
|
|
status.flags.pred_horiz_pos_rel = true;
|
|
|
|
status.flags.pred_horiz_pos_abs = true;
|
|
|
|
status.flags.using_gps = true;
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 00:18:39 -03:00
|
|
|
// get variances
|
|
|
|
bool AP_ExternalAHRS_MicroStrain5::get_variances(float &velVar, float &posVar, float &hgtVar, Vector3f &magVar, float &tasVar) const
|
2023-08-18 16:14:40 -03:00
|
|
|
{
|
2024-10-15 00:18:39 -03:00
|
|
|
velVar = gnss_data[gnss_instance].speed_accuracy * vel_gate_scale;
|
|
|
|
posVar = gnss_data[gnss_instance].horizontal_position_accuracy * pos_gate_scale;
|
|
|
|
hgtVar = gnss_data[gnss_instance].vertical_position_accuracy * hgt_gate_scale;
|
|
|
|
tasVar = 0;
|
|
|
|
return true;
|
2023-08-18 16:14:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // AP_EXTERNAL_AHRS_MICROSTRAIN5_ENABLED
|