Using PID controls to center robot on target with camera

Using the camera, we can get how many pixels we are off target. Is there a way to feed that into the PID controls and then have the PID controls output into the robotdrive?

As far as I know, in the provided PID function you have to give it a source (such as an encoder), you cannot give it a number. Also, I’m not sure if the PID output works with robotdrive (arcade mode).

Are you programming in the command based style? If so it is actually really easy to do with a PID subsystem.

What do you mean by command based style?

See the WPILib Cookbook located in the Documents area on the WPILib FIRSTForge project:

http://firstforge.wpi.edu/sf/go/doc1297?nav=1

Right now, we are using IterativeRobot.

You will have to create your own PID class that extends the current PID class and override some of the methods to get it to work the way that you want it to.

Can you give me some example code on how to do that?

Unfortunately there isn’t really any example code, and I don’t have any written right now.

A PID takes a setpoint and a processVariable as inputs, and adjusts its output to make the plant’s output (the processVariable) equal to the setpoint.

You want the “how many pixels we are off target” to be zero, right?

So your setpoint would be 0 (zero), and “how many pixels we are off target” is your process variable. The output from the PID would be your drivetrain rotation command (assuming that “how many pixels we are off target” is a measure of aim, not range).

The input of the PID wants a source though, not a number.

http://www.wbrobotics.com/javadoc/edu/wpi/first/wpilibj/PIDSource.html
Create a new class that extends PIDSource and rewrite the pidGet() method to get the offset of the target from 120 and return that value.

Give it a source that always returns zero for the setpoint.

Give it a source that returns “how many pixels we are off target” for the processVariable.

Look through VerticalTurretRotation and HorizontalTurretRotation, that’s how we do it.