Advertisement
Guest User

Untitled

a guest
Apr 6th, 2015
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. String ipAddress = "192.168.1.2" ;
  2. String passWord = "admin" ;
  3.  
  4. /*****************************************************************************************************************
  5.  
  6. LINK BETWEEN EZ BUILDER AND KINECT - PROCESSING SKETCH CODE
  7.  
  8. I, the author of this post cannott take credit for this code !!! I acknowledge the following sources :
  9.  
  10. Chapter 6 (Kinect-networked Puppet) from the book Arduino and Kinect Projects: Design, Build, Blow Their Minds
  11. (Technology in Action)Paperback– April 17, 2012 by Enrique Ramos Melgar and Ciriaco Castro Diez.
  12. Source code for this book can be downloaded at the book's webpage : http://www.apress.com/9781430241676#main-desc
  13.  
  14. and
  15.  
  16. simple-openni OpenNI library for Processing. Webpage is : http://code.google.com/p/simple-openni/
  17.  
  18. and
  19.  
  20. Tutorials and examples from the processing website : https://www.processing.org/
  21.  
  22. and
  23.  
  24. Sample code, manuals, tutorials, and videos at the EZ-Robot website : https://www.ez-robot.com/
  25.  
  26.  
  27. ********************************************************************************************************************/
  28.  
  29.  
  30.  
  31. import SimpleOpenNI.*;
  32. SimpleOpenNI kinect;
  33.  
  34. // Left Arm Vectors
  35. PVector lHand = new PVector();
  36. PVector lElbow = new PVector();
  37. PVector lShoulder = new PVector();
  38. // Left Leg Vectors
  39. PVector lFoot = new PVector();
  40. PVector lKnee = new PVector();
  41. PVector lHip = new PVector();
  42. // Right Arm Vectors
  43. PVector rHand = new PVector();
  44. PVector rElbow = new PVector();
  45. PVector rShoulder = new PVector();
  46. // Right Leg Vectors
  47. PVector rFoot = new PVector();
  48. PVector rKnee = new PVector();
  49. PVector rHip = new PVector();
  50.  
  51. // Articulation Angles
  52. float[] angles = new float[9];
  53.  
  54. String lines[] ;
  55. int servoPosition ;
  56. double lastSent ;
  57.  
  58. void SkeletonPosition(Integer joint, String Pin ) {
  59. servoPosition = int(map(degrees(angles[joint]), 90.0, -90.0, 180.0, 0.0)) ;
  60. if (servoPosition >0) {
  61. lines = loadStrings("http://" + ipAddress + "/Exec?password=" + passWord + "&script=servo%28%22" + Pin + "%22," + Integer.toString(servoPosition) + "%29");
  62. }
  63. }
  64.  
  65.  
  66. public void setup() {
  67.  
  68. size(640, 480);
  69. kinect = new SimpleOpenNI(this);
  70.  
  71. if (kinect.isInit() == false)
  72. {
  73. println("Can't init SimpleOpenNI, maybe the camera is not connected!");
  74. exit();
  75. return;
  76. }
  77.  
  78. // enable depthMap generation
  79. kinect.enableDepth();
  80.  
  81. // enable skeleton generation for all joints
  82. kinect.enableUser();
  83.  
  84. stroke(0, 0, 255);
  85. strokeWeight(3);
  86. smooth();
  87. lastSent = millis() ;
  88. }
  89.  
  90.  
  91. public void draw() {
  92.  
  93. // update the Kinect
  94. kinect.update();
  95.  
  96. // draw depthImageMap
  97. image(kinect.depthImage(), 0, 0);
  98.  
  99. int[] userList = kinect.getUsers();
  100. for (int i=0; i<userList.length; i++)
  101. {
  102. // draw the skeleton if it's available
  103. if (kinect.isTrackingSkeleton(userList[i])) {
  104. updateAngles();
  105. drawSkeleton(userList[i]);
  106. if ((millis() - lastSent) >= 100) {
  107. lastSent = millis() ;
  108. SkeletonPosition(5, "V0" ); // Left Elbow
  109. SkeletonPosition(1, "V1" ); // Right Elbow
  110. SkeletonPosition(2, "V2" ); // Right Shoulder
  111. SkeletonPosition(6, "V3" ); // Left Shoulder
  112. SkeletonPosition(4, "V4" ); // Left Hip
  113. SkeletonPosition(8, "V5" ); // Right Hip
  114. SkeletonPosition(3, "V6" ); // Left Knee
  115. SkeletonPosition(7, "V7" ); // Right Knee
  116. SkeletonPosition(0, "V8" ); // Orientation (for future use)
  117. }
  118. }
  119. }
  120. }
  121.  
  122.  
  123.  
  124. void drawSkeleton(int userId) {
  125. kinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
  126.  
  127. kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
  128. kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
  129. kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
  130.  
  131. kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
  132. kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  133. kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
  134.  
  135. kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  136. kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  137.  
  138. kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
  139. kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
  140. kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
  141.  
  142. kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
  143. kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
  144. kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
  145. }
  146.  
  147. private void updateAngles() {
  148.  
  149. // Left Arm
  150. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_HAND, lHand);
  151. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_ELBOW, lElbow);
  152. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_SHOULDER,
  153. lShoulder);
  154. // Left Leg
  155. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_FOOT, lFoot);
  156. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_KNEE, lKnee);
  157. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_LEFT_HIP, lHip);
  158. // Right Arm
  159. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_HAND, rHand);
  160. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_ELBOW,
  161. rElbow);
  162. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_SHOULDER,
  163. rShoulder);
  164. // Right Leg
  165. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_FOOT, rFoot);
  166. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_KNEE, rKnee);
  167. kinect.getJointPositionSkeleton(1, SimpleOpenNI.SKEL_RIGHT_HIP, rHip);
  168.  
  169. // Compute Body Rotation. It needs three-dimensional vectors
  170. angles[0] = atan2(PVector.sub(rShoulder, lShoulder).z,
  171. PVector.sub(rShoulder, lShoulder).x);
  172.  
  173. // Convert to Projective
  174. kinect.convertRealWorldToProjective(rFoot, rFoot);
  175. kinect.convertRealWorldToProjective(rKnee, rKnee);
  176. kinect.convertRealWorldToProjective(rHip, rHip);
  177. kinect.convertRealWorldToProjective(lFoot, lFoot);
  178. kinect.convertRealWorldToProjective(lKnee, lKnee);
  179. kinect.convertRealWorldToProjective(lHip, lHip);
  180. kinect.convertRealWorldToProjective(lHand, lHand);
  181. kinect.convertRealWorldToProjective(lElbow, lElbow);
  182. kinect.convertRealWorldToProjective(lShoulder, lShoulder);
  183. kinect.convertRealWorldToProjective(rHand, rHand);
  184. kinect.convertRealWorldToProjective(rElbow, rElbow);
  185. kinect.convertRealWorldToProjective(rShoulder, rShoulder);
  186.  
  187. // Left-Side Angles
  188. angles[1] = angle(lShoulder, lElbow, lHand);
  189. angles[2] = angle(rShoulder, lShoulder, lElbow);
  190. angles[3] = angle(lHip, lKnee, lFoot);
  191. angles[4] = angle(new PVector(lHip.x, 0), lHip, lKnee);
  192. // Right-Side Angles
  193. angles[5] = angle(rHand, rElbow, rShoulder);
  194. angles[6] = angle(rElbow, rShoulder, lShoulder );
  195. angles[7] = angle(rFoot, rKnee, rHip);
  196. angles[8] = angle(rKnee, rHip, new PVector(rHip.x, 0));
  197. }
  198.  
  199. float angle(PVector a, PVector b, PVector c) {
  200. // Gets the angle between a, b, and c
  201. float angle01 = atan2(a.y - b.y, a.x - b.x);
  202. float angle02 = atan2(b.y - c.y, b.x - c.x);
  203. float ang = angle02 - angle01;
  204. return ang;
  205. }
  206.  
  207.  
  208. // -----------------------------------------------------------------
  209. // SimpleOpenNI events
  210.  
  211. void onNewUser(SimpleOpenNI curkinect, int userId)
  212. {
  213. println("onNewUser - userId: " + userId);
  214. println("\tstart tracking skeleton");
  215.  
  216. curkinect.startTrackingSkeleton(userId);
  217. }
  218.  
  219. void onLostUser(SimpleOpenNI curkinect, int userId)
  220. {
  221. println("onLostUser - userId: " + userId);
  222. }
  223.  
  224. void onVisibleUser(SimpleOpenNI curkinect, int userId)
  225. {
  226. //println("onVisibleUser - userId: " + userId);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement