ZMQ_SmartServoFRI.java
Go to the documentation of this file.
1 package grl.driver;
2 
3 import static com.kuka.roboticsAPI.motionModel.BasicMotions.ptp;
4 
5 import java.nio.ByteBuffer;
6 
7 import org.zeromq.ZMQ;
8 
9 import com.kuka.connectivity.fastRobotInterface.FRIConfiguration;
10 import com.kuka.connectivity.fastRobotInterface.FRISession;
11 import com.kuka.connectivity.motionModel.smartServo.ISmartServoRuntime;
12 import com.kuka.connectivity.motionModel.smartServo.SmartServo;
13 import com.kuka.connectivity.userInterface.smartServo.ServoMotionUtilities;
14 import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication;
15 import com.kuka.roboticsAPI.controllerModel.Controller;
16 import com.kuka.roboticsAPI.deviceModel.JointPosition;
17 import com.kuka.roboticsAPI.deviceModel.LBR;
18 import com.kuka.roboticsAPI.geometricModel.PhysicalObject;
19 
21 
22 /**
23  * Creates a FRI Session.
24  */
25 public class ZMQ_SmartServoFRI extends RoboticsAPIApplication
26 {
27  private Controller _lbrController;
28  private LBR _lbr;
29  private String _hostName;
30  private String _controllingLaptopIPAddress;
31  private PhysicalObject _toolAttachedToLBR;
32  private ISmartServoRuntime theSmartServoRuntime = null;
33 
34  @Override
35  public void initialize()
36  {
37  _lbrController = (Controller) getContext().getControllers().toArray()[0];
38  _lbr = (LBR) _lbrController.getDevices().toArray()[0];
39  // **********************************************************************
40  // *** change next line to the FRIClient's IP address ***
41  // **********************************************************************
42  _hostName = "192.170.10.100";
43  // **********************************************************************
44  // *** change next line to the KUKA address and Port Number ***
45  // **********************************************************************
46  _controllingLaptopIPAddress = "tcp://172.31.1.100:30010";
47 
48 
49  // FIXME: Set proper Weights or use the plugin feature
50  double[] translationOfTool =
51  { 0, 0, 0 };
52  double mass = 0;
53  double[] centerOfMassInMillimeter =
54  { 0, 0, 0 };
55  _toolAttachedToLBR = ServoMotionUtilities.createTool(_lbr,
56  "SimpleJointMotionSampleTool", translationOfTool, mass,
57  centerOfMassInMillimeter);
58  }
59 
60  @Override
61  public void run()
62  {
63  // Configure and start FRI session
64  FRIConfiguration friConfiguration = FRIConfiguration.createRemoteConfiguration(_lbr, _hostName);
65  friConfiguration.setSendPeriodMilliSec(4);
66  FRISession friSession = new FRISession(friConfiguration);
67 
68  // TODO: remove default start pose
69  // move do default start pose
70  _toolAttachedToLBR.move(ptp(Math.toRadians(10), Math.toRadians(10), Math.toRadians(10), Math.toRadians(-90), Math.toRadians(10), Math.toRadians(10),Math.toRadians(10)));
71 
72 
73  // Prepare ZeroMQ context and dealer
74  ZMQ.Context context = ZMQ.context(1);
75  ZMQ.Socket subscriber = context.socket(ZMQ.DEALER);
76  subscriber.connect(_controllingLaptopIPAddress);
77  subscriber.setRcvHWM(1000000);
78 
79 
80 
81  JointPosition initialPosition = new JointPosition(
82  _lbr.getCurrentJointPosition());
83  SmartServo aSmartServoMotion = new SmartServo(initialPosition);
84 
85  // Set the motion properties to 20% of systems abilities
86  aSmartServoMotion.setJointAccelerationRel(0.2);
87  aSmartServoMotion.setJointVelocityRel(0.2);
88 
89  aSmartServoMotion.setMinimumTrajectoryExecutionTime(20e-3);
90 
91  _toolAttachedToLBR.getDefaultMotionFrame().moveAsync(aSmartServoMotion);
92 
93  // Fetch the Runtime of the Motion part
94  theSmartServoRuntime = aSmartServoMotion.getRuntime();
95 
96  // create an JointPosition Instance, to play with
97  JointPosition destination = new JointPosition(
98  _lbr.getJointCount());
99 
100 
101  byte [] data = subscriber.recv();
102  ByteBuffer bb = ByteBuffer.wrap(data);
103 
104  JointState jointState = JointState.getRootAsJointState(bb);
105 
106 
107  // move to start pose
108  //_lbr.move(ptp(jointState.position(0), jointState.position(1), jointState.position(2), jointState.position(3), jointState.position(4), jointState.position(5), jointState.position(6)));
109  //.setJointAccelerationRel(acceleration));
110 
111 
112 
113  // Receive Flat Buffer and Move to Position
114  // TODO: make into while loop and add exception to exit loop
115  // TODO: add a message that we send to the driver with data log strings
116  // TODO: add a message we receive (and send) that tells the other application to stop running
117  for (int i=0; i<100000; i++) {
118  // TODO: IMPORTANT: this recv call must be made asynchronous
119  data = subscriber.recv();
120  bb = ByteBuffer.wrap(data);
121 
122  jointState = JointState.getRootAsJointState(bb);
123 
124  for (int k = 0; k < destination.getAxisCount(); ++k)
125  {
126  destination.set(k, jointState.position(k));
127  }
128 
129  theSmartServoRuntime.setDestination(destination);
130  // TODO: test to make sure the accelerations are present
131  //double[] acceleration = {jointState.acceleration(0),jointState.acceleration(1),jointState.acceleration(2),jointState.acceleration(3),jointState.acceleration(4),jointState.acceleration(5),jointState.acceleration(6)};
132 
133  // TODO: Test move command
134  //_lbr.moveAsync(ptp(jointState.position(0), jointState.position(1), jointState.position(2), jointState.position(3), jointState.position(4), jointState.position(5), jointState.position(6)));
135  //.setJointAccelerationRel(acceleration));
136  }
137 
138  // done
139  subscriber.close();
140  context.term();
141  System.exit(1);
142  friSession.close();
143  }
144 
145  /**
146  * main.
147  *
148  * @param args
149  * args
150  */
151  public static void main(final String[] args)
152  {
153  final ZMQ_SmartServoFRI app = new ZMQ_SmartServoFRI();
154  app.runApplication();
155  }
156 
157 }
static void main(final String[] args)