Hello again!
The PID algorithm is very very simple. The output of the PID will be the duty that will be applied to PWM channels (basically the power applied to motor. Below is the basic algorithm. You will note there is no time involved because the PID updates at a constant rate.
Position_Error = Encoder_Position - Requested_Position;
int32_t Output = Position_Error * Kp;
Integral += Position_Error * Ki;
Output += Integral;
Output += (Position_Error - Old_Position_Error) * Kd;
Old_Position_Error = Position_Error;
Now... We have to apply this output as duty to PWM channels. Please note that this implementation uses trapezoidal logic (basically meaning that only 2 outputs for the motor can be active at one time). If you want to use all 3 of them you will need to build somekind of a sinusoidal commutation matrix. (search the net for it there are tons of examples and explanations). For the DC motor things are simpler as there will be no commutation between phases as only 2 of them are used.
Using l6203 as driver is possible. You will have to alter the code a little. You will have to use only channels TIM1CH1 and TIM2CH2 (forget about the complementary channels) and do something like this in PWM_Set_Duty
void PWM_Set_Duty_DC(int16_t duty)
{
duty = Check_Max_Current(duty); // this is current limiting do not remove
if(duty > 0)
TIM1->CCR1 = duty;
else
TIM1->CCR2 = - duty;
}
Mihai.