1 package com.neuronrobotics.sdk.addons.kinematics.ik;
3 import java.util.ArrayList;
5 import com.neuronrobotics.sdk.addons.kinematics.DHChain;
6 import com.neuronrobotics.sdk.addons.kinematics.DHLink;
7 import com.neuronrobotics.sdk.addons.kinematics.DhInverseSolver;
8 import com.neuronrobotics.sdk.addons.kinematics.math.RotationNR;
9 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
13 boolean debug =
false;
26 Math.pow(tr.
getX(), 2)+
27 Math.pow(tr.
getY(), 2)+
28 Math.pow(tr.
getZ(), 2)
35 ArrayList<DHLink> links = chain.
getLinks();
36 int linkNum = jointSpaceVector.length;
42 double z = target.
getZ();
43 double y = target.
getY();
44 double x = target.
getX();
55 tool=linkOffset(links.get(6));
57 TransformNR wristCenterOffsetTransform = linkOffset(links.get(5)).
times(tool);
60 newCenter = target.
times(wristCenterOffsetTransform.
inverse());
71 System.out.println(
"Singularity! try something else");
74 if(debug)System.out.println(
"Wrist center for IK "+x+
","+y+
","+z);
75 double baseVectorAngle = Math.toDegrees(Math.atan2(y , x));
76 double elbowLink1CompositeLength = length(l1Offset);
77 double elbowLink2CompositeLength=length(l3Offset);
78 double wristVect = length(newCenter);
79 if(debug)System.out.println(
"elbowLink1CompositeLength "+elbowLink1CompositeLength);
80 if(debug)System.out.println(
"elbowLink2CompositeLength "+elbowLink2CompositeLength);
81 if(debug)System.out.println(
"Elbo Hypotinuse "+wristVect);
82 double elbowTiltAngle =-( Math.toDegrees(
85 Math.pow(elbowLink2CompositeLength,2)+
86 Math.pow(elbowLink1CompositeLength,2)
87 -Math.pow(wristVect,2)
90 (2*elbowLink2CompositeLength*elbowLink1CompositeLength)
93 if(debug)System.out.println(
"Elbow angle "+elbowTiltAngle);
94 jointSpaceVector[2]=elbowTiltAngle - Math.toDegrees(links.get(2).getTheta());
98 double elZ = tipOnXVect.
getZ();
99 double elX = tipOnXVect.
getX();
100 double L1 = length(l1Offset);
101 double L2 = length(l3Offset);
103 if(debug)System.out.println(
"L1 "+L1+
" l2 "+L2+
" z "+elZ+
" x "+elX);
111 double asinVal = elZ/L2;
112 if(asinVal>1 || asinVal<-1)
113 throw new RuntimeException(
"Target outside workspace, passive links too short to reach "+L2);
114 double theta2 = Math.asin(asinVal);
116 double L3 = L2*Math.cos(theta2);
117 double theta1 = Math.acos(
125 jointSpaceVector[0]=-(90-(Math.toDegrees(theta1)+baseVectorAngle));
129 }
catch (Throwable t){
131 throw new RuntimeException(
"error calculating base angle: \nL1 "+L1+
136 " \ntheta2 "+Math.toDegrees(theta2)+
137 " \nasinVal "+asinVal
146 double theta3 = Math.atan2(sphericalElbowTartget.
getZ(), sphericalElbowTartget.
getX());
147 jointSpaceVector[1]=-Math.toDegrees(theta3) ;
155 double[] wristLinks=
new double[jointSpaceVector.length];
156 for(
int i=0;i<3;i++) {
157 wristLinks[i]=jointSpaceVector[i];
159 for(
int i=3;i<jointSpaceVector.length;i++) {
162 ArrayList<TransformNR> chainToLoad =
new ArrayList<>();
166 new RotationNR(Math.toDegrees(links.get(5).getAlpha()),0,0)));
169 .
times(virtualcenter);
172 if(wristMOvedToCenter0.
getX()==0&&wristMOvedToCenter0.
getY()==0) {
173 System.out.println(
"Singularity! try something else");
176 double closest= (Math.toDegrees(Math.atan2(wristMOvedToCenter0.
getY(), wristMOvedToCenter0.
getX()))-Math.toDegrees(links.get(3).getTheta()));
178 jointSpaceVector[3]=closest;
179 wristLinks[3]=jointSpaceVector[3];
180 if(jointSpaceVector.length==4)
181 return jointSpaceVector;
183 chainToLoad =
new ArrayList<>();
194 .
times(virtualcenter);
197 if(wristMOvedToCenter1.
getX()==0&&wristMOvedToCenter1.
getY()==0) {
198 System.out.println(
"Singularity! try something else");
201 jointSpaceVector[4]=(Math.toDegrees(Math.atan2(wristMOvedToCenter1.
getY(), wristMOvedToCenter1.
getX()))-
202 Math.toDegrees(links.get(4).getTheta())-
204 wristLinks[4]=jointSpaceVector[4];
205 if(jointSpaceVector.length==5)
206 return jointSpaceVector;
207 chainToLoad =
new ArrayList<>();
216 tool=linkOffset(links.get(6));
222 jointSpaceVector[5]=(Math.toDegrees(qWrist3.
getRotationAzimuth())-Math.toDegrees(links.get(5).getTheta()));
224 return jointSpaceVector;
TransformNR inverseOffset(TransformNR t)
ArrayList< DHLink > getLinks()
Matrix forwardKinematicsMatrix(double[] jointSpaceVector, boolean store)
Matrix DhStep(double jointValue)
double[] inverseKinematics6dof(TransformNR target, double[] jointSpaceVector, DHChain chain)
double[] inverseKinematics(TransformNR target, double[] jointSpaceVector, DHChain chain)
double getRotationAzimuth()