2018-03-04 05:41:06 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
DEVO Telemetry library
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEVOM_SYNC_BYTE 0xAA
|
|
|
|
|
|
|
|
|
|
|
|
#define AP_SERIALMANAGER_DEVO_TELEM_BAUD 38400
|
|
|
|
#define AP_SERIALMANAGER_DEVO_BUFSIZE_RX 0
|
|
|
|
#define AP_SERIALMANAGER_DEVO_BUFSIZE_TX 32
|
|
|
|
|
|
|
|
#include "AP_Devo_Telem.h"
|
2019-02-13 00:42:56 -04:00
|
|
|
|
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2018-03-04 05:41:06 -04:00
|
|
|
#include <AP_GPS/AP_GPS.h>
|
|
|
|
#include <AP_BattMonitor/AP_BattMonitor.h>
|
2019-02-13 19:12:14 -04:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2018-03-04 05:41:06 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2019-02-13 00:44:33 -04:00
|
|
|
void AP_DEVO_Telem::init()
|
2018-03-04 05:41:06 -04:00
|
|
|
{
|
2019-02-13 00:44:33 -04:00
|
|
|
const AP_SerialManager& serial_manager = AP::serialmanager();
|
|
|
|
|
2018-03-04 05:41:06 -04:00
|
|
|
// check for DEVO_DPort
|
|
|
|
if ((_port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Devo_Telem, 0))) {
|
|
|
|
_port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);
|
|
|
|
// initialise uart
|
|
|
|
_port->begin(AP_SERIALMANAGER_DEVO_TELEM_BAUD, AP_SERIALMANAGER_DEVO_BUFSIZE_RX, AP_SERIALMANAGER_DEVO_BUFSIZE_TX);
|
|
|
|
|
|
|
|
hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_DEVO_Telem::tick, void));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-11 12:29:26 -04:00
|
|
|
uint32_t AP_DEVO_Telem::gpsDdToDmsFormat(int32_t ddm)
|
2018-03-04 05:41:06 -04:00
|
|
|
{
|
2021-11-11 12:29:26 -04:00
|
|
|
int32_t deg = (int32_t)(ddm * 1e-7);
|
|
|
|
float mm = (ddm * 1.0e-7 - deg) * 60.0f;
|
2018-03-04 05:41:06 -04:00
|
|
|
|
2019-04-04 19:07:18 -03:00
|
|
|
mm = ((float)deg * 100.0f + mm) /100.0f;
|
2018-03-04 05:41:06 -04:00
|
|
|
|
|
|
|
if ((mm < -180.0f) || (mm > 180.0f)) {
|
|
|
|
mm = 0.0f;
|
|
|
|
}
|
|
|
|
|
2019-04-04 19:07:18 -03:00
|
|
|
return mm * 1.0e7f;
|
2018-03-04 05:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
send_frames - sends updates down telemetry link
|
2019-02-13 00:46:31 -04:00
|
|
|
should be called at 1hz
|
2018-03-04 05:41:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEVO_SPEED_FACTOR 0.0194384f
|
|
|
|
|
2019-02-13 19:12:14 -04:00
|
|
|
void AP_DEVO_Telem::send_frames()
|
2018-03-04 05:41:06 -04:00
|
|
|
{
|
|
|
|
// return immediately if not initialised
|
|
|
|
if (_port == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 01:01:12 -04:00
|
|
|
struct PACKED {
|
|
|
|
uint8_t header; ///< 0xAA for a valid packet
|
|
|
|
int32_t lon;
|
|
|
|
int32_t lat;
|
|
|
|
int32_t alt;
|
|
|
|
int16_t speed;
|
|
|
|
int16_t temp;
|
|
|
|
int16_t volt;
|
|
|
|
uint8_t checksum8;
|
|
|
|
} devoPacket{};
|
|
|
|
|
|
|
|
devoPacket.header = DEVOM_SYNC_BYTE;
|
|
|
|
|
2019-02-13 00:42:56 -04:00
|
|
|
const AP_AHRS &_ahrs = AP::ahrs();
|
2018-03-04 05:41:06 -04:00
|
|
|
const AP_GPS &gps = AP::gps();
|
2018-04-23 20:47:18 -03:00
|
|
|
Location loc;
|
2018-03-04 05:41:06 -04:00
|
|
|
|
2018-04-23 20:47:18 -03:00
|
|
|
if (_ahrs.get_position(loc)) {
|
2018-03-04 05:41:06 -04:00
|
|
|
devoPacket.lat = gpsDdToDmsFormat(loc.lat);
|
|
|
|
devoPacket.lon = gpsDdToDmsFormat(loc.lng);
|
|
|
|
devoPacket.speed = (int16_t)(gps.ground_speed() * DEVO_SPEED_FACTOR * 100.0f); // * 100 for cm
|
|
|
|
|
|
|
|
/*
|
|
|
|
Note that this isn't actually barometric altitude, it is the
|
|
|
|
inertial nav estimate of altitude above home.
|
|
|
|
*/
|
|
|
|
float alt;
|
|
|
|
_ahrs.get_relative_position_D_home(alt);
|
2021-11-11 12:29:26 -04:00
|
|
|
devoPacket.alt = alt * -100.0f; // coordinates was in NED, so it needs to change sign. Protocol requires in cm!
|
2018-03-04 05:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
devoPacket.volt = roundf(AP::battery().voltage() * 10.0f);
|
2019-02-13 19:12:14 -04:00
|
|
|
devoPacket.temp = gcs().custom_mode(); // Send mode as temperature
|
2018-03-04 05:41:06 -04:00
|
|
|
|
2019-02-13 01:01:12 -04:00
|
|
|
// emit the packet to the port byte-by-byte, calculating checksum
|
|
|
|
// as we go. Note we are stepping backwards through the structure
|
|
|
|
// - presumably to get endianness correct on the entries!
|
2018-03-04 05:41:06 -04:00
|
|
|
uint8_t *b = (uint8_t *)&devoPacket;
|
|
|
|
for (uint8_t i = sizeof(devoPacket)-1; i !=0; i--) {
|
|
|
|
_port->write(b, 1);
|
|
|
|
devoPacket.checksum8 += *b++; // Add Checksum
|
|
|
|
}
|
|
|
|
_port->write(&devoPacket.checksum8, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_DEVO_Telem::tick(void)
|
|
|
|
{
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
if (now - _last_frame_ms > 1000) {
|
|
|
|
_last_frame_ms = now;
|
2019-02-13 19:12:14 -04:00
|
|
|
send_frames();
|
2018-03-04 05:41:06 -04:00
|
|
|
}
|
|
|
|
}
|