itblanco

AAD_Kinect_UR_Robot

Jun 11th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.29 KB | None | 0 0
  1. import processing.net.*;
  2. import kinect4WinSDK.*;
  3.  
  4. Kinect kinect;
  5. ArrayList <SkeletonData> bodies;
  6. PVector[] jointVecs = new PVector[4];
  7.  
  8. ////////////////////////
  9. ////// S E T U P ///////
  10. ////////////////////////
  11.  
  12. float[] distanceFromKinect = {1.2, 2.4};                    // How far from the Kinect do you want to track things? (in meters)
  13.  
  14. // Big Robot Settings:
  15. URobot bigRobot;
  16. String bigRobotIP = "192.168.9.2";
  17. int bigRobotJoint = Kinect.NUI_SKELETON_POSITION_HAND_LEFT; // Which joint do you want to track?
  18. float[] bigRobot_X_limits = {-0.9, 0.9};                    // Limits of the robot on the X axis
  19. float[] bigRobot_Y_limits = {-0.3, -0.8};                    // Limits of the robot on the Y axis
  20. float[] bigRobot_Z_limits = {0.2, 1.2};                     // Limits of the robot on the Z axis
  21.  
  22. //Small Robot Settings:
  23. URobot smallRobot;
  24. String smallRobotIP = "192.168.9.1";
  25. int smallRobotJoint = Kinect.NUI_SKELETON_POSITION_HAND_RIGHT; // Which joint do you want to track?
  26. float[] smallRobot_X_limits = {-0.6, 0.6};                     // Limits of the robot on the X axis
  27. float[] smallRobot_Y_limits = {-0.1, -0.6};                     // Limits of the robot on the Y axis
  28. float[] smallRobot_Z_limits = {0.2, 0.75};                      // Limits of the robot on the Z axis
  29.  
  30. ////////////////////////
  31. ////// S E T U P ///////
  32. ////////////////////////
  33.  
  34. void setup() {
  35.   //size(640, 480);
  36.   fullScreen();
  37.  
  38.   bigRobot = new URobot(this, bigRobotIP, 30003);
  39.   bigRobot.movej(bigRobot.homePosition, 0.6);
  40.   smallRobot = new URobot(this, smallRobotIP, 30003);
  41.   smallRobot.movej(smallRobot.homePosition, 0.6);
  42.  
  43.   kinect = new Kinect(this);
  44.   bodies = new ArrayList<SkeletonData>();
  45.  
  46.   for (int i = 0; i < jointVecs.length; i++) {
  47.     jointVecs[i] = new PVector(width/2, height/2);
  48.   }
  49. }
  50.  
  51. void draw() {
  52.   background(0);
  53.   sendKinectPoint(smallRobot, smallRobotJoint, #FF00C0);
  54.   sendKinectPoint(bigRobot, bigRobotJoint, #B6FF00);
  55.   drawKinectBodies();
  56. }
  57.  
  58. void sendKinectPoint(URobot r, int skeletonJoint, color col) {
  59.   if (bodies.size() > 0) {
  60.     SkeletonData s = bodies.get(0);
  61.     if (s.skeletonPositionTrackingState[skeletonJoint] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED) {
  62.       if(skeletonJoint == Kinect.NUI_SKELETON_POSITION_HAND_LEFT) {
  63.         PVector kinectpt = s.skeletonPositions[skeletonJoint];
  64.         jointVecs[0] = new PVector(map(kinectpt.x * width, 0, width, bigRobot_X_limits[0], bigRobot_X_limits[1]),
  65.                                    map(kinectpt.y * height, 0, height, bigRobot_Z_limits[1], bigRobot_Z_limits[0]),
  66.                                    map(kinectpt.z, distanceFromKinect[0] * 10000, distanceFromKinect[1] * 10000, bigRobot_Y_limits[0], bigRobot_Y_limits[1]));
  67.          
  68.         float dist = PVector.dist(jointVecs[0], jointVecs[1]);
  69.  
  70.         if (dist > 0.01) {
  71.           r.movej(new Pose(jointVecs[0].x, jointVecs[0].z, jointVecs[0].y, PI/2, 0, 0), 0.4);
  72.         }
  73.         drawCircle(new PVector(kinectpt.x * width, kinectpt.y * height), col);
  74.         jointVecs[1] = jointVecs[0].copy(); println("left hand: " + jointVecs[0]);
  75.       }
  76.       if(skeletonJoint == Kinect.NUI_SKELETON_POSITION_HAND_RIGHT) {
  77.         PVector kinectpt = s.skeletonPositions[skeletonJoint];
  78.         jointVecs[2] = new PVector(map(kinectpt.x * width, 0, width, smallRobot_X_limits[0], smallRobot_X_limits[1]),
  79.                                    map(kinectpt.y * height, 0, height, smallRobot_Z_limits[1], smallRobot_Z_limits[0]),
  80.                                    map(kinectpt.z, distanceFromKinect[0] * 10000, distanceFromKinect[1] * 10000, smallRobot_Y_limits[0], smallRobot_Y_limits[1]));
  81.          
  82.         float dist = PVector.dist(jointVecs[2], jointVecs[3]);
  83.  
  84.         if (dist > 0.01) {
  85.           r.movej(new Pose(jointVecs[2].x, jointVecs[2].z, jointVecs[2].y, PI/2, 0, 0), 0.4);
  86.         }
  87.         drawCircle(new PVector(kinectpt.x * width, kinectpt.y * height), col);
  88.         jointVecs[3] = jointVecs[2].copy(); println("right hand: " + jointVecs[2]);
  89.       }
  90.     }
  91.   }
  92. }
  93.  
  94. void drawCircle(PVector loc, color col) {
  95.   pushStyle();
  96.   noStroke();
  97.   fill(col);
  98.   ellipse(loc.x, loc.y, 60, 60);
  99.   popStyle();
  100. }
  101.  
  102. void drawKinectBodies() {
  103.   for (int i=0; i<bodies.size (); i++)
  104.   {
  105.     drawSkeleton(bodies.get(i));
  106.     drawPosition(bodies.get(i));
  107.   }
  108. }
  109.  
  110. public String movec(Pose p1, Pose p2) {
  111.   return "movec(" + p1.pose + ", " + p2.pose + ")\n";
  112. }
  113.  
  114. public String movej(Pose p) {
  115.   return "movej(" + p.pose + ")\n";
  116. }
  117.  
  118. public String movej(JointPose p) {
  119.   return "movej(" + p.pose + ")\n";
  120. }
  121.  
  122. public String movel(Pose p) {
  123.   return "movel(" + p.pose + ")\n";
  124. }
  125.  
  126. public String movel(JointPose p) {
  127.   return "movel(" + p.pose + ")\n";
  128. }
  129.  
  130. public String movep(Pose p) {
  131.   return "movep(" + p.pose + ")\n";
  132. }
  133.  
  134. public String movep(JointPose p) {
  135.   return "movep(" + p.pose + ")\n";
  136. }
  137.  
  138. class Pose {
  139.   String pose;
  140.  
  141.   Pose(float x, float y, float z, float rx, float ry, float rz) {
  142.     pose = String.format("p[%.9s, %.9s, %.9s, %.9s, %.9s, %.9s]", x, y, z, rx, ry, rz);
  143.   }
  144.  
  145.   Pose(PVector p1, PVector p2) {
  146.   }
  147.  
  148.   String interpolate_pose(Pose p, float alpha) {
  149.     return "interpolate_pose(" + pose + ", " + p.pose + ", " + alpha + ")\n";
  150.   }
  151. }
  152.  
  153. class JointPose {
  154.   String pose;
  155.  
  156.   JointPose(float x, float y, float z, float rx, float ry, float rz) {
  157.     pose = String.format("[%.9s, %.9s, %.9s, %.9s, %.9s, %.9s]", x, y, z, rx, ry, rz);
  158.   }
  159. }
  160.  
  161. class URobot {
  162.   Client client;
  163.   JointPose homePosition;
  164.  
  165.   URobot(PApplet parent, String robotIP, int robotPort) {
  166.     client = new Client(parent, robotIP, robotPort);
  167.     homePosition = new JointPose(0, -PI/2, 0, -PI/2, 0, 0);
  168.   }
  169.  
  170.   public void movec(Pose p1, Pose p2) {
  171.     client.write("movec(" + p1.pose + ", " + p2.pose + ")\n");
  172.   }
  173.  
  174.   public void movej(Pose p, float speed) {
  175.     client.write("movej(" + p.pose + ", v=" + speed + ")\n");
  176.   }
  177.  
  178.   public void movej(JointPose p, float speed) {
  179.     client.write("movej(" + p.pose + ", v=" + speed + ")\n");
  180.   }
  181.  
  182.   public void movel(Pose p) {
  183.     client.write("movel(" + p.pose + ")\n");
  184.   }
  185.  
  186.   public void movel(JointPose p) {
  187.     client.write("movel(" + p.pose + ")\n");
  188.   }
  189.  
  190.   public void movep(Pose p) {
  191.     client.write("movep(" + p.pose + ")\n");
  192.   }
  193.  
  194.   public void movep(JointPose p) {
  195.     client.write("movep(" + p.pose + ")\n");
  196.   }
  197.  
  198.   public void set_tcp(Pose p) {
  199.     client.write("set_tcp(" + p.pose + ")\n");
  200.   }
  201. }
  202.  
  203. void drawPosition(SkeletonData _s) {
  204.   noStroke();
  205.   fill(0, 100, 255);
  206.   String s1 = str(_s.dwTrackingID);
  207.   text(s1, _s.position.x*width, _s.position.y*height);
  208. }
  209.  
  210. void drawSkeleton(SkeletonData _s) {
  211.   // Body
  212.   DrawBone(_s,
  213.     Kinect.NUI_SKELETON_POSITION_HEAD,
  214.     Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER);
  215.   DrawBone(_s,
  216.     Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER,
  217.     Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT);
  218.   DrawBone(_s,
  219.     Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER,
  220.     Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT);
  221.   DrawBone(_s,
  222.     Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER,
  223.     Kinect.NUI_SKELETON_POSITION_SPINE);
  224.   DrawBone(_s,
  225.     Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT,
  226.     Kinect.NUI_SKELETON_POSITION_SPINE);
  227.   DrawBone(_s,
  228.     Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT,
  229.     Kinect.NUI_SKELETON_POSITION_SPINE);
  230.   DrawBone(_s,
  231.     Kinect.NUI_SKELETON_POSITION_SPINE,
  232.     Kinect.NUI_SKELETON_POSITION_HIP_CENTER);
  233.   DrawBone(_s,
  234.     Kinect.NUI_SKELETON_POSITION_HIP_CENTER,
  235.     Kinect.NUI_SKELETON_POSITION_HIP_LEFT);
  236.   DrawBone(_s,
  237.     Kinect.NUI_SKELETON_POSITION_HIP_CENTER,
  238.     Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);
  239.   DrawBone(_s,
  240.     Kinect.NUI_SKELETON_POSITION_HIP_LEFT,
  241.     Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);
  242.  
  243.   // Left Arm
  244.   DrawBone(_s,
  245.     Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT,
  246.     Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT);
  247.   DrawBone(_s,
  248.     Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT,
  249.     Kinect.NUI_SKELETON_POSITION_WRIST_LEFT);
  250.   DrawBone(_s,
  251.     Kinect.NUI_SKELETON_POSITION_WRIST_LEFT,
  252.     Kinect.NUI_SKELETON_POSITION_HAND_LEFT);
  253.  
  254.   // Right Arm
  255.   DrawBone(_s,
  256.     Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT,
  257.     Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT);
  258.   DrawBone(_s,
  259.     Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT,
  260.     Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT);
  261.   DrawBone(_s,
  262.     Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT,
  263.     Kinect.NUI_SKELETON_POSITION_HAND_RIGHT);
  264.  
  265.   // Left Leg
  266.   DrawBone(_s,
  267.     Kinect.NUI_SKELETON_POSITION_HIP_LEFT,
  268.     Kinect.NUI_SKELETON_POSITION_KNEE_LEFT);
  269.   DrawBone(_s,
  270.     Kinect.NUI_SKELETON_POSITION_KNEE_LEFT,
  271.     Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT);
  272.   DrawBone(_s,
  273.     Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT,
  274.     Kinect.NUI_SKELETON_POSITION_FOOT_LEFT);
  275.  
  276.   // Right Leg
  277.   DrawBone(_s,
  278.     Kinect.NUI_SKELETON_POSITION_HIP_RIGHT,
  279.     Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT);
  280.   DrawBone(_s,
  281.     Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT,
  282.     Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT);
  283.   DrawBone(_s,
  284.     Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT,
  285.     Kinect.NUI_SKELETON_POSITION_FOOT_RIGHT);
  286. }
  287.  
  288. void DrawBone(SkeletonData _s, int _j1, int _j2) {
  289.   noFill();
  290.   stroke(255, 255, 0);
  291.   if (_s.skeletonPositionTrackingState[_j1] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED &&
  292.     _s.skeletonPositionTrackingState[_j2] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED) {
  293.     line(_s.skeletonPositions[_j1].x*width,
  294.       _s.skeletonPositions[_j1].y*height,
  295.       _s.skeletonPositions[_j2].x*width,
  296.       _s.skeletonPositions[_j2].y*height);
  297.   }
  298. }
  299.  
  300. void appearEvent(SkeletonData _s) {
  301.   if (_s.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED)
  302.   {
  303.     return;
  304.   }
  305.   synchronized(bodies) {
  306.     bodies.add(_s);
  307.   }
  308. }
  309.  
  310. void disappearEvent(SkeletonData _s) {
  311.   synchronized(bodies) {
  312.     for (int i=bodies.size ()-1; i>=0; i--)
  313.     {
  314.       if (_s.dwTrackingID == bodies.get(i).dwTrackingID)
  315.       {
  316.         bodies.remove(i);
  317.       }
  318.     }
  319.   }
  320. }
  321.  
  322. void moveEvent(SkeletonData _b, SkeletonData _a) {
  323.   if (_a.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED)
  324.   {
  325.     return;
  326.   }
  327.   synchronized(bodies) {
  328.     for (int i=bodies.size ()-1; i>=0; i--)
  329.     {
  330.       if (_b.dwTrackingID == bodies.get(i).dwTrackingID)
  331.       {
  332.         bodies.get(i).copy(_a);
  333.         break;
  334.       }
  335.     }
  336.   }
  337. }
Add Comment
Please, Sign In to add comment