AP_UAVCAN.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. * Author: Eugene Shamaev, Siddharth Bharat Purohit
  16. */
  17. #ifndef AP_UAVCAN_H_
  18. #define AP_UAVCAN_H_
  19. #include <uavcan/uavcan.hpp>
  20. #include <AP_HAL/CAN.h>
  21. #include <AP_HAL/Semaphores.h>
  22. #include <AP_Param/AP_Param.h>
  23. #include <uavcan/helpers/heap_based_pool_allocator.hpp>
  24. #include "AP_UAVCAN_Servers.h"
  25. #include "HVcallback.h"
  26. #ifndef UAVCAN_NODE_POOL_SIZE
  27. #define UAVCAN_NODE_POOL_SIZE 8192
  28. #endif
  29. #ifndef UAVCAN_NODE_POOL_BLOCK_SIZE
  30. #define UAVCAN_NODE_POOL_BLOCK_SIZE 64
  31. #endif
  32. #ifndef UAVCAN_SRV_NUMBER
  33. #define UAVCAN_SRV_NUMBER 18
  34. #endif
  35. #define AP_UAVCAN_SW_VERS_MAJOR 1
  36. #define AP_UAVCAN_SW_VERS_MINOR 0
  37. #define AP_UAVCAN_HW_VERS_MAJOR 1
  38. #define AP_UAVCAN_HW_VERS_MINOR 0
  39. #define AP_UAVCAN_MAX_LED_DEVICES 4
  40. #define HV_motor_number 3
  41. #define LV_motor_number 2
  42. // fwd-declare callback classes
  43. class ButtonCb;
  44. /*
  45. Frontend Backend-Registry Binder: Whenever a message of said DataType_ from new node is received,
  46. the Callback will invoke registery to register the node as separate backend.
  47. */
  48. #define UC_REGISTRY_BINDER(ClassName_, DataType_) \
  49. class ClassName_ : public AP_UAVCAN::RegistryBinder<DataType_> { \
  50. typedef void (*CN_Registry)(AP_UAVCAN*, uint8_t, const ClassName_&); \
  51. public: \
  52. ClassName_() : RegistryBinder() {} \
  53. ClassName_(AP_UAVCAN* uc, CN_Registry ffunc) : \
  54. RegistryBinder(uc, (Registry)ffunc) {} \
  55. }
  56. //---------selfdefine type start-------------
  57. struct DRIVEBOX_ABNORMAL
  58. {
  59. uint16_t drive0:8;
  60. uint16_t drive1:8;
  61. };
  62. union DRIVEBOX_ABNORMAL_UNI
  63. {
  64. uint16_t all;
  65. struct DRIVEBOX_ABNORMAL bit;
  66. };
  67. struct PROPELLER_MOTOR_ABNORMAL
  68. {//电机缠绕等级
  69. uint16_t motor0:4;
  70. uint16_t motor1:4;
  71. uint16_t motor2:4;
  72. uint16_t motor3:4;
  73. uint16_t motor4:4;
  74. uint16_t motor5:4;
  75. uint16_t motor6:4;
  76. uint16_t motor7:4;
  77. };
  78. //电机缠绕等级
  79. union PROPELLER_ABNORMAL_UNI
  80. {
  81. uint32_t all;
  82. struct PROPELLER_MOTOR_ABNORMAL bit;
  83. };
  84. struct STM32board_set_N25
  85. {
  86. float track_P;
  87. float track_I;
  88. float track_D;
  89. float brush_P;
  90. float brush_I;
  91. float brush_D;
  92. float Lowspeed_protect;
  93. float Highspeed_protect;
  94. int16_t Speed_seg;
  95. int16_t Max_current;
  96. int16_t Max_time;
  97. int16_t Reboot;
  98. uint8_t reverse_motor;
  99. };
  100. struct ESCTelemetryData {
  101. uint32_t error_count; //# Resets when the motor restarts
  102. float voltage; //# Volt
  103. float current; //# Ampere. Can be negative in case of a regenerative braking.
  104. float temperature; //#
  105. int32_t rpm; //# Negative value indicates reverse rotation
  106. uint8_t power_rating_pct;// # Instant demand factor in percent (percent of maximum power); range 0% to 127%.
  107. uint8_t esc_index;
  108. };
  109. //---------selfdefine type end-------------
  110. class AP_UAVCAN : public AP_HAL::CANProtocol {
  111. public:
  112. AP_UAVCAN();
  113. ~AP_UAVCAN();
  114. //--------self define start--------------------
  115. struct ESCTelemetryData thruster[6];
  116. struct HVmes HVmotor1;//高压电调1
  117. struct HVmes HVmotor2;//高压电调2
  118. struct HVmes HVmotor3;//高压电调3
  119. int16_t motor_from_stm32[12]; //STM32发来的数据
  120. uint16_t motor_stall_flag;//堵转标志
  121. uint32_t propellerblock_flag;//推进器缠绕
  122. int16_t temperature_48Vpower;//48V温度
  123. int16_t board_voltage;//48V电源
  124. int16_t driverleakstate;//驱动仓漏水
  125. static AP_UAVCAN *_singleton;
  126. static AP_UAVCAN *get_singleton() {
  127. return _singleton;
  128. }
  129. int16_t last_thrust_hv[HV_motor_number];
  130. //--------self define end--------------------
  131. static const struct AP_Param::GroupInfo var_info[];
  132. // Return uavcan from @driver_index or nullptr if it's not ready or doesn't exist
  133. static AP_UAVCAN *get_uavcan(uint8_t driver_index);
  134. void init(uint8_t driver_index, bool enable_filters) override;
  135. uavcan::Node<0>* get_node() { return _node; }
  136. uint8_t get_driver_index() { return _driver_index; }
  137. ///// SRV output /////
  138. void SRV_push_servos(void);
  139. ///// LED /////
  140. bool led_write(uint8_t led_index, uint8_t red, uint8_t green, uint8_t blue);
  141. // buzzer
  142. void set_buzzer_tone(float frequency, float duration_s);
  143. template <typename DataType_>
  144. class RegistryBinder {
  145. protected:
  146. typedef void (*Registry)(AP_UAVCAN* _ap_uavcan, uint8_t _node_id, const RegistryBinder& _cb);
  147. AP_UAVCAN* _uc;
  148. Registry _ffunc;
  149. public:
  150. RegistryBinder() :
  151. _uc(),
  152. _ffunc(),
  153. msg() {}
  154. RegistryBinder(AP_UAVCAN* uc, Registry ffunc) :
  155. _uc(uc),
  156. _ffunc(ffunc),
  157. msg(nullptr) {}
  158. void operator()(const uavcan::ReceivedDataStructure<DataType_>& _msg) {
  159. msg = &_msg;
  160. _ffunc(_uc, _msg.getSrcNodeID().get(), *this);
  161. }
  162. const uavcan::ReceivedDataStructure<DataType_> *msg;
  163. };
  164. private:
  165. class SystemClock: public uavcan::ISystemClock, uavcan::Noncopyable {
  166. public:
  167. SystemClock() = default;
  168. void adjustUtc(uavcan::UtcDuration adjustment) override {
  169. utc_adjustment_usec = adjustment.toUSec();
  170. }
  171. uavcan::MonotonicTime getMonotonic() const override {
  172. return uavcan::MonotonicTime::fromUSec(AP_HAL::micros64());
  173. }
  174. uavcan::UtcTime getUtc() const override {
  175. return uavcan::UtcTime::fromUSec(AP_HAL::micros64() + utc_adjustment_usec);
  176. }
  177. static SystemClock& instance() {
  178. static SystemClock inst;
  179. return inst;
  180. }
  181. private:
  182. int64_t utc_adjustment_usec;
  183. };
  184. // This will be needed to implement if UAVCAN is used with multithreading
  185. // Such cases will be firmware update, etc.
  186. class RaiiSynchronizer {};
  187. void loop(void);
  188. ///// SRV output /////
  189. void SRV_send_actuator();
  190. void SRV_send_esc();
  191. ///// LED /////
  192. void led_out_send();
  193. // buzzer
  194. void buzzer_send();
  195. // SafetyState
  196. void safety_state_send();
  197. uavcan::PoolAllocator<UAVCAN_NODE_POOL_SIZE, UAVCAN_NODE_POOL_BLOCK_SIZE, AP_UAVCAN::RaiiSynchronizer> _node_allocator;
  198. // UAVCAN parameters
  199. AP_Int8 _uavcan_node;
  200. AP_Int32 _servo_bm;
  201. AP_Int32 _esc_bm;
  202. AP_Int16 _servo_rate_hz;
  203. uavcan::Node<0> *_node;
  204. uint8_t _driver_index;
  205. char _thread_name[9];
  206. bool _initialized;
  207. #ifdef HAS_UAVCAN_SERVERS
  208. AP_UAVCAN_Servers _servers;
  209. #endif
  210. ///// SRV output /////
  211. struct {
  212. uint16_t pulse;
  213. bool esc_pending;
  214. bool servo_pending;
  215. } _SRV_conf[UAVCAN_SRV_NUMBER];
  216. uint8_t _SRV_armed;
  217. uint32_t _SRV_last_send_us;
  218. HAL_Semaphore SRV_sem;
  219. ///// LED /////
  220. struct led_device {
  221. uint8_t led_index;
  222. uint8_t red;
  223. uint8_t green;
  224. uint8_t blue;
  225. };
  226. struct {
  227. led_device devices[AP_UAVCAN_MAX_LED_DEVICES];
  228. uint8_t devices_count;
  229. uint64_t last_update;
  230. } _led_conf;
  231. HAL_Semaphore _led_out_sem;
  232. // buzzer
  233. struct {
  234. HAL_Semaphore sem;
  235. float frequency;
  236. float duration;
  237. uint8_t pending_mask; // mask of interfaces to send to
  238. } _buzzer;
  239. // safety status send state
  240. uint32_t _last_safety_state_ms;
  241. // safety button handling
  242. static void handle_button(AP_UAVCAN* ap_uavcan, uint8_t node_id, const ButtonCb &cb);
  243. };
  244. namespace AP {
  245. AP_UAVCAN &uavcan();
  246. };
  247. #endif /* AP_UAVCAN_H_ */