Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String ipAddress = "192.168.1.2" ;
- String passWord = "admin" ;
- /*****************************************************************************************************************
- LINK BETWEEN EZ BUILDER AND KINECT - PROCESSING SKETCH CODE
- I, the author of this post cannott take credit for this code !!! I acknowledge the following sources :
- Chapter 6 (Kinect-networked Puppet) from the book Arduino and Kinect Projects: Design, Build, Blow Their Minds
- (Technology in Action)Paperback– April 17, 2012 by Enrique Ramos Melgar and Ciriaco Castro Diez.
- Source code for this book can be downloaded at the book's webpage : http://www.apress.com/9781430241676#main-desc
- and
- simple-openni OpenNI library for Processing. Webpage is : http://code.google.com/p/simple-openni/
- and
- Tutorials and examples from the processing website : https://www.processing.org/
- and
- Sample code, manuals, tutorials, and videos at the EZ-Robot website : https://www.ez-robot.com/
- ********************************************************************************************************************/
- import SimpleOpenNI.*;
- SimpleOpenNI kinect;
- // Left Arm Vectors
- PVector lHand = new PVector();
- PVector lElbow = new PVector();
- PVector lShoulder = new PVector();
- // Left Leg Vectors
- PVector lFoot = new PVector();
- PVector lKnee = new PVector();
- PVector lHip = new PVector();
- // Right Arm Vectors
- PVector rHand = new PVector();
- PVector rElbow = new PVector();
- PVector rShoulder = new PVector();
- // Right Leg Vectors
- PVector rFoot = new PVector();
- PVector rKnee = new PVector();
- PVector rHip = new PVector();
- // Articulation Angles
- float[] angles = new float[9];
- String lines[] ;
- int servoPosition ;
- double lastSent ;
- void SkeletonPosition(Integer joint, String Pin ) {
- servoPosition = int(map(degrees(angles[joint]), 90.0, -90.0, 180.0, 0.0)) ;
- if (servoPosition >0) {
- lines = loadStrings("http://" + ipAddress + "/Exec?password=" + passWord + "&script=servo%28%22" + Pin + "%22," + Integer.toString(servoPosition) + "%29");
- }
- }
- public void setup() {
- size(640, 480);
- kinect = new SimpleOpenNI(this);
- if (kinect.isInit() == false)
- {
- println("Can't init SimpleOpenNI, maybe the camera is not connected!");
- exit();
- return;
- }
- // enable depthMap generation
- kinect.enableDepth();
- // enable skeleton generation for all joints
- kinect.enableUser();
- stroke(0, 0, 255);
- strokeWeight(3);
- smooth();
- lastSent = millis() ;
- }
- public void draw() {
- // update the Kinect
- kinect.update();
- // draw depthImageMap
- image(kinect.depthImage(), 0, 0);
- int[] userList = kinect.getUsers();
- for (int i=0; i<userList.length; i++)
- {
- // draw the skeleton if it's available
- if (kinect.isTrackingSkeleton(userList[i])) {
- updateAngles();
- drawSkeleton(userList[i]);
- if ((millis() - lastSent) >= 100) {
- lastSent = millis() ;
- SkeletonPosition(5, "V0" ); // Left Elbow
- SkeletonPosition(1, "V1" ); // Right Elbow
- SkeletonPosition(2, "V2" ); // Right Shoulder
- SkeletonPosition(6, "V3" ); // Left Shoulder
- SkeletonPosition(4, "V4" ); // Left Hip
- SkeletonPosition(8, "V5" ); // Right Hip
- SkeletonPosition(3, "V6" ); // Left Knee
- SkeletonPosition(7, "V7" ); // Right Knee
- SkeletonPosition(0, "V8" ); // Orientation (for future use)
- }
- }
- }
- }
- void drawSkeleton(int userId) {
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
- kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
- }
- private void updateAngles() {
- // Left Arm
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_HAND, lHand);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_ELBOW, lElbow);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_SHOULDER,
- lShoulder);
- // Left Leg
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_FOOT, lFoot);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_KNEE, lKnee);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_HIP, lHip);
- // Right Arm
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_HAND, rHand);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_ELBOW,
- rElbow);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_SHOULDER,
- rShoulder);
- // Right Leg
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_FOOT, rFoot);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_KNEE, rKnee);
- kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_HIP, rHip);
- // Compute Body Rotation. It needs three-dimensional vectors
- angles[0] = atan2(PVector.sub(rShoulder, lShoulder).z,
- PVector.sub(rShoulder, lShoulder).x);
- // Convert to Projective
- kinect.convertRealWorldToProjective(rFoot, rFoot);
- kinect.convertRealWorldToProjective(rKnee, rKnee);
- kinect.convertRealWorldToProjective(rHip, rHip);
- kinect.convertRealWorldToProjective(lFoot, lFoot);
- kinect.convertRealWorldToProjective(lKnee, lKnee);
- kinect.convertRealWorldToProjective(lHip, lHip);
- kinect.convertRealWorldToProjective(lHand, lHand);
- kinect.convertRealWorldToProjective(lElbow, lElbow);
- kinect.convertRealWorldToProjective(lShoulder, lShoulder);
- kinect.convertRealWorldToProjective(rHand, rHand);
- kinect.convertRealWorldToProjective(rElbow, rElbow);
- kinect.convertRealWorldToProjective(rShoulder, rShoulder);
- // Left-Side Angles
- angles[1] = angle(lShoulder, lElbow, lHand);
- angles[2] = angle(rShoulder, lShoulder, lElbow);
- angles[3] = angle(lHip, lKnee, lFoot);
- angles[4] = angle(new PVector(lHip.x, 0), lHip, lKnee);
- // Right-Side Angles
- angles[5] = angle(rHand, rElbow, rShoulder);
- angles[6] = angle(rElbow, rShoulder, lShoulder );
- angles[7] = angle(rFoot, rKnee, rHip);
- angles[8] = angle(rKnee, rHip, new PVector(rHip.x, 0));
- }
- float angle(PVector a, PVector b, PVector c) {
- // Gets the angle between a, b, and c
- float angle01 = atan2(a.y - b.y, a.x - b.x);
- float angle02 = atan2(b.y - c.y, b.x - c.x);
- float ang = angle02 - angle01;
- return ang;
- }
- // -----------------------------------------------------------------
- // SimpleOpenNI events
- void onNewUser(SimpleOpenNI curkinect, int userId)
- {
- println("onNewUser - userId: " + userId);
- println("\tstart tracking skeleton");
- curkinect.startTrackingSkeleton(userId);
- }
- void onLostUser(SimpleOpenNI curkinect, int userId)
- {
- println("onLostUser - userId: " + userId);
- }
- void onVisibleUser(SimpleOpenNI curkinect, int userId)
- {
- //println("onVisibleUser - userId: " + userId);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement