Hi Everyone,
A part of my final internship is to use the Kinect to track a hand and use the cordinates to control the servo motor. .
I had written a program which recognizes the user's hand , converts the position to X-Axis cordinates (relative to the 640 x 480 window).
However the program seems to have a bug and i am not able to recognise where the bug is.
The program recognises my Hand as expected ( pointed out by a bright red spot drawn using ellipse command) and a println command reproduces the x_cordinate values and everything is in oder).
When i move my hand from left to right, the servo first makes a -180° turn (somewhere between 40 and 55 from the println values). Then the servo motor follows my hand but stops at a certain point (around 240 from the println values).
I have included both my arduino program and the Processing program.
I have been stuck here for the past three weeks, it would be great if someone can help me out please
Arduino Program
#include <Servo.h>
Servo servo1; Servo servo2;
void setup() {
pinMode(1,OUTPUT);
servo1.attach(14); //analog pin 0
//servo1.setMaximumPulse(2000);
//servo1.setMinimumPulse(700);
servo2.attach(15); //analog pin 1
Serial.begin(19200);
Serial.println("Ready");
}
void loop() {
static int v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v*10 + (int)(ch - '0');
break;
case 's':
servo1.write(v);
v = 0;
break;
case 'w':
servo2.write(v);
v = 0;
break;
case 'd':
servo2.detach();
break;
case 'a':
servo2.attach(15);
break;
}
}
}
Processing Program
import SimpleOpenNI.*;
import processing.serial.*;
SimpleOpenNI kinect;
Serial port;
float spos=90;
float currentX;
float lastX;
float currentY;
float lastY;
int x;
int y;
float interpolatedX;
float interpolatedY;
ArrayList<PVector> handPositions;
PVector currentHand;
PVector previousHand;
void setup(){
size (640,480);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 19200);
kinect= new SimpleOpenNI(this);
kinect.setMirror(true);
kinect.enableDepth();
kinect.enableGesture();
kinect.enableHands();
kinect.addGesture("RaiseHand");
handPositions = new ArrayList();
}
void draw(){
kinect.update();
image(kinect.depthImage(),0,0);
for (int i=1;i<handPositions.size();i++){
currentHand=handPositions.get(i);
previousHand=handPositions.get(i-1);
currentX=(currentHand.x);
lastX=(previousHand.x);
currentY=(currentHand.y);
lastY=(previousHand.y);
float interpolatedX = lerp(lastX,currentX,0.3f);
lastX = int(interpolatedX);
float interpolatedY = lerp(lastY,currentY,0.3f);
lastY = int(interpolatedY);
x= int(lastX);
y= int(lastY);
}
fill(255,0,0);
ellipse(lastX,lastY,15,15);
spos= lastX/4;
x= int(lastX);
println (x);
port.write("s"+spos);
}
void onCreateHands(int handId, PVector position, float time){
kinect.convertRealWorldToProjective(position,position);
handPositions.add(position);
}
void onUpdateHands(int handId, PVector position, float time){
kinect.convertRealWorldToProjective(position,position);
handPositions.add(position);
}
void onDestroyHands(int handId, float time) {
handPositions.clear();
kinect.addGesture("RaiseHand");
}
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
kinect.startTrackingHands(endPosition);
kinect.removeGesture("RaiseHand");
}