FRIMode.java
Go to the documentation of this file.
1 package grl;
2 
3 import static com.kuka.roboticsAPI.motionModel.BasicMotions.positionHold;
4 import static com.kuka.roboticsAPI.motionModel.MMCMotions.handGuiding;
5 
6 import java.util.concurrent.TimeUnit;
7 import java.util.concurrent.TimeoutException;
8 
9 import com.kuka.connectivity.fastRobotInterface.FRIConfiguration;
10 import com.kuka.connectivity.fastRobotInterface.FRIJointOverlay;
11 import com.kuka.connectivity.fastRobotInterface.FRISession;
12 import com.kuka.connectivity.fastRobotInterface.FRIChannelInformation.FRISessionState;
13 import com.kuka.roboticsAPI.deviceModel.LBR;
14 import com.kuka.roboticsAPI.executionModel.CommandInvalidException;
15 import com.kuka.roboticsAPI.geometricModel.Tool;
16 import com.kuka.roboticsAPI.motionModel.HandGuidingMotion;
17 import com.kuka.roboticsAPI.motionModel.IMotionContainer;
18 import com.kuka.roboticsAPI.motionModel.controlModeModel.AbstractMotionControlMode;
19 import com.kuka.roboticsAPI.motionModel.controlModeModel.JointImpedanceControlMode;
20 import com.kuka.task.ITaskLogger;
21 
22 /**
23  *
24  * Activate FRI mode in a separate thread
25  *
26  */
27 public class FRIMode implements Runnable {
28 
29  private LBR _lbr;
30 
31 
32 
33  private AbstractMotionControlMode _activeMotionControlMode;
34  private FRISession _friSession = null;
35  private FRIJointOverlay _motionOverlay = null;
36  private static volatile boolean useHandGuidingMotion;
37  private static volatile boolean isEnableEnded;
38  private static volatile boolean stop = false;
39 
40  IMotionContainer currentMotion = null;
41  ITaskLogger _logger = null;
42  int iter = 0;
43 
44  /**
45  *
46  * @param flangeAttachment
47  * @param maxAllowedJoints
48  * @param minAllowedJoints
49  */
50  public FRIMode(LBR lbr, FRISession friSession) {
51  _lbr = lbr;
52  _friSession = friSession;
53  useHandGuidingMotion = false;
54  }
55 
56  public void setLogger(ITaskLogger logger) {
57  _logger = logger;
58  }
59 
60  /**
61  *
62  * @param setControlMode
63  */
64  public void setControlMode(AbstractMotionControlMode teachModeControlMode) {
65  _activeMotionControlMode = teachModeControlMode;
66  }
67 
68  public void enable() {
69  synchronized (this) {
70 
71  useHandGuidingMotion = true;
72  }
73  }
74 
75  /**
76  * Tell the hand guiding motion (teach mode) to stop
77  * and the thread exits. Make sure
78  * isEnableEnded returns true before calling this!
79  * @return false on failure; true on successfully starting the shutdown process
80  */
81  public synchronized boolean stop(){
82  if(!isEnableEnded) return false;
83  stop = true;
84 
85  if(currentMotion !=null) currentMotion.cancel();
86 
87  return true;
88  }
89 
90 
91  /**
92  *
93  * Cancel the motion, thread stays in existence.
94  */
95  public boolean cancel(){
96  //warn("Cancel received");
97 
98  synchronized (this) {
99 
100  useHandGuidingMotion = false;
101 
102  if(currentMotion !=null) currentMotion.cancel();
103  }
104  return true;
105  //warn("Cancel complete");
106  }
107 
108  /**
109  * @brief true there are no outstanding calls made to enable() and you can switch modes
110  *
111  * @see enable()
112  *
113  * The sunrise HandGuidingMotion mode has an API
114  * quirk where you need to push the physical button
115  * to end the hand guiding mode. This tells you if
116  * the motion is both over and the button has been
117  * pushed so the action is really truly complete.
118  * @return true if you can switch modes now, false if someone still needs to press the physical button
119  */
120  public synchronized boolean isEnableEnded(){
121  return isEnableEnded;
122  }
123 
124  /**
125  * This only prints if the logger has been set!
126  * @param str
127  */
128  private void warn(String str) {
129  if (_logger != null) {
130  _logger.warn(str);
131  }
132  }
133 
134  //@Override
135  public void run() {
136 
137  // trying to use kuka's provided handguidingmotion but it isn't working now.
138  // using an if statement to default to old behavior.
139  synchronized(this) {
140  useHandGuidingMotion = false;
141  }
142  _motionOverlay = null;
143 
144  while(!stop) {
145  //warn("Starting new hand guiding motion");
146 
147  try {
148 
149  // see kuka documentation 1.9 for details
150  synchronized(this) {
151 
152  if (!useHandGuidingMotion) {
153  //warn("breaking hand guiding motion");
154  try {
155  if(!isEnableEnded && _logger!=null)
156  {
157  _logger.info("FRI Joint Overlay Complete!");
158  }
159  isEnableEnded = true;
160  this.wait(100);
161  } catch (InterruptedException e) {
162  warn("Interruption in FRIMode: " + e.toString());
163  }
164  continue;
165  }
166 
167  warn("creating FRI Joint Overlay " + useHandGuidingMotion);
168  isEnableEnded = false;
169  _motionOverlay = new FRIJointOverlay(_friSession);
170 // _handGuidingMotion = handGuiding()
171 // .setAxisLimitsMax(_maxAllowedJointLimits)
172 // .setAxisLimitsMin(_minAllowedJointLimits)
173 // .setAxisLimitsEnabled(true, true, true, true, true, true, true)
174 // .setAxisLimitViolationFreezesAll(false).setPermanentPullOnViolationAtStart(true);
175  isEnableEnded = false;
176  }
177  //warn("hand guiding motion moving... " + useHandGuidingMotion);
178  //if (_handGuidingMotionContainer == null || _handGuidingMotionContainer.isFinished()) {
179  if( _friSession.getFRIChannelInformation().getFRISessionState().compareTo(FRISessionState.COMMANDING_ACTIVE) != 0
180  && _friSession.getFRIChannelInformation().getFRISessionState().compareTo(FRISessionState.COMMANDING_WAIT) != 0)
181  {
182  _logger.info("FRI Joint Overlay starting...");
183 
184  try {
185  _friSession.await(10, TimeUnit.SECONDS);
186 
187  currentMotion = _lbr.move(positionHold(_activeMotionControlMode, -1, TimeUnit.SECONDS).addMotionOverlay(_motionOverlay));
188 
189  } catch (TimeoutException e) {
190  _logger.error("FRISession timed out, closing...");
191  e.printStackTrace();
192  _friSession.close();
193  return;
194  }
195  }
196 
197  //}
198  //warn("done hand guiding");
199 
200  } catch (CommandInvalidException e) {
201  currentMotion = null;
202  //_handGuidingMotionContainer = null;
203  warn(e.toString());
204  } catch (IllegalStateException e) {
205  currentMotion = null;
206  //_handGuidingMotionContainer = null;
207  warn(e.toString());
208  }
209  }
210 
211  warn("Teach Mode Thread Exiting");
212  }
213 
214 }
void setLogger(ITaskLogger logger)
Definition: FRIMode.java:56
void setControlMode(AbstractMotionControlMode teachModeControlMode)
Definition: FRIMode.java:64
FRIMode(LBR lbr, FRISession friSession)
Definition: FRIMode.java:50
boolean cancel()
Definition: FRIMode.java:95
synchronized boolean isEnableEnded()
true there are no outstanding calls made to enable() and you can switch modes
Definition: FRIMode.java:120
synchronized boolean stop()
Definition: FRIMode.java:81
void run()
Definition: FRIMode.java:135
void enable()
Definition: FRIMode.java:68