void Motor_Control() { /* switch(uStateSwicth) { case StopSwitch: // 停车 { xStatus = 0; yStatus = 0; xCarParam.Speed_X = 0; xCarParam.Speed_Y = 0; xCarParam.Speed_Z = 0; xCarParam.EncoderSumY = 0; xCarParam.EncoderSumX = 0; xCarParam.CarDistanceX = 0; xCarParam.CarDistanceY = 0; break; } case CascadeSwitch: // 小车控制速度 { SpeedX_Control(); SpeedY_Control(); break; } } */ //=================EndSwitch================================= Position_PID(&IMU,xCarParam.yaw,tarYaw); xCarParam.Speed_Z = IMU.result; xCarParam.Speed_Y = 0; xCarParam.Speed_X = 0; // 三个速度限幅函数 xCarParam.Speed_X = LimitProtect(SpeedMaxX,-SpeedMaxX,xCarParam.Speed_X); xCarParam.Speed_Y = LimitProtect(SpeedMaxY,-SpeedMaxY,xCarParam.Speed_Y); xCarParam.Speed_Z = LimitProtect(SpeedMaxZ,-SpeedMaxZ,xCarParam.Speed_Z); // 计算占空比 motor[0].duty = xCarParam.Speed_Y + xCarParam.Speed_X + xCarParam.Speed_Z; motor[1].duty = xCarParam.Speed_Y - xCarParam.Speed_X + xCarParam.Speed_Z; motor[2].duty = xCarParam.Speed_Y - xCarParam.Speed_X - xCarParam.Speed_Z; motor[3].duty = xCarParam.Speed_Y + xCarParam.Speed_X - xCarParam.Speed_Z; // 增量式PID 自己有限幅函数 Increment_PID(&(motor[0].pid),encoder_data_quaddec[0],motor[0].duty); Increment_PID(&(motor[1].pid),encoder_data_quaddec[1],motor[1].duty); Increment_PID(&(motor[2].pid),encoder_data_quaddec[2],motor[2].duty); Increment_PID(&(motor[3].pid),encoder_data_quaddec[3],motor[3].duty); // 电机调速 setMotorDuty(&motor[0],motor[0].pid.result); setMotorDuty(&motor[1],motor[1].pid.result); setMotorDuty(&motor[2],motor[2].pid.result); setMotorDuty(&motor[3],motor[3].pid.result); }
时间: 2024-04-18 19:23:36 浏览: 99
这段代码是用于控制电机的函数。
首先,代码中有一段被注释掉的switch语句,根据uStateSwicth的不同值来执行不同的操作。当uStateSwicth等于StopSwitch时,表示停车状态,会将一些参数和状态重置为0。当uStateSwicth等于CascadeSwitch时,表示小车控制速度状态,会调用SpeedX_Control和SpeedY_Control函数。
接下来,在注释掉的代码之后,通过位置PID控制来计算小车的速度控制信号。具体来说,通过Position_PID函数计算出目标偏航角与当前偏航角的差值,并将其作为输入量。然后将计算得到的速度控制信号赋值给xCarParam.Speed_Z。
然后,将xCarParam.Speed_Y和xCarParam.Speed_X设置为0。
接着,对三个速度进行限幅处理,确保其在设定的上限和下限范围内。
然后,根据计算得到的速度以及电机的布置方式,计算出每个电机的占空比。
接下来,使用增量式PID控制算法对每个电机进行调速。通过Increment_PID函数计算出每个电机的控制量。
最后,使用setMotorDuty函数将计算得到的控制量设置给每个电机。
这段代码实现了电机的控制逻辑,根据目标偏航角和速度的设定,通过PID控制算法实现电机的调速。
相关问题
#define StopSwitch 0 #define CascadeSwitch 1 CAR xCarParam = {0}; float tarYaw = 90.0f; uint8_t uStateSwicth = 0; uint8_t xStatus = 0; uint8_t yStatus = 0; uint16_t SpeedMaxX = 200; uint16_t SpeedMaxY = 200; uint16_t SpeedMaxZ = 400; uint16_t uStartSpeed = 0; uint16_t speed_up_cnt_x = 0; uint16_t speed_up_cnt_y = 0; uint8_t increase_rat_x = 4; uint8_t increase_rat_y = 4; float Kx = 1.0f; float Ky = 1.0f; void SpeedX_Control(void); void SpeedY_Control(void); void MoveReset(void) { xCarParam.Speed_X = 0; xCarParam.Speed_Y = 0; xCarParam.Speed_Z = 0; xCarParam.Status = 0; xCarParam.EncoderSumX = 0; xCarParam.EncoderSumY = 0; xCarParam.CarDistanceX = 0; xCarParam.CarDistanceY = 0; } void Move(uint8_t CoordX,uint8_t CoordY) { pit_disable(MOTOR_PIT); MoveReset(); xCarParam.CarDistanceX = (int64_t)((CoordX - xCarParam.nowCoordX)*20.0f*Kx); xCarParam.CarDistanceY = (int64_t)((CoordY - xCarParam.nowCoordY)*20.0f*Ky); uStateSwicth = CascadeSwitch; pit_enable(MOTOR_PIT); } void Stop() { pit_disable(MOTOR_PIT); MoveReset(); uStateSwicth = StopSwitch; pit_enable(MOTOR_PIT); }
这段代码是一个移动控制的函数,通过控制小车的速度和距离来实现移动。
首先定义了一些常量和变量,如停止开关和级联开关的状态,小车的参数,目标偏航角,速度上限,起始速度等。
然后定义了一些函数,如速度控制函数SpeedX_Control和SpeedY_Control,以及MoveReset函数用于重置小车参数。
Move函数用于控制小车移动到指定的坐标位置。它首先禁用了定时器中断,然后重置小车参数。接着计算出小车在X和Y方向上需要移动的距离,并将其存储在CarDistanceX和CarDistanceY中。最后将级联开关状态设置为1,重新启用定时器中断。
Stop函数用于停止小车的运动。它也首先禁用了定时器中断,然后重置小车参数。然后将级联开关状态设置为0,重新启用定时器中断。
这段代码中还有一些其他的变量和参数,具体功能需要根据上下文来确定。
void TestDelay(uint32 delay); void TestDelay(uint32 delay) { static volatile uint32 DelayTimer = 0; while (DelayTimer<delay) { DelayTimer++; } DelayTimer=0; } extern void CAN2_ORED_0_31_MB_IRQHandler(void); #if 1 // #include "Can_Ipw.h" #define MSG_ID 20u #define RX_MB_IDX 1U #define TX_MB_IDX 0U volatile int exit_code = 0; extern Flexcan_Ip_StateType Can_Ipw_xStatus0; /* User includes / uint8 dummyData[8] = {1,2,3,4,5,6,7}; /! \brief The main function for the project. \details The startup initialization sequence is the following: * - startup asm routine * - main() / //extern const Clock_Ip_ClockConfigType Clock_Ip_aClockConfig[1]; extern void CAN0_ORED_0_31_MB_IRQHandler(void); int main(void) { uint8 u8TimeOut = 100U; CanIf_bTxFlag = FALSE; CanIf_bRxFlag = FALSE; / Initialize the Mcu driver / #if (MCU_PRECOMPILE_SUPPORT == STD_ON) Mcu_Init(NULL_PTR); #elif (MCU_PRECOMPILE_SUPPORT == STD_OFF) Mcu_Init(&Mcu_Config); / Initialize the clock tree and apply PLL as system clock / Mcu_InitClock(McuClockSettingConfig_0); while ( MCU_PLL_LOCKED != Mcu_GetPllStatus() ) { / Busy wait until the System PLL is locked / } #endif / (MCU_PRECOMPILE_SUPPORT == STD_ON) / / Write your code here / Mcu_DistributePllClock(); Mcu_SetMode(McuModeSettingConf_0); / Initialize Platform driver */ Platform_Init(NULL_PTR); Port_Init(&Port_Config); Spi_Init(&Spi_Config); #if 1 // CanTrcv_TJA1145_Init(); uint8 SWK_WUF_Detection = 0u; uint8 tempRegVal = 0u; /SBC mode StandBy/ /SBC_SetMode(CANTRCV_TRCVMODE_STANDBY);/ /Disable wakepin/ Sbc_Reg_Write(CanTrcv_Tja1145_Wpe, 0x00, FALSE); /Set Lock control register/ Sbc_Reg_Write(CanTrcv_Tja1145_Lc, 0x00, FALSE); /Can baudrate config/ Sbc_Reg_Write(CanTrcv_Tja1145_Dr, CANTRCV_TJA1145_CAN_DATA_RATE, FALSE); /Set CAN control register/ Sbc_Reg_Write(CanTrcv_Tja1145_Cc, 0x31, FALSE); Sbc_Reg_Read(CanTrcv_Tja1145_Ts, &tempRegVal); Sbc_Reg_Read(CanTrcv_Tja1145_Tes, &SWK_WUF_Detection); Sbc_Reg_Write(CanTrcv_Tja1145_Mc, CanTrcv_Tja1145_Mc_MC_Normal, FALSE); Sbc_Reg_Write(CanTrcv_Tja1145_Cc,CanTrcv_Tja1145_Cc_CMC_Active,FALSE); #endif // Clock_Ip_Init(&Clock_Ip_aClockConfig[0]); volatile Flexcan_Ip_StatusType result = 1; volatile Flexcan_Ip_StatusType result1 = 1; IntCtrl_Ip_EnableIrq(FlexCAN0_1_IRQn); IntCtrl_Ip_InstallHandler(FlexCAN0_1_IRQn, CAN0_ORED_0_31_MB_IRQHandler, NULL_PTR); // Dio_WriteChannel(DioConf_DioChannel_DioChannel_O_S_STB_CAN3_M, STD_LOW);//CAN3 STB Flexcan_Ip_DataInfoType rx_info = { .msg_id_type = FLEXCAN_MSG_ID_STD, .data_length = 8u, .is_polling = TRUE, .is_remote = FALSE }; Flexcan_Ip_MsgBuffType rxData; FlexCAN_Ip_Init(CanController_0, &Can_Ipw_xStatus0, &Flexcan_aCtrlConfigPB[0U]);// while (1) { if(rx_compli==1) { FlexCAN_Ip_SetStartMode(CanController_0); FlexCAN_Ip_ConfigRxMb(CanController_0, RX_MB_IDX, &rx_info, MSG_ID); // rx_info.is_polling = FALSE; FlexCAN_Ip_Send(CanController_0, TX_MB_IDX, &rx_info, MSG_ID, (uint8 *)&dummyData); FlexCAN_Ip_Receive(CanController_0, RX_MB_IDX, &rxData, TRUE); while(FlexCAN_Ip_GetTransferStatus(CanController_0, RX_MB_IDX)
Sorry, I'm an AI language model and I don't have enough context to understand your question. Could you please provide more information or clarify your question?
阅读全文