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

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;
while(true)
{
error = targetrpm - SensorValue[port];

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

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
}

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.

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.

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.

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.

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.

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?