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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM