// Returns the distance between the index finger and thumb of the rightmost
  // hand in the field of view of the Leap Motion sensor
  private Double getGripDistance(Frame frame) {
    Double gripDist = -1.0;
    if (frame.isValid()) {
      if (!frame.hands().isEmpty() && frame.fingers().count() >= 2) {

        // right hand used to control arm
        Hand rightHand = frame.hands().rightmost();

        // get the two fingers with smallest X co-ords
        Finger thumb = null, index = null;
        Float minX = 9999f, minX2 = minX;
        Iterator<Finger> it = rightHand.fingers().iterator();
        while (it.hasNext()) {
          Finger finger = it.next();
          Float tipXPos = finger.stabilizedTipPosition().getX();
          if (tipXPos < minX) {
            minX2 = minX;
            index = thumb;
            minX = tipXPos;
            thumb = finger;
          } else if (tipXPos < minX2) {
            minX2 = tipXPos;
            index = finger;
          }
        }
        Vector thumbTip = thumb.stabilizedTipPosition();
        Vector indexTip = index.stabilizedTipPosition();
        gripDist = (double) thumbTip.distanceTo(indexTip);
      }
    }

    return gripDist;
  }
 // Returns the orientation of the palm given a frame of data
 // The double returned is a real number between 0 and 1 indicating
 // the component of the normal to the palm on the plane of the
 // computer screen
 private Double getPalmOrientation(Frame frame) {
   Double armOrt = -1.0;
   if (frame.isValid()) {
     if (!frame.hands().isEmpty()) {
       Vector normal = frame.hands().rightmost().palmNormal();
       armOrt = Math.sqrt(normal.getX() * normal.getX() + normal.getY() * normal.getY());
     }
   }
   return armOrt;
 }
 // Checks if the user wants to lock the position of the gripper
 // For locking the gripper, there need to be at least two hands in the
 // sensor's field of view, and the leftmost hand must have no fingers
 // outstretched (clenched fist with the back of the hand facing the sensor)
 private boolean checkGripperLock(Frame frame) {
   boolean locked = false;
   if (frame.isValid()) {
     if (frame.hands().count() > 1) {
       Hand leftHand = frame.hands().leftmost();
       if (leftHand.fingers().isEmpty()) locked = true;
     }
   }
   return locked;
 }