Hi Robert,
Don't worry, I know the difference between sign & magnitude and locked antiphase.
If you're working with locked anti-phase, like the UHU chip does, then you need to look at the output from the PID filter, if it's positive you add to the dutycycle and if it negative you subtract the ABS value of the PID_Out from the duty cycle. Basically....
Code:
'At startup set the PWM dutycycle to 50%
PID_Error = Position - Setpoint
Gosub PID
If PID_Out.15 = 1 then 'Output is negative
DutyCycle = 512 - ABS PID_Out
Else 'Output is positive
DutyCycle = 512 + PID_Out
Endif
So again, if your dutycycle register is 50% (512) when no torque is delivered (ie. you are working with locked antiphase) then you really should clamp the output of the PID to +/-511, if you don't do that the dutycycle register may over- or underflow. If, for example, the output of the PID filter is allowed to swing to -640, and you subtract that from 512 you end up with -128. -128 in a 16bit variable is the same as 65407 so if you take the lower 10 bits (=895) of that and stuff it into your dutycycle register you'll end up with a dutycycle of 87% instead of 0 - torque will get delivered in wrong direction.....
Also, with a powerstage such as the one on the HP-UHU you need to make sure that dutycycle never reaches 0 or 100% because then the bootstrap capacitors in the highside MOSFET drivers no longer have any time to recharge and bad things will happen.
And, to show you that it does work, here's a link to a video I just uploaded:
Yes, it uses sign & magnitude and no it's not step and direction. It takes serial commands from a host CPU or PC. I've also got a couple of videos there featuring the HP-UHU. I've been quite involved in the HP-UHU project so I know it pretty well.
And as a friendly note, if you think that this PID-code doesn't work for your application then by all means feel free to use something else.
Sincerely,
/Henrik.
Bookmarks