PX4 Firmware
PX4 Autopilot Software http://px4.io
crsf_telemetry.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "crsf_telemetry.h"
35 #include <lib/rc/crsf.h>
36 
38  _uart_fd(uart_fd)
39 {
40 }
41 
43 {
44  const int update_rate_hz = 10;
45 
46  if (now - _last_update <= 1_s / (update_rate_hz * num_data_types)) {
47  return false;
48  }
49 
50  bool sent = false;
51 
52  switch (_next_type) {
53  case 0:
54  sent = send_battery();
55  break;
56 
57  case 1:
58  sent = send_gps();
59  break;
60 
61  case 2:
62  sent = send_attitude();
63  break;
64 
65  case 3:
66  sent = send_flight_mode();
67  break;
68  }
69 
70  _last_update = now;
71  _next_type = (_next_type + 1) % num_data_types;
72 
73  return sent;
74 }
75 
77 {
79 
80  if (!_battery_status_sub.update(&battery_status)) {
81  return false;
82  }
83 
84  uint16_t voltage = battery_status.voltage_filtered_v * 10;
85  uint16_t current = battery_status.current_filtered_a * 10;
86  int fuel = battery_status.discharged_mah;
87  uint8_t remaining = battery_status.remaining * 100;
88  return crsf_send_telemetry_battery(_uart_fd, voltage, current, fuel, remaining);
89 }
90 
92 {
93  vehicle_gps_position_s vehicle_gps_position;
94 
95  if (!_vehicle_gps_position_sub.update(&vehicle_gps_position)) {
96  return false;
97  }
98 
99  int32_t latitude = vehicle_gps_position.lat;
100  int32_t longitude = vehicle_gps_position.lon;
101  uint16_t groundspeed = vehicle_gps_position.vel_d_m_s / 3.6f * 10.f;
102  uint16_t gps_heading = math::degrees(vehicle_gps_position.cog_rad) * 100.f;
103  uint16_t altitude = vehicle_gps_position.alt + 1000;
104  uint8_t num_satellites = vehicle_gps_position.satellites_used;
105 
106  return crsf_send_telemetry_gps(_uart_fd, latitude, longitude, groundspeed,
107  gps_heading, altitude, num_satellites);
108 }
109 
111 {
112  vehicle_attitude_s vehicle_attitude;
113 
114  if (!_vehicle_attitude_sub.update(&vehicle_attitude)) {
115  return false;
116  }
117 
118  matrix::Eulerf attitude = matrix::Quatf(vehicle_attitude.q);
119  int16_t pitch = attitude(1) * 1e4f;
120  int16_t roll = attitude(0) * 1e4f;
121  int16_t yaw = attitude(2) * 1e4f;
122  return crsf_send_telemetry_attitude(_uart_fd, pitch, roll, yaw);
123 }
124 
126 {
127  vehicle_status_s vehicle_status;
128 
129  if (!_vehicle_status_sub.update(&vehicle_status)) {
130  return false;
131  }
132 
133  const char *flight_mode = "(unknown)";
134 
135  switch (vehicle_status.nav_state) {
136  case vehicle_status_s::NAVIGATION_STATE_MANUAL:
137  flight_mode = "Manual";
138  break;
139 
140  case vehicle_status_s::NAVIGATION_STATE_ALTCTL:
141  flight_mode = "Altitude";
142  break;
143 
144  case vehicle_status_s::NAVIGATION_STATE_POSCTL:
145  flight_mode = "Position";
146  break;
147 
148  case vehicle_status_s::NAVIGATION_STATE_AUTO_RTL:
149  flight_mode = "Return";
150  break;
151 
152  case vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION:
153  flight_mode = "Mission";
154  break;
155 
156  case vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER:
157  case vehicle_status_s::NAVIGATION_STATE_AUTO_RCRECOVER:
158  case vehicle_status_s::NAVIGATION_STATE_AUTO_RTGS:
159  case vehicle_status_s::NAVIGATION_STATE_DESCEND:
160  case vehicle_status_s::NAVIGATION_STATE_AUTO_TAKEOFF:
161  case vehicle_status_s::NAVIGATION_STATE_AUTO_LAND:
162  case vehicle_status_s::NAVIGATION_STATE_AUTO_FOLLOW_TARGET:
163  case vehicle_status_s::NAVIGATION_STATE_AUTO_PRECLAND:
164  flight_mode = "Auto";
165  break;
166 
167  case vehicle_status_s::NAVIGATION_STATE_AUTO_LANDENGFAIL:
168  case vehicle_status_s::NAVIGATION_STATE_AUTO_LANDGPSFAIL:
169  flight_mode = "Failure";
170  break;
171 
172  case vehicle_status_s::NAVIGATION_STATE_ACRO:
173  flight_mode = "Acro";
174  break;
175 
176  case vehicle_status_s::NAVIGATION_STATE_TERMINATION:
177  flight_mode = "Terminate";
178  break;
179 
180  case vehicle_status_s::NAVIGATION_STATE_OFFBOARD:
181  flight_mode = "Offboard";
182  break;
183 
184  case vehicle_status_s::NAVIGATION_STATE_STAB:
185  flight_mode = "Stabilized";
186  break;
187 
188  case vehicle_status_s::NAVIGATION_STATE_RATTITUDE:
189  flight_mode = "Rattitude";
190  break;
191  }
192 
193  return crsf_send_telemetry_flight_mode(_uart_fd, flight_mode);
194 }
bool crsf_send_telemetry_gps(int uart_fd, int32_t latitude, int32_t longitude, uint16_t groundspeed, uint16_t gps_heading, uint16_t altitude, uint8_t num_satellites)
Send telemetry GPS information.
Definition: crsf.cpp:462
uORB::Subscription _vehicle_attitude_sub
bool crsf_send_telemetry_battery(int uart_fd, uint16_t voltage, uint16_t current, int fuel, uint8_t remaining)
Send telemetry battery information.
Definition: crsf.cpp:449
hrt_abstime _last_update
bool crsf_send_telemetry_attitude(int uart_fd, int16_t pitch, int16_t roll, int16_t yaw)
Send telemetry Attitude information.
Definition: crsf.cpp:478
constexpr T degrees(T radians)
Definition: Limits.hpp:91
uORB::Subscription _vehicle_gps_position_sub
bool update(const hrt_abstime &now)
Send telemetry data.
uORB::Subscription _battery_status_sub
Euler angles class.
Definition: AxisAngle.hpp:18
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
static constexpr int num_data_types
number of different telemetry data types
Quaternion< float > Quatf
Definition: Quaternion.hpp:544
CRSFTelemetry(int uart_fd)
bool update(void *dst)
Update the struct.
RC protocol definition for CSRF (TBS Crossfire).
uORB::Subscription _vehicle_status_sub
bool crsf_send_telemetry_flight_mode(int uart_fd, const char *flight_mode)
Send telemetry Flight Mode information.
Definition: crsf.cpp:490