TeachMode.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 
8 import com.kuka.roboticsAPI.deviceModel.LBR;
9 import com.kuka.roboticsAPI.executionModel.CommandInvalidException;
10 import com.kuka.roboticsAPI.geometricModel.Tool;
11 import com.kuka.roboticsAPI.motionModel.HandGuidingMotion;
12 import com.kuka.roboticsAPI.motionModel.IMotionContainer;
13 import com.kuka.roboticsAPI.motionModel.controlModeModel.JointImpedanceControlMode;
14 import com.kuka.task.ITaskLogger;
15 
16 /**
17  *
18  * In TeachModeTest you will need to set
19  * boolean useTeachModeObject = false;
20  *
21  * This object is supposed to make it easy to enable/disable teach
22  * mode with a single call to start() and stop() when you want to
23  * enable and disable teach mode.
24  *
25  * @todo the method used to simulate teach mode is a little jumpy, needs to be improved.
26  *
27  * @author Andrew Hundt
28  *
29  */
30 public class TeachMode implements Runnable {
31 
32  private LBR lbr;
33 
34  IMotionContainer _handGuidingMotionContainer = null;
35  HandGuidingMotion _handGuidingMotion;
36  private double[] _maxAllowedJointLimits;
37  private double[] _minAllowedJointLimits;
38  Tool _flangeAttachment;
39  JointImpedanceControlMode _teachControlMode;
40  private static volatile boolean useHandGuidingMotion;
41  private static volatile boolean isEnableEnded;
42  private static volatile boolean stop = false;
43  ITaskLogger _logger = null;
44  int iter = 0;
45 
46  /**
47  *
48  * @param flangeAttachment
49  * @param maxAllowedJoints
50  * @param minAllowedJoints
51  */
52  public TeachMode(Tool flangeAttachment, double[] maxAllowedJoints, double[] minAllowedJoints) {
53  _handGuidingMotion = handGuiding();
54  _maxAllowedJointLimits = maxAllowedJoints;
55  _minAllowedJointLimits = minAllowedJoints;
56  _flangeAttachment = flangeAttachment;
57  useHandGuidingMotion = false;
58  }
59 
60  public void setLogger(ITaskLogger logger) {
61  _logger = logger;
62  }
63 
64  /**
65  *
66  * @param teachModeControlMode
67  */
68  public void setTeachModeControlMode(JointImpedanceControlMode teachModeControlMode) {
69  _teachControlMode = teachModeControlMode;
70  }
71 
72  public void enable() {
73  synchronized (this) {
74 
75  useHandGuidingMotion = true;
76  }
77  }
78 
79  /**
80  * Tell the hand guiding motion (teach mode) to stop
81  * and the thread exits. Make sure
82  * isEnableEnded returns true before calling this!
83  * @return false on failure; true on successfully starting the shutdown process
84  */
85  public synchronized boolean stop(){
86  if(!isEnableEnded) return false;
87  stop = true;
88  return true;
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 (_handGuidingMotionContainer != null) {
103 // _handGuidingMotionContainer.cancel();
104 // _handGuidingMotionContainer = null;
105 // return true;
106 // }
107  }
108  return true;
109  //warn("Cancel complete");
110  }
111 
112  /**
113  * @brief true there are no outstanding calls made to enable() and you can switch modes
114  *
115  * @see enable()
116  *
117  * The sunrise HandGuidingMotion mode has an API
118  * quirk where you need to push the physical button
119  * to end the hand guiding mode. This tells you if
120  * the motion is both over and the button has been
121  * pushed so the action is really truly complete.
122  * @return true if you can switch modes now, false if someone still needs to press the physical button
123  */
124  public synchronized boolean isEnableEnded(){
125  return isEnableEnded;
126  }
127 
128  /**
129  * This only prints if the logger has been set!
130  * @param str
131  */
132  private void warn(String str) {
133  if (_logger != null) {
134  _logger.warn(str);
135  }
136  }
137 
138  //@Override
139  public void run() {
140 
141  // trying to use kuka's provided handguidingmotion but it isn't working now.
142  // using an if statement to default to old behavior.
143  synchronized(this) {
144  useHandGuidingMotion = false;
145  }
146  _handGuidingMotionContainer = null;
147 
148  while(!stop) {
149  //warn("Starting new hand guiding motion");
150 
151  try {
152 
153  // see kuka documentation 1.9 for details
154  synchronized(this) {
155 
156  if (!useHandGuidingMotion) {
157  //warn("breaking hand guiding motion");
158  try {
159  if(!isEnableEnded && _logger!=null)
160  {
161  _logger.info("Teach Mode and HandGuidingMotion Complete!");
162  }
163  isEnableEnded = true;
164  this.wait(100);
165  } catch (InterruptedException e) {
166  warn("Interruption in TeachMode: " + e.toString());
167  }
168  continue;
169  }
170 
171  warn("creating hand guiding motion " + useHandGuidingMotion);
172  isEnableEnded = false;
173  _handGuidingMotion = handGuiding()
174  .setAxisLimitsMax(_maxAllowedJointLimits)
175  .setAxisLimitsMin(_minAllowedJointLimits)
176  .setAxisLimitsEnabled(true, true, true, true, true, true, true)
177  .setAxisLimitViolationFreezesAll(false).setPermanentPullOnViolationAtStart(true);
178  isEnableEnded = false;
179  }
180  //warn("hand guiding motion moving... " + useHandGuidingMotion);
181  //if (_handGuidingMotionContainer == null || _handGuidingMotionContainer.isFinished()) {
182  _handGuidingMotionContainer = _flangeAttachment.move(_handGuidingMotion);
183  //}
184  //warn("done hand guiding");
185 
186  } catch (CommandInvalidException e) {
187  _handGuidingMotionContainer = null;
188  warn(e.toString());
189  } catch (IllegalStateException e) {
190  _handGuidingMotionContainer = null;
191  warn(e.toString());
192  }
193  }
194 
195  warn("Teach Mode Thread Exiting");
196  }
197 
198 }
void setLogger(ITaskLogger logger)
Definition: TeachMode.java:60
synchronized boolean isEnableEnded()
true there are no outstanding calls made to enable() and you can switch modes
Definition: TeachMode.java:124
boolean cancel()
Definition: TeachMode.java:95
TeachMode(Tool flangeAttachment, double[] maxAllowedJoints, double[] minAllowedJoints)
Definition: TeachMode.java:52
synchronized boolean stop()
Definition: TeachMode.java:85
void enable()
Definition: TeachMode.java:72
void setTeachModeControlMode(JointImpedanceControlMode teachModeControlMode)
Definition: TeachMode.java:68