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
  • bad_boy_y
    0 bad_boy_y over 13 years ago in reply to billabott

    the motor is continuous turning?

    • 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 bad_boy_y

    Yes...The motor rotates contionously and stops when my hand reaches 0 or 640 ...

    • 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 bad_boy_y

    Yes...The motor rotates contionously and stops when my hand reaches 0 or 640 ...

    • 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

    You said: "The motor rotates continuously and stops when my hand reaches 0 or 640 ..."

    Your use of the word continuously in that manner suggests that the servo is rotating 360 non-stop!

    But, if I were to assume that you truely have a standard servo, then I know that its behavior is +/- 90 degrees from its center position based on if the PWM of the control signal being more or less than 50% duty cycle.  Does that make sense to you?

     

    If so, POINT ONE is that your real requirement is to scale the 0 to 639 xpos onto the Pulse width of the output pin controlling the servo.  Let us say that an xpos of 639 corresponds to 10% duty cycle and an xpos of 0 is 90% duty cycle.  [Could be the other way around, depending on your motor and/or circuit.]  You should work toward making your code do that (if I have understood your project correctly).

     

    POINT TWO, it seems to me that when you turn on the equipment one of the first things the arduino software should do is INITIALIZE the PWM to 50% and thereby CENTERING the servo motor arm position.  That corresponds to xpos of what?  Answer = 319.  There is no need for the servo to be moving unless the hand is detected within the field of view.  Therefore, it is unnecessary to pass any values to the arduino until there is a confirmed hand in the field of view.  You may even want to do a sort of debouncing of the detected hand position value to make sure that it is really present for more that a millisecond.

     

    Optional Question to ponder: If the hand being tracked leaves the field of view,  what position should the servo assume?

    • 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

    Thanks for taking the effort to go through my whole program ... As i mentioned earlier, i am a total beginner to programming..

     

    Point One : The program behaves exactly as the example program. The motor follows the mouse in the example program and in mine the motor follows the hand.

     

    Point Two : The Servo doesnt move until the hand appears within the view of the camera and once the hand is recognised it follows the hand irrespective of the position it is now. And once the hand goes beyond field of view, the motor stops (The whole Point TWo part actually works)

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

    Well, that sounds wonderful.  Could you please clarify again what it is that does not work the way you wish?

    • 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

    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 want it to move continously like in this video

     

    http://www.youtube.com/watch?v=S1SEuHJAZYg

     

     

    The program in the video is similiar to our program except that i used nearest pixel detection for the program in the video and here i am using gesture detection

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

    Eukleidēs is said to have replied to Ptolemy Lagides' request for an easier way of learning mathematics that "there is no Royal Road to geometry."

    • 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 do not understand, my english is rather bad

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

    Follow the link and see what you can learn, if you please.

    • 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

    Thanks but the tracking part is working fine in my program (you can see i have introduced a prinln statement to check the X cordinate values). The problem is with the servo motor.

    In an earlier program (http://www.youtube.com/watch?v=S1SEuHJAZYg), i used the nearest pixel detection and the x-cordinate values from it to run the motor. It worked fine as you can see in the video.

    Now i replaced the nearest pixel detection with Hand tracking/gesture detection. Now the servo motor does not rotate as i want it to (or like as in the video) however the printed X cordinate values are as expected! I need help to fix this. I am a beginner with arduino, kinect and programming!

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

    okay, I have a question.   Does your servo buzz?  If so, this is due to updating too frequently.  I would suggest that you change the serial communication rate way down from 19200 to 9600 or even 4800.   To help you understand this idea think about "What is the maximum resolution  of the port that is driving the servo?"  Isn't it 1 in 2^10  (1024)?  That is called 10 bit resolution.  Oh yeah, don't forget to update that 19200 number in both programs.

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

    My latest thought on your servo problem: That there may be a hardware/wiring/circuit loading problem or that particular  servo has a defect.  It would be up to you to write a test program and provide the same/similar set of values the you have observed coming to the arduino's serial port.

    • 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