Electronic – Not sure how to go from PID error to PWM duty

controlpid controllerpwm

I am trying to learn the basics of PID. I have made a contraption to try to implement a PID controller.

enter image description here

I am not using carbon fiber, but rather a chopstick.

So I am controlling the speed of the propeller with a PWM signal through a mosfet from an STM32. I think 14Khz…Dont recall what frequency I set it to.
My counter control register which controls my duty cycle can vary from 0 to 1000.

I am reading the pot value through the an ADC channel.

In my attempt to implement one control method at a time. P then I then D
I never got past P because I dont know what to do with the value I get.

For example, Let say my chopstick is level (parallel to floor) at an ADC value of 2000. and I currently read 1200 then my error is 800.
According to PID literature this gets multiplied by a gain. lets say the gain is .5 So now my P value is 400.
But what do i Do with that?
I cant set the PWM CCR to 400 that is too low a duty cycle and it will drop even more.

So that is my issue how do I translate the value to something useful.

All I am doing right now is that if my error is grater than zero then
PWM = PWM – 1;
and vice versa for error less than zero
Obviously it works really slow because I am using the value of 1 but this is NOT proportional control. At least i dont think it is.

EDit: I think has to do with my issue.
enter image description here
enter image description here
SO Like I said in the comments I can get it to work (sort of) so long as when i disturb it I dont make it go above a certain point, otherwise It will fly all the way vertical have a huge error and then drop have another huge error and just keep going and going. And god forbid I try to make a target point that is above 0 deg that stays put for a few seconds and then oscillates again. I just think the complexity of this system requires more then the simple PID tutorials you see everywhere involving speed control of a motor. I would follow the edx course on this but I dont understand the math and their notations. No real number examples or anything. I do not learn well just seeing bunch of variables and no real world examples.

Best Answer

You need to make a distinction between the signals, the angle of the chopstick (range -90 to +90 degrees, assuming down = 0 degrees), the duty cycle of the motor (range 0-100% for zero to max speed), and the numbers in your system that represent those signals.

Do your PID mathematics in the signal domain. It doesn't matter whether you have a 11 bit ADC reading 2000 when the pendulum is at 90 to the floor, or an 8 bit ADC reading 200, they both represent 90 to the floor.

The first thing to do therefore is to draw your block diagram, and express gains, signal ranges etc in sensible signal units. Then, the first thing you do when you read an angle, scale and offset it into a signal. The last thing you do when you have a PID controller output signal is to scale and offset it into a number that will control 0-100% duty cycle at your motor.