BowlerKernel
GradiantDecent.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 
8 // TODO: Auto-generated Javadoc
12 public class GradiantDecent implements DhInverseSolver{
13 
15  private final DHChain dhChain;
16 
18  private final boolean debug;
19 
26  public GradiantDecent(DHChain dhChain, boolean debug) {
27  this.dhChain = dhChain;
28  // TODO Auto-generated constructor stub
29  this.debug = debug;
30  }
31 
32  /* (non-Javadoc)
33  * @see com.neuronrobotics.sdk.addons.kinematics.DhInverseSolver#inverseKinematics(com.neuronrobotics.sdk.addons.kinematics.math.TransformNR, double[], com.neuronrobotics.sdk.addons.kinematics.DHChain)
34  */
35  public double[] inverseKinematics(TransformNR target,double[] jointSpaceVector,
36  DHChain chain ) {
37  ArrayList<DHLink> links = chain.getLinks();
38  int linkNum = jointSpaceVector.length;
39  double [] inv = new double[linkNum];
40 
41  GradiantDecentNode [] increments = new GradiantDecentNode[linkNum];
42  for(int i=0;i<linkNum;i++){
43  increments[i] = new GradiantDecentNode(dhChain,i,jointSpaceVector, target, dhChain.getUpperLimits()[i],dhChain.getlowerLimits()[i] );
44  }
45  double posOffset = 10;
46  int iter=0;
47  double vect=0;
48  double orent = 0;
49  boolean stopped;
50  boolean notArrived = false;
51  boolean [] stop = new boolean [increments.length];
52  double previousV =dhChain.forwardKinematics(jointSpaceVector).getOffsetVectorMagnitude(target);
53  double previousO =dhChain.forwardKinematics(jointSpaceVector).getOffsetOrentationMagnitude(target);
54  do{
55  stopped = true;
56  for(int i=increments.length-1;i>=0;i--){
57  stop[i]=increments[i].step();
58  if(!stop[i]){
59  stopped = false;
60  }
61  }
62  vect = dhChain.forwardKinematics(jointSpaceVector).getOffsetVectorMagnitude(target);
63  orent = dhChain.forwardKinematics(jointSpaceVector).getOffsetOrentationMagnitude(target);
64  if(previousV>=vect && previousO>=orent){
65  for(int i=0;i<inv.length;i++){
66  inv[i]=jointSpaceVector[i];
67  }
68  previousV=vect;
69  previousO=orent;
70  }
71 
72  notArrived = (previousV > posOffset|| previousO > .001);
73  if(stopped == true && notArrived == true){
74  stopped = false;
75  for(int i=0;i<increments.length;i++){
76  increments[i].jitter();
77  }
78  //ThreadUtil.wait(100);
79  }
80 
81  if(debug){
82  //dhChain.getViewer().updatePoseDisplay(dhChain.getChain(jointSpaceVector));
83  }
84  }while(++iter<200 && notArrived && stopped == false);//preincrement and check
85  if(debug){
86  System.out.println("Numer of iterations #"+iter+" \n\tStalled = "+stopped+" \n\tArrived = "+!notArrived+" \n\tFinal offset= "+vect+" \n\tFinal orent= "+orent);
87  }
88  return inv;
89  }
90 
91 }
TransformNR forwardKinematics(double[] jointSpaceVector)
Definition: DHChain.java:140
double[] inverseKinematics(TransformNR target, double[] jointSpaceVector, DHChain chain)