2024-10-06 21:38:02 +08:00
|
|
|
|
#include "dm_motor_drv.h"
|
|
|
|
|
#include "fdcan.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_enable: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD>motor_t<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ϳ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* ֧<EFBFBD>ֵĿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_enable(hcan_t* hcan, motor_t *motor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
switch(motor->ctrl.mode)
|
|
|
|
|
{
|
|
|
|
|
case mit_mode:
|
|
|
|
|
enable_motor_mode(hcan, motor->id, MIT_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case pos_mode:
|
|
|
|
|
enable_motor_mode(hcan, motor->id, POS_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case spd_mode:
|
|
|
|
|
enable_motor_mode(hcan, motor->id, SPD_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case psi_mode:
|
|
|
|
|
enable_motor_mode(hcan, motor->id, PSI_MODE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_disable: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD>motor_t<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ϳ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* ֧<EFBFBD>ֵĿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_disable(hcan_t* hcan, motor_t *motor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
switch(motor->ctrl.mode)
|
|
|
|
|
{
|
|
|
|
|
case mit_mode:
|
|
|
|
|
disable_motor_mode(hcan, motor->id, MIT_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case pos_mode:
|
|
|
|
|
disable_motor_mode(hcan, motor->id, POS_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case spd_mode:
|
|
|
|
|
disable_motor_mode(hcan, motor->id, SPD_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case psi_mode:
|
|
|
|
|
disable_motor_mode(hcan, motor->id, PSI_MODE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
dm_motor_clear_para(motor);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_ctrl_send: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD>motor_t<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ϳ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* ֧<EFBFBD>ֵĿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_ctrl_send(hcan_t* hcan, motor_t *motor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
switch(motor->ctrl.mode)
|
|
|
|
|
{
|
|
|
|
|
case mit_mode:
|
|
|
|
|
mit_ctrl(hcan, motor, motor->id, motor->ctrl.pos_set, motor->ctrl.vel_set, motor->ctrl.kp_set, motor->ctrl.kd_set, motor->ctrl.tor_set);
|
|
|
|
|
break;
|
|
|
|
|
case pos_mode:
|
|
|
|
|
pos_ctrl(hcan, motor->id, motor->ctrl.pos_set, motor->ctrl.vel_set);
|
|
|
|
|
break;
|
|
|
|
|
case spd_mode:
|
|
|
|
|
spd_ctrl(hcan, motor->id, motor->ctrl.vel_set);
|
|
|
|
|
break;
|
|
|
|
|
case psi_mode:
|
|
|
|
|
psi_ctrl(hcan, motor->id,motor->ctrl.pos_set, motor->ctrl.vel_set, motor->ctrl.cur_set);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_clear: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD>motor_t<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ϳ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD><EFBFBD>ٶȡ<EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(KP)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(KD)<EFBFBD><EFBFBD>Ť<EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_clear_para(motor_t *motor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
motor->ctrl.kd_set = 0;
|
|
|
|
|
motor->ctrl.kp_set = 0;
|
|
|
|
|
motor->ctrl.pos_set = 0;
|
|
|
|
|
motor->ctrl.vel_set = 0;
|
|
|
|
|
motor->ctrl.tor_set = 0;
|
|
|
|
|
motor->ctrl.cur_set = 0;
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_clear_err: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD>ƽṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>Ӧģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_clear_err(hcan_t* hcan, motor_t *motor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
switch(motor->ctrl.mode)
|
|
|
|
|
{
|
|
|
|
|
case mit_mode:
|
|
|
|
|
clear_err(hcan, motor->id, MIT_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case pos_mode:
|
|
|
|
|
clear_err(hcan, motor->id, POS_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case spd_mode:
|
|
|
|
|
clear_err(hcan, motor->id, SPD_MODE);
|
|
|
|
|
break;
|
|
|
|
|
case psi_mode:
|
|
|
|
|
clear_err(hcan, motor->id, PSI_MODE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: dm4310_fbdata: <EFBFBD><EFBFBD>ȡDM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor: ָ<EFBFBD><EFBFBD>motor_t<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: rx_data: ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD>ӽ<EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡDM4310<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
|
|
|
|
|
* ״̬<EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD><EFBFBD>ٶȡ<EFBFBD>Ť<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶Ȳ<EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void dm_motor_fbdata(motor_t *motor, uint8_t *rx_data)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
motor->para.id = (rx_data[0])&0x0F;
|
|
|
|
|
motor->para.state = (rx_data[0])>>4;
|
|
|
|
|
motor->para.p_int=(rx_data[1]<<8)|rx_data[2];
|
|
|
|
|
motor->para.v_int=(rx_data[3]<<4)|(rx_data[4]>>4);
|
|
|
|
|
motor->para.t_int=((rx_data[4]&0xF)<<8)|rx_data[5];
|
|
|
|
|
motor->para.pos = uint_to_float(motor->para.p_int, -motor->tmp.PMAX, motor->tmp.PMAX, 16); // (-12.5,12.5)
|
|
|
|
|
motor->para.vel = uint_to_float(motor->para.v_int, -motor->tmp.VMAX, motor->tmp.VMAX, 12); // (-45.0,45.0)
|
|
|
|
|
motor->para.tor = uint_to_float(motor->para.t_int, -motor->tmp.TMAX, motor->tmp.TMAX, 12); // (-18.0,18.0)
|
|
|
|
|
motor->para.Tmos = (float)(rx_data[6]);
|
|
|
|
|
motor->para.Tcoil = (float)(rx_data[7]);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: float_to_uint: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: x_float: <EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: x_min: <EFBFBD><EFBFBD>Χ<EFBFBD><EFBFBD>Сֵ
|
|
|
|
|
* @param[in]: x_max: <EFBFBD><EFBFBD>Χ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: bits: Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>
|
|
|
|
|
* @retval: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> x <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ [x_min, x_max] <EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>䣬ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊһ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
int float_to_uint(float x_float, float x_min, float x_max, int bits)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
/* Converts a float to an unsigned int, given range and number of bits */
|
|
|
|
|
float span = x_max - x_min;
|
|
|
|
|
float offset = x_min;
|
|
|
|
|
return (int) ((x_float-offset)*((float)((1<<bits)-1))/span);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: uint_to_float: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: x_int: <EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: x_min: <EFBFBD><EFBFBD>Χ<EFBFBD><EFBFBD>Сֵ
|
|
|
|
|
* @param[in]: x_max: <EFBFBD><EFBFBD>Χ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: bits: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>
|
|
|
|
|
* @retval: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> x_int <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ [x_min, x_max] <EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>䣬ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
float uint_to_float(int x_int, float x_min, float x_max, int bits)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
/* converts unsigned int to float, given range and number of bits */
|
|
|
|
|
float span = x_max - x_min;
|
|
|
|
|
float offset = x_min;
|
|
|
|
|
return ((float)x_int)*span/((float)((1<<bits)-1)) + offset;
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: enable_motor_mode: <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: mode_id: ģʽID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void enable_motor_mode(hcan_t* hcan, uint16_t motor_id, uint16_t mode_id)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint16_t id = motor_id + mode_id;
|
|
|
|
|
|
|
|
|
|
data[0] = 0xFF;
|
|
|
|
|
data[1] = 0xFF;
|
|
|
|
|
data[2] = 0xFF;
|
|
|
|
|
data[3] = 0xFF;
|
|
|
|
|
data[4] = 0xFF;
|
|
|
|
|
data[5] = 0xFF;
|
|
|
|
|
data[6] = 0xFF;
|
|
|
|
|
data[7] = 0xFC;
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: disable_motor_mode: <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: mode_id: ģʽID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void disable_motor_mode(hcan_t* hcan, uint16_t motor_id, uint16_t mode_id)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint16_t id = motor_id + mode_id;
|
|
|
|
|
|
|
|
|
|
data[0] = 0xFF;
|
|
|
|
|
data[1] = 0xFF;
|
|
|
|
|
data[2] = 0xFF;
|
|
|
|
|
data[3] = 0xFF;
|
|
|
|
|
data[4] = 0xFF;
|
|
|
|
|
data[5] = 0xFF;
|
|
|
|
|
data[6] = 0xFF;
|
|
|
|
|
data[7] = 0xFD;
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: save_pos_zero: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㺯<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: mode_id: ģʽID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void save_pos_zero(hcan_t* hcan, uint16_t motor_id, uint16_t mode_id)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint16_t id = motor_id + mode_id;
|
|
|
|
|
|
|
|
|
|
data[0] = 0xFF;
|
|
|
|
|
data[1] = 0xFF;
|
|
|
|
|
data[2] = 0xFF;
|
|
|
|
|
data[3] = 0xFF;
|
|
|
|
|
data[4] = 0xFF;
|
|
|
|
|
data[5] = 0xFF;
|
|
|
|
|
data[6] = 0xFF;
|
|
|
|
|
data[7] = 0xFE;
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: clear_err: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: mode_id: ģʽID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void clear_err(hcan_t* hcan, uint16_t motor_id, uint16_t mode_id)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint16_t id = motor_id + mode_id;
|
|
|
|
|
|
|
|
|
|
data[0] = 0xFF;
|
|
|
|
|
data[1] = 0xFF;
|
|
|
|
|
data[2] = 0xFF;
|
|
|
|
|
data[3] = 0xFF;
|
|
|
|
|
data[4] = 0xFF;
|
|
|
|
|
data[5] = 0xFF;
|
|
|
|
|
data[6] = 0xFF;
|
|
|
|
|
data[7] = 0xFB;
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: mit_ctrl: MITģʽ<EFBFBD>µĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: pos: λ<EFBFBD>ø<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: vel: <EFBFBD>ٶȸ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: kp: λ<EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: kd: λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: torq: ת<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MITģʽ<EFBFBD>µĿ<EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void mit_ctrl(hcan_t* hcan, motor_t *motor, uint16_t motor_id, float pos, float vel,float kp, float kd, float tor)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint16_t pos_tmp,vel_tmp,kp_tmp,kd_tmp,tor_tmp;
|
|
|
|
|
uint16_t id = motor_id + MIT_MODE;
|
|
|
|
|
|
|
|
|
|
pos_tmp = float_to_uint(pos, -motor->tmp.PMAX, motor->tmp.PMAX, 16);
|
|
|
|
|
vel_tmp = float_to_uint(vel, -motor->tmp.VMAX, motor->tmp.VMAX, 12);
|
|
|
|
|
tor_tmp = float_to_uint(tor, -motor->tmp.TMAX, motor->tmp.TMAX, 12);
|
|
|
|
|
kp_tmp = float_to_uint(kp, KP_MIN, KP_MAX, 12);
|
|
|
|
|
kd_tmp = float_to_uint(kd, KD_MIN, KD_MAX, 12);
|
|
|
|
|
|
|
|
|
|
data[0] = (pos_tmp >> 8);
|
|
|
|
|
data[1] = pos_tmp;
|
|
|
|
|
data[2] = (vel_tmp >> 4);
|
|
|
|
|
data[3] = ((vel_tmp&0xF)<<4)|(kp_tmp>>8);
|
|
|
|
|
data[4] = kp_tmp;
|
|
|
|
|
data[5] = (kd_tmp >> 4);
|
|
|
|
|
data[6] = ((kd_tmp&0xF)<<4)|(tor_tmp>>8);
|
|
|
|
|
data[7] = tor_tmp;
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: pos_speed_ctrl: λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD>ƺ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: vel: <EFBFBD>ٶȸ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void pos_ctrl(hcan_t* hcan,uint16_t motor_id, float pos, float vel)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint16_t id;
|
|
|
|
|
uint8_t *pbuf, *vbuf;
|
|
|
|
|
uint8_t data[8];
|
|
|
|
|
|
|
|
|
|
id = motor_id + POS_MODE;
|
|
|
|
|
pbuf=(uint8_t*)&pos;
|
|
|
|
|
vbuf=(uint8_t*)&vel;
|
|
|
|
|
|
|
|
|
|
data[0] = *pbuf;
|
|
|
|
|
data[1] = *(pbuf+1);
|
|
|
|
|
data[2] = *(pbuf+2);
|
|
|
|
|
data[3] = *(pbuf+3);
|
|
|
|
|
|
|
|
|
|
data[4] = *vbuf;
|
|
|
|
|
data[5] = *(vbuf+1);
|
|
|
|
|
data[6] = *(vbuf+2);
|
|
|
|
|
data[7] = *(vbuf+3);
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: speed_ctrl: <EFBFBD>ٶȿ<EFBFBD><EFBFBD>ƺ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: vel: <EFBFBD>ٶȸ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void spd_ctrl(hcan_t* hcan, uint16_t motor_id, float vel)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint16_t id;
|
|
|
|
|
uint8_t *vbuf;
|
|
|
|
|
uint8_t data[4];
|
|
|
|
|
|
|
|
|
|
id = motor_id + SPD_MODE;
|
|
|
|
|
vbuf=(uint8_t*)&vel;
|
|
|
|
|
|
|
|
|
|
data[0] = *vbuf;
|
|
|
|
|
data[1] = *(vbuf+1);
|
|
|
|
|
data[2] = *(vbuf+2);
|
|
|
|
|
data[3] = *(vbuf+3);
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 4);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: pos_speed_ctrl: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
* @param[in]: hcan: ָ<EFBFBD><EFBFBD>CAN_HandleTypeDef<EFBFBD>ṹ<EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: motor_id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: pos: λ<EFBFBD>ø<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: vel: <EFBFBD>ٶȸ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param[in]: i: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: ͨ<EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ٶȿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void psi_ctrl(hcan_t* hcan, uint16_t motor_id, float pos, float vel, float cur)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint16_t id;
|
|
|
|
|
uint8_t *pbuf, *vbuf, *ibuf;
|
|
|
|
|
uint8_t data[8];
|
|
|
|
|
|
|
|
|
|
uint16_t u16_vel = vel*100;
|
|
|
|
|
uint16_t u16_cur = cur*10000;
|
|
|
|
|
|
|
|
|
|
id = motor_id + PSI_MODE;
|
|
|
|
|
pbuf=(uint8_t*)&pos;
|
|
|
|
|
vbuf=(uint8_t*)&u16_vel;
|
|
|
|
|
ibuf=(uint8_t*)&u16_cur;
|
|
|
|
|
|
|
|
|
|
data[0] = *pbuf;
|
|
|
|
|
data[1] = *(pbuf+1);
|
|
|
|
|
data[2] = *(pbuf+2);
|
|
|
|
|
data[3] = *(pbuf+3);
|
|
|
|
|
|
|
|
|
|
data[4] = *vbuf;
|
|
|
|
|
data[5] = *(vbuf+1);
|
|
|
|
|
|
|
|
|
|
data[6] = *ibuf;
|
|
|
|
|
data[7] = *(ibuf+1);
|
|
|
|
|
|
|
|
|
|
fdcanx_send_data(hcan, id, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: read_motor_data: <EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD>ȡ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>can id
|
|
|
|
|
* @param[in]: rid: <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
2024-10-23 15:47:41 +08:00
|
|
|
|
void read_motor_data(uint16_t id, uint8_t rid)
|
2024-10-06 21:38:02 +08:00
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t can_id_l = id & 0x0F;
|
|
|
|
|
uint8_t can_id_h = (id >> 4) & 0x0F;
|
|
|
|
|
|
|
|
|
|
uint8_t data[4] = {can_id_l, can_id_h, 0x33, rid};
|
|
|
|
|
fdcanx_send_data(&hfdcan1, 0x7FF, data, 4);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: read_motor_ctrl_fbdata: <EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>can id
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
2024-10-23 15:47:41 +08:00
|
|
|
|
void read_motor_ctrl_fbdata(uint16_t id)
|
2024-10-06 21:38:02 +08:00
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t can_id_l = id & 0x0F;
|
|
|
|
|
uint8_t can_id_h = (id >> 4) & 0x0F;
|
|
|
|
|
|
|
|
|
|
uint8_t data[4] = {can_id_l, can_id_h, 0xCC, 0x00};
|
|
|
|
|
fdcanx_send_data(&hfdcan1, 0x7FF, data, 4);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: write_motor_data: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>can id
|
|
|
|
|
* @param[in]: rid: <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param[in]: d0-d3: д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
|
|
|
|
void write_motor_data(uint16_t id, uint8_t rid, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t can_id_l = id & 0x0F;
|
|
|
|
|
uint8_t can_id_h = (id >> 4) & 0x0F;
|
|
|
|
|
|
|
|
|
|
uint8_t data[8] = {can_id_l, can_id_h, 0x55, rid, d0, d1, d2, d3};
|
|
|
|
|
fdcanx_send_data(&hfdcan1, 0x7FF, data, 8);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
************************************************************************
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @brief: save_motor_data: <EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in]: id: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>can id
|
|
|
|
|
* @param[in]: rid: <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2024-10-06 21:38:02 +08:00
|
|
|
|
* @retval: void
|
2024-10-23 15:47:41 +08:00
|
|
|
|
* @details: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-06 21:38:02 +08:00
|
|
|
|
************************************************************************
|
|
|
|
|
**/
|
2024-10-23 15:47:41 +08:00
|
|
|
|
void save_motor_data(uint16_t id, uint8_t rid)
|
2024-10-06 21:38:02 +08:00
|
|
|
|
{
|
2024-10-23 15:47:41 +08:00
|
|
|
|
uint8_t can_id_l = id & 0x0F;
|
|
|
|
|
uint8_t can_id_h = (id >> 4) & 0x0F;
|
|
|
|
|
|
|
|
|
|
uint8_t data[4] = {can_id_l, can_id_h, 0xAA, 0x01};
|
|
|
|
|
fdcanx_send_data(&hfdcan1, 0x7FF, data, 4);
|
2024-10-06 21:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|