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_Devo_Telem/AP_Devo_Telem.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415/*16DEVO Telemetry library17*/18192021#include "AP_Devo_Telem.h"2223#if AP_DEVO_TELEM_ENABLED2425#include <AP_AHRS/AP_AHRS.h>26#include <AP_GPS/AP_GPS.h>27#include <AP_BattMonitor/AP_BattMonitor.h>28#include <AP_SerialManager/AP_SerialManager.h>29#include <GCS_MAVLink/GCS.h>3031#define DEVOM_SYNC_BYTE 0xAA32#define AP_SERIALMANAGER_DEVO_TELEM_BAUD 3840033#define AP_SERIALMANAGER_DEVO_BUFSIZE_RX 034#define AP_SERIALMANAGER_DEVO_BUFSIZE_TX 323536extern const AP_HAL::HAL& hal;3738void AP_DEVO_Telem::init()39{40const AP_SerialManager& serial_manager = AP::serialmanager();4142// check for DEVO_DPort43if ((_port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Devo_Telem, 0))) {44_port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);45// initialise uart46_port->begin(AP_SERIALMANAGER_DEVO_TELEM_BAUD, AP_SERIALMANAGER_DEVO_BUFSIZE_RX, AP_SERIALMANAGER_DEVO_BUFSIZE_TX);4748hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_DEVO_Telem::tick, void));49}50}515253uint32_t AP_DEVO_Telem::gpsDdToDmsFormat(int32_t ddm)54{55int32_t deg = (int32_t)(ddm * 1e-7);56float mm = (ddm * 1.0e-7 - deg) * 60.0f;5758mm = ((float)deg * 100.0f + mm) *0.01f;5960if ((mm < -180.0f) || (mm > 180.0f)) {61mm = 0.0f;62}6364return mm * 1.0e7f;65}666768/*69send_frames - sends updates down telemetry link70should be called at 1hz71*/7273#define DEVO_SPEED_FACTOR 0.0194384f7475void AP_DEVO_Telem::send_frames()76{77// return immediately if not initialised78if (_port == nullptr) {79return;80}8182struct PACKED {83uint8_t header; ///< 0xAA for a valid packet84int32_t lon;85int32_t lat;86int32_t alt;87int16_t speed;88int16_t temp;89int16_t volt;90uint8_t checksum8;91} devoPacket{};9293devoPacket.header = DEVOM_SYNC_BYTE;9495const AP_AHRS &_ahrs = AP::ahrs();96const AP_GPS &gps = AP::gps();97Location loc;9899if (_ahrs.get_location(loc)) {100devoPacket.lat = gpsDdToDmsFormat(loc.lat);101devoPacket.lon = gpsDdToDmsFormat(loc.lng);102devoPacket.speed = (int16_t)(gps.ground_speed() * DEVO_SPEED_FACTOR * 100.0f); // * 100 for cm103104/*105Note that this isn't actually barometric altitude, it is the106inertial nav estimate of altitude above home.107*/108float alt;109_ahrs.get_relative_position_D_home(alt);110devoPacket.alt = alt * -100.0f; // coordinates was in NED, so it needs to change sign. Protocol requires in cm!111}112113114115devoPacket.volt = roundf(AP::battery().voltage() * 10.0f);116devoPacket.temp = gcs().custom_mode(); // Send mode as temperature117118// emit the packet to the port byte-by-byte, calculating checksum119// as we go. Note we are stepping backwards through the structure120// - presumably to get endianness correct on the entries!121uint8_t *b = (uint8_t *)&devoPacket;122for (uint8_t i = sizeof(devoPacket)-1; i !=0; i--) {123_port->write(b, 1);124devoPacket.checksum8 += *b++; // Add Checksum125}126_port->write(&devoPacket.checksum8, 1);127}128129void AP_DEVO_Telem::tick(void)130{131uint32_t now = AP_HAL::millis();132133if (now - _last_frame_ms > 1000) {134_last_frame_ms = now;135send_frames();136}137}138#endif139140141