Line data Source code
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 : i-BUS telemetry for FlySky/Turnigy receivers and other peripherals 17 : (eg iA6B, iA10) by Nicole Ashley <[email protected]>. 18 : 19 : Originally based on work by Jan Verhulst: 20 : https://github.com/ArduPilot/ardupilot/pull/16545 21 : 22 : Libraries used for reference and inspiration: 23 : 24 : * iBUStelemetry 25 : https://github.com/Hrastovc/iBUStelemetry 26 : 27 : * IBusBM 28 : https://github.com/bmellink/IBusBM 29 : 30 : * BetaFlight 31 : https://github.com/betaflight/betaflight/blob/master/src/main/telemetry/ibus_shared.c 32 : */ 33 : 34 : #pragma once 35 : 36 : #include <AP_HAL/AP_HAL_Boards.h> 37 : 38 : #ifndef AP_IBUS_TELEM_ENABLED 39 : #define AP_IBUS_TELEM_ENABLED HAL_PROGRAM_SIZE_LIMIT_KB > 2048 40 : #endif 41 : 42 : #if AP_IBUS_TELEM_ENABLED 43 : 44 : #include <AP_HAL/AP_HAL.h> 45 : #include <AP_Math/vector3.h> 46 : #include <AP_AHRS/AP_AHRS_config.h> 47 : #include <AP_BattMonitor/AP_BattMonitor_config.h> 48 : #include <AP_GPS/AP_GPS_config.h> 49 : #include <AP_RPM/AP_RPM_config.h> 50 : 51 : class AP_IBus_Telem 52 : { 53 : public: 54 2098 : AP_IBus_Telem() {} 55 : 56 : CLASS_NO_COPY(AP_IBus_Telem); 57 : 58 : void init(); 59 : 60 : typedef struct { 61 : uint8_t sensor_type; // Sensor type (IBUS_SENSOR_TYPE_* above) 62 : uint8_t sensor_length; // Data length for defined sensor (can be 2 or 4 bytes) 63 : } SensorDefinition; 64 : 65 : typedef struct { 66 : uint8_t ap_mode; 67 : uint8_t ibus_mode; 68 : } ModeMap; 69 : 70 : private: 71 : 72 : typedef union { 73 : uint16_t uint16; 74 : uint32_t uint32; 75 : int16_t int16; 76 : int32_t int32; 77 : uint8_t byte[4]; 78 : } SensorValue; 79 : 80 : struct PACKED CommandPacket { 81 : uint8_t message_length; 82 : uint8_t sensor_id : 4; 83 : uint8_t command : 4; 84 : uint16_t checksum; 85 : }; 86 : 87 : void tick(); 88 : void handle_incoming_message(const CommandPacket &incoming); 89 : void handle_discover_command(const CommandPacket &incoming); 90 : void handle_type_command(const CommandPacket &incoming, const SensorDefinition &sensor); 91 : void handle_value_command(const CommandPacket &incoming, const SensorDefinition &sensor); 92 : void handle_2_byte_value_command(const CommandPacket &incoming, const SensorValue &value); 93 : void handle_4_byte_value_command(const CommandPacket &incoming, const SensorValue &value); 94 : SensorValue get_sensor_value(uint8_t sensor_id); 95 : #if AP_BATTERY_ENABLED 96 : uint16_t get_average_cell_voltage_cV(); 97 : uint16_t get_current_cAh(); 98 : uint8_t get_battery_or_fuel_level_pct(); 99 : #endif 100 : #if AP_RPM_ENABLED 101 : uint16_t get_rpm(); 102 : #endif 103 : #if AP_GPS_ENABLED 104 : uint8_t get_gps_status(); 105 : #endif 106 : #if AP_AHRS_ENABLED 107 : uint16_t get_distance_from_home_m(); 108 : #endif 109 : uint16_t get_vehicle_mode(); 110 : void populate_checksum(uint8_t *packet, const uint16_t size); 111 : 112 : static const uint8_t PROTOCOL_COMMAND_DISCOVER = 0x80; // Command to discover a sensor (lowest 4 bits are sensor) 113 : static const uint8_t PROTOCOL_COMMAND_TYPE = 0x90; // Command to request a sensor type (lowest 4 bits are sensor) 114 : static const uint8_t PROTOCOL_COMMAND_VALUE = 0xA0; // Command to request a sensor's value (lowest 4 bits are sensor) 115 : static const uint8_t PROTOCOL_TIMEGAP = 3; // Packets are received very ~7ms so use ~half that for the gap 116 : static const uint8_t PROTOCOL_FOUR_LENGTH = 0x04; // indicate that the message has 4 bytes 117 : static const uint8_t PROTOCOL_SIX_LENGTH = 0x06; // indicate that the message has 6 bytes 118 : static const uint8_t PROTOCOL_EIGHT_LENGTH = 0x08; // indicate that the message has 8 bytes 119 : static const uint8_t PROTOCOL_INCOMING_MESSAGE_LENGTH = PROTOCOL_FOUR_LENGTH; // All incoming messages are the same length 120 : 121 : AP_HAL::UARTDriver *port; 122 : bool initialized; 123 : uint32_t last_received_message_time_ms; // When the last message was received 124 : }; 125 : 126 : #endif