Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/captureFrame.java
Created November 15, 2017 21:06
Show Gist options
  • Select an option

  • Save stephenlb/63d9d1628239c7fe20e8ca2f50ba51b4 to your computer and use it in GitHub Desktop.

Select an option

Save stephenlb/63d9d1628239c7fe20e8ca2f50ba51b4 to your computer and use it in GitHub Desktop.
public JSONObject handleHand(Hand hand){
boolean isLeft = hand.isLeft();
String handName = (isLeft) ? "left" : "right";
Vector direction = hand.direction();
int yaw = (int) Math.toDegrees(direction.yaw());
int pitch = (int) Math.toDegrees(direction.pitch());
// Normalize Yaw and Pitch
yaw = normalizeDegree(yaw);
pitch *= (isLeft) ? -1 : 1;
pitch = normalizeDegree(pitch);
// Get PWM Values
yaw = (int) yawDegreeToPWM(yaw);
pitch = (int) pitchDegreeToPWM(pitch);
JSONObject payload = new JSONObject();
int theByte = fingersToByte(hand);
int oldYaw = (isLeft) ? oldLeftYaw : oldRightYaw;
int oldPitch = (isLeft) ? oldLeftPitch : oldRightPitch;
if( (Math.abs(oldPitch - pitch) > 5) || (Math.abs(oldYaw - yaw) > 5) ) {
try {
payload.put(handName + "_yaw", yaw);
payload.put(handName + "_pitch", pitch);
} catch (JSONException e) {
e.printStackTrace();
}
if (isLeft) {
this.oldLeftYaw = yaw;
this.oldLeftPitch = pitch;
} else {
this.oldRightYaw = yaw;
this.oldRightPitch = pitch;
}
}
else{
try {
payload.put(handName + "_yaw", oldYaw);
payload.put(handName + "_pitch", oldPitch);
} catch (JSONException e) {
e.printStackTrace();
}
}
return payload;
}
public void captureFrame(Controller controller) {
// Get the most recent frame and report some basic information
Frame frame = controller.frame();
JSONObject payload = new JSONObject();
for (Hand hand : frame.hands()) {
try {
if (hand.isLeft()) {
payload.put("left_hand", handleHand(hand));
} else {
payload.put("right_hand", handleHand(hand));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
if(!payload.toString().equals("{}")) {
pubnub.publish()
.message(payload)
.channel(CHANNEL)
.async(new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
// handle publish result, status always present, result if successful
// status.isError to see if error happened
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment