forked from Lukaszm94/RobotSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinversekinematicsengine.cpp
293 lines (258 loc) · 7.48 KB
/
inversekinematicsengine.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
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
#include "inversekinematicsengine.h"
#include <iostream>
#include <cmath>
#define PI 3.14159265
using namespace std;
InverseKinematicsEngine::InverseKinematicsEngine(QObject *parent) : QObject(parent)
{
debugPrinting = false;
}
void InverseKinematicsEngine::setTCP(Point newTCP)
{
newTCP = Point(newTCP.x, newTCP.y, newTCP.z - machineParameters.h);
P_T = newTCP;
}
void InverseKinematicsEngine::setMachineParameters(MachineParameters machineParameters)
{
this->machineParameters = machineParameters;
}
void InverseKinematicsEngine::setDeltas(Deltas deltas)
{
this->deltas = deltas;
}
void InverseKinematicsEngine::setApproachVector(ApproachVector approachVector)
{
this->approachVector = approachVector;
}
bool InverseKinematicsEngine::solve()
{
// 2
P_P = calculateP_P();
if(debugPrinting)
cout << "P_P: " << P_P << endl;
// 4
double S_1 = calculateS_1();
double C_1 = calculateC_1();
if(debugPrinting)
cout << "C_1= " << C_1 <<", S_1= " << S_1 << endl;
// 6
double S_5 = calculateS_5(S_1, C_1);
double C_5 = calculateC_5(S_5);
if(debugPrinting)
cout << "C_5= " << C_5 <<", S_5= " << S_5 << endl;
// 7
double S_234 = calculateS_234(C_5);
double C_234 = calculateC_234(C_5, S_1, C_1);
if(debugPrinting)
cout << "C_234= " << C_234 <<", S_234= " << S_234 << endl;
// 8
P_R = calculateP_R(C_1, S_1, C_234, S_234);
if(debugPrinting)
cout << "P_R: " << P_R << endl;
// 9
double a = calculateA();
double b = calculateB(a);
if(debugPrinting)
cout << "a= " << a <<", b= " << b << endl;
// 11
double S_2 = calculateS_2(a, b);
double C_2 = calculateC_2(a, b);
if(debugPrinting)
cout << "C_2= " << C_2 <<", S_2= " << S_2 << endl;
// 12
double S_3 = calculateS_3(a, b);
double C_3 = calculateC_3(a);
if(debugPrinting)
cout << "C_3= " << C_3 <<", S_3= " << S_3 << endl;
// 13
double S_23 = calculateS_23(a, b);
double C_23 = calculateC_23(a, b);
if(debugPrinting)
cout << "C_23= " << C_23 <<", S_23= " << S_23 << endl;
// 14
double S_4 = calculateS_4(C_234, S_234, C_23, S_23);
double C_4 = calculateC_4(C_234, S_234, C_23, S_23);
if(debugPrinting)
cout << "C_4= " << C_4 <<", S_4= " << S_4 << endl;
// resolve fi
if(isSinCosValid(S_1, C_1)) {
machineCoordinates.f_1 = getFi(S_1, C_1);
} else {
return false;
}
if(isSinCosValid(S_2, C_2)) {
machineCoordinates.f_2 = getFi(S_2, C_2);
} else {
return false;
}
if(isSinCosValid(S_3, C_3)) {
machineCoordinates.f_3 = getFi(S_3, C_3);
} else {
return false;
}
if(isSinCosValid(S_4, C_4)) {
machineCoordinates.f_4 = getFi(S_4, C_4);
} else {
return false;
}
if(isSinCosValid(S_5, C_5)) {
machineCoordinates.f_5 = getFi(S_5, C_5);
} else {
return false;
}
return true;
}
MachineCoordinates InverseKinematicsEngine::getMachineCoordinates()
{
return machineCoordinates;
}
double InverseKinematicsEngine::sSqrt(double x)
{
if(x < 0.0) {
cerr << "Sqrt argument less than 0 (=" << x <<"), returning 1" << std::endl;
return 1.0;
}
return sqrt(x);
}
double InverseKinematicsEngine::pow2(double x)
{
return x * x;
}
Point InverseKinematicsEngine::calculateP_P()
{
Point P_P;
double l = machineParameters.l_5 + machineParameters.l_6;
double cos_theta = cos(approachVector.theta * PI / 180);
double sin_theta = sin(approachVector.theta * PI / 180);
double cos_psi = cos(approachVector.psi * PI / 180);
double sin_psi = sin(approachVector.psi * PI / 180);
P_P.x = P_T.x - l * cos_theta * cos_psi;
P_P.y = P_T.y - l * cos_theta * sin_psi;
P_P.z = P_T.z - l * sin_theta;
return P_P;
}
double InverseKinematicsEngine::calculateS_1()
{
double S_1 = (machineParameters.e * P_P.x + deltas.d_1 * P_P.y * sSqrt(pow2(P_P.x) + pow2(P_P.y) - pow2(machineParameters.e))) / (pow2(P_P.x) + pow2(P_P.y));
return S_1;
}
double InverseKinematicsEngine::calculateC_1()
{
double C_1 = (-machineParameters.e * P_P.y + deltas.d_1 * P_P.x * sSqrt(pow2(P_P.x) + pow2(P_P.y) - pow2(machineParameters.e))) / (pow2(P_P.x) + pow2(P_P.y));
return C_1;
}
double InverseKinematicsEngine::calculateS_5(double S_1, double C_1)
{
double cos_theta = cos(approachVector.theta * PI / 180);
double cos_psi = cos(approachVector.psi * PI / 180);
double sin_psi = sin(approachVector.psi * PI / 180);
double S_5 = cos_theta * (sin_psi * C_1 - cos_psi * S_1);
return S_5;
}
double InverseKinematicsEngine::calculateC_5(double S_5)
{
return deltas.d_5 * sSqrt(1 - pow2(S_5));
}
double InverseKinematicsEngine::calculateS_234(double C_5)
{
double sin_theta = sin(approachVector.theta * PI / 180);
if(C_5 == 0.0) {
return -999999.9;
}
double S_234 = sin_theta / C_5;
return S_234;
}
double InverseKinematicsEngine::calculateC_234(double C_5, double S_1, double C_1)
{
double cos_theta = cos(approachVector.theta * PI / 180);
double cos_psi = cos(approachVector.psi * PI / 180);
double sin_psi = sin(approachVector.psi * PI / 180);
if(C_5 == 0.0) {
return -999999.9;
}
double C_234 = cos_theta * (cos_psi * C_1 + sin_psi * S_1) / C_5;
return C_234;
}
Point InverseKinematicsEngine::calculateP_R(double C_1, double S_1, double C_234, double S_234)
{
Point P_R;
P_R.x = P_P.x - machineParameters.l_4 * C_1 * C_234;
P_R.y = P_P.y - machineParameters.l_4 * S_1 * C_234;
P_R.z = P_P.z - machineParameters.l_4 * S_234;
return P_R;
}
double InverseKinematicsEngine::calculateA()
{
double a = -machineParameters.l_1 + deltas.d_1 * sSqrt(pow2(P_R.x) + pow2(P_R.y) - pow2(machineParameters.e));
return a;
}
double InverseKinematicsEngine::calculateB(double a)
{
double b = (pow2(a) + pow2(P_R.z) + pow2(machineParameters.l_2) - pow2(machineParameters.l_3)) / (2 * machineParameters.l_2);
return b;
}
double InverseKinematicsEngine::calculateS_2(double a, double b)
{
double S_2 = (P_R.z * b + deltas.d_2 * a * sSqrt(pow2(a) + pow2(P_R.z) - pow2(b))) / (pow2(a) + pow2(P_R.z));
return S_2;
}
double InverseKinematicsEngine::calculateC_2(double a, double b)
{
double C_2 = (a * b - deltas.d_2 * P_R.z * sSqrt(pow2(a) + pow2(P_R.z) - pow2(b))) / (pow2(a) + pow2(P_R.z));
return C_2;
}
double InverseKinematicsEngine::calculateS_3(double a, double b)
{
double S_3 = -deltas.d_2 * sSqrt(pow2(a) + pow2(P_R.z) - pow2(b)) / machineParameters.l_3;
return S_3;
}
double InverseKinematicsEngine::calculateC_3(double a)
{
double C_3 = (pow2(a) + pow2(P_R.z) - pow2(machineParameters.l_2) - pow2(machineParameters.l_3)) / (2 * machineParameters.l_2 * machineParameters.l_2);
return C_3;
}
double InverseKinematicsEngine::calculateS_23(double a, double b)
{
double S_23 = (P_R.z - machineParameters.l_2 * (P_R.z * b + deltas.d_2 * a * sSqrt(pow2(a) + pow2(P_R.z) - pow2(b))) / (pow2(a) + pow2(P_R.z))) / machineParameters.l_3;
return S_23;
}
double InverseKinematicsEngine::calculateC_23(double a, double b)
{
double C_23 = (a - machineParameters.l_2 * (a * b - deltas.d_2 * P_R.z * sSqrt(pow2(a) + pow2(P_R.z) - pow2(b))) / (pow2(a) + pow2(P_R.z))) / machineParameters.l_3;
return C_23;
}
double InverseKinematicsEngine::calculateS_4(double C_234, double S_234, double C_23, double S_23)
{
double S_4 = S_234 * C_23 - C_234 * S_23;
return S_4;
}
double InverseKinematicsEngine::calculateC_4(double C_234, double S_234, double C_23, double S_23)
{
double C_4 = C_234 * C_23 + S_234 * S_23;
return C_4;
}
double InverseKinematicsEngine::getFi(double S, double C)
{
double fi = 0.0;
/*if(abs(S)<=abs(C))
fi = asin(S);
else
fi = acos(C);
return (fi * 180 / PI);*/
fi = atan2(S, C) * 180 / PI;
/*if(fi < 0.0) {
fi += 360;
}*/
return fi;
}
bool InverseKinematicsEngine::isSinCosValid(double S, double C)
{
if(abs(S)>1 || abs(C)>1 || S==NAN || C==NAN)
{
return false;
}
else
{
return true;
}
}