Hi Evereyone,
I have started learning to work with Kinect and processing for my Final Thesis. Now i have to control a Servo motor by waving my hand in front of Kinect.
I have included println(x) to display the value of the X cordinates.
The program works fine (the x coordinates displayed are as expected) as long as there is no servo connected however when i plugin this command to communicate with the servo
spos= x/4;port.write("s"+spos);
the program acts in a weird manner and i can see that there are plenty of random values in the x values.
Can someone please help me with this?
Program for the Arduino Environment
Program for the Processing Environment#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 1Serial.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 + 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;}}
}
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);println(x);fill(255,0,0);ellipse(lastX,lastY,15,15);//line(interpolatedX,interpolatedY,15,15);spos= x/4;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");}
1