#include "LK9025.h" #include "stdlib.h" #include "general_def.h" #include "daemon.h" #include "bsp_dwt.h" #include "bsp_log.h" static uint8_t idx; static LKMotorInstance *lkmotor_instance[LK_MOTOR_MX_CNT] = {NULL}; static CANInstance *sender_instance; // 多电机发送时使用的caninstance(当前保存的是注册的第一个电机的caninstance) // 后续考虑兼容单电机和多电机指令. #define MULTI_MOTOR //多电机指令 //#define SINGLE_MOTOR //单电机指令 //将-32~32 映射为-2000到2000 static int16_t float_to_int16(float x, float x_abs_max,float out_abs_max) { int16_t output = (int16_t)(x/x_abs_max * out_abs_max); return output; } /** * @brief 电机反馈报文解析 * * @param _instance 发生中断的caninstance */ static void LKMotorDecode(CANInstance *_instance) { LKMotorInstance *motor = (LKMotorInstance *)_instance->id; // 通过caninstance保存的father id获取对应的motorinstance LKMotor_Measure_t *measure = &motor->measure; uint8_t *rx_buff = _instance->rx_buff; DaemonReload(motor->daemon); // 喂狗 measure->feed_dt = DWT_GetDeltaT(&measure->feed_dwt_cnt); if(measure->init_flag == 0) { measure->offset_ecd = (uint16_t)((rx_buff[7] << 8) | rx_buff[6]); measure->init_flag = 1; } measure->last_ecd = measure->ecd; measure->ecd = (uint16_t)((rx_buff[7] << 8) | rx_buff[6]) - measure->offset_ecd; //上电位置为ecd 0点 measure->angle_single_round = ECD_ANGLE_COEF_LK * measure->ecd; measure->speed_rads = (1 - SPEED_SMOOTH_COEF) * measure->speed_rads + DEGREE_2_RAD * SPEED_SMOOTH_COEF * (float)((int16_t)(rx_buff[5] << 8 | rx_buff[4])); measure->real_current = (1 - CURRENT_SMOOTH_COEF) * measure->real_current + CURRENT_SMOOTH_COEF * (float)((int16_t)(rx_buff[3] << 8 | rx_buff[2])); measure->temperature = rx_buff[1]; if (measure->ecd - measure->last_ecd > 32768) measure->total_round--; else if (measure->ecd - measure->last_ecd < -32768) measure->total_round++; measure->total_angle = measure->total_round * 360 + measure->angle_single_round; } static void LKMotorLostCallback(void *motor_ptr) { LKMotorInstance *motor = (LKMotorInstance *)motor_ptr; LOGWARNING("[LKMotor] motor lost, id: %d", motor->motor_can_ins->tx_id); } LKMotorInstance *LKMotorInit(Motor_Init_Config_s *config) { LKMotorInstance *motor = (LKMotorInstance *)malloc(sizeof(LKMotorInstance)); motor = (LKMotorInstance *)malloc(sizeof(LKMotorInstance)); memset(motor, 0, sizeof(LKMotorInstance)); motor->motor_settings = config->controller_setting_init_config; PIDInit(&motor->current_PID, &config->controller_param_init_config.current_PID); PIDInit(&motor->speed_PID, &config->controller_param_init_config.speed_PID); PIDInit(&motor->angle_PID, &config->controller_param_init_config.angle_PID); motor->other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; motor->other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; config->can_init_config.id = motor; config->can_init_config.can_module_callback = LKMotorDecode; #ifdef MULTI_MOTOR //多电机指令 config->can_init_config.rx_id = 0x140 + config->can_init_config.tx_id; config->can_init_config.tx_id = config->can_init_config.tx_id + 0x280 - 1; // 这样在发送写入buffer的时候更方便,因为下标从0开始,LK多电机发送id为0x280 #endif #ifdef SINGLE_MOTOR //单电机指令 config->can_init_config.rx_id = 0x140 + config->can_init_config.tx_id; config->can_init_config.tx_id = 0x140 + config->can_init_config.tx_id; #endif motor->motor_can_ins = CANRegister(&config->can_init_config); if (idx == 0) // 多电机指令用第一个电机的can instance发送数据 { sender_instance = motor->motor_can_ins; sender_instance->tx_id = 0x280; // 修改tx_id为0x280,用于多电机发送,不用管其他LKMotorInstance的tx_id,它们仅作初始化用 } LKMotorEnable(motor); DWT_GetDeltaT(&motor->measure.feed_dwt_cnt); lkmotor_instance[idx++] = motor; Daemon_Init_Config_s daemon_config = { .callback = LKMotorLostCallback, .owner_id = motor, .reload_count = 5, // 50ms }; motor->daemon = DaemonRegister(&daemon_config); return motor; } /* 第一个电机的can instance用于发送数据,向其tx_buff填充数据 */ void LKMotorControl() { float pid_measure, pid_ref; int16_t set; LKMotorInstance *motor; LKMotor_Measure_t *measure; Motor_Control_Setting_s *setting; for (size_t i = 0; i < idx; ++i) { motor = lkmotor_instance[i]; measure = &motor->measure; setting = &motor->motor_settings; pid_ref = motor->pid_ref; if ((setting->close_loop_type & ANGLE_LOOP) && setting->outer_loop_type == ANGLE_LOOP) { if (setting->angle_feedback_source == OTHER_FEED) pid_measure = *motor->other_angle_feedback_ptr; else pid_measure = measure->real_current; pid_ref = PIDCalculate(&motor->angle_PID, pid_measure, pid_ref); if (setting->feedforward_flag & SPEED_FEEDFORWARD) pid_ref += *motor->speed_feedforward_ptr; } if ((setting->close_loop_type & SPEED_LOOP) && setting->outer_loop_type & (ANGLE_LOOP | SPEED_LOOP)) { if (setting->speed_feedback_source == OTHER_FEED) pid_measure = *motor->other_speed_feedback_ptr; else pid_measure = measure->speed_rads; pid_ref = PIDCalculate(&motor->speed_PID, pid_measure, pid_ref); if (setting->feedforward_flag & CURRENT_FEEDFORWARD) pid_ref += *motor->current_feedforward_ptr; } if (setting->close_loop_type & CURRENT_LOOP) { //pid_ref = PIDCalculate(&motor->current_PID, measure->real_current, pid_ref); } set = (int16_t)pid_ref; if (setting->motor_reverse_flag == MOTOR_DIRECTION_REVERSE) set *= -1; //int16_t tmp = float_to_int16(set,I_MAX,COMMAND_MAX); // 这里随便写的,为了兼容多电机命令.后续应该将tx_id以更好的方式表达电机id,单独使用一个CANInstance,而不是用第一个电机的CANInstance memcpy(sender_instance->tx_buff + (motor->motor_can_ins->tx_id - 0x280) * 2, &set, sizeof(int16_t)); // motor->motor_can_ins->tx_buff[0] = 0xA2; //A1转矩闭环 A2速度闭环 // int32_t tmp = -1080*100;//(int32_t)set; // memcpy(motor->motor_can_ins->tx_buff + 4 , &tmp, sizeof(int32_t)); if (motor->stop_flag == MOTOR_STOP) { // 若该电机处于停止状态,直接将发送buff置零 memset(sender_instance->tx_buff + (motor->motor_can_ins->tx_id - 0x280) * 2, 0, sizeof(int16_t)); //memset( motor->motor_can_ins->tx_buff + 4 , 0, 4); } #ifdef SINGLE_MOTOR CANTransmit(motor->motor_can_ins, 0.2); #endif } #ifdef MULTI_MOTOR if (idx) // 如果有电机注册了 CANTransmit(sender_instance, 0.2); #endif } void LKMotorStop(LKMotorInstance *motor) { motor->stop_flag = MOTOR_STOP; } void LKMotorEnable(LKMotorInstance *motor) { motor->stop_flag = MOTOR_ENALBED; } void LKMotorSetRef(LKMotorInstance *motor, float ref) { motor->pid_ref = ref; } uint8_t LKMotorIsOnline(LKMotorInstance *motor) { return DaemonIsOnline(motor->daemon); }