Hello,
I am working on creating a PID loop for my robot’s single wheel flywheel. I have looked it over several times and cannot seem to find what is wrong. When I set a new target, the motor power goes to 127, but it does not change from there.
This is my first time creating a PID loop, so any help is appreciated
Thanks
A couple of tips.
-
It’s far easier just to cut&paste you code into the forum rather than attaching a PDF file. That lets us easily test your code.
-
Lets look at what you are asking the code to do.
You set a target of 1000
You convert the target from inches to encoder ticks with the assumption that 5" wheels are being used.
1000 inches becomes 22918 ticks.
you reset the encoder each time around the loop and calculate speed, speed is calculated like this.
time = time1[T1] / 1000;
speed = (SensorValue[QE] / time) * 15
time will probably always 0 as that is an integer calculation, change it to this to force it to float.
time = time1[T1] / 1000.0;
You will probably run into a divide by zero error with this line of code the first time around the loop.
speed = (SensorValue[QE] / time) * 15;
as you clear timer 1 and then read it before any “wait1Msec” calls in the code, always try and test for divide by zero.
if( time > 0 )
speed = (SensorValue[QE] / time) * 15;
else
speed = 0;
Initially error will be large as speed is low. You calculate the motor drive as error (22918) x Kp (0.2) = 4583 and then clip to 127. When the motor speeds up error will drop as the speed variable increases, how much depends on where the encoder is, watch the values of speed and error in the debugger and lower the target speed and Kp as necessary.
Try making those changes and see what happens.
Thank you! All of your suggestions make sense. I will try debugging in a couple hours when I am back in the lab and report my findings.