Skip to content

Using the API

Josh edited this page Oct 5, 2023 · 6 revisions

First create a Pixy2 camera object with Pixy2 pixy = Pixy2.createInstance(link) and supply the link type of your choosing. Next, initialize the Pixy2 camera with pixy.init(arg). You can either omit arg, or add a value based on the link type.

The Pixy2 can now be called on with the various provided methods as outlined in the documentation included in the code and on the Pixy2 website.

Latest Javadocs

Examples

Turning on the LEDs

public class PixyCameraExample {

	private static final Pixy2 pixy;

	public static void initialize() {
		pixy = Pixy2.createInstance(new SPILink()); // Creates a new Pixy2 camera using SPILink
		pixy.init(); // Initializes the camera and prepares to send/receive data
		pixy.setLamp((byte) 1, (byte) 1); // Turns the LEDs on
		pixy.setLED(200, 30, 255); // Sets the RGB LED to purple
	}
}

Finding the Biggest Target in the Frame

public class PixyCameraExample {

	private static final Pixy2 pixy;

	public static void initialize() {
		pixy = Pixy2.createInstance(new SPILink()); // Creates a new Pixy2 camera using SPILink
		pixy.init(); // Initializes the camera and prepares to send/receive data
		pixy.setLamp((byte) 1, (byte) 1); // Turns the LEDs on
		pixy.setLED(255, 255, 255); // Sets the RGB LED to full white
	}

	public static Block getBiggestBlock() {
		// Gets the number of "blocks", identified targets, that match signature 1 on the Pixy2,
		// does not wait for new data if none is available,
		// and limits the number of returned blocks to 25, for a slight increase in efficiency
		int blockCount = pixy.getCCC().getBlocks(false, Pixy2CCC.CCC_SIG1, 25);
		System.out.println("Found " + blockCount + " blocks!"); // Reports number of blocks found
		if (blockCount <= 0) {
			return null; // If blocks were not found, stop processing
		}
		ArrayList<Block> blocks = pixy.getCCC().getBlockCache(); // Gets a list of all blocks found by the Pixy2
		Block largestBlock = null;
		for (Block block : blocks) { // Loops through all blocks and finds the widest one
			if (largestBlock == null) {
				largestBlock = block;
			} else if (block.getWidth() > largestBlock.getWidth()) {
				largestBlock = block;
			}
		}
		return largestBlock;
	}
}

Example of Use in a Robot

Charger Robotics 2019