AP_UAVCAN.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. //---------selfdefine type end-------------
  101. class AP_UAVCAN : public AP_HAL::CANProtocol {
  102. public:
  103. AP_UAVCAN();
  104. ~AP_UAVCAN();
  105. //--------self define start--------------------
  106. struct HVmes HVmotor1;//高压电调1
  107. struct HVmes HVmotor2;//高压电调2
  108. struct HVmes HVmotor3;//高压电调3
  109. int16_t motor_from_stm32[12]; //STM32发来的数据
  110. uint16_t motor_stall_flag;//堵转标志
  111. uint32_t propellerblock_flag;//推进器缠绕
  112. int16_t temperature_48Vpower;//48V温度
  113. int16_t board_voltage;//48V电源
  114. int16_t driverleakstate;//驱动仓漏水
  115. static AP_UAVCAN *_singleton;
  116. static AP_UAVCAN *get_singleton() {
  117. return _singleton;
  118. }
  119. //--------self define end--------------------
  120. static const struct AP_Param::GroupInfo var_info[];
  121. // Return uavcan from @driver_index or nullptr if it's not ready or doesn't exist
  122. static AP_UAVCAN *get_uavcan(uint8_t driver_index);
  123. void init(uint8_t driver_index, bool enable_filters) override;
  124. uavcan::Node<0>* get_node() { return _node; }
  125. uint8_t get_driver_index() { return _driver_index; }
  126. ///// SRV output /////
  127. void SRV_push_servos(void);
  128. ///// LED /////
  129. bool led_write(uint8_t led_index, uint8_t red, uint8_t green, uint8_t blue);
  130. // buzzer
  131. void set_buzzer_tone(float frequency, float duration_s);
  132. template <typename DataType_>
  133. class RegistryBinder {
  134. protected:
  135. typedef void (*Registry)(AP_UAVCAN* _ap_uavcan, uint8_t _node_id, const RegistryBinder& _cb);
  136. AP_UAVCAN* _uc;
  137. Registry _ffunc;
  138. public:
  139. RegistryBinder() :
  140. _uc(),
  141. _ffunc(),
  142. msg() {}
  143. RegistryBinder(AP_UAVCAN* uc, Registry ffunc) :
  144. _uc(uc),
  145. _ffunc(ffunc),
  146. msg(nullptr) {}
  147. void operator()(const uavcan::ReceivedDataStructure<DataType_>& _msg) {
  148. msg = &_msg;
  149. _ffunc(_uc, _msg.getSrcNodeID().get(), *this);
  150. }
  151. const uavcan::ReceivedDataStructure<DataType_> *msg;
  152. };
  153. private:
  154. class SystemClock: public uavcan::ISystemClock, uavcan::Noncopyable {
  155. public:
  156. SystemClock() = default;
  157. void adjustUtc(uavcan::UtcDuration adjustment) override {
  158. utc_adjustment_usec = adjustment.toUSec();
  159. }
  160. uavcan::MonotonicTime getMonotonic() const override {
  161. return uavcan::MonotonicTime::fromUSec(AP_HAL::micros64());
  162. }
  163. uavcan::UtcTime getUtc() const override {
  164. return uavcan::UtcTime::fromUSec(AP_HAL::micros64() + utc_adjustment_usec);
  165. }
  166. static SystemClock& instance() {
  167. static SystemClock inst;
  168. return inst;
  169. }
  170. private:
  171. int64_t utc_adjustment_usec;
  172. };
  173. // This will be needed to implement if UAVCAN is used with multithreading
  174. // Such cases will be firmware update, etc.
  175. class RaiiSynchronizer {};
  176. void loop(void);
  177. ///// SRV output /////
  178. void SRV_send_actuator();
  179. void SRV_send_esc();
  180. ///// LED /////
  181. void led_out_send();
  182. // buzzer
  183. void buzzer_send();
  184. // SafetyState
  185. void safety_state_send();
  186. uavcan::PoolAllocator<UAVCAN_NODE_POOL_SIZE, UAVCAN_NODE_POOL_BLOCK_SIZE, AP_UAVCAN::RaiiSynchronizer> _node_allocator;
  187. // UAVCAN parameters
  188. AP_Int8 _uavcan_node;
  189. AP_Int32 _servo_bm;
  190. AP_Int32 _esc_bm;
  191. AP_Int16 _servo_rate_hz;
  192. uavcan::Node<0> *_node;
  193. uint8_t _driver_index;
  194. char _thread_name[9];
  195. bool _initialized;
  196. #ifdef HAS_UAVCAN_SERVERS
  197. AP_UAVCAN_Servers _servers;
  198. #endif
  199. ///// SRV output /////
  200. struct {
  201. uint16_t pulse;
  202. bool esc_pending;
  203. bool servo_pending;
  204. } _SRV_conf[UAVCAN_SRV_NUMBER];
  205. uint8_t _SRV_armed;
  206. uint32_t _SRV_last_send_us;
  207. HAL_Semaphore SRV_sem;
  208. ///// LED /////
  209. struct led_device {
  210. uint8_t led_index;
  211. uint8_t red;
  212. uint8_t green;
  213. uint8_t blue;
  214. };
  215. struct {
  216. led_device devices[AP_UAVCAN_MAX_LED_DEVICES];
  217. uint8_t devices_count;
  218. uint64_t last_update;
  219. } _led_conf;
  220. HAL_Semaphore _led_out_sem;
  221. // buzzer
  222. struct {
  223. HAL_Semaphore sem;
  224. float frequency;
  225. float duration;
  226. uint8_t pending_mask; // mask of interfaces to send to
  227. } _buzzer;
  228. // safety status send state
  229. uint32_t _last_safety_state_ms;
  230. // safety button handling
  231. static void handle_button(AP_UAVCAN* ap_uavcan, uint8_t node_id, const ButtonCb &cb);
  232. };
  233. namespace AP {
  234. AP_UAVCAN &uavcan();
  235. };
  236. #endif /* AP_UAVCAN_H_ */