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_Frsky_Telem/AP_Frsky_SPort_Passthrough.h
Views: 1798
1
#pragma once
2
3
#include "AP_Frsky_SPort.h"
4
5
#if AP_FRSKY_SPORT_PASSTHROUGH_ENABLED
6
7
#include <AP_RCTelemetry/AP_RCTelemetry.h>
8
9
#include "AP_Frsky_SPortParser.h"
10
#include "AP_Frsky_MAVlite.h"
11
12
#include "AP_Frsky_Telem.h"
13
14
#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
15
#include "AP_Frsky_MAVlite_SPortToMAVlite.h"
16
#include "AP_Frsky_MAVlite_MAVliteToSPort.h"
17
#include "AP_Frsky_MAVliteMsgHandler.h"
18
19
#define SPORT_TX_PACKET_DUPLICATES 1 // number of duplicates packets we send (fport only)
20
#endif
21
22
class AP_Frsky_SPort_Passthrough : public AP_Frsky_SPort, public AP_RCTelemetry
23
{
24
public:
25
26
AP_Frsky_SPort_Passthrough(AP_HAL::UARTDriver *port, bool use_external_data, AP_Frsky_Parameters *&frsky_parameters) :
27
AP_Frsky_SPort(port),
28
AP_RCTelemetry(WFQ_LAST_ITEM),
29
_frsky_parameters(frsky_parameters),
30
_use_external_data(use_external_data)
31
{
32
singleton = this;
33
}
34
35
static AP_Frsky_SPort_Passthrough *get_singleton(void) {
36
return singleton;
37
}
38
39
bool init() override;
40
bool init_serial_port() override;
41
42
// passthrough WFQ scheduler
43
bool is_packet_ready(uint8_t idx, bool queue_empty) override;
44
void process_packet(uint8_t idx) override;
45
void adjust_packet_weight(bool queue_empty) override;
46
// setup ready for passthrough operation
47
void setup_wfq_scheduler(void) override;
48
49
bool get_next_msg_chunk(void) override;
50
51
bool get_telem_data(sport_packet_t* packet_array, uint8_t &packet_count, const uint8_t max_size) override;
52
#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
53
bool set_telem_data(const uint8_t frame, const uint16_t appid, const uint32_t data) override;
54
#endif //HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
55
56
void queue_text_message(MAV_SEVERITY severity, const char *text) override
57
{
58
AP_RCTelemetry::queue_message(severity, text);
59
}
60
61
enum PassthroughPacketType : uint8_t {
62
TEXT = 0, // 0x5000 status text (dynamic)
63
ATTITUDE = 1, // 0x5006 Attitude and range (dynamic)
64
GPS_LAT = 2, // 0x800 GPS lat
65
GPS_LON = 3, // 0x800 GPS lon
66
VEL_YAW = 4, // 0x5005 Vel and Yaw
67
AP_STATUS = 5, // 0x5001 AP status
68
GPS_STATUS = 6, // 0x5002 GPS status
69
HOME = 7, // 0x5004 Home
70
BATT_2 = 8, // 0x5008 Battery 2 status
71
BATT_1 = 9, // 0x5008 Battery 1 status
72
PARAM = 10, // 0x5007 parameters
73
RPM = 11, // 0x500A rpm sensors 1 and 2
74
UDATA = 12, // user data
75
#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
76
MAV = 13, // mavlite
77
#endif //HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
78
TERRAIN = 14, // 0x500B terrain data
79
WIND = 15, // 0x500C wind data
80
WAYPOINT = 16, // 0x500D waypoint data
81
WFQ_LAST_ITEM // must be last
82
};
83
84
protected:
85
86
void send() override;
87
88
private:
89
90
AP_Frsky_Parameters *&_frsky_parameters;
91
92
enum PassthroughParam : uint8_t {
93
NONE = 0,
94
FRAME_TYPE = 1,
95
BATT_FS_VOLTAGE = 2,
96
BATT_FS_CAPACITY = 3,
97
BATT_CAPACITY_1 = 4,
98
BATT_CAPACITY_2 = 5,
99
TELEMETRY_FEATURES = 6
100
};
101
102
enum PassthroughFeatures : uint8_t {
103
BIDIR = 0,
104
SCRIPTING = 1,
105
};
106
107
// methods to convert flight controller data to FrSky SPort Passthrough (OpenTX) format
108
uint32_t calc_param(void);
109
uint32_t calc_batt(uint8_t instance);
110
uint32_t calc_ap_status(void);
111
uint32_t calc_home(void);
112
uint32_t calc_velandyaw(void);
113
uint32_t calc_attiandrng(void);
114
uint32_t calc_rpm(void);
115
uint32_t calc_terrain(void);
116
uint32_t calc_wind(void);
117
uint32_t calc_waypoint(void);
118
119
// use_external_data is set when this library will
120
// be providing data to another transport, such as FPort
121
bool _use_external_data;
122
123
struct {
124
sport_packet_t packet;
125
bool pending;
126
} external_data;
127
128
struct {
129
uint32_t chunk; // a "chunk" (four characters/bytes) at a time of the queued message to be sent
130
uint8_t repeats; // send each message "chunk" 3 times to make sure the entire messsage gets through without getting cut
131
uint8_t char_index; // index of which character to get in the message
132
} _msg_chunk;
133
134
// passthrough default sensor id
135
uint8_t downlink_sensor_id = 0x1B;
136
137
#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
138
// bidirectional sport telemetry
139
struct {
140
uint8_t uplink_sensor_id = 0x0D;
141
uint8_t downlink1_sensor_id = 0x34;
142
uint8_t downlink2_sensor_id = 0x67;
143
uint8_t tx_packet_duplicates;
144
ObjectBuffer_TS<AP_Frsky_SPort::sport_packet_t> rx_packet_queue{SPORT_PACKET_QUEUE_LENGTH};
145
ObjectBuffer_TS<AP_Frsky_SPort::sport_packet_t> tx_packet_queue{SPORT_PACKET_QUEUE_LENGTH};
146
} _SPort_bidir;
147
148
AP_Frsky_SPortParser _sport_handler;
149
AP_Frsky_MAVlite_SPortToMAVlite sport_to_mavlite;
150
AP_Frsky_MAVlite_MAVliteToSPort mavlite_to_sport;
151
152
// tx/rx sport packet processing
153
void queue_rx_packet(const AP_Frsky_SPort::sport_packet_t sp);
154
void process_rx_queue(void);
155
void process_tx_queue(void);
156
157
// create an object to handle incoming mavlite messages; a
158
// callback method is provided to allow the handler to send responses
159
bool send_message(const AP_Frsky_MAVlite_Message &txmsg);
160
AP_Frsky_MAVliteMsgHandler mavlite{FUNCTOR_BIND_MEMBER(&AP_Frsky_SPort_Passthrough::send_message, bool, const AP_Frsky_MAVlite_Message &)};
161
#endif
162
void set_sensor_id(AP_Int8 idx, uint8_t &sensor);
163
void send_sport_frame(uint8_t frame, uint16_t appid, uint32_t data);
164
165
// true if we need to respond to the last polling byte
166
bool is_passthrough_byte(const uint8_t byte) const;
167
168
uint8_t _paramID;
169
170
uint32_t calc_gps_status(void);
171
172
static AP_Frsky_SPort_Passthrough *singleton;
173
};
174
175
namespace AP {
176
AP_Frsky_SPort_Passthrough *frsky_passthrough_telem();
177
};
178
179
180
#endif // AP_FRSKY_SPORT_PASSTHROUGH_ENABLED
181
182