找回密码
 注册

QQ登录

只需一步,快速开始

搜索

stm32 通过CAN 电机控制源码 C语言程序

[复制链接]
baipiaozhe 发表于 2022-6-23 16:13:58 | 显示全部楼层 |阅读模式
完整源码: 14.CAN.zip (639.44 KB, 售价: 1 E币)
以下是部分源码
  1. #include "CAN_receive.h"
  2. #include "main.h"

  3. extern CAN_HandleTypeDef hcan1;
  4. extern CAN_HandleTypeDef hcan2;
  5. //motor data read
  6. #define get_motor_measure(ptr, data)                                    \
  7.     {                                                                   \
  8.         (ptr)->last_ecd = (ptr)->ecd;                                   \
  9.         (ptr)->ecd = (uint16_t)((data)[0] << 8 | (data)[1]);            \
  10.         (ptr)->speed_rpm = (uint16_t)((data)[2] << 8 | (data)[3]);      \
  11.         (ptr)->given_current = (uint16_t)((data)[4] << 8 | (data)[5]);  \
  12.         (ptr)->temperate = (data)[6];                                   \
  13.     }
  14. /*
  15. motor data,  0:chassis motor1 3508;1:chassis motor3 3508;2:chassis motor3 3508;3:chassis motor4 3508;
  16. 4:yaw gimbal motor 6020;5:pitch gimbal motor 6020;6:trigger motor 2006;
  17. 电机数据, 0:底盘电机1 3508电机,  1:底盘电机2 3508电机,2:底盘电机3 3508电机,3:底盘电机4 3508电机;
  18. 4:yaw云台电机 6020电机; 5:pitch云台电机 6020电机; 6:拨弹电机 2006电机*/
  19. static motor_measure_t motor_chassis[7];

  20. static CAN_TxHeaderTypeDef  gimbal_tx_message;
  21. static uint8_t              gimbal_can_send_data[8];
  22. static CAN_TxHeaderTypeDef  chassis_tx_message;
  23. static uint8_t              chassis_can_send_data[8];

  24. /**
  25.   * @brief          hal CAN fifo call back, receive motor data
  26.   * @param[in]      hcan, the point to CAN handle
  27.   * @retval         none
  28.   */
  29. /**
  30.   * @brief          hal库CAN回调函数,接收电机数据
  31.   * @param[in]      hcan:CAN句柄指针
  32.   * @retval         none
  33.   */
  34. void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
  35. {
  36.     CAN_RxHeaderTypeDef rx_header;
  37.     uint8_t rx_data[8];

  38.     HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data);

  39.     switch (rx_header.StdId)
  40.     {
  41.         case CAN_3508_M1_ID:
  42.         case CAN_3508_M2_ID:
  43.         case CAN_3508_M3_ID:
  44.         case CAN_3508_M4_ID:
  45.         case CAN_YAW_MOTOR_ID:
  46.         case CAN_PIT_MOTOR_ID:
  47.         case CAN_TRIGGER_MOTOR_ID:
  48.         {
  49.             static uint8_t i = 0;
  50.             //get motor id
  51.             i = rx_header.StdId - CAN_3508_M1_ID;
  52.             get_motor_measure(&motor_chassis<i>, rx_data);
  53.             break;
  54.         }

  55.         default:
  56.         {
  57.             break;
  58.         }
  59.     }
  60. }



  61. /**
  62.   * @brief          send control current of motor (0x205, 0x206, 0x207, 0x208)
  63.   * @param[in]      yaw: (0x205) 6020 motor control current, range [-30000,30000]
  64.   * @param[in]      pitch: (0x206) 6020 motor control current, range [-30000,30000]
  65.   * @param[in]      shoot: (0x207) 2006 motor control current, range [-10000,10000]
  66.   * @param[in]      rev: (0x208) reserve motor control current
  67.   * @retval         none
  68.   */
  69. /**
  70.   * @brief          发送电机控制电流(0x205,0x206,0x207,0x208)
  71.   * @param[in]      yaw: (0x205) 6020电机控制电流, 范围 [-30000,30000]
  72.   * @param[in]      pitch: (0x206) 6020电机控制电流, 范围 [-30000,30000]
  73.   * @param[in]      shoot: (0x207) 2006电机控制电流, 范围 [-10000,10000]
  74.   * @param[in]      rev: (0x208) 保留,电机控制电流
  75.   * @retval         none
  76.   */
  77. void CAN_cmd_gimbal(int16_t yaw, int16_t pitch, int16_t shoot, int16_t rev)
  78. {
  79.     uint32_t send_mail_box;
  80.     gimbal_tx_message.StdId = CAN_GIMBAL_ALL_ID;
  81.     gimbal_tx_message.IDE = CAN_ID_STD;
  82.     gimbal_tx_message.RTR = CAN_RTR_DATA;
  83.     gimbal_tx_message.DLC = 0x08;
  84.     gimbal_can_send_data[0] = (yaw >> 8);
  85.     gimbal_can_send_data[1] = yaw;
  86.     gimbal_can_send_data[2] = (pitch >> 8);
  87.     gimbal_can_send_data[3] = pitch;
  88.     gimbal_can_send_data[4] = (shoot >> 8);
  89.     gimbal_can_send_data[5] = shoot;
  90.     gimbal_can_send_data[6] = (rev >> 8);
  91.     gimbal_can_send_data[7] = rev;
  92.     HAL_CAN_AddTxMessage(&GIMBAL_CAN, &gimbal_tx_message, gimbal_can_send_data, &send_mail_box);
  93. }

  94. /**
  95.   * @brief          send CAN packet of ID 0x700, it will set chassis motor 3508 to quick ID setting
  96.   * @param[in]      none
  97.   * @retval         none
  98.   */
  99. /**
  100.   * @brief          发送ID为0x700的CAN包,它会设置3508电机进入快速设置ID
  101.   * @param[in]      none
  102.   * @retval         none
  103.   */
  104. void CAN_cmd_chassis_reset_ID(void)
  105. {
  106.     uint32_t send_mail_box;
  107.     chassis_tx_message.StdId = 0x700;
  108.     chassis_tx_message.IDE = CAN_ID_STD;
  109.     chassis_tx_message.RTR = CAN_RTR_DATA;
  110.     chassis_tx_message.DLC = 0x08;
  111.     chassis_can_send_data[0] = 0;
  112.     chassis_can_send_data[1] = 0;
  113.     chassis_can_send_data[2] = 0;
  114.     chassis_can_send_data[3] = 0;
  115.     chassis_can_send_data[4] = 0;
  116.     chassis_can_send_data[5] = 0;
  117.     chassis_can_send_data[6] = 0;
  118.     chassis_can_send_data[7] = 0;

  119.     HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
  120. }


  121. /**
  122.   * @brief          send control current of motor (0x201, 0x202, 0x203, 0x204)
  123.   * @param[in]      motor1: (0x201) 3508 motor control current, range [-16384,16384]
  124.   * @param[in]      motor2: (0x202) 3508 motor control current, range [-16384,16384]
  125.   * @param[in]      motor3: (0x203) 3508 motor control current, range [-16384,16384]
  126.   * @param[in]      motor4: (0x204) 3508 motor control current, range [-16384,16384]
  127.   * @retval         none
  128.   */
  129. /**
  130.   * @brief          发送电机控制电流(0x201,0x202,0x203,0x204)
  131.   * @param[in]      motor1: (0x201) 3508电机控制电流, 范围 [-16384,16384]
  132.   * @param[in]      motor2: (0x202) 3508电机控制电流, 范围 [-16384,16384]
  133.   * @param[in]      motor3: (0x203) 3508电机控制电流, 范围 [-16384,16384]
  134.   * @param[in]      motor4: (0x204) 3508电机控制电流, 范围 [-16384,16384]
  135.   * @retval         none
  136.   */
  137. void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4)
  138. {
  139.     uint32_t send_mail_box;
  140.     chassis_tx_message.StdId = CAN_CHASSIS_ALL_ID;
  141.     chassis_tx_message.IDE = CAN_ID_STD;
  142.     chassis_tx_message.RTR = CAN_RTR_DATA;
  143.     chassis_tx_message.DLC = 0x08;
  144.     chassis_can_send_data[0] = motor1 >> 8;
  145.     chassis_can_send_data[1] = motor1;
  146.     chassis_can_send_data[2] = motor2 >> 8;
  147.     chassis_can_send_data[3] = motor2;
  148.     chassis_can_send_data[4] = motor3 >> 8;
  149.     chassis_can_send_data[5] = motor3;
  150.     chassis_can_send_data[6] = motor4 >> 8;
  151.     chassis_can_send_data[7] = motor4;

  152.     HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
  153. }

  154. const motor_measure_t *get_yaw_gimbal_motor_measure_point(void)
  155. {
  156.     return &motor_chassis[4];
  157. }


  158. const motor_measure_t *get_pitch_gimbal_motor_measure_point(void)
  159. {
  160.     return &motor_chassis[5];
  161. }


  162. const motor_measure_t *get_trigger_motor_measure_point(void)
  163. {
  164.     return &motor_chassis[6];
  165. }



  166. const motor_measure_t *get_chassis_motor_measure_point(uint8_t i)
  167. {
  168.     return &motor_chassis[(i & 0x03)];
  169. }
  170. </i>
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-12-21 23:47

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表