Code for Pixy Cam

package frc.robot;

import io.github.pseudoresonance.pixy2api.*;
import io.github.pseudoresonance.pixy2api.Pixy2CCC.Block;
import java.util.ArrayList;

public class BallTrackingPixy {
private Pixy2 pixycam;
private ArrayList target;

public BallTrackingPixy() {
pixycam = Pixy2.createInstance(Pixy2.LinkType.SPI);
}

public void ballTrackingTeleopPeriodic() {
target = pixycam.getCCC().getBlocks();
}

public double getBallX() {
return target.get(0).getX();

}

public double getBallY() {
return target.get(1).getY();
}
}

So I have this code for the ball tracking. Is there anything wrong with it? Cant test yet currently cause we don’t have our robot running yet.

1 Like

No idea what the answer to your question is but can you get an electronic board and hook it up that way?

Currently we cant, someone on our team is working on the board and we cant test any code yet. I was just wondering if this code is right or wrong. But I guess you cant answer till I test right?

No, I can’t answer because I don’t know :slight_smile:

Connecting pixy cam to roborio is challenging since there isn’t that much support for it and not a lot of teams use it. Do you have code for using the x and y values to make the motors run yet. Because one problem you might run into is that if the communication isnt fast enough u might have a turning over shoot problem.
CS_aim_bad

1 Like

I haven’t used this particular pixy library, but we programmed our own Java pixy library last year, so I’m familiar with the Pixy process at least.

You’re on the right track, but there’s still some issues.

First is the object you’re looking at. The getBlocks() function returns a list of Block objects. Each block has it’s own X and Y values accessible by getX() and getY(). However, the way you access the block itself is with get(0) and get(1) . This actually returns entirely different objects, which would represent 2 different power cells.

Next is the consideration that you could get 0 detected power cells . In this case, get(0) would return no object. This would result in your code crashing entirely, and your robot would reboot.

Generally the way you would handle this is by making sure that target has 1 or more objects in it, and only run a .get(X) for a valid index. It’d look something like if(target.length()>0)P{target.get(0); /*code*/}. It’s probably also wise to rename target to targets to more accurately represent that it’s 0 or more targets, not just one.

The next issue that will come up as is that calling getBallX() no longer makes sense when you have no targets, as you no longer have a valid value to return. As a result, you’d want to add an boolean haveValidTargets() function that can inform any commands whether they can trust the output of getBallX()

The upshot of this is that getX and getY methods are mostly extra work in the subsystem. You can instead return the Block object representing the first or best target, and your Command or other code can access the .getX and .getY of the target directly (assuming that you do in fact have a valid target).

Another thing worth considering is that you do not need a full robot to test the Pixy code. You can simply power the RoboRio using any 12V wall wart (just put ferrules on the end, and check your polarity) and wire up the pixy to SPI. This lets you test all the important detection and tracking parts, and once the robot is complete you can finish up the code relating to running the motors.

3 Likes

We’re not using it this year, but last year we created this class to interface over it with i2c

1 Like

Does this code only work for the IC2 port?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.