forked from tinkersprojects/Delta-Kinematics-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeltaKinematics.cpp
174 lines (115 loc) · 4.03 KB
/
DeltaKinematics.cpp
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
/**********************************************************************************************
* Arduino Delta Kinematics- Version 1.0
* by William Bailes <williambailes@gmail.com> http://tinkersprojects.com/
*
* This Library is licensed under a GPLv3 License
*
* made with infomation from the following documents
* - http://forums.trossenrobotics.com/tutorials/introduction-129/delta-robot-kinematics-3276/
* - https://www.marginallyclever.com/other/samples/fk-ik-test.html
* - https://github.com/Tomdf/Delta_Robots/blob/master/Diagrams/Delta%20Robot%20Kinematics%20-%20Trossen%20Robotics.pdf
* - http://hypertriangle.com/~alex/delta-robot-tutorial/
**********************************************************************************************/
#include "DeltaKinematics.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <math.h>
/******************* SETUP *******************/
DeltaKinematics::DeltaKinematics(double _ArmLength,double _RodLength,double _BassTri,double _PlatformTri)
{
PlatformTri = _PlatformTri; // end effector
BassTri = _BassTri; // base
RodLength = _RodLength;
ArmLength = _ArmLength;
x = y = z = a = b = c = 0.0;
}
// forward kinematics: (thetaA, thetaB, thetaC) -> (x0, y0, z0)
int DeltaKinematics::forward()
{
return forward(a, b, c);
}
int DeltaKinematics::forward(double thetaA, double thetaB, double thetaC)
{
x=0.0;
y=0.0;
z=0.0;
double t = (BassTri-PlatformTri)*tan30/2.0;
double dtr = pi/180.0;
thetaA *= dtr;
thetaB *= dtr;
thetaC *= dtr;
double y1 = -(t + ArmLength*cos(thetaA));
double z1 = -ArmLength*sin(thetaA);
double y2 = (t + ArmLength*cos(thetaB))*sin30;
double x2 = y2*tan60;
double z2 = -ArmLength*sin(thetaB);
double y3 = (t + ArmLength*cos(thetaC))*sin30;
double x3 = -y3*tan60;
double z3 = -ArmLength*sin(thetaC);
double dnm = (y2-y1)*x3-(y3-y1)*x2;
double w1 = y1*y1 + z1*z1;
double w2 = x2*x2 + y2*y2 + z2*z2;
double w3 = x3*x3 + y3*y3 + z3*z3;
// x = (a1*z + b1)/dnm
double a1 = (z2-z1)*(y3-y1)-(z3-z1)*(y2-y1);
double b1 = -((w2-w1)*(y3-y1)-(w3-w1)*(y2-y1))/2.0;
// y = (a2*z + b2)/dnm;
double a2 = -(z2-z1)*x3+(z3-z1)*x2;
double b2 = ((w2-w1)*x3 - (w3-w1)*x2)/2.0;
// a*z^2 + b*z + c = 0
double aV = a1*a1 + a2*a2 + dnm*dnm;
double bV = 2.0*(a1*b1 + a2*(b2-y1*dnm) - z1*dnm*dnm);
double cV = (b2-y1*dnm)*(b2-y1*dnm) + b1*b1 + dnm*dnm*(z1*z1 - RodLength*RodLength);
// discriminant
double dV = bV*bV - 4.0*aV*cV;
if (dV < 0.0)
{
return non_existing_povar_error; // non-existing povar. return error,x,y,z
}
z = -0.5*(bV+sqrt(dV))/aV;
x = (a1*z + b1)/dnm;
y = (a2*z + b2)/dnm;
return no_error;
}
// inverse kinematics
// helper functions, calculates angle thetaA (for YZ-pane)
int DeltaKinematics::delta_calcAngleYZ(double *Angle, double x0, double y0, double z0)
{
double y1 = -0.5 * tan30 * BassTri; // f/2 * tan(30 deg)
y0 -= 0.5 * tan30 * PlatformTri; // shift center to edge
// z = a + b*y
double aV = (x0*x0 + y0*y0 + z0*z0 +ArmLength*ArmLength - RodLength*RodLength - y1*y1)/(2.0*z0);
double bV = (y1-y0)/z0;
// discriminant
double dV = -(aV+bV*y1)*(aV+bV*y1)+ArmLength*(bV*bV*ArmLength+ArmLength);
if (dV < 0)
{
return non_existing_povar_error; // non-existing povar. return error, theta
}
double yj = (y1 - aV*bV - sqrt(dV))/(bV*bV + 1); // choosing outer povar
double zj = aV + bV*yj;
*Angle = atan2(-zj,(y1 - yj)) * 180.0/pi;
return no_error; // return error, theta
}
// inverse kinematics: (x0, y0, z0) -> (thetaA, thetaB, thetaC)
int DeltaKinematics::inverse()
{
inverse(x, y, z);
}
int DeltaKinematics::inverse(double x0, double y0, double z0)
{
a = 0;
b = 0;
c = 0;
int error = delta_calcAngleYZ(&a, x0, y0, z0);
if(error != no_error)
return error;
error = delta_calcAngleYZ(&b, x0*cos120 + y0*sin120, y0*cos120-x0*sin120, z0);
if(error != no_error)
return error;
error = delta_calcAngleYZ(&c, x0*cos120 - y0*sin120, y0*cos120+x0*sin120, z0);
return error;
}