element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need with Servo control using Kinect (Hand Tracking) + Processing + Arduino
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 18 replies
  • Subscribers 403 subscribers
  • Views 1876 views
  • Users 0 members are here
Related

Need with Servo control using Kinect (Hand Tracking) + Processing + Arduino

Former Member
Former Member over 13 years ago

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");

   }

  • Sign in to reply
  • Cancel
Parents
  • billabott
    0 billabott over 13 years ago

    It is a shame you did not include more comments in your code.

     

    I think it would be most helpful to you and to 'US' if you can go thru the code and add as many comments as you can.

     

     

    The only thing that jumps out at me is in the following code segment:

          }

        fill(255,0,0);

        ellipse(lastX,lastY,15,15); 

        spos= lastX;

        x= int(lastX);

        println (x);

        port.write("s"+spos);    // reviewer not sure if float value is what is needed/intended to be sent via write

        }

    Should the float value spos be cast as type int() before prefixing an "s" to the string being written out to the serial port?

     

    Best of Luck to You.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    Am Sorry, i am new to both processing and programming. I have included the comments now

     

    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);                                                  // Mirrors the image

      kinect.enableDepth();                                                  // Enable Depth Map Generation

      kinect.enableGesture();                                               // Enables Gesture generation

      kinect.enableHands();                                                 // Enables Hand identification

      kinect.addGesture("RaiseHand");                                // Recignises the Raised Hand Gesture

      handPositions = new ArrayList();                                 // Stores the values from PVector (hand positions over time)

     

    }

    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); 

    // This part of the code has been adapted from the tutorial on the arduino website  http://www.arduino.cc/playground/Learning/SingleServoExample   

     

    spos= lastX/4;                                                  // Calculate the servo position from lastX 

        x= int(lastX);

        println (x);

        port.write("s"+spos);                                         // Output the Servo Positions from 0 to 180

        }

           

     

     

        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");

       }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    Am Sorry, i am new to both processing and programming. I have included the comments now

     

    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);                                                  // Mirrors the image

      kinect.enableDepth();                                                  // Enable Depth Map Generation

      kinect.enableGesture();                                               // Enables Gesture generation

      kinect.enableHands();                                                 // Enables Hand identification

      kinect.addGesture("RaiseHand");                                // Recignises the Raised Hand Gesture

      handPositions = new ArrayList();                                 // Stores the values from PVector (hand positions over time)

     

    }

    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); 

    // This part of the code has been adapted from the tutorial on the arduino website  http://www.arduino.cc/playground/Learning/SingleServoExample   

     

    spos= lastX/4;                                                  // Calculate the servo position from lastX 

        x= int(lastX);

        println (x);

        port.write("s"+spos);                                         // Output the Servo Positions from 0 to 180

        }

           

     

     

        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");

       }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • billabott
    0 billabott over 13 years ago in reply to Former Member

    //Output the servo position ( from 0 to 180)

    port.write("s"+spos);

     

    as found on : http://rouvelle.com/rai_sp_12/

     

    Can you be a little be more accurate on how your servo is moving and possibly

    investigate the range of values allowed for spos?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    I adapted that piece of the code from herehttp://www.arduino.cc/playground/Learning/SingleServoExample

     

    Well ummmmm, you see we have this 640x480 window

     

    When my hand moves towards the left, the motor also rotates and stops when i reach "0" and when i move my hand towards the right then the motor turns in the opposite direction and stops when my hand reaches "640".

     

    Basically the program is the same as in http://www.arduino.cc/playground/Learning/SingleServoExample here the X-cordinates from the mouse is used to control the motor while i use the X coordinate from the Hand tracking to control the motor

     


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube