Line data Source code
1 : /* 2 : * This file is free software: you can redistribute it and/or modify it 3 : * under the terms of the GNU General Public License as published by the 4 : * Free Software Foundation, either version 3 of the License, or 5 : * (at your option) any later version. 6 : * 7 : * This file is distributed in the hope that it will be useful, but 8 : * WITHOUT ANY WARRANTY; without even the implied warranty of 9 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 : * See the GNU General Public License for more details. 11 : * 12 : * You should have received a copy of the GNU General Public License along 13 : * with this program. If not, see <http://www.gnu.org/licenses/>. 14 : * 15 : * Code by Andrew Tridgell and Siddharth Bharat Purohit 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "AP_RCProtocol_config.h" 21 : 22 : #if AP_RCPROTOCOL_ST24_ENABLED 23 : 24 : #include "AP_RCProtocol_Backend.h" 25 : #include "SoftSerial.h" 26 : 27 : #define ST24_DATA_LEN_MAX 64 28 : #define ST24_MAX_FRAMELEN 70 29 : #define ST24_STX1 0x55 30 : #define ST24_STX2 0x55 31 : 32 : /* define range mapping here, -+100% -> 1000..2000 */ 33 : #define ST24_RANGE_MIN 0.0f 34 : #define ST24_RANGE_MAX 4096.0f 35 : 36 : #define ST24_TARGET_MIN 1000.0f 37 : #define ST24_TARGET_MAX 2000.0f 38 : 39 : /* pre-calculate the floating point stuff as far as possible at compile time */ 40 : #define ST24_SCALE_FACTOR ((ST24_TARGET_MAX - ST24_TARGET_MIN) / (ST24_RANGE_MAX - ST24_RANGE_MIN)) 41 : #define ST24_SCALE_OFFSET (int)(ST24_TARGET_MIN - (ST24_SCALE_FACTOR * ST24_RANGE_MIN + 0.5f)) 42 : 43 : class AP_RCProtocol_ST24 : public AP_RCProtocol_Backend { 44 : public: 45 2094 : AP_RCProtocol_ST24(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {} 46 : void process_pulse(uint32_t width_s0, uint32_t width_s1) override; 47 : void process_byte(uint8_t byte, uint32_t baudrate) override; 48 : private: 49 : void _process_byte(uint8_t byte); 50 : static uint8_t st24_crc8(uint8_t *ptr, uint8_t len); 51 : enum ST24_PACKET_TYPE { 52 : ST24_PACKET_TYPE_CHANNELDATA12 = 0, 53 : ST24_PACKET_TYPE_CHANNELDATA24, 54 : ST24_PACKET_TYPE_TRANSMITTERGPSDATA 55 : }; 56 : 57 : #pragma pack(push, 1) 58 : typedef struct { 59 : uint8_t header1; ///< 0x55 for a valid packet 60 : uint8_t header2; ///< 0x55 for a valid packet 61 : uint8_t length; ///< length includes type, data, and crc = sizeof(type)+sizeof(data[payload_len])+sizeof(crc8) 62 : uint8_t type; ///< from enum ST24_PACKET_TYPE 63 : uint8_t st24_data[ST24_DATA_LEN_MAX]; 64 : uint8_t crc8; ///< crc8 checksum, calculated by st24_common_crc8 and including fields length, type and st24_data 65 : } ReceiverFcPacket; 66 : 67 : /** 68 : * RC Channel data (12 channels). 69 : * 70 : * This is incoming from the ST24 71 : */ 72 : typedef struct { 73 : uint16_t t; ///< packet counter or clock 74 : uint8_t rssi; ///< signal strength 75 : uint8_t packet_count; ///< Number of UART packets sent since reception of last RF frame (this tells something about age / rate) 76 : uint8_t channel[18]; ///< channel data, 12 channels (12 bit numbers) 77 : } ChannelData12; 78 : 79 : /** 80 : * RC Channel data (12 channels). 81 : * 82 : */ 83 : typedef struct { 84 : uint16_t t; ///< packet counter or clock 85 : uint8_t rssi; ///< signal strength 86 : uint8_t packet_count; ///< Number of UART packets sent since reception of last RF frame (this tells something about age / rate) 87 : uint8_t channel[36]; ///< channel data, 24 channels (12 bit numbers) 88 : } ChannelData24; 89 : 90 : /** 91 : * Telemetry packet 92 : * 93 : * This is outgoing to the ST24 94 : * 95 : * imuStatus: 96 : * 8 bit total 97 : * bits 0-2 for status 98 : * - value 0 is FAILED 99 : * - value 1 is INITIALIZING 100 : * - value 2 is RUNNING 101 : * - values 3 through 7 are reserved 102 : * bits 3-7 are status for sensors (0 or 1) 103 : * - mpu6050 104 : * - accelerometer 105 : * - primary gyro x 106 : * - primary gyro y 107 : * - primary gyro z 108 : * 109 : * pressCompassStatus 110 : * 8 bit total 111 : * bits 0-3 for compass status 112 : * - value 0 is FAILED 113 : * - value 1 is INITIALIZING 114 : * - value 2 is RUNNING 115 : * - value 3 - 15 are reserved 116 : * bits 4-7 for pressure status 117 : * - value 0 is FAILED 118 : * - value 1 is INITIALIZING 119 : * - value 2 is RUNNING 120 : * - value 3 - 15 are reserved 121 : * 122 : */ 123 : typedef struct { 124 : uint16_t t; ///< packet counter or clock 125 : int32_t lat; ///< lattitude (degrees) +/- 90 deg 126 : int32_t lon; ///< longitude (degrees) +/- 180 deg 127 : int32_t alt; ///< 0.01m resolution, altitude (meters) 128 : int16_t vx, vy, vz; ///< velocity 0.01m res, +/-320.00 North-East- Down 129 : uint8_t nsat; ///<number of satellites 130 : uint8_t voltage; ///< 25.4V voltage = 5 + 255*0.1 = 30.5V, min=5V 131 : uint8_t current; ///< 0.5A resolution 132 : int16_t roll, pitch, yaw; ///< 0.01 degree resolution 133 : uint8_t motorStatus; ///< 1 bit per motor for status 1=good, 0= fail 134 : uint8_t imuStatus; ///< inertial measurement unit status 135 : uint8_t pressCompassStatus; ///< baro / compass status 136 : } TelemetryData; 137 : 138 : #pragma pack(pop) 139 : 140 : enum ST24_DECODE_STATE { 141 : ST24_DECODE_STATE_UNSYNCED = 0, 142 : ST24_DECODE_STATE_GOT_STX1, 143 : ST24_DECODE_STATE_GOT_STX2, 144 : ST24_DECODE_STATE_GOT_LEN, 145 : ST24_DECODE_STATE_GOT_TYPE, 146 : ST24_DECODE_STATE_GOT_DATA 147 : }; 148 : 149 : enum ST24_DECODE_STATE _decode_state = ST24_DECODE_STATE_UNSYNCED; 150 : uint8_t _rxlen; 151 : 152 : ReceiverFcPacket _rxpacket; 153 : 154 : SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1}; 155 : }; 156 : 157 : #endif // AP_RCPROTOCOL_ST24_ENABLED