PixyCam Communication with Roborio through SPI on MXP ports

Hello, I am a programming co-captain for team 3929. This year, we have decided to opt for a PixyCam vision processing system. We have tried using the I2C ports, but were unable to achieve this (we did not know what internal register address to pass into the read() method), we decided to use the SPI ports instead. Here is the relevant code:

import edu.wpi.first.wpilibj.SerialPort;
 import edu.wpi.first.wpilibj.SerialPort.Port;
public class Robot extends IterativeRobot {
	final String defaultAuto = "Default";
	final String customAuto = "My Auto";
	String autoSelected;
	SendableChooser<String> chooser = new SendableChooser<>();
	
	SerialPort pixy;
	Port port = Port.kMXP;
      public void robotInit() {
		chooser.addDefault("Default Auto", defaultAuto);
		chooser.addObject("My Auto", customAuto);
		SmartDashboard.putData("Auto choices", chooser);
		
		pixy = new SerialPort(19200, port);
		pixy.setReadBufferSize(2);
		
	}

	@Override
	public void teleopPeriodic() {
		//This prints out zero
		System.out.println(pixy.read(2).length);
               
               //print outs are like: **;

		}
}

Thank you! We appreciate any help, the mech team is getting pretty heated :)**

Regardless if you use i2c or spi to get data from pixy, you need to be to interpret the data coming across. Look at the section called ‘The serial protocol’ on http://www.cmucam.org/projects/cmucam5/wiki/Porting_Guide

We were able to get our pixy working with i2c. Found some posts and code on CD that helped immensely. Take a look at our pixy reading code here:
https://github.com/BHSRobotix/Steamworks2017/blob/master/src/org/usfirst/frc2876/Steamworks2017/subsystems/PixyI2C.java

Here’s an example of a team using serial port, I don’t know if it works:
https://github.com/AllSparks2848/PixyCamTest/blob/master/src/org/usfirst/frc/team2850/robot/Pixy.java

Notice that both examples are reading bytes and than interpreting them according to the pixy serial protocol.

If all that is too much, then try using the pixy via digital and analog inputs.
I haven’t seen any examples of code that uses that method but you could search on github… something like https://github.com/search?utf8=%E2%9C%93&q=frc+pixy+analog&type=Code&ref=searchresults

Thank you for the links. We have been referencing the cmucam wiki for the pixy cam, and do have code for data interpretation, but we neglected to include it because that code is not the issue, at least as far as we know. We were not able to test the interpreting code because our code to receive data, and place them into arrays, is not working.

One thing that are unsure about is which spi protocol to use, or whether we should use the UART protocol, since one of the SerialPort arguments, (19200), is the default UART baudrate.

Here is a link to our team’s Bitbucket with the pixy code from last year.

It uses I2C to communicate and read the first signature identified. The class is set up to run in it’s own thread on the RoboRIO and periodically update with new data. Hope it’s helpful!