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/libraries/AP_Frsky_Telem/AP_Frsky_Backend.cpp
Views: 1798
#include "AP_Frsky_Backend.h"12#if AP_FRSKY_TELEM_ENABLED34#include <AP_Baro/AP_Baro.h>5#include <AP_AHRS/AP_AHRS.h>6#include <AP_RPM/AP_RPM.h>7#include <AP_RCTelemetry/AP_RCTelemetry.h>89extern const AP_HAL::HAL& hal;1011bool AP_Frsky_Backend::init()12{13// if SPort Passthrough is using external data then it will14// override this to do nothing:15return init_serial_port();16}1718bool AP_Frsky_Backend::init_serial_port()19{20if (!hal.scheduler->thread_create(21FUNCTOR_BIND_MEMBER(&AP_Frsky_Backend::loop, void),22"FrSky",231024,24AP_HAL::Scheduler::PRIORITY_RCIN,251)) {26return false;27}28// we don't want flow control for either protocol29_port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);30return true;31}3233/*34thread to loop handling bytes35*/36void AP_Frsky_Backend::loop(void)37{38// initialise uart (this must be called from within tick b/c the UART begin must be called from the same thread as it is used from)39_port->begin(initial_baud(), 0, 0);4041while (true) {42hal.scheduler->delay(1);43send();44}45}4647/*48* prepare altitude between vehicle and home location data49* for FrSky D and SPort protocols50*/51void AP_Frsky_Backend::calc_nav_alt(void)52{53_SPort_data.vario_vspd = (int32_t)(AP_RCTelemetry::get_vspeed_ms()*100); //convert to cm/s5455float current_height = AP_RCTelemetry::get_nav_alt_m();56_SPort_data.alt_nav_meters = float_to_uint16(current_height);57_SPort_data.alt_nav_cm = float_to_uint16((current_height - _SPort_data.alt_nav_meters) * 100);58}5960/*61* format the decimal latitude/longitude to the required degrees/minutes62* for FrSky D and SPort protocols63*/64float AP_Frsky_Backend::format_gps(float dec)65{66uint8_t dm_deg = (uint8_t) dec;67return (dm_deg * 100.0f) + (dec - dm_deg) * 60;68}6970/*71* prepare gps data72* for FrSky D and SPort protocols73*/74void AP_Frsky_Backend::calc_gps_position(void)75{76AP_AHRS &_ahrs = AP::ahrs();7778Location loc;7980if (_ahrs.get_location(loc)) {81float lat = format_gps(fabsf(loc.lat/10000000.0f));82_SPort_data.latdddmm = lat;83_SPort_data.latmmmm = (lat - _SPort_data.latdddmm) * 10000;84_SPort_data.lat_ns = (loc.lat < 0) ? 'S' : 'N';8586float lon = format_gps(fabsf(loc.lng/10000000.0f));87_SPort_data.londddmm = lon;88_SPort_data.lonmmmm = (lon - _SPort_data.londddmm) * 10000;89_SPort_data.lon_ew = (loc.lng < 0) ? 'W' : 'E';9091float alt = loc.alt * 0.01f;92_SPort_data.alt_gps_meters = float_to_uint16(alt);93_SPort_data.alt_gps_cm = float_to_uint16((alt - _SPort_data.alt_gps_meters) * 100);9495const float speed = AP::ahrs().groundspeed();96_SPort_data.speed_in_meter = float_to_int16(speed);97_SPort_data.speed_in_centimeter = float_to_uint16((speed - _SPort_data.speed_in_meter) * 100);98} else {99_SPort_data.latdddmm = 0;100_SPort_data.latmmmm = 0;101_SPort_data.lat_ns = 0;102_SPort_data.londddmm = 0;103_SPort_data.lonmmmm = 0;104_SPort_data.alt_gps_meters = 0;105_SPort_data.alt_gps_cm = 0;106_SPort_data.speed_in_meter = 0;107_SPort_data.speed_in_centimeter = 0;108}109110_SPort_data.yaw = (uint16_t)((_ahrs.yaw_sensor / 100) % 360); // heading in degree based on AHRS and not GPS111}112113/*114* prepare rpm data115* for FrSky D and SPort protocols116*/117bool AP_Frsky_Backend::calc_rpm(const uint8_t instance, int32_t &value) const118{119#if AP_RPM_ENABLED120const AP_RPM* rpm = AP::rpm();121if (rpm == nullptr) {122return false;123}124125float rpm_value;126if (!rpm->get_rpm(instance, rpm_value)) {127return false;128}129value = static_cast<int32_t>(roundf(rpm_value));130return true;131#else132return false;133#endif134}135136#endif // AP_FRSKY_TELEM_ENABLED137138139