BowlerKernel
SearchTreeSolver.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.kinematics;
2 
3 import java.util.ArrayList;
4 
5 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
6 
7 // TODO: Auto-generated Javadoc
11 public class SearchTreeSolver implements DhInverseSolver {
12 
14  private DHChain dhChain;
15 
17  private double [] upper;
18 
20  private double [] lower;
21 
23  private boolean debug;
24 
26  double startingIncrement = 1.5;//degrees
27 
30 
38  this.setDhChain(dhChain);
39  this.debug = debug;
42  }
43 
44  /* (non-Javadoc)
45  * @see com.neuronrobotics.sdk.addons.kinematics.DhInverseSolver#inverseKinematics(com.neuronrobotics.sdk.addons.kinematics.math.TransformNR, double[], com.neuronrobotics.sdk.addons.kinematics.DHChain)
46  */
47  @Override
48  public double[] inverseKinematics(TransformNR target,double[] jointSpaceVector,
49  DHChain chain ) {
50  ArrayList<DHLink> links = chain.getLinks();
52  searchTree step=new searchTree(jointSpaceVector,startingIncrement);;
53  boolean done = false;
54  configuration conf = new configuration(jointSpaceVector, target);
55 // double previousV =conf.getOffsetOrentationMagnitude();
56 // double previousO =conf.getOffsetVectorMagnitude();
57  int iter = 1000;
58  int i = 0;
59  do{
60  double [] current = conf.getJoints();
61  conf = step.getBest(current);
62 
63  double vect = conf.getOffsetOrentationMagnitude();
64  double orent = conf.getOffsetVectorMagnitude();
65 
66  if(vect<10 && orent< .05){
67  done = true;
68  System.out.println("SearchTreeSolver Success stats: \n\tIterations = "+i+" out of "+iter+"\n"+conf);
69  }
70  if(i++==iter){
71  done = true;
72  System.err.println("SearchTreeSolver FAILED stats: \n\tIterations = "+i+" out of "+iter+"\n"+conf);
73  }
74  }while(! done);
75 
76  return conf.getJoints();
77  }
78 
85  return target;
86  }
87 
93  public void setTarget(TransformNR target) {
94  this.target = target;
95  }
96 
102  public DHChain getDhChain() {
103  return dhChain;
104  }
105 
111  public void setDhChain(DHChain dhChain) {
112  this.dhChain = dhChain;
113  }
114 
121  public TransformNR fk(double[] jointSpaceVector){
122  return getDhChain().forwardKinematics(jointSpaceVector);
123  }
124 
128  private class searchTree{
129 
131  //double[] start;
132  searchNode [] nodes;
133 
140  public searchTree(double[] jointSpaceVector,double startingIncrement){
141  nodes = new searchNode [jointSpaceVector.length];
142  for(int i=0;i<jointSpaceVector.length;i++){
143  nodes[i] = new searchNode(i,jointSpaceVector[i],startingIncrement);
144  }
145 
146  }
147 
154  public configuration getBest(double[] jointSpaceVector){
155  ArrayList<configuration> configurations = new ArrayList<configuration> ();
156  for(int i=0;i<jointSpaceVector.length;i++){
157  nodes[i].setCurrent(jointSpaceVector[i]);
158  }
159  double [] tmp = new double[6];
160  int num = 3;
161  for(int i=0;i<num;i++){
162  try{
163  tmp[0]=nodes[0].get(i);
164  for(int i1=0;i1<num;i1++){
165  try{
166  tmp[1]=nodes[1].get(i1);
167  for(int i2=0;i2<num;i2++){
168  try{
169  tmp[2]=nodes[2].get(i2);
170  for(int i3=0;i3<num;i3++){
171  try{
172  tmp[3]=nodes[3].get(i3);
173  for(int i4=0;i4<num;i4++){
174  try{
175  tmp[4]=nodes[4].get(i4);
176  for(int i5=0;i5<num;i5++){
177  try{
178  tmp[5]=nodes[5].get(i5);
179  boolean same = false;
180  configuration tempConf = new configuration(tmp.clone(),getTarget());
181  for(configuration c:configurations){
182  if(tempConf.same(c))
183  same = true;
184  }
185  if(!same)
186  configurations.add(tempConf);
187  }catch(Exception ex){}
188  }
189  }catch(Exception ex){}
190  }
191  }catch(Exception ex){}
192  }
193  }catch(Exception ex){}
194  }
195  }catch(Exception ex){}
196  }
197  }catch(Exception ex){}
198  }
199 
200  int best = 0;
201  int i=0;
202  double orent=configurations.get(0).getOffsetOrentationMagnitude();
203  double vect =configurations.get(0).getOffsetVectorMagnitude();
204  for(configuration c:configurations){
205  double tmpOrent = c.getOffsetOrentationMagnitude();
206  double tmpVector= c.getOffsetVectorMagnitude();
207  if(
208  tmpOrent<=orent &&
209  tmpVector<=vect){
210  orent=tmpOrent;
211  vect=tmpVector;
212  best = i;
213  }
214  i++;
215  }
216  //System.out.println("Selecting "+best+" config");
217  return configurations.get(best);
218  }
219  }
220 
224  private class configuration{
225 
227  private final double[] joints;
228 
231 
233  private final TransformNR target;
234 
236  double o;
237 
239  double v;
240 
247  public configuration(double [] joints, TransformNR t){
248  this.joints = joints.clone();
249  target = t;
250  }
251 
258  if(transform == null){
259  transform = fk(getJoints());
262  }
263  return transform;
264  }
265 
271  public double[] getJoints() {
272  return joints;
273  }
274 
281  getTransform();
282  return o;
283  }
284 
290  public double getOffsetVectorMagnitude(){
291  getTransform();
292  return v;
293  }
294 
301  public boolean same(configuration c){
302  for(int i=0;i<6;i++){
303  if( c.getJoints()[i]>getJoints()[i]+.1 ||
304  c.getJoints()[i]<getJoints()[i]-.1){
305  return false;
306  }
307  }
308  return true;
309  }
310 
311  /* (non-Javadoc)
312  * @see java.lang.Object#toString()
313  */
314  public String toString(){
315  getTransform();
316  String s="\tTarget = "+target.toString()+"\n\tVector = "+v+"\n\tOrent "+o+"\n\tCurrent = "+getTransform().toString();
317  return s;
318  }
319  }
320 
324  private class searchNode{
325 
327  private double start;
328 
330  private final double startingIncrement;
331 
333  private final int link;
334 
342  public searchNode(int link,double start, double inc){
343  this.link = link;
344  this.start = start;
345  startingIncrement = inc;
346  if (inc<0)
347  throw new RuntimeException("Increment must be positive");
348  }
349 
355  public void setCurrent(double d) {
356  start=d;
357  }
358 
364  double getUpper(){
365  double b = start+startingIncrement;
366  if( b>getDhChain().getUpperLimits()[link] ||
367  b<getDhChain().getlowerLimits()[link]
368  ){
369  throw new RuntimeException("Limit bounded");
370  }
371  return b;
372  }
373 
379  double getLower(){
380  double b= start-startingIncrement;
381  if( b>getDhChain().getUpperLimits()[link] ||
382  b<getDhChain().getlowerLimits()[link]
383  ){
384 
385  throw new RuntimeException("Limit bounded");
386  }
387  return b;
388  }
389 
395  double getNone(){
396  return start;
397  }
398 
405  double get(int index){
406  switch(index){
407  case 0:
408  return getLower();
409  case 1:
410  return getUpper();
411  case 2:
412  return getNone();
413  default:
414  throw new RuntimeException("Index must be 0-2");
415  }
416  }
417  }
418 
419 }
TransformNR forwardKinematics(double[] jointSpaceVector)
Definition: DHChain.java:140
searchTree(double[] jointSpaceVector, double startingIncrement)
double[] inverseKinematics(TransformNR target, double[] jointSpaceVector, DHChain chain)