Saturday, December 8, 2012

Serially

Working to get the Arduino and Processing to communicate, and have the Arduino use the tracking to control the servo motor. Right now I'm just working on having the Arduino set the motor on and off depending on whether the colour red is being detected in the frame. While the two do seem to be communicating i can't see what the Arduinos serial monitor is reading, and there look to be bugs with my coding as well. When I attach the servo at all the port that I am using disappears and only reappears when I pull out the firewire and reattach it. The Code im using found below:

PROCESSING:
import processing.serial.*; //This allows us to use serial objects
import JMyron.*;

JMyron m;
Serial port; // Create object from Serial class
int val; // Data received from the serial port

void setup(){
  int w = 320;
  int h = 240;
 
  size(w,h);
  m = new JMyron();
  m.start(320,240);
  m.findGlobs(1);
  println("Myron " + m.version());

String portName = Serial.list()[1]; //The serial port should match the one the Arduino is hooked to
port = new Serial(this, portName, 9600); //Establish the connection rate
}

void draw()
{
  //track colour
  m.trackColor(255,0,0,200);
 
  //minimum pixels in the glob required to result in a box
  m.minDensity(100);
 
  //fetch new data from camera
  m.update();
  //get current image and store it into array of integers
  int[] img = m.image();
 
  //define floats for red green and blue channels
  float ir,ig,ib;
 
  //first draw the camera view onto the screen
  loadPixels();
 
  //itterate through every pixel on the screen
  for(int i=0;i<width*height;i++){
       /*
        //this code inverts all pixels by subtracting their color value from the maximum
        //this is not necessary, but allows you to see better where your globs should occur
        //on your FTIR screen.
        //for some reason, human eyes distinguish white on black better than black on white.
        ir = 255-red(img[i]);
        ig = 255-green(img[i]);
        ib = 255-blue(img[i]);
       
        //add the inverted pixel to an array
        pixels[i] = color(ir,ig,ib);
        */
       
       
        //you can invert the displayed webcam colour by uncommenting the above code and commenting this line)
      pixels[i] = img[i];
  }
  updatePixels();
 
 
  //draw an averaged color block where the mouse is.
  noStroke();
  int c = m.average(mouseX-20,mouseY-20,mouseX+20,mouseY+20);
  fill(red(c),green(c),blue(c));
  rect(mouseX-20,mouseY-20,40,40);

  noFill();
 
  //initiate a 2-d array that will hold blob centers x and y position
  int[][] a;

 //fetch them
  a = m.globCenters();
 
  //draw them
  stroke(255,255,0);
  fill (128,0,128,128);
 
  //for every blob center
  print("i see" +a.length+"blobs\n");

  //itterate through all blobs
  for(int i=0;i<a.length;i++){
   
   
    //output to the console which blob this is
    print("blob"+ i + " : ");
    int[] p = a[i];
    point(p[0],p[1]);
    port.write('H'); // send an H to indicate mouse is over square
    }
  
 
  //draw bounding boxes of globs
  a = m.globBoxes();
  stroke(255,0,0);
  for(int i=0;i<a.length;i++){
    int[] b = a[i];
    rect(b[0], b[1], b[2], b[3]);
  }

  //draw edge points (same as last, but vector based)
  int list[][][] = m.globEdgePoints(20);
  stroke(0,128,0);
 
  for(int i=0;i<list.length;i++){
    int[][] contour = list[i];
    if(contour!=null){
      beginShape(LINE_LOOP);
      for(int j=0;j<contour.length;j++){   
        vertex( contour[j][0]  ,  contour[j][1] );
      }
      endShape();
     }
  }
            port.write('L');
}

public void stop(){
  m.stop();
  super.stop();
 }

ARDUINO:
  #include <Servo.h>

Servo myservo; //create sservo object to control a servo

char val; // Data received from the serial port

void setup()
{
pinMode(9, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
myservo.attach(9);
}

void loop()
{
if (Serial.available())
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}

if (val == 'H')
{ // If H was received
digitalWrite(9, HIGH); // turn the LED on
}
else
{
digitalWrite(9, LOW); // Otherwise turn it OFF
}
}

And some visual aid..

No comments:

Post a Comment