-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDHChain.java
More file actions
495 lines (419 loc) · 12.8 KB
/
DHChain.java
File metadata and controls
495 lines (419 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.neuronrobotics.sdk.addons.kinematics;
import java.io.InputStream;
import java.util.ArrayList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import Jama.Matrix;
import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
import com.neuronrobotics.sdk.addons.kinematics.time.ITimeProvider;
import com.neuronrobotics.sdk.addons.kinematics.time.TimeKeeper;
import com.neuronrobotics.sdk.addons.kinematics.xml.XmlFactory;
import com.neuronrobotics.sdk.common.Log;
// Auto-generated Javadoc
/**
* The Class DHChain.
*/
public class DHChain extends TimeKeeper{
/** The links. */
private ArrayList<DHLink> links = new ArrayList<DHLink>();
/** The chain. */
private ArrayList<TransformNR> chain = new ArrayList<TransformNR>();
/** The int chain. */
private ArrayList<TransformNR> intChain = new ArrayList<TransformNR>();
/** The upper limits. */
private double[] upperLimits;
/** The lower limits. */
private double[] lowerLimits;
/** The debug. */
private boolean debug=false;
/** The is. */
private DhInverseSolver is;
/** The kin. */
public AbstractKinematicsNR kin;
/** The factory. */
private LinkFactory factory;
/**
* Instantiates a new DH chain.
*
* @param kin the kin
*/
public DHChain( AbstractKinematicsNR kin){
this.kin = kin;
}
/**
* Adds the link.
*
* @param link the link
*/
public void addLink(DHLink link){
if(!getLinks().contains(link)){
getLinks().add(link);
}
}
/**
* Removes the link.
*
* @param link the link
*/
public void removeLink(DHLink link){
if(getLinks().contains(link)){
getLinks().remove(link);
}
}
// public DHChain(double [] upperLimits,double [] lowerLimits, boolean debugViewer ) {
// this(upperLimits, lowerLimits);
//
// }
//
// public DHChain(double [] upperLimits,double [] lowerLimits ) {
//
// this.upperLimits = upperLimits;
// this.lowerLimits = lowerLimits;
// getLinks().add(new DHLink( 13, Math.toRadians(180), 32, Math.toRadians(-90)));//0->1
// getLinks().add(new DHLink( 25, Math.toRadians(-90), 93, Math.toRadians(180)));//1->2
// getLinks().add(new DHLink( 11, Math.toRadians(90), 24, Math.toRadians(90)));//2->3
// getLinks().add(new DHLink( 128, Math.toRadians(-90), 0, Math.toRadians(90)));//3->4
//
// getLinks().add(new DHLink( 0, Math.toRadians(0), 0, Math.toRadians(-90)));//4->5
// getLinks().add(new DHLink( 25, Math.toRadians(90), 0, Math.toRadians(0)));//5->tool
//
// forwardKinematics(new double [] {0,0,0,0,0,0});
// }
/**
* Inverse kinematics.
*
* @param target the target
* @param jointSpaceVector the joint space vector
* @return the double[]
* @throws Exception the exception
*/
public double[] inverseKinematics(TransformNR target,double[] jointSpaceVector )throws Exception {
if(getLinks() == null)
return null;
//is = new GradiantDecent(this,debug);
//is = new SearchTreeSolver(this,debug);
if(getInverseSolver() == null)
setInverseSolver(new ComputedGeometricModel(this,debug));
double [] inv = getInverseSolver().inverseKinematics(target, jointSpaceVector,this);
if(debug){
//getViewer().updatePoseDisplay(getChain(jointSpaceVector));
}
//Log.info( "Inverse Kinematics took "+(System.currentTimeMillis()-start)+"ms");
return inv;
}
/**
* Forward kinematics.
*
* @param jointSpaceVector the joint space vector
* @return the transform nr
*/
public TransformNR forwardKinematics(double[] jointSpaceVector) {
return forwardKinematics(jointSpaceVector, true);
}
/**
* Forward kinematics.
*
* @param jointSpaceVector the joint space vector
* @param store the store
* @return the transform nr
*/
public TransformNR forwardKinematics(double[] jointSpaceVector, boolean store) {
return new TransformNR(forwardKinematicsMatrix(jointSpaceVector, store) );
}
/**
* Cross product.
*
* @param a the a
* @param b the b
* @return the double[]
*/
private double [] crossProduct(double[] a, double[] b){
double [] xProd = new double [3];
xProd[0]=a[1]*b[2]-a[2]*b[1];
xProd[1]=a[2]*b[0]-a[0]*b[2];
xProd[2]=a[0]*b[1]-a[1]*b[0];
return xProd;
}
/**
* Forward kinematics matrix.
*
* @param jointSpaceVector the joint space vector
* @param store the store
* @return the matrix
*/
public Matrix forwardKinematicsMatrix(double[] jointSpaceVector, boolean store) {
return forwardKinematicsMatrix(jointSpaceVector,store?getCachedChain():null);
}
public Matrix forwardKinematicsMatrix(double[] jointSpaceVector, ArrayList<TransformNR> chainToLoad) {
if(getLinks() == null)
return new TransformNR().getMatrixTransform();
if (jointSpaceVector.length!=getLinks().size())
throw new IndexOutOfBoundsException("DH links do not match defined links. expected "+getLinks().size()+" got "+jointSpaceVector.length);
Matrix current = new TransformNR().getMatrixTransform();
for(int i=0;i<getLinks().size();i++) {
LinkFactory factory2 = getFactory();
ArrayList<LinkConfiguration> linkConfigurations = factory2.getLinkConfigurations();
LinkConfiguration conf= linkConfigurations.get(i);
Matrix step;
if(conf.isPrismatic())
step= getLinks().get(i).DhStep(jointSpaceVector[i]);
else
step= getLinks().get(i).DhStep(Math.toRadians(jointSpaceVector[i]));
//Log.info( "Current:\n"+current+"Step:\n"+step);
current = current.times(step);
try {
final TransformNR pose =forwardOffset(new TransformNR(current));
if(chainToLoad!=null){
if(intChain.size()<=i)
intChain.add(new TransformNR(step));
else{
intChain.set(i, new TransformNR(step));
}
if(chainToLoad.size()<=i)
chainToLoad.add(pose);
else{
chainToLoad.set(i, pose);
}
}
}catch(java.lang.RuntimeException ex) {
String pose="[\n";
for(int x=0;x<jointSpaceVector.length;x++) {
pose=pose+" "+jointSpaceVector[x]+" , \n";
}
pose+="]";
throw new RuntimeException(ex.getMessage()+" for pose "+pose);
}
}
//Log.info( "Final:\n"+current);
return current;
}
/**
* Forward offset.
*
* @param transformNR the transform nr
* @return the transform nr
*/
private TransformNR forwardOffset(TransformNR transformNR) {
return kin.forwardOffset(transformNR);
}
/**
* Sets the chain.
*
* @param chain the new chain
*/
public void setChain(ArrayList<TransformNR> chain) {
if(chain!=null)
this.chain = chain;
getCachedChain().clear();
//else
// new RuntimeException().printStackTrace();
}
/**
* Gets the chain.
*
* @param jointSpaceVector the joint space vector
* @return the chain
*/
public ArrayList<TransformNR> getChain(double[] jointSpaceVector) {
ArrayList<TransformNR> chainToLoad = new ArrayList<TransformNR>();
forwardKinematicsMatrix(jointSpaceVector,chainToLoad);
return chainToLoad;
}
/**
* Gets the cached chain.
*
* @return the cached chain
*/
public ArrayList<TransformNR> getCachedChain() {
if(chain==null)
chain=new ArrayList<TransformNR>();
return chain;
}
/**
* Gets the upper limits.
*
* @return the upper limits
*/
public double[] getUpperLimits() {
// Auto-generated method stub
return upperLimits;
}
/**
* Gets the lower limits.
*
* @return the lower limits
*/
public double[] getlowerLimits() {
// Auto-generated method stub
return lowerLimits;
}
/**
* Sets the links.
*
* @param links the new links
*/
public void setLinks(ArrayList<DHLink> links) {
this.links = links;
}
/**
* Gets the links.
*
* @return the links
*/
public ArrayList<DHLink> getLinks() {
return links;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
String s="";
for(DHLink l:getLinks()){
s+=l.toString()+"\n";
}
return s;
}
/**
* Gets the inverse solver.
*
* @return the inverse solver
*/
public DhInverseSolver getInverseSolver() {
if(is==null){
is=new DhInverseSolver() {
@Override
public double[] inverseKinematics(TransformNR target,
double[] jointSpaceVector,DHChain chain ) {
ArrayList<DHLink> links = chain.getLinks();
// THis is the jacobian for the given configuration
//Matrix jacobian = chain.getJacobian(jointSpaceVector);
Matrix taskSpacMatrix = target.getMatrixTransform();
int linkNum = jointSpaceVector.length;
double [] inv = new double[linkNum];
// this is an ad-hock kinematic model for d-h parameters and only works for specific configurations
double d = links.get(1).getD()- links.get(2).getD();
double r = links.get(0).getR();
double lengthXYPlaneVect = Math.sqrt(Math.pow(target.getX(),2)+Math.pow(target.getY(),2));
double angleXYPlaneVect = Math.asin(target.getY()/lengthXYPlaneVect);
double angleRectangleAdjustedXY =Math.asin(d/lengthXYPlaneVect);
double lengthRectangleAdjustedXY = lengthXYPlaneVect* Math.cos(angleRectangleAdjustedXY)-r;
double orentation = angleXYPlaneVect-angleRectangleAdjustedXY;
if(Math.abs(Math.toDegrees(orentation))<0.01){
orentation=0;
}
double ySet = lengthRectangleAdjustedXY*Math.sin(orentation);
double xSet = lengthRectangleAdjustedXY*Math.cos(orentation);
double zSet = target.getZ() - links.get(0).getD();
if(links.size()>4){
zSet+=links.get(4).getD();
}
// Actual target for anylitical solution is above the target minus the z offset
TransformNR overGripper = new TransformNR(
xSet,
ySet,
zSet,
target.getRotation());
double l1 = links.get(1).getR();// First link length
double l2 = links.get(2).getR();
double vect = Math.sqrt(xSet*xSet+ySet*ySet+zSet*zSet);
/*
println ( "TO: "+target);
println ( "Trangular TO: "+overGripper);
println ( "lengthXYPlaneVect: "+lengthXYPlaneVect);
println( "angleXYPlaneVect: "+Math.toDegrees(angleXYPlaneVect));
println( "angleRectangleAdjustedXY: "+Math.toDegrees(angleRectangleAdjustedXY));
println( "lengthRectangleAdjustedXY: "+lengthRectangleAdjustedXY);
println( "r: "+r);
println( "d: "+d);
println( "x Correction: "+xSet);
println( "y Correction: "+ySet);
println( "Orentation: "+Math.toDegrees(orentation));
println( "z: "+zSet);
*/
if (vect > l1+l2 || vect<0 ||lengthRectangleAdjustedXY<0 ) {
throw new RuntimeException("Hypotenus too long: "+vect+" longer then "+l1+l2);
}
//from https://www.mathsisfun.com/algebra/trig-solving-sss-triangles.html
double a=l2;
double b=l1;
double c=vect;
double A =Math.acos((Math.pow(b,2)+ Math.pow(c,2) - Math.pow(a,2)) / (2.0*b*c));
double B =Math.acos((Math.pow(c,2)+ Math.pow(a,2) - Math.pow(b,2)) / (2.0*a*c));
double C =Math.PI-A-B;//Rule of triangles
double elevation = Math.asin(zSet/vect);
/*
println( "vect: "+vect);
println( "A: "+Math.toDegrees(A));
println( "elevation: "+Math.toDegrees(elevation));
println( "l1 from x/y plane: "+Math.toDegrees(A+elevation));
println( "l2 from l1: "+Math.toDegrees(C));
*/
inv[0] = Math.toDegrees(orentation);
inv[1] = -Math.toDegrees((A+elevation+links.get(1).getTheta()));
if((int)links.get(1).getAlpha() ==180){
inv[2] = (Math.toDegrees(C))-180-//interior angle of the triangle, map to external angle
Math.toDegrees(links.get(2).getTheta());// offset for kinematics
}
if((int)links.get(1).getAlpha() ==0){
inv[2] = -(Math.toDegrees(C))+Math.toDegrees(links.get(2).getTheta());// offset for kinematics
}
if(links.size()>3)
inv[3] =(inv[1] -inv[2]);// keep it parallell
// We know the wrist twist will always be 0 for this model
if(links.size()>4)
inv[4] = inv[0];//keep the tool orentation paralell from the base
for(int i=0;i<inv.length;i++){
if(Math.abs(inv[i]) < 0.01){
inv[i]=0;
}
// println( "Link#"+i+" is set to "+inv[i]);
}
int i=3;
if(links.size()>3)
i=5;
//copy over remaining links so they do not move
for(;i<inv.length && i<jointSpaceVector.length ;i++){
inv[i]=jointSpaceVector[i];
}
return inv;
}
};
}
return is;
}
/**
* Sets the inverse solver.
*
* @param is the new inverse solver
*/
public void setInverseSolver(DhInverseSolver is) {
this.is = is;
}
/**
* Gets the factory.
*
* @return the factory
*/
public LinkFactory getFactory() {
return factory;
}
/**
* Sets the factory.
*
* @param factory the new factory
*/
public void setFactory(LinkFactory factory) {
upperLimits = factory.getUpperLimits();
lowerLimits = factory.getLowerLimits();
this.factory = factory;
}
@Override
public void setTimeProvider(ITimeProvider t) {
super.setTimeProvider(t);
for(DHLink l:getLinks()) {
if (l.getSlaveMobileBase()!=null)
l.getSlaveMobileBase().setTimeProvider(getTimeProvider());
}
}
}