Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/AP_Periph/compass.cpp
Views: 1798
#include "AP_Periph.h"12#ifdef HAL_PERIPH_ENABLE_MAG34/*5magnetometer support6*/78#include <dronecan_msgs.h>910#ifndef AP_PERIPH_MAG_MAX_RATE11#define AP_PERIPH_MAG_MAX_RATE 25U12#endif1314#ifndef AP_PERIPH_PROBE_CONTINUOUS15#define AP_PERIPH_PROBE_CONTINUOUS 016#endif1718#ifndef AP_PERIPH_MAG_HIRES19#define AP_PERIPH_MAG_HIRES 020#endif2122extern const AP_HAL::HAL &hal;2324/*25update CAN magnetometer26*/27void AP_Periph_FW::can_mag_update(void)28{29if (!compass.available()) {30return;31}3233#if AP_PERIPH_MAG_MAX_RATE > 034// don't flood the bus with very high rate magnetometers35const uint32_t now_ms = AP_HAL::millis();36if (now_ms - last_mag_update_ms < (1000U / AP_PERIPH_MAG_MAX_RATE)) {37return;38}39#endif4041compass.read();4243#if AP_PERIPH_PROBE_CONTINUOUS44if (option_is_set(PeriphOptions::PROBE_CONTINUOUS) && !hal.util->get_soft_armed() && (compass.get_count() == 0)) {45static uint32_t last_probe_ms;46uint32_t now = AP_HAL::millis();47if (now - last_probe_ms >= 1000) {48last_probe_ms = now;49compass.init();50}51}52#endif5354if (last_mag_update_ms == compass.last_update_ms()) {55return;56}57if (!compass.healthy()) {58return;59}6061last_mag_update_ms = compass.last_update_ms();62Vector3f field_Ga = compass.get_field() * 0.001;6364#if !AP_PERIPH_MAG_HIRES65// normal message, float16 values66uavcan_equipment_ahrs_MagneticFieldStrength pkt {};6768for (uint8_t i=0; i<3; i++) {69pkt.magnetic_field_ga[i] = field_Ga[i];70}7172uint8_t buffer[UAVCAN_EQUIPMENT_AHRS_MAGNETICFIELDSTRENGTH_MAX_SIZE];73uint16_t total_size = uavcan_equipment_ahrs_MagneticFieldStrength_encode(&pkt, buffer, !periph.canfdout());7475canard_broadcast(UAVCAN_EQUIPMENT_AHRS_MAGNETICFIELDSTRENGTH_SIGNATURE,76UAVCAN_EQUIPMENT_AHRS_MAGNETICFIELDSTRENGTH_ID,77CANARD_TRANSFER_PRIORITY_LOW,78&buffer[0],79total_size);80#else81// High resolution magnetometer, for magnetic surveys82dronecan_sensors_magnetometer_MagneticFieldStrengthHiRes pkt {};8384for (uint8_t i=0; i<3; i++) {85pkt.magnetic_field_ga[i] = field_Ga[i];86}8788uint8_t buffer[DRONECAN_SENSORS_MAGNETOMETER_MAGNETICFIELDSTRENGTHHIRES_MAX_SIZE];89uint16_t total_size = dronecan_sensors_magnetometer_MagneticFieldStrengthHiRes_encode(&pkt, buffer, !periph.canfdout());9091canard_broadcast(DRONECAN_SENSORS_MAGNETOMETER_MAGNETICFIELDSTRENGTHHIRES_SIGNATURE,92DRONECAN_SENSORS_MAGNETOMETER_MAGNETICFIELDSTRENGTHHIRES_ID,93CANARD_TRANSFER_PRIORITY_LOW,94&buffer[0],95total_size);96#endif // AP_PERIPH_MAG_HIRES97}9899#endif // HAL_PERIPH_ENABLE_MAG100101102