Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Pixy I2C (http://www.chiefdelphi.com/forums/showthread.php?t=134384)

mikefas 11-02-2015 19:12

Pixy I2C
 
I am trying to figure out how to interface a Pixy Camera with our Roborio using the i2C port. Does anyone have experience with the Pixy or i2C communications?

Also, can we instead use an Arduino as a mediator, we already wrote the code for the Arduino and It would be easier to do it via the Arduino to Roborio if possible.

Rakusan2 11-02-2015 19:18

Re: Pixy I2C
 
My team got the Arduino communicating with the roborio over I2C using the following code on the roborio
Code:

//this is outside a method
I2C Wire = new I2C(Port.kOnboard, 4);

//this is inside a method
if (Global.driver.Buttons.Back.changedDown) {
        String WriteString = "go"; //this is being sent to the Arduino
        char[] CharArray = WriteString.toCharArray();
        byte[] WriteData = new byte[CharArray.length];
        for (int i = 0; i < CharArray.length; i++) {
                WriteData[i] = (byte) CharArray[i];
        }
        Wire.transaction(WriteData, WriteData.length, null, 0);
}

And this code on the Arduino which was made to turn on an LED when it received the message "go"
Code:

#include <Wire.h>

void setup()
{
  pinMode (13, OUTPUT);
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  String LED = "";
 
  while ( Wire.available() > 0 )
  {
    char n=(char)Wire.read();
    if(((int)n)>((int)(' ')))
  LED += n;
  }
 
  if (LED == "go")
  {
   
    digitalWrite (13, HIGH);
 
   
  }
}


gpetilli 12-02-2015 08:58

Re: Pixy I2C
 
We are using pixy to lock onto yellow totes in AUTO. My team found it easier to decode packets via the serial port (connected to roboRIOC) than the I2C. We can share code if you need it.

matthewdenny 12-02-2015 09:16

Re: Pixy I2C
 
I'd be interested in the code.

mikefas 12-02-2015 10:32

Re: Pixy I2C
 
That would be great, if serial is possible it would be a lot easier.

AlexanderTheOK 13-02-2015 11:25

Re: Pixy I2C
 
I would also like to see said code.

2B || !2B 13-02-2015 16:57

Re: Pixy I2C
 
Hi there. Here is what out team is using to pry information out of the pixy. I hope this helps! I'm not sure why the code block is anti-tabbing...

(the code gpetilli mentioned)

Code:

import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.SerialPort.Port;
//Warning: if the pixy is plugged in through mini usb, this code WILL NOT WORK b/c the pixy is smart and detects where it should send data
public class Pixy {
SerialPort pixy;
Port port = Port.kMXP;
PixyPacket[] packets;
PixyException pExc;
String print;
public Pixy() {
pixy = new SerialPort(19200, port);
pixy.setReadBufferSize(14);
packets = new PixyPacket[7];
pExc = new PixyException(print);
}
//This method parses raw data from the pixy into readable integers
public int cvt(byte upper, byte lower) {
return (((int)upper & 0xff) << 8) | ((int)lower & 0xff);
}
public void pixyReset(){
pixy.reset();
}
//This method gathers data, then parses that data, and assigns the ints to global variables
public PixyPacket readPacket(int Signature) throws PixyException {
int Checksum;
int Sig;
byte[] rawData = new byte[32];
try{
rawData = pixy.read(32);
} catch (RuntimeException e){
}
if(rawData.length < 32){
System.out.println("byte array length is broken");
return null;
}
for (int i = 0; i <= 16; i++) {
int syncWord = cvt(rawData[i+1], rawData[i+0]); //Parse first 2 bytes
if (syncWord == 0xaa55) { //Check is first 2 bytes equal a "sync word", which indicates the start of a packet of valid data
syncWord = cvt(rawData[i+3], rawData[i+2]); //Parse the next 2 bytes
if (syncWord != 0xaa55){ //Shifts everything in the case that one syncword is sent
i -= 2;
}
//This next block parses the rest of the data
Checksum = cvt(rawData[i+5], rawData[i+4]);
Sig = cvt(rawData[i+7], rawData[i+6]);
if(Sig <= 0 || Sig > packets.length){
break;
}
packets[Sig - 1] = new PixyPacket();
packets[Sig - 1].X = cvt(rawData[i+9], rawData[i+8]);
packets[Sig - 1].Y = cvt(rawData[i+11], rawData[i+10]);
packets[Sig - 1].Width = cvt(rawData[i+13], rawData[i+12]);
packets[Sig - 1].Height = cvt(rawData[i+15], rawData[i+14]);
//Checks whether the data is valid using the checksum *This if block should never be entered*
if (Checksum != Sig + packets[Sig - 1].X + packets[Sig - 1].Y + packets[Sig - 1].Width + packets[Sig - 1].Height) {
packets[Sig - 1] = null;
throw pExc;
}
break;
}
}
//Assigns our packet to a temp packet, then deletes data so that we dont return old data
PixyPacket pkt = packets[Signature - 1];
packets[Signature - 1] = null;
return pkt;
}
}

Code:

public class PixyException extends Exception {
public PixyException(String message){
super(message);
}
}

Code:

public class PixyPacket {
public int X;
public int Y;
public int Width;
public int Height;
}


gsampel 14-02-2015 10:13

Re: Pixy I2C
 
2B || !2B ,
I am the lead programmer on Mike's team, thank you so much for your code, We are cutting it a little close to the deadline and this definitely helps.
Also, awesome name.
Thanks,
gsampel

gsampel 14-02-2015 11:46

Re: Pixy I2C
 
How exactly do we wire the Pixy, the stock cable does not seem like the correct one to connect directly to the RoboRio

gpetilli 14-02-2015 21:01

Re: Pixy I2C
 
Quote:

Originally Posted by gsampel (Post 1443947)
How exactly do we wire the Pixy, the stock cable does not seem like the correct one to connect directly to the RoboRio

In case you are a newer team, you can remove the roboRIO from your robot before you bag it and work on code any time there is a competition anywhere (Typically next 6 Thur, Fri, Sat).

Look at the Pixy Docs - i dont remember if they call it rs232 or serial port. roboRIO also has a serial port. I will ask our electrical team to post more details as to what they did.

gsampel 14-02-2015 21:11

Re: Pixy I2C
 
Is this the sort of thing were we can put together our own cable, or will we need to use the actual serial cable? I don't think we have one, so we would have to make one, probably out of a few scrapped PWM cables. I would find it really helpful to see a picture of your connection or what cable you used.
thanks,
gsampel

gpetilli 15-02-2015 07:54

Re: Pixy I2C
 
Quote:

Originally Posted by gsampel (Post 1444162)
Is this the sort of thing were we can put together our own cable, or will we need to use the actual serial cable? I don't think we have one, so we would have to make one, probably out of a few scrapped PWM cables. I would find it really helpful to see a picture of your connection or what cable you used.
thanks,
gsampel

yes, you can make your own cable. I seem to remember needing the hook the PIXY up via USB to a laptop to turn on the serial output. Serial outputs have multiple names so look for serial, uart, or rs232.

Sayol4 19-03-2015 19:54

Re: Pixy I2C
 
Does anyone have a picture or instructions on how to connect the pixy to the Roborio using the ribbon cables?


All times are GMT -5. The time now is 10:26.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi