View Single Post
  #1   Spotlight this post!  
Unread 17-01-2017, 22:44
question question is offline
Registered User
FRC #2472
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2014
Location: Minnesota
Posts: 7
question is an unknown quantity at this point
Using Pixy camera with an Arduino as a coprocessor for vision?

I am attempting to use The pixy camera for vision, I am able to get communication between pixy camera and the arduino using the basic example from the pixy camera library, However when I modified it to receive data from the roborio and respond with an appropriate action(forward,left,right,back). However the i2c.transaction is returning true, demonstrating that the transaction failed. My concern is that the i2c on the robot uses 3.3 volts, but the arduino uses 5 volts. Here is my relevant code for the roborio
Code:
public class Robot extends IterativeRobot{
long delay=System.currentTimeMillis()+500;
I2C i2c=new I2C(Port.kOnboard,168);
byte[] toSend=new byte[1];
byte[] reply=new byte[1];
Running this portion in test mode(will eventually be used as part of autonomous)
Code:
public void testPeriodic(){
if(System.currentTimeMillis()>=delay){
toSend[0]=21;
boolean b=i2c.transaction(toSend,1,reply,1);
if(b==false)System.out.println(""+reply[0]);
delay=System.currentTimeMillis()+500;
}
}
My Arduino code, the code inside the loop method is all from a pixy example.
Code:
#include <SPI.h>
#include <Pixy.h>
#include <Wire.h>
// This is the main Pixy object
Pixy pixy;
byte action;
void setup()
{
  Wire.begin(84);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
  Serial.print("Starting...\n");

  pixy.init();
}

void loop()
{

  static int i = 0;
  int j;
  uint16_t blocks;
  char buf[32];
  blocks = pixy.getBlocks();
  if (blocks)
  {
    i++;
    if (i % 50 == 0)
    {
      sprintf(buf, "Detected %d:\n", blocks);
      Serial.print(buf);
      for (j = 0; j < blocks; j++)
      {
        sprintf(buf, "  block %d: ", j);
        Serial.print(buf);
        pixy.blocks[j].print();
      }
    }
  }
}
void receiveEvent()
{
  while (Wire.available())
  {
    char c = Wire.read();
    if (c = 21) //return high goal shooter
    {
      Serial.print("high goal");
      for (int i = 0; i < 7; i++)
      {
        if (pixy.blocks[i].signature == 1)
        {
          if (pixy.blocks[i].x < 156) {
            //turn left
            action = 5;
          }
          if (pixy.blocks[i].x > 163) {
            //turn right
            action = 4;
          }
          if (pixy.blocks[i].y > 180) {
            //go backward
            action = 3;
          }
          if (pixy.blocks[i].y < 170) {
            //go forward
            action = 2;
          }
          if (pixy.blocks[i].x < 163 && pixy.blocks[i].x > 156 && pixy.blocks[i].y < 180 && pixy.blocks[i].y > 170) {
            //shoot!!
            action = 1;

          }

        }
      }
    }
    if (c = 42) //return gear liner upper...
    {

    }
    Wire.write(action + 7);
  }
}
I am sort of lost on why this isn't working(unless the problem is the voltage difference).
Reply With Quote