CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Devo_Telem/AP_Devo_Telem.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
/*
17
DEVO Telemetry library
18
*/
19
20
21
22
#include "AP_Devo_Telem.h"
23
24
#if AP_DEVO_TELEM_ENABLED
25
26
#include <AP_AHRS/AP_AHRS.h>
27
#include <AP_GPS/AP_GPS.h>
28
#include <AP_BattMonitor/AP_BattMonitor.h>
29
#include <AP_SerialManager/AP_SerialManager.h>
30
#include <GCS_MAVLink/GCS.h>
31
32
#define DEVOM_SYNC_BYTE 0xAA
33
#define AP_SERIALMANAGER_DEVO_TELEM_BAUD 38400
34
#define AP_SERIALMANAGER_DEVO_BUFSIZE_RX 0
35
#define AP_SERIALMANAGER_DEVO_BUFSIZE_TX 32
36
37
extern const AP_HAL::HAL& hal;
38
39
void AP_DEVO_Telem::init()
40
{
41
const AP_SerialManager& serial_manager = AP::serialmanager();
42
43
// check for DEVO_DPort
44
if ((_port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Devo_Telem, 0))) {
45
_port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);
46
// initialise uart
47
_port->begin(AP_SERIALMANAGER_DEVO_TELEM_BAUD, AP_SERIALMANAGER_DEVO_BUFSIZE_RX, AP_SERIALMANAGER_DEVO_BUFSIZE_TX);
48
49
hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_DEVO_Telem::tick, void));
50
}
51
}
52
53
54
uint32_t AP_DEVO_Telem::gpsDdToDmsFormat(int32_t ddm)
55
{
56
int32_t deg = (int32_t)(ddm * 1e-7);
57
float mm = (ddm * 1.0e-7 - deg) * 60.0f;
58
59
mm = ((float)deg * 100.0f + mm) *0.01f;
60
61
if ((mm < -180.0f) || (mm > 180.0f)) {
62
mm = 0.0f;
63
}
64
65
return mm * 1.0e7f;
66
}
67
68
69
/*
70
send_frames - sends updates down telemetry link
71
should be called at 1hz
72
*/
73
74
#define DEVO_SPEED_FACTOR 0.0194384f
75
76
void AP_DEVO_Telem::send_frames()
77
{
78
// return immediately if not initialised
79
if (_port == nullptr) {
80
return;
81
}
82
83
struct PACKED {
84
uint8_t header; ///< 0xAA for a valid packet
85
int32_t lon;
86
int32_t lat;
87
int32_t alt;
88
int16_t speed;
89
int16_t temp;
90
int16_t volt;
91
uint8_t checksum8;
92
} devoPacket{};
93
94
devoPacket.header = DEVOM_SYNC_BYTE;
95
96
const AP_AHRS &_ahrs = AP::ahrs();
97
const AP_GPS &gps = AP::gps();
98
Location loc;
99
100
if (_ahrs.get_location(loc)) {
101
devoPacket.lat = gpsDdToDmsFormat(loc.lat);
102
devoPacket.lon = gpsDdToDmsFormat(loc.lng);
103
devoPacket.speed = (int16_t)(gps.ground_speed() * DEVO_SPEED_FACTOR * 100.0f); // * 100 for cm
104
105
/*
106
Note that this isn't actually barometric altitude, it is the
107
inertial nav estimate of altitude above home.
108
*/
109
float alt;
110
_ahrs.get_relative_position_D_home(alt);
111
devoPacket.alt = alt * -100.0f; // coordinates was in NED, so it needs to change sign. Protocol requires in cm!
112
}
113
114
115
116
devoPacket.volt = roundf(AP::battery().voltage() * 10.0f);
117
devoPacket.temp = gcs().custom_mode(); // Send mode as temperature
118
119
// emit the packet to the port byte-by-byte, calculating checksum
120
// as we go. Note we are stepping backwards through the structure
121
// - presumably to get endianness correct on the entries!
122
uint8_t *b = (uint8_t *)&devoPacket;
123
for (uint8_t i = sizeof(devoPacket)-1; i !=0; i--) {
124
_port->write(b, 1);
125
devoPacket.checksum8 += *b++; // Add Checksum
126
}
127
_port->write(&devoPacket.checksum8, 1);
128
}
129
130
void AP_DEVO_Telem::tick(void)
131
{
132
uint32_t now = AP_HAL::millis();
133
134
if (now - _last_frame_ms > 1000) {
135
_last_frame_ms = now;
136
send_frames();
137
}
138
}
139
#endif
140
141