Robotc Flywheel PID help


I am trying to at a PID controller to our single-flywheel shooter, but I do not know exactly what I’m doing and cant quite get it to work so any help would be appreciated. This is all I have so far.
float k = .02;
int wheel;
int speed;
int desired = 400;
SensorValue(wheelen) = 0;
wheel = SensorValue(wheelen);

int error = desired-wheel;
motor(flywheelF) = speed + (error * k);

motor(flywheelC) = speed + (error * k);
motor(flywheelB) = speed + (error * k);


It looks like you have made a positional P loop instead.

If you are trying to make it move at a certain speed, you have to calculate velocity and then compare it to a target. I think you tried to do it with the “speed” variable but I don’t see where you set its value. Hope this helps.


As Matt said, so far you have a P loop out of the entire PID. In addition, you should try writing the PID in a task. It should look something like this

float kP = 0.3 (or number that you tune);
float kI = 0.05 (or number you tune);
float kD = 0.1 (or number you tune);
int targetrpm (a number that you constantly add);

task FlywheelPID()
int error;
int integral;
int derivative;
int lasterror;
int speed;
error = targetrpm - SensorValue[port];

if(abs(error) < 50) // or your integral limit
integral += error;
integral = 0;

derivative = error - lasterror;
lasterror = error;
speed = (error*kP) + (integral *kI) + (derivative *kD);

motor[port] = speed;
//at this point add the target by your desired rpm which you find via calculation or you do another task for rpm

Here is a popular post by Jpearman on PID if you need further help:


All of the above are correct, I would also like to point out that you need a while loop to constantly adjust the speed over time.


You could also just use a bang-bang controller. It worked very well for our kids during NBN.

You can modify it somewhat to have the bottom be > 0. So, for instance, check your speed, if it is less than you want then set the output to 127. If it is more than what you want set it to 50. repeat this every 20ms.

The 20 ms is important on the Cortex because of delays in the actual requesting value getting sent out to the motors.

The value of 50 is experimental. It depends a bit on your friction and such, but you want it low enough that your flywheel will not be increasing speed any longer, but high enough that it doesn’t decrease speed to quickly.

The value of 127 is probably ok, but is experimental to some degree. Sometimes you want two values, the value of 127 until the flywheel first crosses the requested speed, then a second value when it is holding speed.

This bang-bang reacts instantly.


I think you can just use a PID controller @TriDragon to check speed and the error. This will adjust real time without any delay. It seems a bang-bang controller will not be as accurate as these value are estimations. The robot will be doing all of the calculations for adjusting the speed.


There is plenty of delay in a PID, it may be real time in checking and update (20ms), but it will have phase lag due to the equations. And for the OP, new to PID, tuning and such will take a good bit of time. You need a lot of gain in the PID to get the response time of resolving speed after each shot. You also need good sensors for a derivative, and the resolution of the sensors for the Cortex systems is fairly poor. For NBN, there was very little time between shots, << 1sec, so it needed very quick response. If this game can take a few seconds to recover then PI will work fine.


Yes, but simply using the ziegler nichols method one can very easily calibrate the PIDs and the bang-bang method results in a lot of variation. So long as you have a negative integral, small derivative, and large proportion then there will be quick adjustments. Also, the equations are being computed by the brain, so the lag time is very very small.


@TriDragon Also, the flywheel should not be set to a new speed immediately. The motors can be very easily damaged.


Yes, it’s a good start, but real PID control generally requires more than just that type of tuning for good results…

It results in a lot of jumping around of the commanded power, but not of the overall system. Mechanical inertia, as well as, electrical response time smooths that out.

I really hope you don’t have a negative integral…

I am assuming the brain has the power to do the equations, although bang-bang taxes the processor much less. The lag has nothing to do with the brain, it is inherent in the controls equations and their gains.


all this complicated pid is why i did a catapult


2bc or 1bc




ok my bad. How do you load it? I’m thinking of just having the first ball roll down from the top bracket to the bottom bracket, and have the second ball be held up by that first ball.


basically, our catapult lays in a near-horizontal position, and is fed from a roller at the opposite end from the axis of the arm


The negative integral helps the derivative. It is popularly used in gyroscope PID coding. The ziegler nichols method is a mathematical model proved for tuning PIDs, so it is extremely accurate. In fact, I was able to tune our chassis within 15 minutes. I would also just be very weary about using the bang-bang method. Motors will be taxed and damage them permanently.


what is bang-bang?


so when your catapult pulls down how far does it travel? when it reaches the top of its arc does it have to drop down to be loaded? Does the arm have to be stopped in some way to allow loading?


i can send a video sometime soon
why did you quote my question about bang bang?


it was your most recent message, my bad.