-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelta.h
executable file
·329 lines (280 loc) · 12.2 KB
/
delta.h
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
#ifndef _DELTA_H_
#define _DELTA_H_
#include <cmath>
#include "svector.h"
static float toRad(float deg) {
return deg * M_PI / 180;
}
struct DeltaGeometry {
// Geometry description ------------------------------------------------------------------------
float RodRadius = 176.50; // from carriage joint to effector joint
float Alpha = 90; // angle from x-axis to A tower
float Beta = 210; // angle from x-axis to B tower
float Gamma = 330; // angle from x-axis to C tower
float RodLength = 362; // rod length from joint to joint
float RadiusCorrectionA = 0; // Delta radius correction A tower
float RadiusCorrectionB = 0; // Delta radius correction B tower
float RadiusCorrectionC = 0; // Delta radius correction C tower
float DiagonalCorrectionA = 0; // Diagonal correction A tower
float DiagonalCorrectionB = 0; // Diagonal correction B tower
float DiagonalCorrectionC = 0; // Diagonal correction C tower
float TowerAOffset = 0; // Tower A offset
float TowerBOffset = 0; // Tower B offset
float TowerCOffset = 0; // Tower C offset
void print() {
printf("RR = %f RL = %f A(%f, %f, %f) O(%f, %f, %f) RC(%f, %f, %f)\n", RodRadius, RodLength, Alpha, Beta, Gamma,
TowerAOffset, TowerBOffset, TowerCOffset, RadiusCorrectionA, RadiusCorrectionB, RadiusCorrectionC);
}
};
struct DeltaPrinter {
/* =========== Parameter essential for delta calibration ===================
C, Y-Axis
| |___| CARRIAGE_HORIZONTAL_OFFSET (recommend set it to 0)
| | \------------------------------------------
|_________ X-axis | \ |
/ \ | \ DELTA_DIAGONAL_ROD (length) Each move this Rod Height
/ \ \ is calculated
/ \ \ Carriage is at printer center! |
A B \_____/--------------------------------
|--| END_EFFECTOR_HORIZONTAL_OFFSET (recommend set it to 0)
|----| ROD_RADIUS (Horizontal rod pivot to pivot measure)
|-----------| PRINTER_RADIUS (recommend set it to ROD_RADIUS)
Column angles are measured from X-axis counterclockwise
"Standard" positions: alpha_A = 210, alpha_B = 330, alpha_C = 90
*/
DeltaPrinter() {}
static const int sm_width = 1024;
static const int sm_height = 1024;
float smx[sm_height * sm_width];
float smy[sm_height * sm_width];
float smz[sm_height * sm_width];
float smxy[sm_height * sm_width];
void setGeo(DeltaGeometry& Geo) {
// Geo.TowerAOffset = -1;
// Geo.TowerBOffset = 2.5;
// Geo.TowerCOffset = -1.5;
// Geo.RodRadius = 158;
Geo.RodLength = 363;
}
void surfaceMap(DeltaGeometry& IKGeo, DeltaGeometry& FKGeo) {
float DeltaRadius = FKGeo.RodRadius;
float W = DeltaRadius;
float x0 = -W;
float y0 = -W;
float dx = 2 * W / (sm_width - 1);
float dy = 2 * W / (sm_height - 1);
for (int i = 0; i < sm_height; ++i) {
for (int j = 0; j < sm_width; ++j) {
float xx = x0 + dx * j;
float yy = y0 + dy * i;
float zz = 0;
float za = 0, zb = 0, zc = 0;
float tx = 0, ty = 0, tz = 0;
inverseKinematics(xx, yy, zz, za, zb, zc, IKGeo);
forwardKinematics(za, zb, zc, tx, ty, tz, FKGeo);
int idx = (sm_height - i - 1) * sm_width + j;
if (sqrt(xx * xx + yy * yy) >= DeltaRadius) {
smx[idx] = 30000;
smy[idx] = 30000;
smz[idx] = 30000;
smxy[idx] = 30000;
} else {
smx[idx] = (tx - xx);
smy[idx] = (ty - yy);
smxy[idx] = sqrt(smx[idx] * smx[idx] + smy[idx] * smy[idx]);
smz[idx] = tz - zz;
}
}
}
}
void calibrate1(DeltaGeometry& IKGeo, DeltaGeometry& FKGeo) {
using namespace svector;
float Alpha = 90;
float Beta = 210;
float Gamma = 330;
float r = 160;
float4 r0(0, 0, 0);
float4 rA0(r * cos(toRad(Alpha)), r * sin(toRad(Alpha)), 0);
float4 rA1(-r * cos(toRad(Alpha)), -r * sin(toRad(Alpha)), 0);
float4 rB0(r * cos(toRad(Beta)), r * sin(toRad(Beta)), 0);
float4 rB1(-r * cos(toRad(Beta)), -r * sin(toRad(Beta)), 0);
float4 rC0(r * cos(toRad(Gamma)), r * sin(toRad(Gamma)), 0);
float4 rC1(-r * cos(toRad(Gamma)), -r * sin(toRad(Gamma)), 0);
float k = IKGeo.RodRadius / r;
float ZA0 = (FK(IK(rA0, IKGeo), FKGeo) - rA0).z;
float ZA1 = (FK(IK(rA1, IKGeo), FKGeo) - rA1).z;
float DZA = (ZA0 * (1 + k) + ZA1 * (1 - k)) / 2;
float ZB0 = (FK(IK(rB0, IKGeo), FKGeo) - rB0).z;
float ZB1 = (FK(IK(rB1, IKGeo), FKGeo) - rB1).z;
float DZB = (ZB0 * (1 + k) + ZB1 * (1 - k)) / 2;
float ZC0 = (FK(IK(rC0, IKGeo), FKGeo) - rC0).z;
float ZC1 = (FK(IK(rC1, IKGeo), FKGeo) - rC1).z;
float DZC = (ZC0 * (1 + k) + ZC1 * (1 - k)) / 2;
float ZR0 = (FK(IK(r0, IKGeo), FKGeo) - r0).z;
float ZT = (ZA0 + ZA1 + ZB0 + ZB1 + ZC0 + ZC1) / 6;
printf("--------------------------------------------\n");
printf("IKGeo.TowerAOffset = %f, ZA0 = %f, ZA1 = %f, DZA = %f\n", IKGeo.TowerAOffset, ZA0, ZA1, DZA);
printf("IKGeo.TowerBOffset = %f, ZB0 = %f, ZB1 = %f, DZB = %f\n", IKGeo.TowerBOffset, ZB0, ZB1, DZB);
printf("IKGeo.TowerCOffset = %f, ZC0 = %f, ZC1 = %f, DZC = %f\n", IKGeo.TowerCOffset, ZC0, ZC1, DZC);
printf("IKGeo.RodRadius = %f, ZR0 = %f\n", IKGeo.RodRadius, ZR0);
printf("IKGeo.RodLength = %f, ZT = %f\n", IKGeo.RodLength, ZT);
IKGeo.TowerAOffset += DZA;
IKGeo.TowerBOffset += DZB;
IKGeo.TowerCOffset += DZC;
IKGeo.RodLength -= ZT;
if (fabs(ZT) < 0.1) {
IKGeo.RodRadius += 1.5 * ZR0;
}
}
float cost(DeltaGeometry& IKGeo, DeltaGeometry& FKGeo, svector::float4* v, float* z, int N) {
float cost = 0;
for (int i = 0; i < N; ++i) {
float d = FK(IK(v[i], IKGeo), FKGeo).z - z[i];
cost += d * d;
}
return sqrt(cost) / N;
}
float randf() {
return (float)rand() / (RAND_MAX + 1.0f);
}
DeltaGeometry geoNeighbour(DeltaGeometry& geo) {
int geoField;
int change;
DeltaGeometry t = geo;
float dTOffset = 0.01;
for (int i = 0; i < 1; ++i) {
geoField = rand() % 3;
change = rand() % 2;
switch(geoField) {
case 0 : t.TowerAOffset += (change ? dTOffset : -dTOffset); break;
case 1 : t.TowerBOffset += (change ? dTOffset : -dTOffset); break;
case 2 : t.TowerCOffset += (change ? dTOffset : -dTOffset); break;
case 3 : t.RodRadius += (change ? 0.01 : -0.01); break;
case 4 : t.RodLength += (change ? 0.01 : -0.01); break;
// case 4 : t.Alpha += change ? 0.01 : -0.01; break;
// case 5 : t.Beta += change ? 0.01 : -0.01; break;
// case 6 : t.Gamma += change ? 0.01 : -0.01; break;
// case 7 : t.RadiusCorrectionA += change ? 0.01 : -0.01; break;
// case 8 : t.RadiusCorrectionB += change ? 0.01 : -0.01; break;
// case 9 : t.RadiusCorrectionC += change ? 0.01 : -0.01; break;
}
}
return t;
}
void calibrate(DeltaGeometry& IKGeo, DeltaGeometry& FKGeo) {
using namespace svector;
float Alpha = 90;
float Beta = 210;
float Gamma = 330;
float r = 160;
static const int N = 7;
float4 v[N];
v[0] = float4(0, 0, 0);
v[1] = float4(r * cos(toRad(Alpha)), r * sin(toRad(Alpha)), 0);
v[2] = float4(-r * cos(toRad(Alpha)), -r * sin(toRad(Alpha)), 0);
v[3] = float4(r * cos(toRad(Beta)), r * sin(toRad(Beta)), 0);
v[4] = float4(-r * cos(toRad(Beta)), -r * sin(toRad(Beta)), 0);
v[5] = float4(r * cos(toRad(Gamma)), r * sin(toRad(Gamma)), 0);
v[6] = float4(-r * cos(toRad(Gamma)), -r * sin(toRad(Gamma)), 0);
float z[N] = { 0.3, -0.1, 0.2, -1.2, -0.5, 1.3, 0.3 };
//FKGeo.TowerAOffset = 2.5; FKGeo.TowerBOffset = 2.1; FKGeo.TowerCOffset = 3.5;
float T = 1E10;
float TMin = 0.1;
float TAlpha = 0.99;
float old_cost = cost(IKGeo, FKGeo, v, z, N);
float best_cost = old_cost;
float initial_cost = old_cost;
float k = 1e-8; // (1.6*(pow(10,-23)));
printf("Cost = %f\n", old_cost);
int NIter = 100;
int itotal = 0;
DeltaGeometry bestGeo = FKGeo;
while(T > TMin) {
int i = 0;
while (i < NIter) {
DeltaGeometry NewGeo = geoNeighbour(FKGeo);
float new_cost = cost(IKGeo, NewGeo, v, z, N);
if (new_cost == new_cost) { // check if not nan
if(new_cost < old_cost || exp(-(new_cost - old_cost) / (k * T)) > randf()) {
FKGeo = NewGeo;
old_cost = new_cost;
printf("%d - Initial Cost = %f - Best Cost = %f - Cost = %f T = %f - ", itotal, initial_cost, best_cost, new_cost, T);
FKGeo.print();
if (new_cost < best_cost) {
best_cost = new_cost;
bestGeo = NewGeo;
}
}
i++;
itotal++;
}
}
//printf("--------------------------------- T = %f\n", T);
T *= TAlpha;
}
}
svector::float4 IK(const svector::float4 &r, const DeltaGeometry &geo) {
svector::float4 Z;
inverseKinematics(r.x, r.y, r.z, Z.x, Z.y, Z.z, geo);
return Z;
}
svector::float4 FK(const svector::float4 &Z, const DeltaGeometry &geo) {
svector::float4 r;
forwardKinematics(Z.x, Z.y, Z.z, r.x, r.y, r.z, geo);
return r;
}
void inverseKinematics(float x, float y, float z, float &Za, float &Zb, float &Zc, const DeltaGeometry& geo) {
float RLa = geo.RodLength + geo.DiagonalCorrectionA;
float RLb = geo.RodLength + geo.DiagonalCorrectionB;
float RLc = geo.RodLength + geo.DiagonalCorrectionC;
float Ra = geo.RodRadius + geo.RadiusCorrectionA;
float Rb = geo.RodRadius + geo.RadiusCorrectionB;
float Rc = geo.RodRadius + geo.RadiusCorrectionC;
float rAlpha = toRad(geo.Alpha);
float rBeta = toRad(geo.Beta);
float rGamma = toRad(geo.Gamma);
float rax = cos(rAlpha) * Ra - x;
float ray = sin(rAlpha) * Ra - y;
float rbx = cos(rBeta) * Rb - x;
float rby = sin(rBeta) * Rb - y;
float rcx = cos(rGamma) * Rc - x;
float rcy = sin(rGamma) * Rc - y;
Za = z - geo.TowerAOffset + sqrt(RLa * RLa - rax * rax - ray * ray);
Zb = z - geo.TowerBOffset + sqrt(RLb * RLb - rbx * rbx - rby * rby);
Zc = z - geo.TowerCOffset + sqrt(RLc * RLc - rcx * rcx - rcy * rcy);
}
void forwardKinematics(float Za, float Zb, float Zc, float &x, float &y, float &z, const DeltaGeometry& geo) {
using namespace svector;
float rAlpha = toRad(geo.Alpha);
float rBeta = toRad(geo.Beta);
float rGamma = toRad(geo.Gamma);
float RLa = geo.RodLength + geo.DiagonalCorrectionA;
float RLb = geo.RodLength + geo.DiagonalCorrectionB;
float RLc = geo.RodLength + geo.DiagonalCorrectionC;
float Ra = geo.RodRadius + geo.RadiusCorrectionA;
float Rb = geo.RodRadius + geo.RadiusCorrectionB;
float Rc = geo.RodRadius + geo.RadiusCorrectionC;
float Xa = cos(rAlpha) * Ra;
float Ya = sin(rAlpha) * Ra;
float Xb = cos(rBeta) * Rb;
float Yb = sin(rBeta) * Rb;
float Xc = cos(rGamma) * Rc;
float Yc = sin(rGamma) * Rc;
float4 P1(Xa, Ya, Za + geo.TowerAOffset);
float4 P2(Xb, Yb, Zb + geo.TowerBOffset);
float4 P3(Xc, Yc, Zc + geo.TowerCOffset);
float d = norm(P2 - P1);
float4 ex = (P2 - P1) / d;
float i = dot3d(ex, P3 - P1);
float4 iex = i * ex;
float4 ey = (P3 - P1 - iex) / norm(P3 - P1 - iex);
float j = dot3d(ey, P3 - P1);
float4 ez = cross3d(ex, ey);
float xx = (RLa * RLa - RLb * RLb + d * d) / (2 * d);
float yy = (RLa * RLa - RLc * RLc + i * i + j * j - 2 * i * xx) / (2 * j);
float zz = sqrt(RLa * RLa - xx * xx - yy * yy);
float4 p = P1 + xx * ex + yy * ey - zz * ez;
x = p.x; y = p.y; z = p.z;
}
};
#endif