完整源码:
14.CAN.zip
(639.44 KB, 售价: 1 E币)
以下是部分源码
- #include "CAN_receive.h"
- #include "main.h"
- extern CAN_HandleTypeDef hcan1;
- extern CAN_HandleTypeDef hcan2;
- //motor data read
- #define get_motor_measure(ptr, data) \
- { \
- (ptr)->last_ecd = (ptr)->ecd; \
- (ptr)->ecd = (uint16_t)((data)[0] << 8 | (data)[1]); \
- (ptr)->speed_rpm = (uint16_t)((data)[2] << 8 | (data)[3]); \
- (ptr)->given_current = (uint16_t)((data)[4] << 8 | (data)[5]); \
- (ptr)->temperate = (data)[6]; \
- }
- /*
- motor data, 0:chassis motor1 3508;1:chassis motor3 3508;2:chassis motor3 3508;3:chassis motor4 3508;
- 4:yaw gimbal motor 6020;5:pitch gimbal motor 6020;6:trigger motor 2006;
- 电机数据, 0:底盘电机1 3508电机, 1:底盘电机2 3508电机,2:底盘电机3 3508电机,3:底盘电机4 3508电机;
- 4:yaw云台电机 6020电机; 5:pitch云台电机 6020电机; 6:拨弹电机 2006电机*/
- static motor_measure_t motor_chassis[7];
- static CAN_TxHeaderTypeDef gimbal_tx_message;
- static uint8_t gimbal_can_send_data[8];
- static CAN_TxHeaderTypeDef chassis_tx_message;
- static uint8_t chassis_can_send_data[8];
- /**
- * @brief hal CAN fifo call back, receive motor data
- * @param[in] hcan, the point to CAN handle
- * @retval none
- */
- /**
- * @brief hal库CAN回调函数,接收电机数据
- * @param[in] hcan:CAN句柄指针
- * @retval none
- */
- void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
- {
- CAN_RxHeaderTypeDef rx_header;
- uint8_t rx_data[8];
- HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data);
- switch (rx_header.StdId)
- {
- case CAN_3508_M1_ID:
- case CAN_3508_M2_ID:
- case CAN_3508_M3_ID:
- case CAN_3508_M4_ID:
- case CAN_YAW_MOTOR_ID:
- case CAN_PIT_MOTOR_ID:
- case CAN_TRIGGER_MOTOR_ID:
- {
- static uint8_t i = 0;
- //get motor id
- i = rx_header.StdId - CAN_3508_M1_ID;
- get_motor_measure(&motor_chassis<i>, rx_data);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- /**
- * @brief send control current of motor (0x205, 0x206, 0x207, 0x208)
- * @param[in] yaw: (0x205) 6020 motor control current, range [-30000,30000]
- * @param[in] pitch: (0x206) 6020 motor control current, range [-30000,30000]
- * @param[in] shoot: (0x207) 2006 motor control current, range [-10000,10000]
- * @param[in] rev: (0x208) reserve motor control current
- * @retval none
- */
- /**
- * @brief 发送电机控制电流(0x205,0x206,0x207,0x208)
- * @param[in] yaw: (0x205) 6020电机控制电流, 范围 [-30000,30000]
- * @param[in] pitch: (0x206) 6020电机控制电流, 范围 [-30000,30000]
- * @param[in] shoot: (0x207) 2006电机控制电流, 范围 [-10000,10000]
- * @param[in] rev: (0x208) 保留,电机控制电流
- * @retval none
- */
- void CAN_cmd_gimbal(int16_t yaw, int16_t pitch, int16_t shoot, int16_t rev)
- {
- uint32_t send_mail_box;
- gimbal_tx_message.StdId = CAN_GIMBAL_ALL_ID;
- gimbal_tx_message.IDE = CAN_ID_STD;
- gimbal_tx_message.RTR = CAN_RTR_DATA;
- gimbal_tx_message.DLC = 0x08;
- gimbal_can_send_data[0] = (yaw >> 8);
- gimbal_can_send_data[1] = yaw;
- gimbal_can_send_data[2] = (pitch >> 8);
- gimbal_can_send_data[3] = pitch;
- gimbal_can_send_data[4] = (shoot >> 8);
- gimbal_can_send_data[5] = shoot;
- gimbal_can_send_data[6] = (rev >> 8);
- gimbal_can_send_data[7] = rev;
- HAL_CAN_AddTxMessage(&GIMBAL_CAN, &gimbal_tx_message, gimbal_can_send_data, &send_mail_box);
- }
- /**
- * @brief send CAN packet of ID 0x700, it will set chassis motor 3508 to quick ID setting
- * @param[in] none
- * @retval none
- */
- /**
- * @brief 发送ID为0x700的CAN包,它会设置3508电机进入快速设置ID
- * @param[in] none
- * @retval none
- */
- void CAN_cmd_chassis_reset_ID(void)
- {
- uint32_t send_mail_box;
- chassis_tx_message.StdId = 0x700;
- chassis_tx_message.IDE = CAN_ID_STD;
- chassis_tx_message.RTR = CAN_RTR_DATA;
- chassis_tx_message.DLC = 0x08;
- chassis_can_send_data[0] = 0;
- chassis_can_send_data[1] = 0;
- chassis_can_send_data[2] = 0;
- chassis_can_send_data[3] = 0;
- chassis_can_send_data[4] = 0;
- chassis_can_send_data[5] = 0;
- chassis_can_send_data[6] = 0;
- chassis_can_send_data[7] = 0;
- HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
- }
- /**
- * @brief send control current of motor (0x201, 0x202, 0x203, 0x204)
- * @param[in] motor1: (0x201) 3508 motor control current, range [-16384,16384]
- * @param[in] motor2: (0x202) 3508 motor control current, range [-16384,16384]
- * @param[in] motor3: (0x203) 3508 motor control current, range [-16384,16384]
- * @param[in] motor4: (0x204) 3508 motor control current, range [-16384,16384]
- * @retval none
- */
- /**
- * @brief 发送电机控制电流(0x201,0x202,0x203,0x204)
- * @param[in] motor1: (0x201) 3508电机控制电流, 范围 [-16384,16384]
- * @param[in] motor2: (0x202) 3508电机控制电流, 范围 [-16384,16384]
- * @param[in] motor3: (0x203) 3508电机控制电流, 范围 [-16384,16384]
- * @param[in] motor4: (0x204) 3508电机控制电流, 范围 [-16384,16384]
- * @retval none
- */
- void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4)
- {
- uint32_t send_mail_box;
- chassis_tx_message.StdId = CAN_CHASSIS_ALL_ID;
- chassis_tx_message.IDE = CAN_ID_STD;
- chassis_tx_message.RTR = CAN_RTR_DATA;
- chassis_tx_message.DLC = 0x08;
- chassis_can_send_data[0] = motor1 >> 8;
- chassis_can_send_data[1] = motor1;
- chassis_can_send_data[2] = motor2 >> 8;
- chassis_can_send_data[3] = motor2;
- chassis_can_send_data[4] = motor3 >> 8;
- chassis_can_send_data[5] = motor3;
- chassis_can_send_data[6] = motor4 >> 8;
- chassis_can_send_data[7] = motor4;
- HAL_CAN_AddTxMessage(&CHASSIS_CAN, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
- }
- const motor_measure_t *get_yaw_gimbal_motor_measure_point(void)
- {
- return &motor_chassis[4];
- }
- const motor_measure_t *get_pitch_gimbal_motor_measure_point(void)
- {
- return &motor_chassis[5];
- }
- const motor_measure_t *get_trigger_motor_measure_point(void)
- {
- return &motor_chassis[6];
- }
- const motor_measure_t *get_chassis_motor_measure_point(uint8_t i)
- {
- return &motor_chassis[(i & 0x03)];
- }
- </i>
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|