BowlerKernel
ParallelGroup.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.kinematics.parallel;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.function.Consumer;
6 
7 import com.neuronrobotics.sdk.addons.kinematics.AbstractLink;
8 import com.neuronrobotics.sdk.addons.kinematics.DHParameterKinematics;
9 import com.neuronrobotics.sdk.addons.kinematics.LinkConfiguration;
10 import com.neuronrobotics.sdk.addons.kinematics.LinkFactory;
11 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
12 
13 
14 public class ParallelGroup extends DHParameterKinematics {
15 
16  private ArrayList<DHParameterKinematics> constituantLimbs = new ArrayList<DHParameterKinematics>();
17  private HashMap<DHParameterKinematics, TransformNR> tipOffset = new HashMap<DHParameterKinematics, TransformNR>();
18  private HashMap<DHParameterKinematics, String> tipOffsetRelativeToName = new HashMap<>();
19  private HashMap<DHParameterKinematics, Integer> tipOffsetRelativeIndex = new HashMap<>();
21  private String[] toolEngine = new String[] { "https://gist.github.com/33f2c10ab3adc5bd91f0a58ea7f24d14.git",
22  "parallelTool.groovy" };
23  private String name;
24 
26  String name = control.getScriptingName();
28  String refName = tipOffsetRelativeToName.get(s);
29  if(refName.contentEquals(name)) {
30  if(index==tipOffsetRelativeIndex.get(s)) {
31  return getTipOffset(s);
32  }
33  }
34  }
35  return null;
36  }
37 
38  public ParallelGroup(String name) {
39  this.name = name;
40  if (name==null)
41  throw new RuntimeException();
42  }
43 
44  public void addLimb(DHParameterKinematics limb, TransformNR tip, String name, int index) {
45 
46  setupReferencedLimb(limb, tip, name, index);
47 
48  }
49 
52  if (getTipOffset(d) == null) {
53  return d;// this is the first limb with no relative tip
54  }
55  }
56  // this should be impossible
57  throw new RuntimeException("FK lim must be possible, one limb must not have a reference to another");
58  }
59 
65  @Override
66  public TransformNR calcHome() {
67  return getFKLimb().calcHome();
68  }
69 
70  public void setupReferencedLimb(DHParameterKinematics limb, TransformNR tip, String name, int index) {
72  if (d.getScriptingName().contentEquals(name)) {
73  setupReferencedLimbStartup(limb, tip, name, index);
74  return;
75  }
76  }
77  throw new RuntimeException("Limb named: " + name + " does not exist");
78  }
79 
80  public void setupReferencedLimbStartup(DHParameterKinematics limb, TransformNR tip, String name, int index) {
81  if (!getConstituantLimbs().contains(limb)) {
82  getConstituantLimbs().add(limb);
84 
85  getFactory().addLink(limb.getFactory().getLink(c));// adding the configurations the the single
86  // factory
87  }
88  }
89  if (tip != null) {
90  tipOffsetRelativeToName.put(limb, name);
91  tipOffsetRelativeIndex.put(limb, index);
92  getTipOffset().put(limb, tip);
93  System.out.println("Limb "+limb.getScriptingName()+" set relative to "+name);
94  } else {
95  clearReferencedLimb(limb);
97 // fk.addPoseUpdateListener(new ITaskSpaceUpdateListenerNR() {
98 // @Override
99 // public void onTaskSpaceUpdate(AbstractKinematicsNR source, TransformNR pose) {
100 //
101 // }
102 //
103 // @Override
104 // public void onTargetTaskSpaceUpdate(AbstractKinematicsNR source, TransformNR pose) {
105 // HashMap<String, double[]> IKvalues = new HashMap<>();
106 // for (DHParameterKinematics d : getConstituantLimbs()) {
107 // if (getTipOffset(d) != null) {
108 // try {
109 // //System.out.println("Setting Kinematics for follower "+d.getScriptingName());
110 // double[] jointSpaceVect = compute(d, IKvalues, pose);
111 // //System.out.println(fk.getScriptingName()+" is Setting sublimb target "+d.getScriptingName());
112 // d.throwExceptionOnJointLimit(false);
113 // d.setDesiredJointSpaceVector(jointSpaceVect, 0);
114 // } catch (Exception e) {
115 // // TODO Auto-generated catch block
116 // e.printStackTrace();
117 // }
118 // }
119 // }
120 // IKvalues.clear();
121 // }
122 // });
123  }
124  }
125 
127  tipOffsetRelativeToName.remove(limb);
128  tipOffsetRelativeIndex.remove(limb);
129  getTipOffset().remove(limb);
130  }
131 
132  @Override
133  public void disconnectDevice() {
134  // TODO Auto-generated method stub
136  l.disconnect();
137  }
138  close();
139 
140  }
141 
142  @Override
143  public boolean connectDevice() {
144  // TODO Auto-generated method stub
145  return true;
146  }
147 
148  private double[] compute(DHParameterKinematics ldh, HashMap<String, double[]> IKvalues,
149  TransformNR taskSpaceTransform) throws Exception {
150  String scriptingName = ldh.getScriptingName();
151  if (IKvalues.get(scriptingName) == null) {
152  //System.out.println("Perform IK "+ldh.getScriptingName());
153  if (getTipOffset().get(ldh) == null) {
154  // no offset, compute as normal
155  double[] jointSpaceVect = ldh.inverseKinematics(ldh.inverseOffset(taskSpaceTransform));
156  IKvalues.put(scriptingName, jointSpaceVect);
157  } else {
158  TransformNR offset = getTipOffset().get(ldh);
159  String refLimbName = tipOffsetRelativeToName.get(ldh);
160  int index = tipOffsetRelativeIndex.get(ldh);
161  DHParameterKinematics referencedLimb = findReferencedLimb(refLimbName);
162  if (referencedLimb == null)
163  throw new RuntimeException("Referenced limb missing, IK for " + ldh.getScriptingName()
164  + " Failed looking for " + refLimbName);
165  double[] jointSpaceVectReferenced = compute(referencedLimb, IKvalues, taskSpaceTransform);
166 
167  TransformNR transformTOLinksTip = referencedLimb.getChain().getChain(jointSpaceVectReferenced).get(index)
168  .times(offset.inverse());
169  double[] jointSpaceVect = ldh.inverseKinematics(ldh.inverseOffset(transformTOLinksTip));
170  IKvalues.put(scriptingName, jointSpaceVect);
171  }
172  }
173  return IKvalues.get(scriptingName);
174  }
175 
176  private DHParameterKinematics findReferencedLimb(String refLimbName) {
177  DHParameterKinematics referencedLimb = null;
179  if (lm.getScriptingName().toLowerCase().contentEquals(refLimbName.toLowerCase())) {
180  // FOund the referenced limb
181  referencedLimb = lm;
182  }else {
183  //System.out.println("Searching for "+refLimbName+" no match with "+lm.getScriptingName());
184  }
185  }
186  return referencedLimb;
187  }
188 
194  @Override
197  super.setCurrentPoseTarget(currentPoseTarget);
198  //System.out.println("Paralell set to " + currentPoseTarget);
199  }
200  }
202  // TODO Auto-generated method stub
203  return null;
204  }
205  @Override
206  public double[] inverseKinematics(TransformNR taskSpaceTransform) throws Exception {
207 
208  int numBerOfLinks = 0;
210  numBerOfLinks += l.getNumberOfLinks();
211  }
212  double[] linkValues = new double[numBerOfLinks];
213  int limbOffset = 0;
214  HashMap<String, double[]> IKvalues = new HashMap<>();
215 
217  // Use the built in IK model for the limb
218  double[] jointSpaceVect = compute(l, IKvalues, taskSpaceTransform);
219  // Load the link vector into the total vector
220  for (int i = 0; i < jointSpaceVect.length; i++) {
221  linkValues[limbOffset + i] = jointSpaceVect[i];
222  }
223  limbOffset += jointSpaceVect.length;
224  }
225  IKvalues.clear();
226  return linkValues;
227  }
228  public void printError(TransformNR taskSpaceTransform) throws Exception {
229  printError(taskSpaceTransform,t -> {
230  System.out.println(t);
231  });
232  }
233  public void printError(TransformNR taskSpaceTransform, Consumer<String> printer) throws Exception {
234  int numBerOfLinks = 0;
236  numBerOfLinks += l.getNumberOfLinks();
237  }
238  double[] linkValues = new double[numBerOfLinks];
239  int limbOffset = 0;
240  HashMap<String, double[]> IKvalues = new HashMap<>();
241 
243  // Use the built in IK model for the limb
244  double[] jointSpaceVect = compute(l, IKvalues, taskSpaceTransform);
245  // Load the link vector into the total vector
246  for (int i = 0; i < jointSpaceVect.length; i++) {
247  linkValues[limbOffset + i] = jointSpaceVect[i];
248  AbstractLink link = l.getFactory().getLink(l.getLinkConfiguration(i));
249  double val = link.toLinkUnits(jointSpaceVect[i]);
250  Double double1 = new Double(val);
251  if (double1.isNaN() || double1.isInfinite()) {
252  printer.accept("Fault on link " + i + " attempted to set " + double1);
253  }
254  if (val > link.getUpperLimit()) {
255  printer.accept("Fault on link " + i + " attempted to set " + jointSpaceVect[i]);
256  }
257  if (val < link.getLowerLimit()) {
258  printer.accept("Fault on link " + i + " attempted to set " + jointSpaceVect[i]);
259  }
260  }
261  limbOffset += jointSpaceVect.length;
262  }
263  IKvalues.clear();
264  }
265 
266  @Override
267  public TransformNR forwardKinematics(double[] jointSpaceVector) {
269  if(l==getFKLimb())
270  return l.getCurrentTaskSpaceTransform();
271 
272  }
273  throw new RuntimeException( "FK limb is missing!");
274  }
275 
281  public String[] getGitCadToolEngine() {
282  return toolEngine;
283  }
284 
290  public void setGitCadToolEngine(String[] cadEngine) {
291  if (cadEngine != null && cadEngine[0] != null && cadEngine[1] != null)
292  this.toolEngine = cadEngine;
293  }
294 
295  public ArrayList<DHParameterKinematics> getConstituantLimbs() {
296  return constituantLimbs;
297  }
298 
299 // public void setConstituantLimbs(ArrayList<DHParameterKinematics> constituantLimbs) {
300 // this.constituantLimbs = constituantLimbs;
301 // }
302 
303  public HashMap<DHParameterKinematics, TransformNR> getTipOffset() {
304  return tipOffset;
305  }
306 
308  return tipOffset.get(l);
309  }
310 
312  tipOffset.put(l, n);
313  }
314 
316  return tipOffsetRelativeToName.get(l);
317  }
318 
320  return tipOffsetRelativeIndex.get(l);
321  }
322 
323  public void setTipOffset(HashMap<DHParameterKinematics, TransformNR> tipOffset) {
324  this.tipOffset = tipOffset;
325  }
326 
327  public void removeLimb(DHParameterKinematics limb) {
328  if (constituantLimbs.contains(limb)) {
329  clearReferencedLimb( limb);
330  constituantLimbs.remove(limb);
331  setFactory(new LinkFactory());// clear the links
332  for (DHParameterKinematics remaining : constituantLimbs) {
333  for (LinkConfiguration c : remaining.getFactory().getLinkConfigurations()) {
334  getFactory().addLink(remaining.getFactory().getLink(c));// adding the configurations the the single
335  // factory
336  }
337  }
338  }
339  }
340 
341  public String getNameOfParallelGroup() {
342  return name;
343  }
344 
345  public void close() {
346  constituantLimbs.clear();
347  tipOffset.clear();
348  constituantLimbs.clear();
349  tipOffsetRelativeToName.clear();
350 
351  }
352 
353 
354 
355 }
static boolean checkTaskSpaceTransform(AbstractKinematicsNR dev, TransformNR taskSpaceTransform, double seconds)
ArrayList< TransformNR > getChain(double[] jointSpaceVector)
Definition: DHChain.java:262
ArrayList< LinkConfiguration > getLinkConfigurations()
HashMap< DHParameterKinematics, TransformNR > getTipOffset()
HashMap< DHParameterKinematics, TransformNR > tipOffset
DHParameterKinematics findReferencedLimb(String refLimbName)
HashMap< DHParameterKinematics, Integer > tipOffsetRelativeIndex
void printError(TransformNR taskSpaceTransform, Consumer< String > printer)
void setTipOffset(DHParameterKinematics l, TransformNR n)
double[] inverseKinematics(TransformNR taskSpaceTransform)
void setTipOffset(HashMap< DHParameterKinematics, TransformNR > tipOffset)
TransformNR getTipOffsetFromThisLinkInLimb(DHParameterKinematics control, int index)
void setupReferencedLimb(DHParameterKinematics limb, TransformNR tip, String name, int index)
void addLimb(DHParameterKinematics limb, TransformNR tip, String name, int index)
HashMap< DHParameterKinematics, String > tipOffsetRelativeToName
double[] compute(DHParameterKinematics ldh, HashMap< String, double[]> IKvalues, TransformNR taskSpaceTransform)
void setupReferencedLimbStartup(DHParameterKinematics limb, TransformNR tip, String name, int index)