PX4 Firmware
PX4 Autopilot Software http://px4.io
crsf.h
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 /**
35  * @file crsf.h
36  *
37  * RC protocol definition for CSRF (TBS Crossfire).
38  * It is an uninverted protocol at 420000 baudrate.
39  *
40  * RC channels come in at 150Hz.
41  *
42  * @author Beat Küng <beat-kueng@gmx.net>
43  */
44 
45 #pragma once
46 
47 #include <stdint.h>
48 #include <px4_platform_common/defines.h>
49 
51 
52 #define CRSF_FRAME_SIZE_MAX 30 // the actual maximum length is 64, but we're only interested in RC channels and want to minimize buffer size
53 #define CRSF_PAYLOAD_SIZE_MAX (CRSF_FRAME_SIZE_MAX-4)
54 
55 
57  uint8_t device_address; ///< @see crsf_address_t
58  uint8_t length; ///< length of crsf_frame_t (including CRC) minus sizeof(crsf_frame_header_t)
59 };
60 
61 struct crsf_frame_t {
63  uint8_t type; ///< @see crsf_frame_type_t
64  uint8_t payload[CRSF_PAYLOAD_SIZE_MAX + 1]; ///< payload data including 1 byte CRC at end
65 };
66 
67 /**
68  * Configure an UART port to be used for CRSF
69  * @param uart_fd UART file descriptor
70  * @return 0 on success, -errno otherwise
71  */
72 __EXPORT int crsf_config(int uart_fd);
73 
74 
75 /**
76  * Parse the CRSF protocol and extract RC channel data.
77  *
78  * @param now current time
79  * @param frame data to parse
80  * @param len length of frame
81  * @param values output channel values, each in range [1000, 2000]
82  * @param num_values set to the number of parsed channels in values
83  * @param max_channels maximum length of values
84  * @return true if channels successfully decoded
85  */
86 __EXPORT bool crsf_parse(const uint64_t now, const uint8_t *frame, unsigned len, uint16_t *values,
87  uint16_t *num_values, uint16_t max_channels);
88 
89 
90 /**
91  * Send telemetry battery information
92  * @param uart_fd UART file descriptor
93  * @param voltage Voltage [0.1V]
94  * @param current Current [0.1A]
95  * @param fuel drawn mAh
96  * @param remaining battery remaining [%]
97  * @return true on success
98  */
99 __EXPORT bool crsf_send_telemetry_battery(int uart_fd, uint16_t voltage, uint16_t current, int fuel, uint8_t remaining);
100 
101 /**
102  * Send telemetry GPS information
103  * @param uart_fd UART file descriptor
104  * @param latitude latitude [degree * 1e7]
105  * @param longitude longitude [degree * 1e7]
106  * @param groundspeed Ground speed [km/h * 10]
107  * @param gps_heading GPS heading [degree * 100]
108  * @param altitude Altitude [meters + 1000m offset]
109  * @param num_satellites number of satellites used
110  * @return true on success
111  */
112 __EXPORT bool crsf_send_telemetry_gps(int uart_fd, int32_t latitude, int32_t longitude, uint16_t groundspeed,
113  uint16_t gps_heading, uint16_t altitude, uint8_t num_satellites);
114 
115 
116 /**
117  * Send telemetry Attitude information
118  * @param uart_fd UART file descriptor
119  * @param pitch Pitch angle [rad * 1e4]
120  * @param roll Roll angle [rad * 1e4]
121  * @param yaw Yaw angle [rad * 1e4]
122  * @return true on success
123  */
124 __EXPORT bool crsf_send_telemetry_attitude(int uart_fd, int16_t pitch, int16_t roll, int16_t yaw);
125 
126 /**
127  * Send telemetry Flight Mode information
128  * @param uart_fd UART file descriptor
129  * @param flight_mode Flight Mode string (max length = 15)
130  * @return true on success
131  */
132 __EXPORT bool crsf_send_telemetry_flight_mode(int uart_fd, const char *flight_mode);
133 
__EXPORT bool crsf_send_telemetry_flight_mode(int uart_fd, const char *flight_mode)
Send telemetry Flight Mode information.
Definition: crsf.cpp:490
__EXPORT int crsf_config(int uart_fd)
Configure an UART port to be used for CRSF.
Definition: crsf.cpp:147
__EXPORT 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
__EXPORT 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
#define __END_DECLS
Definition: visibility.h:59
__EXPORT bool crsf_parse(const uint64_t now, const uint8_t *frame, unsigned len, uint16_t *values, uint16_t *num_values, uint16_t max_channels)
Parse the CRSF protocol and extract RC channel data.
Definition: crsf.cpp:166
Definition: I2C.hpp:51
uint8_t length
length of crsf_frame_t (including CRC) minus sizeof(crsf_frame_header_t)
Definition: crsf.h:58
uint8_t device_address
Definition: crsf.h:57
#define __BEGIN_DECLS
Definition: visibility.h:58
crsf_frame_header_t header
Definition: crsf.h:62
__EXPORT 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
uint8_t type
Definition: crsf.h:63
#define CRSF_PAYLOAD_SIZE_MAX
Definition: crsf.h:53