PX4 Firmware
PX4 Autopilot Software http://px4.io
telemetry.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 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 #pragma once
35 
36 #include <drivers/drv_hrt.h>
37 
39 {
40 public:
41  struct EscData {
43  int8_t temperature; ///< [deg C]
44  int16_t voltage; ///< [0.01V]
45  int16_t current; ///< [0.01A]
46  int16_t consumption; ///< [mAh]
47  int16_t erpm; ///< [100ERPM]
48  };
49 
50  static constexpr int esc_info_size_blheli32 = 64;
51  static constexpr int esc_info_size_kiss_v1 = 15;
52  static constexpr int esc_info_size_kiss_v2 = 21;
53  static constexpr int max_esc_info_size = esc_info_size_blheli32;
54 
55  struct OutputBuffer {
56  uint8_t buffer[max_esc_info_size];
57  int buf_pos{0};
59  };
60 
62 
63  int init(const char *uart_device);
64 
65  void deinit();
66 
67  void setNumMotors(int num_motors) { _num_motors = num_motors; }
68  int numMotors() const { return _num_motors; }
69 
70  /**
71  * Read telemetry from the UART (non-blocking) and handle timeouts.
72  * @return -1 if no update, -2 timeout, >= 0 for the motor index. Use @latestESCData() to get the data.
73  */
74  int update();
75 
76  /**
77  * Redirect everything that is read into a different buffer.
78  * Future calls to @update will write to that instead of an internal buffer, until @update returns
79  * a value different from -1. No decoding is done.
80  * The caller must ensure the buffer exists until that point.
81  * @param buffer
82  * @return 0 on success <0 on error
83  */
84  int redirectOutput(OutputBuffer &buffer);
85 
86  bool redirectActive() const { return _redirect_output != nullptr; }
87 
88  /**
89  * Get the motor index for which telemetry should be requested.
90  * @return -1 if no request should be made, motor index otherwise
91  */
93 
94  const EscData &latestESCData() const { return _latest_data; }
95 
96  /**
97  * Check whether we are currently expecting to read new data from an ESC
98  */
99  bool expectingData() const { return _current_request_start != 0; }
100 
101  void printStatus() const;
102 
103  static void decodeAndPrintEscInfoPacket(const OutputBuffer &buffer);
104 
105 private:
106  static constexpr int ESC_FRAME_SIZE = 10;
107 
108  /**
109  * set the Baudrate
110  * @param baud
111  * @return 0 on success, <0 on error
112  */
113  int setBaudrate(unsigned baud);
114 
115  void requestNextMotor();
116 
117  /**
118  * Decode a single byte from an ESC feedback frame
119  * @param byte
120  * @param successful_decoding set to true if checksum matches
121  * @return true if received the expected amount of bytes and the next motor can be requested
122  */
123  bool decodeByte(uint8_t byte, bool &successful_decoding);
124 
125  static inline uint8_t updateCrc8(uint8_t crc, uint8_t crc_seed);
126  static uint8_t crc8(const uint8_t *buf, uint8_t len);
127 
128  int _uart_fd{-1};
129  int _num_motors{0};
132 
134 
137 
138  OutputBuffer *_redirect_output{nullptr}; ///< if set, all read bytes are stored here instead of the internal buffer
139 
140  // statistics
143 };
int numMotors() const
Definition: telemetry.h:68
int16_t current
[0.01A]
Definition: telemetry.h:45
static uint8_t crc8(const uint8_t *buf, uint8_t len)
Definition: telemetry.cpp:212
static uint8_t updateCrc8(uint8_t crc, uint8_t crc_seed)
Definition: telemetry.cpp:200
int16_t erpm
[100ERPM]
Definition: telemetry.h:47
uint8_t _frame_buffer[ESC_FRAME_SIZE]
Definition: telemetry.h:130
bool decodeByte(uint8_t byte, bool &successful_decoding)
Decode a single byte from an ESC feedback frame.
Definition: telemetry.cpp:164
int getRequestMotorIndex()
Get the motor index for which telemetry should be requested.
Definition: telemetry.cpp:231
void setNumMotors(int num_motors)
Definition: telemetry.h:67
static constexpr int esc_info_size_kiss_v1
Definition: telemetry.h:51
int8_t temperature
[deg C]
Definition: telemetry.h:43
High-resolution timer with callouts and timekeeping.
void printStatus() const
Definition: telemetry.cpp:194
EscData _latest_data
Definition: telemetry.h:133
int _num_successful_responses
Definition: telemetry.h:142
int update()
Read telemetry from the UART (non-blocking) and handle timeouts.
Definition: telemetry.cpp:90
const EscData & latestESCData() const
Definition: telemetry.h:94
int setBaudrate(unsigned baud)
set the Baudrate
Definition: telemetry.cpp:399
bool expectingData() const
Check whether we are currently expecting to read new data from an ESC.
Definition: telemetry.h:99
void requestNextMotor()
Definition: telemetry.cpp:224
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
int16_t voltage
[0.01V]
Definition: telemetry.h:44
int redirectOutput(OutputBuffer &buffer)
Redirect everything that is read into a different buffer.
Definition: telemetry.cpp:76
bool redirectActive() const
Definition: telemetry.h:86
int16_t consumption
[mAh]
Definition: telemetry.h:46
int _frame_position
Definition: telemetry.h:131
int init(const char *uart_device)
Definition: telemetry.cpp:52
static constexpr int esc_info_size_blheli32
Definition: telemetry.h:50
static constexpr int max_esc_info_size
Definition: telemetry.h:53
static void decodeAndPrintEscInfoPacket(const OutputBuffer &buffer)
Definition: telemetry.cpp:242
OutputBuffer * _redirect_output
if set, all read bytes are stored here instead of the internal buffer
Definition: telemetry.h:138
static constexpr int esc_info_size_kiss_v2
Definition: telemetry.h:52
int _current_motor_index_request
Definition: telemetry.h:135
hrt_abstime _current_request_start
Definition: telemetry.h:136
static constexpr int ESC_FRAME_SIZE
Definition: telemetry.h:106