AP_UAVCAN.h 7.3 KB

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