AP_RCProtocol_ST24.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #pragma once
  18. #include "AP_RCProtocol.h"
  19. #include "SoftSerial.h"
  20. #define ST24_DATA_LEN_MAX 64
  21. #define ST24_MAX_FRAMELEN 70
  22. #define ST24_STX1 0x55
  23. #define ST24_STX2 0x55
  24. /* define range mapping here, -+100% -> 1000..2000 */
  25. #define ST24_RANGE_MIN 0.0f
  26. #define ST24_RANGE_MAX 4096.0f
  27. #define ST24_TARGET_MIN 1000.0f
  28. #define ST24_TARGET_MAX 2000.0f
  29. /* pre-calculate the floating point stuff as far as possible at compile time */
  30. #define ST24_SCALE_FACTOR ((ST24_TARGET_MAX - ST24_TARGET_MIN) / (ST24_RANGE_MAX - ST24_RANGE_MIN))
  31. #define ST24_SCALE_OFFSET (int)(ST24_TARGET_MIN - (ST24_SCALE_FACTOR * ST24_RANGE_MIN + 0.5f))
  32. class AP_RCProtocol_ST24 : public AP_RCProtocol_Backend {
  33. public:
  34. AP_RCProtocol_ST24(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {}
  35. void process_pulse(uint32_t width_s0, uint32_t width_s1) override;
  36. void process_byte(uint8_t byte, uint32_t baudrate) override;
  37. private:
  38. void _process_byte(uint8_t byte);
  39. static uint8_t st24_crc8(uint8_t *ptr, uint8_t len);
  40. enum ST24_PACKET_TYPE {
  41. ST24_PACKET_TYPE_CHANNELDATA12 = 0,
  42. ST24_PACKET_TYPE_CHANNELDATA24,
  43. ST24_PACKET_TYPE_TRANSMITTERGPSDATA
  44. };
  45. #pragma pack(push, 1)
  46. typedef struct {
  47. uint8_t header1; ///< 0x55 for a valid packet
  48. uint8_t header2; ///< 0x55 for a valid packet
  49. uint8_t length; ///< length includes type, data, and crc = sizeof(type)+sizeof(data[payload_len])+sizeof(crc8)
  50. uint8_t type; ///< from enum ST24_PACKET_TYPE
  51. uint8_t st24_data[ST24_DATA_LEN_MAX];
  52. uint8_t crc8; ///< crc8 checksum, calculated by st24_common_crc8 and including fields length, type and st24_data
  53. } ReceiverFcPacket;
  54. /**
  55. * RC Channel data (12 channels).
  56. *
  57. * This is incoming from the ST24
  58. */
  59. typedef struct {
  60. uint16_t t; ///< packet counter or clock
  61. uint8_t rssi; ///< signal strength
  62. uint8_t packet_count; ///< Number of UART packets sent since reception of last RF frame (this tells something about age / rate)
  63. uint8_t channel[18]; ///< channel data, 12 channels (12 bit numbers)
  64. } ChannelData12;
  65. /**
  66. * RC Channel data (12 channels).
  67. *
  68. */
  69. typedef struct {
  70. uint16_t t; ///< packet counter or clock
  71. uint8_t rssi; ///< signal strength
  72. uint8_t packet_count; ///< Number of UART packets sent since reception of last RF frame (this tells something about age / rate)
  73. uint8_t channel[36]; ///< channel data, 24 channels (12 bit numbers)
  74. } ChannelData24;
  75. /**
  76. * Telemetry packet
  77. *
  78. * This is outgoing to the ST24
  79. *
  80. * imuStatus:
  81. * 8 bit total
  82. * bits 0-2 for status
  83. * - value 0 is FAILED
  84. * - value 1 is INITIALIZING
  85. * - value 2 is RUNNING
  86. * - values 3 through 7 are reserved
  87. * bits 3-7 are status for sensors (0 or 1)
  88. * - mpu6050
  89. * - accelerometer
  90. * - primary gyro x
  91. * - primary gyro y
  92. * - primary gyro z
  93. *
  94. * pressCompassStatus
  95. * 8 bit total
  96. * bits 0-3 for compass status
  97. * - value 0 is FAILED
  98. * - value 1 is INITIALIZING
  99. * - value 2 is RUNNING
  100. * - value 3 - 15 are reserved
  101. * bits 4-7 for pressure status
  102. * - value 0 is FAILED
  103. * - value 1 is INITIALIZING
  104. * - value 2 is RUNNING
  105. * - value 3 - 15 are reserved
  106. *
  107. */
  108. typedef struct {
  109. uint16_t t; ///< packet counter or clock
  110. int32_t lat; ///< lattitude (degrees) +/- 90 deg
  111. int32_t lon; ///< longitude (degrees) +/- 180 deg
  112. int32_t alt; ///< 0.01m resolution, altitude (meters)
  113. int16_t vx, vy, vz; ///< velocity 0.01m res, +/-320.00 North-East- Down
  114. uint8_t nsat; ///<number of satellites
  115. uint8_t voltage; ///< 25.4V voltage = 5 + 255*0.1 = 30.5V, min=5V
  116. uint8_t current; ///< 0.5A resolution
  117. int16_t roll, pitch, yaw; ///< 0.01 degree resolution
  118. uint8_t motorStatus; ///< 1 bit per motor for status 1=good, 0= fail
  119. uint8_t imuStatus; ///< inertial measurement unit status
  120. uint8_t pressCompassStatus; ///< baro / compass status
  121. } TelemetryData;
  122. #pragma pack(pop)
  123. enum ST24_DECODE_STATE {
  124. ST24_DECODE_STATE_UNSYNCED = 0,
  125. ST24_DECODE_STATE_GOT_STX1,
  126. ST24_DECODE_STATE_GOT_STX2,
  127. ST24_DECODE_STATE_GOT_LEN,
  128. ST24_DECODE_STATE_GOT_TYPE,
  129. ST24_DECODE_STATE_GOT_DATA
  130. };
  131. enum ST24_DECODE_STATE _decode_state = ST24_DECODE_STATE_UNSYNCED;
  132. uint8_t _rxlen;
  133. ReceiverFcPacket _rxpacket;
  134. SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1};
  135. };