uint32_t MOT_GetPosDif(uint32_t pos1, uint32_t pos2, uint32_t *gt)
{
if( pos1 > pos2)
{
*gt = 0;
return (pos1 - pos2);
}
else
{
*gt = 1;
return (pos2 - pos1);
}
}
void MOT_PositionLoopDouble(void)
{
uint32_t position1, position2, posdif, gt;
uint32_t pwm1, pwm2;
position1 = motor1_data_double->position;
position2 = motor2_data_double->position;
posdif = MOT_GetPosDif(position1, position2, >);
if (posdif > glob_mot_data.max_pos_diff)
{
//retry
glob_mot_data.pos_diff_count++;
if (glob_mot_data.pos_diff_count > glob_mot_data.max_pos_diff_count)
{
//maximum retries - unable to adjust speed
glob_mot_data.pos_diff_count = 0;
//send stop command
MOT_SendData(motor1_data_double, CAN_COM_STOP, 0);
MOT_SendData(motor2_data_double, CAN_COM_STOP, 0);
sys_status |= MOT_POS_DIFF;
}
}
pwm1 = motor1_data_double->speed;
pwm2 = motor2_data_double->speed;
//no position difference - maximum speed
if (posdif < glob_mot_data.max_pos_diff)
{
MOT_SendData(motor1_data_double, CAN_COM_SSET, glob_mot_data.pwm_max);
MOT_SendData(motor2_data_double, CAN_COM_SSET, glob_mot_data.pwm_max);
}
else if (gt == 0) //position1 > position2
{
//adjust pwm
pwm1 -= glob_mot_data.pwm_delta;
pwm2 += glob_mot_data.pwm_delta;
//check min pwm limit
if (pwm1 < glob_mot_data.pwm_min)
pwm1 = glob_mot_data.pwm_min;
//check max pwm limit
if (pwm2 > glob_mot_data.pwm_max)
pwm2 = glob_mot_data.pwm_max;
}
else if (gt == 1) //position2 > position1
{
//adjust pwm
pwm1 += glob_mot_data.pwm_delta;
pwm2 -= glob_mot_data.pwm_delta;
//check max pwm limit
if (pwm1 > glob_mot_data.pwm_max)
pwm1 = glob_mot_data.pwm_max;
//check min pwm limit
if (pwm2 < glob_mot_data.pwm_min)
pwm2 = glob_mot_data.pwm_min;
}
//update speed
MOT_SendData(motor1_data_double, CAN_COM_SSET, pwm1);
MOT_SendData(motor2_data_double, CAN_COM_SSET, pwm2);
}
|