Log in

View Full Version : Using PID controls to center robot on target with camera


guoruiwu1994
17-02-2012, 09:36
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).

notmattlythgoe
17-02-2012, 09:40
Are you programming in the command based style? If so it is actually really easy to do with a PID subsystem.

guoruiwu1994
17-02-2012, 10:35
What do you mean by command based style?

Ether
17-02-2012, 10:40
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

guoruiwu1994
17-02-2012, 10:42
Right now, we are using IterativeRobot.

notmattlythgoe
17-02-2012, 10:50
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.

guoruiwu1994
17-02-2012, 10:53
Can you give me some example code on how to do that?

notmattlythgoe
17-02-2012, 11:07
Unfortunately there isn't really any example code, and I don't have any written right now.

Ether
17-02-2012, 11:36
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?

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

guoruiwu1994
17-02-2012, 11:40
The input of the PID wants a source though, not a number.

notmattlythgoe
17-02-2012, 11:56
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.

Ether
17-02-2012, 12:11
The input of the PID wants a source though, not a number.

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.

jesusrambo
18-02-2012, 14:33
https://github.com/jesusrambo/PurpaDrank/blob/master/PurpleDrank/src/edu/wpi/first/wpilibj/templates/commands/VerticalTurretRotation.java

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