电机输出是车轮的动力来源,智能车的控制最后都要落实到电机输出。
参考 https://feichashao.com/ftm/
输出到电机的电压是不变的(12V),要控制输出动力大小,就要控制占空比的大小。
占空比越大,动力输出越大。(可以当作是线性的)
对于平衡直立车,输出动力的大小应该是 直立控制量+速度控制量+方向控制量 的叠加,而且左右轮各自的量是不一样的。
获得了直立速度方向控制量的总和,就可以输出到电机上了。
K60
void PWMSetMotor2(s32 speed_pwmL,s32 speed_pwmR)
{
if(speed_pwmL < MOTOR_MIN)
{
speed_pwmL = MOTOR_MIN;
}
if(speed_pwmL > MOTOR_MAX)
{
speed_pwmL = MOTOR_MAX;
}
if(speed_pwmR < MOTOR_MIN)
{
speed_pwmR = MOTOR_MIN;
}
if(speed_pwmR > MOTOR_MAX)
{
speed_pwmR = MOTOR_MAX;
}
if(speed_pwmL > 0)
{
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH0,(u32)speed_pwmL);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH1,0);
}
else
{
speed_pwmL = -speed_pwmL;
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH1,(u32)speed_pwmL);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH0,0);
}
if(speed_pwmR > 0)
{
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH2,(u32)speed_pwmR);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH3,0);
}
else
{
speed_pwmR = -speed_pwmR;
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH3,(u32)speed_pwmR);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH2,0);
}
}
{
if(speed_pwmL < MOTOR_MIN)
{
speed_pwmL = MOTOR_MIN;
}
if(speed_pwmL > MOTOR_MAX)
{
speed_pwmL = MOTOR_MAX;
}
if(speed_pwmR < MOTOR_MIN)
{
speed_pwmR = MOTOR_MIN;
}
if(speed_pwmR > MOTOR_MAX)
{
speed_pwmR = MOTOR_MAX;
}
if(speed_pwmL > 0)
{
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH0,(u32)speed_pwmL);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH1,0);
}
else
{
speed_pwmL = -speed_pwmL;
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH1,(u32)speed_pwmL);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH0,0);
}
if(speed_pwmR > 0)
{
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH2,(u32)speed_pwmR);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH3,0);
}
else
{
speed_pwmR = -speed_pwmR;
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH3,(u32)speed_pwmR);
FTM_PWM_Duty(MOTOR_FTMN,MOTOR_FTMCH2,0);
}
}
XS128
/**********************************************************************
Function: PWMSetMotor(long speed_pwmL,long speed_pwmR)
Description: Set PWM Duty.
Input: Left and Right PWM Duty.
Output:
Notice:
feichashao 2014-4-19
**********************************************************************/
void PWMSetMotor(long speed_pwmL,long speed_pwmR)
{
if(speed_pwmL < MOTOR_MIN)
{
speed_pwmL = MOTOR_MIN;
}
if(speed_pwmL > MOTOR_MAX)
{
speed_pwmL = MOTOR_MAX;
}
if(speed_pwmR < MOTOR_MIN)
{
speed_pwmR = MOTOR_MIN;
}
if(speed_pwmR > MOTOR_MAX)
{
speed_pwmR = MOTOR_MAX;
}
if(speed_pwmL > 0)
{
Set_Motor_Duty(0, (int)speed_pwmL);
Set_Motor_Duty(1, 0);
}
else
{
speed_pwmL = -speed_pwmL;
Set_Motor_Duty(1, (int)speed_pwmL);
Set_Motor_Duty(0, 0);
}
if(speed_pwmR > 0)
{
Set_Motor_Duty(4, (int)speed_pwmR);
Set_Motor_Duty(5, 0);
}
else
{
speed_pwmR = -speed_pwmR;
Set_Motor_Duty(5, (int)speed_pwmR);
Set_Motor_Duty(4, 0);
}
}
Function: PWMSetMotor(long speed_pwmL,long speed_pwmR)
Description: Set PWM Duty.
Input: Left and Right PWM Duty.
Output:
Notice:
feichashao 2014-4-19
**********************************************************************/
void PWMSetMotor(long speed_pwmL,long speed_pwmR)
{
if(speed_pwmL < MOTOR_MIN)
{
speed_pwmL = MOTOR_MIN;
}
if(speed_pwmL > MOTOR_MAX)
{
speed_pwmL = MOTOR_MAX;
}
if(speed_pwmR < MOTOR_MIN)
{
speed_pwmR = MOTOR_MIN;
}
if(speed_pwmR > MOTOR_MAX)
{
speed_pwmR = MOTOR_MAX;
}
if(speed_pwmL > 0)
{
Set_Motor_Duty(0, (int)speed_pwmL);
Set_Motor_Duty(1, 0);
}
else
{
speed_pwmL = -speed_pwmL;
Set_Motor_Duty(1, (int)speed_pwmL);
Set_Motor_Duty(0, 0);
}
if(speed_pwmR > 0)
{
Set_Motor_Duty(4, (int)speed_pwmR);
Set_Motor_Duty(5, 0);
}
else
{
speed_pwmR = -speed_pwmR;
Set_Motor_Duty(5, (int)speed_pwmR);
Set_Motor_Duty(4, 0);
}
}