KUKA iiwa — VREP显示机械臂位置

   机械臂控制器中Background task通过UDP向外部程序每隔50ms循环发送当前各轴位置:

 1 package sampleBackgroundTask;
 2 
 3 import javax.inject.Inject;
 4 import java.util.concurrent.TimeUnit;
 5 import com.kuka.roboticsAPI.applicationModel.tasks.CycleBehavior;
 6 import com.kuka.roboticsAPI.applicationModel.tasks.RoboticsAPICyclicBackgroundTask;
 7 import com.kuka.roboticsAPI.deviceModel.JointPosition;
 8 import com.kuka.roboticsAPI.deviceModel.LBR;
 9 import java.net.*;
10 import java.io.IOException; 
11 
12 
13 public class BackgroundTask extends RoboticsAPICyclicBackgroundTask {
14     @Inject
15     private LBR robot; 
16     
17     byte[] posArr;    
18     int port = 30003;
19     InetAddress IP = null; 
20     DatagramSocket datagramSocket = null; 
21     DatagramPacket packet; 
22 
23     @Override
24     public void initialize() {
25         // initialize your task here
26         initializeCyclic(2000, 50, TimeUnit.MILLISECONDS, CycleBehavior.BestEffort);
27         
28         try {
29             datagramSocket = new DatagramSocket();   
30             IP =  InetAddress.getByName("192.168.1.50"); // get PC's address
31         } 
32         catch (SocketException e)         { e.printStackTrace(); } 
33         catch (UnknownHostException e)  { e.printStackTrace(); } 
34     }
35 
36     @Override
37     public void runCyclic() {
38         // your task execution starts here
39          JointPosition jPos = robot.getCurrentJointPosition();  
40         posArr = jPos.toString().getBytes();  
41         packet = new DatagramPacket(posArr, posArr.length, IP, port); 
42         try {
43             datagramSocket.send(packet);
44         } 
45         catch (IOException e)
46         {  e.printStackTrace(); } 
47     }
48 }
View Code

   虚拟模型关节设为Passive模式,VREP中的Lua代码获取KUKA iiwa各轴位置并设置关节角:

 1 function sysCall_init()
 2     -- do some initialization here:
 3     local socket = require 'socket'
 4     local host = "192.168.1.50"
 5     local port = 30003
 6     udp = assert(socket.udp())
 7     udp:settimeout(-1)
 8     assert(udp:setsockname(host, port))
 9     
10     -- Get joint handles
11     jointHandles = {-1,-1,-1,-1,-1,-1,-1}
12     for i=1,7,1 do
13         jointHandles[i] = sim.getObjectHandle('LBR_iiwa_7_R800_joint'..i)
14     end
15 end
16 
17 
18 function sysCall_actuation()
19     local data,receip,receport = udp:receivefrom()
20     if data then
21         --print("udp:receivefrom: " .. data .. receip, receport)
22         local jointPositions = getJointPositions(data)
23         for i=1,7,1 do
24             sim.setJointPosition(jointHandles[i], jointPositions[i])
25         end
26     end
27 end
28 
29 
30 function getJointPositions(s)
31     local result = {}
32     local s = string.sub(s, 2, -2) -- Return a substring of the string
33     for match in string.gmatch(s, "[^,]+") do
34         table.insert(result, tonumber(match))  
35     end
36     return result
37 end

  手动拖拽实际的机械臂,VREP中的虚拟模型按照轴的反馈位置实时更新:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考:

Tutorial:Networking with UDP

你可能感兴趣的:(KUKA iiwa — VREP显示机械臂位置)