451 views

# Robotc Flywheel PID help

1. last week

### TheBakingRobots

Dec 4

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);
wait1Msec(2000);

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

2. ### mattjiang

Dec 5 Irvine, CA 2496Y

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.

3. ### Ashwin_D.

Dec 5 California 2496J
Edited last week by Ashwin_D.

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);

{
int error;
int integral;
int derivative;
int lasterror;
int speed;
while(true)
{
error = targetrpm - SensorValue[port];

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

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

motor[port] = speed;
}
wait1Msec(25);
//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: https://www.vexforum.com/index.php/6465-a-pid-controller-in-robotc/0

4. ### Kselva20

Dec 5 5249Z

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.

5. ### TriDragon

Dec 5 Tampa, FL 6430 - Mentor

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.

6. ### Kselva20

Dec 6 5249Z

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.

7. ### TriDragon

Dec 6 Tampa, FL 6430 - Mentor

@Kselva20 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.

8. ### Kselva20

Dec 6 5249Z

@TriDragon 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.

9. ### Kselva20

Dec 6 5249Z

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

10. ### TriDragon

Dec 6 Tampa, FL 6430 - Mentor

@Kselva20 Yes, but simply using the ziegler nichols method one can very easily calibrate the PIDs

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

@Kselva20 bang-bang method results in a lot of variation

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.

@Kselva20 So long as you have a negative integral,

I really hope you don't have a negative integral....

@Kselva20 the equations are being computed by the brain, so the lag time is very very small.

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.

11. ### Joey The Great

Dec 6 Suspended Maryland 24k

all this complicated pid is why i did a catapult

12. ### meepmeepme

Dec 6 Honolulu, Hawaii (Saint Louis ... 42700B/C

@Joey The Great all this complicated pid is why i did a catapult

2bc or 1bc

13. ### Joey The Great

Dec 6 Suspended Maryland 24k

2bc

14. ### meepmeepme

Dec 6 Honolulu, Hawaii (Saint Louis ... 42700B/C

@Joey The Great 2bc

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.

15. ### Joey The Great

Dec 6 Suspended Maryland 24k

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

16. ### Kselva20

Dec 6 5249Z

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.

17. ### Joey The Great

Dec 6 Suspended Maryland 24k

what is bang-bang?

18. ### meepmeepme

Dec 6 Honolulu, Hawaii (Saint Louis ... 42700B/C

@Joey The Great 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?

19. ### Joey The Great

Dec 6 Suspended Maryland 24k

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

20. ### meepmeepme

Dec 6 Honolulu, Hawaii (Saint Louis ... 42700B/C

@Joey The Great 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