-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkinematics.java
More file actions
249 lines (183 loc) · 6.95 KB
/
Copy pathkinematics.java
File metadata and controls
249 lines (183 loc) · 6.95 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
import java.util.Arrays;
public class kinematics{
public static double[][] jacob2D( double[] link, double[] theta){
// link[] contains the length of the two links
// theta[] contains the two angles
double l1 = link[0];
double l2 = link[1];
double t1 = Math.toRadians( theta[0]);
double t2 = Math.toRadians( theta[1]);
/* jacobian
[ df1(x)/ dx1 df1(x)/dx2
df2(x)/ dx1 df2(x)/dx2 ]
*/
double df1x1 = -l1 * Math.sin(t1) + -Math.sin(t1 + t2);
double df1x2 = -l2 * Math.sin(t1 + t2);
double df2x1 = l1 * Math.cos(t1) + l2 * Math.cos( t1 + t2);
double df2x2 = l2 * Math.cos(t1 + t2);
double[][] jacob = new double[][]{
{ df1x1, df1x1},
{ df2x1, df2x2}
};
return jacob;
}
public static double[] fwdKinPos( double[] theta, double[] link){
double l1 = link[0];
double l2 = link[1];
double t1 = Math.toRadians( theta[0]);
double t2 = Math.toRadians( theta[1]);
double x = l1 * Math.cos( t1) + l2 * Math.cos( t1 + t2);
double y = l1 * Math.sin( t1) + l2 * Math.sin( t1 + t2);
double xy[] = new double[]{
x, y
};
return xy;
}
public static double[][] newtonJacob( double[] theta, double[] link, double alpha){
// estimate the jacobian
/*
double th11[] = new double[]{ theta[0] + alpha, theta[1]};
double th12[] = new double[]{ theta[0] - alpha, theta[1]};
double th21[] = new double[]{ theta[0], theta[1] + alpha};
double th22[] = new double[]{ theta[0], theta[1] - alpha};
double P11[] = fwdKinPos( th11, link);
double P12[] = fwdKinPos( th12, link);
double P21[] = fwdKinPos( th21, link);
double P22[] = fwdKinPos( th22, link);
double J11 = (P11[0] - P12[0]) / ( 2 * alpha);
double J12 = (P11[1] - P12[1]) / ( 2 * alpha);
double J21 = (P21[0] - P22[0]) / ( 2 * alpha);
double J22 = (P21[1] - P22[1]) / ( 2 * alpha);
double J[][] = new double[][]{
{ J11, J21},
{ J12, J22}
};
*/
/*
* J = [ dxth1 dxth2
* dyth1 dyth2]
*
* x = l1 cos(th1) + l2 cos( th1 + th2)
* y = l2 sin(th1) + l2 sin( th1 + th2)
*
* dxth1 = -l1 sin( th1) - l2 sin( th1 + th2)
* dyth1 = l1 cos( th1) - l2 cos( th1 + th2)
* dxth2 = -l2 sin( th1 + th2)
* dyth2 = l2 cos( th1 + th2)
*
*/
double th1 = theta[0];
double th2 = theta[1];
double l1 = link[0];
double l2 = link[1];
double dxth1 = -l1 * Math.sin( th1) - l2 * Math.sin( th1 + th2);
double dyth1 = l1 * Math.cos( th1) - l2 * Math.cos( th1 + th2);
double dxth2 = -l2 * Math.sin( th1 + th2);
double dyth2 = l2 * Math.cos( th1 + th2);
double J[][] = new double[][]{
{ dxth1, dxth2},
{ dyth1, dyth2}
};
return J;
}
public static double[][] inv2x2Mat( double[][] m){
/*
* m = [ a b
* c d]
*
* determinate = 1 / ( ad - bc)
*
* inv m = det * [ d -b
* -c a]
*/
double a = m[0][0];
double b = m[0][1];
double c = m[1][0];
double d = m[1][1];
double det = 1 /( a * d - b * c);
double outpt[][] = new double[][]{
{ det * d, det * -b},
{ det * -c, det * a}
};
return outpt;
}
public static int[] InvKin( double[] pos, double[] link, double[] theta0, int m){
// pos[] x,y position of the desired point
// link[] length of the two links
// theta0[] initial guess of the thetas of the two links
int outpt[];
if( m == 0){ // newton numerical
double alpha = 0.00001;
double err = 1;
double w[] = new double[]{ pos[0], pos[1]}; // input position
double dPos[] = new double[2]; // difference between current and actual position
double cPos[] = new double[2]; // current position using th
double J[][] = new double[2][2]; // jacobian
double invJ[][] = new double[2][2]; // inverse jacobian
double dTh[] = new double[2]; // change in theta
double th[] = theta0;
cPos = fwdKinPos( th, link);
// iterate until th is the actual theta
int j = 0;
do {
// calculate the difference between actual and current position
dPos[0] = w[0] - cPos[0];
dPos[1] = w[1] - cPos[1];
// calculate Jacobian
J = newtonJacob( th, link, alpha);
invJ = inv2x2Mat( J);
// dth = invJ * dPos
dTh[0] = invJ[0][0] * dPos[0] + invJ[0][1] * dPos[1];
dTh[1] = invJ[1][0] * dPos[0] + invJ[1][1] * dPos[1];
//dTh[0] = J[0][0] / -dPos[0] + J[0][1] / -dPos[1];
//dTh[1] = J[1][0] / -dPos[0] + J[1][1] / -dPos[1];
// update th
th[0] = (th[0] + dTh[0]);
th[1] = (th[1] + dTh[1]);
cPos = fwdKinPos( th, link);
j++;
if( j > 3000) {
System.out.println("err " + euclidDist( cPos, w));
break;
}
} while( euclidDist( cPos, w) > err );
outpt = new int[]{
(int) ( th[0]) % 360,
(int) ( th[1]) % 360
};
} else { // analytical
double x = pos[0];
double y = pos[1];
double l1 = link[0];
double l2 = link[1];
double th2 = Math.acos( ( x * x + y * y - l1 * l1 - l2 * l2) / ( 2 * l1 * l2));
//double th1 = Math.asin( ( l2 * Math.asin( th2) / Math.sqrt( x * x + y * y)) + Math.atan2(y, x));
double th1 = Math.atan( y / x) - Math.atan( ( l2 * Math.sin(th2)) / ( l1 + l2 * Math.cos(th2)));
outpt = new int[]{
(int) Math.toDegrees( th1),
(int) Math.toDegrees( th2)
};
}
return outpt;
}
public static double euclidDist( double[] p1, double[] p2){
// return the euclidiean distance between two points
double x1 = p1[0];
double x2 = p2[0];
double y1 = p1[1];
double y2 = p2[1];
double x = ( x1 - x2) * ( x1 - x2);
double y = ( y1 - y2) * ( y1 - y2);
return Math.sqrt( x + y);
}
public static double lawOfCos( double sA, double sB, double sC){
// using the law of cosines return the angle corresponding to point a
// the inputs are the length of the side opposite to the angle
System.out.println("a " + sA);
System.out.println("b " + sB);
System.out.println("c " + sC);
double cosA = ( sB * sB + sC * sC - sA * sA) / ( 2 * sC * sB);
System.out.println(cosA);
return Math.toDegrees( ( Math.acos( cosA)));
}
}