# Flywheel PID

Could someone pls explain the theory behind PID velocity control and how to implement it. I use Robotc by the way. Is it measuring how fast the flywheel its going and the direction its going in because that is basically what velocity is. Speed + Direction.

What I do is “trick” a normal pd controller into thinking that the rpm of the flywheel is distance. I then take the output of the pd loop and have another loop take that value and add it it to the current value of the motors.

the basis is that you measure how fast it is going, and yes also direction, however you wheels should really only spin one way. what you do then, is tell the robot how fast you want it to spin, and then find the error between how fast you are spinning and how fast you want to be spinning, and adjust based on that. as for what exactly a PID loop is, it’s complicated, but basicly it is equations that set the motor current based on the error, and three values, called Kp,Ki and Kd, that is the P constant (proportion) the I constant (integral) and the D constant (derivative) hence the name PID, Ki Kp and Kd are experimentally determined. there are good guides online that explain this, I will try to find one and post it here

If you have previous experience with pid controls you know it try to set the difference between the target value and real value(error) equal to zero. In this case, using encoders you control the numbers of ticks per loop repetition.

for example:

``````
while(true)
{
speed=SensorValue(encoder);

SensorValue(encoder)=0;

error=target_speed - speed;

"the rest of pid..."

wait1msec(25);
}
``````

the speed is the number of ticks in 25 milliseconds

is very important to check if both the sensor and the motor are at the same direction, if you give the motor a positive output the encoder returns positive values.
An also restrict the pid motor output between 0 and 127, only going forward.

Here is a link to two robotC implementations that I did for flywheel PID that I was playing with this summer. There is one for a single flywheel and one for dual. The dual flywheel is more recent and polished. It uses my PID controller library which is also included in the linked repo. This code is as is and is not intended to work out of the box, it’s more intended to be used as example code.

[EDIT]I just want to point out that I am using an I loop, with an I constant of 0.004 and an epsilon value (ramping/slew rate) of 50 per second. The I loop kind of works as if it was a P loop since you’re taking RPM’s of a wheel, which is the rate of change (derivative) of the position, or P. So in a sense you’re taking the I of the D, which is P. Weird concept but I have fun thinking about it. The gear ratio was 23.52:1, which worked nicely for double flywheels.[/EDIT]

Is there an easy way to make this into EasyC v5? I know you have a library in EasyC, but can you use the implementation. Also, does the single flywheel code not use the library? It looks like it does but I’m just wondering.