dronecan.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "dronecan.h"
  2. #include "drv_vkweigher_vls300.h"
  3. #include <string.h>
  4. #include "soft_timer.h"
  5. #include <dronecan.remoteid.ArmStatus.h>
  6. #include <uavcan.protocol.NodeStatus.h>
  7. #include "user_rid.h"
  8. static struct dronecan __dronecan = {0};
  9. uint8_t memory_pool[DRONECAN_CANARD_MEMORY_POOL_SIZE] = {0};
  10. CanardInstance canard_ins;
  11. bool dronecan_lock_status = false;
  12. void dronecan_lock() {
  13. dronecan_lock_status = 1;
  14. }
  15. void dronecan_unlock() {
  16. dronecan_lock_status = 0;
  17. }
  18. int dronecan_request_or_respond(uint8_t dest_id,
  19. CanardTxTransfer *tx_transfer) {
  20. int ret = 0;
  21. if (dronecan_lock_status == UNLOCK) {
  22. dronecan_lock();
  23. ret = canardRequestOrRespondObj(&canard_ins, dest_id, tx_transfer);
  24. dronecan_unlock();
  25. }
  26. return ret;
  27. }
  28. static void onTransferReceived(CanardInstance *ins,
  29. CanardRxTransfer *transfer) {
  30. // if (flyfire_onRecieved(ins, transfer)) {
  31. // return;
  32. // } else if (hbwEscOnRecieved(ins, transfer)) {
  33. // return;
  34. // } else if (sinemotorEscOnRecieved(ins, transfer)) {
  35. // return;
  36. // } else if (tmotorEscOnRecieved(ins, transfer)) {
  37. // return;
  38. // } else if (uavcanEscOnRecieved(ins, transfer)) {
  39. // return;
  40. // } else
  41. if (vkWeigher_Vls300_OnRecieved(ins, transfer)) {
  42. return;
  43. }
  44. if (remoteid_OnRecieved(ins, transfer)) {
  45. return;
  46. }
  47. }
  48. static bool shouldAcceptTransfer(const CanardInstance *ins,
  49. uint64_t *out_data_type_signature,
  50. uint16_t data_type_id,
  51. CanardTransferType transfer_type,
  52. uint8_t source_node_id) {
  53. bool ret = false;
  54. // /* 火萤降落伞相关消息 */
  55. // if (flyfireShouldAcceptTransfer(ins, out_data_type_signature, data_type_id,
  56. // transfer_type, source_node_id)) {
  57. // return true;
  58. // }
  59. // /* 好盈电调相关消息 */
  60. // if (hbwEscShouldAcceptTransfer(ins, out_data_type_signature, data_type_id,
  61. // transfer_type, source_node_id)) {
  62. // return true;
  63. // }
  64. // /* 弦动电调相关消息 */
  65. // if (sinemotorEscShouldAcceptTransfer(ins, out_data_type_signature,
  66. // data_type_id, transfer_type,
  67. // source_node_id)) {
  68. // return true;
  69. // }
  70. // /* tmotor电调相关消息 */
  71. // if (tmotorEscShouldAcceptTransfer(ins, out_data_type_signature, data_type_id,
  72. // transfer_type, source_node_id)) {
  73. // return true;
  74. // }
  75. // /* uavcan电调相关消息 */
  76. // if (uavcanEscShouldAcceptTransfer(ins, out_data_type_signature, data_type_id,
  77. // transfer_type, source_node_id)) {
  78. // return true;
  79. // }
  80. /* dronecan weigher 称重器相关消息 */
  81. if (vkWeigher_Vls300_shouldAcceptTransfer(ins, out_data_type_signature,
  82. data_type_id, transfer_type,
  83. source_node_id)) {
  84. ret = true;
  85. }
  86. if (data_type_id == DRONECAN_REMOTEID_ARMSTATUS_ID &&
  87. transfer_type == CanardTransferTypeBroadcast) {
  88. *out_data_type_signature = DRONECAN_REMOTEID_ARMSTATUS_SIGNATURE;
  89. ret = true;
  90. }
  91. if (data_type_id == UAVCAN_PROTOCOL_NODESTATUS_ID &&
  92. transfer_type == CanardTransferTypeBroadcast) {
  93. *out_data_type_signature = UAVCAN_PROTOCOL_NODESTATUS_SIGNATURE;
  94. ret = true;
  95. }
  96. return ret;
  97. }
  98. // static void send_node_status(struct dronecan *dcan) {
  99. // static uint8_t transfer_id = 0;
  100. // struct uavcan_protocol_NodeStatus msg;
  101. // uint8_t buffer[UAVCAN_PROTOCOL_NODESTATUS_MAX_SIZE] = {0};
  102. // msg.uptime_sec = systime_now_ms() / 1000;
  103. // msg.sub_mode = 0;
  104. // msg.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL;
  105. // msg.health = UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK;
  106. // uint32_t len = uavcan_protocol_NodeStatus_encode(&msg, buffer);
  107. // CanardTxTransfer tx_transfer = {
  108. // .data_type_signature = UAVCAN_PROTOCOL_NODESTATUS_SIGNATURE,
  109. // .data_type_id = UAVCAN_PROTOCOL_NODESTATUS_ID,
  110. // .inout_transfer_id = &transfer_id,
  111. // .priority = CANARD_TRANSFER_PRIORITY_MEDIUM,
  112. // .transfer_type = CanardTransferTypeBroadcast,
  113. // .payload = buffer,
  114. // .payload_len = len,
  115. // .iface_mask = 0xff, /* 发送到所有接口 */
  116. // };
  117. // dronecan_broadcast(dcan, &tx_transfer);
  118. // }
  119. void dronecan_tx_processing(void) {
  120. struct dronecan *dcan = &__dronecan;
  121. // Transmit the frames in the queue.
  122. while (1) {
  123. CanardCANFrame tx_frame = {0};
  124. CanardCANFrame *tx_frame_p = NULL;
  125. if( dronecan_lock_status == UNLOCK )
  126. {
  127. dronecan_lock();
  128. tx_frame_p = canardPeekTxQueue(&canard_ins);
  129. if (tx_frame_p == NULL)
  130. {
  131. /* 没有发送消息, 结束发送 */
  132. dronecan_unlock();
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. break;
  139. }
  140. /* 将消息拷贝出来 */
  141. memcpy(&tx_frame, tx_frame_p, sizeof(tx_frame));
  142. canardPopTxQueue(&canard_ins);
  143. dronecan_unlock(dcan);
  144. /* 通过 can 接口发送消息 */
  145. can_send_msg_normal(tx_frame.data, tx_frame.data_len, tx_frame.id);
  146. }
  147. static int clear_time = 0;
  148. /* 每秒清理一次 canard 过期消息 */
  149. if( HAL_GetTick() - clear_time > 1000 )
  150. {
  151. if( dronecan_lock_status == UNLOCK )
  152. {
  153. clear_time = HAL_GetTick();
  154. dronecan_lock();
  155. canardCleanupStaleTransfers(&canard_ins, Get_Systimer_Us());
  156. dronecan_unlock();
  157. }
  158. }
  159. }
  160. /**
  161. * @brief dronecan 回调函数, 当 can 接口接收到消息时调用
  162. *
  163. * @param msg can 消息
  164. * @param iface_id can 接口 id, 用于管理多个不同 can 接口
  165. * @return int 1-是 dronecan 消息, 0-不是 dronecan 消息
  166. */
  167. int dronecan_rx_callback(CAN_RxHeaderTypeDef Rxhead,uint8_t data[]) {
  168. int ret = 0;
  169. CanardCANFrame rx_frame = {0};
  170. rx_frame.id = Rxhead.ExtId;
  171. if (Rxhead.IDE == CAN_ID_EXT) {
  172. rx_frame.id |= CANARD_CAN_FRAME_EFF;
  173. }
  174. if (Rxhead.RTR == CAN_RTR_REMOTE) {
  175. rx_frame.id |= CANARD_CAN_FRAME_RTR;
  176. }
  177. rx_frame.data_len = Rxhead.DLC;
  178. memcpy(rx_frame.data, &data[0], Rxhead.DLC);
  179. uint64_t timestamp = Get_Systimer_Us();
  180. int16_t res;
  181. if(dronecan_lock_status == UNLOCK)
  182. {
  183. dronecan_lock();
  184. res = canardHandleRxFrame(&canard_ins, &rx_frame, timestamp);
  185. dronecan_unlock();
  186. }
  187. if (res == CANARD_OK) {
  188. ret = 1;
  189. }
  190. return ret;
  191. }
  192. // static void timer_out_cb(void *arg) {
  193. // if (arg) {
  194. // struct dronecan *dcan = arg;
  195. // send_node_status(dcan);
  196. // }
  197. // }
  198. void dronecan_init(void) {
  199. struct dronecan *dcan = &__dronecan;
  200. canardInit(&canard_ins, &memory_pool,
  201. DRONECAN_CANARD_MEMORY_POOL_SIZE, onTransferReceived,
  202. shouldAcceptTransfer, dcan);
  203. canardSetLocalNodeID(&canard_ins,
  204. DRONECAN_CANARD_DEFAULT_LOCAL_NODE_ID);
  205. }
  206. /**
  207. * @brief dronecan 发送广播消息
  208. *
  209. * @param dcan dronecan 对象
  210. * @param tx_transfer 发送消息对象
  211. * @return int 发送到 canard 发送队列的消息计数
  212. */
  213. int dronecan_broadcast(CanardTxTransfer *tx_transfer) {
  214. int ret = 0;
  215. if (dronecan_lock_status == UNLOCK) {
  216. dronecan_lock();
  217. ret = canardBroadcastObj(&canard_ins, tx_transfer);
  218. dronecan_unlock();
  219. }
  220. return ret;
  221. }