-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgrammingDemo.cpp
More file actions
334 lines (290 loc) · 8.75 KB
/
ProgrammingDemo.cpp
File metadata and controls
334 lines (290 loc) · 8.75 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
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
330
331
332
333
334
// ProgrammingDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include "ensc-488.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//robot constants for lengths
const double a1 = 0.195;
const double a2 = 0.142;
const double d1 = 0.405;
const double d2 = 0.07;
const double d4 = 0.140;
bool where(double theta1, double theta2, double d3, double theta4, JOINT& conf); //Where function used to find where the robot will end up with joint parameters
bool kin(double theta1, double theta2, double d3, double theta4, JOINT &conf); //Solves the Transform Matrices from joint parameters
bool solve(double x, double y, double z, double phi, JOINT &conf1); //Solve function used to find joint parameters of the end effector location
bool invkin(double x, double y, double z, double phi, JOINT& conf1, JOINT& conf2); //Solves the Inverse Kinematics using the robots parameters
struct T {
double result[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
};
T matrixMul(double T1[4][4], double T2[4][4]);
double valueRounding(double value);
int main(int argc, char* argv[]) {
double theta1 = 0; //First Joint Parameter
double theta2 = 0; //Second Joint Parameter
double d3 = 0; //Third Joint Parameter
double theta4 = 0; //Fourth Joint Parameter
double x;
double y;
double z;
double phi;
JOINT q;
printf("Keep this window in focus, and...\n");
char ch;
int c;
double i = 1.0;
const int ESC = 27;
printf("Press any key to continue \n");
printf("Press ESC to exit \n");
c = _getch();
while (1) {
if (c != ESC) {
cout << "Press 1 to initiate WHERE function. Press 2 to initiate SOLVE function. Press 3 to Grasp.\n";
ch = _getch();
if (ch == '1') { //Forward Kinematics (WHERE/KIN)
cout << "Where\nPlease input the values for each Joint (Theta 1 (deg), Theta 2 (deg), Distance 3 (m), Theta 4 (deg))\nTheta 1: ";
cin >> theta1; //Enter in the First Joint Angle
cout << "Theta 2: ";
cin >> theta2; //Enter in the Second Joint Angle
cout << "Distance 3: ";
cin >> d3; //Enter in the Third Joint Translation
cout << "Theta 4: ";
cin >> theta4;//Enter in the Fourth Joint Angle
printf("Joint vector 1: [%lf, %lf, %lf, %lf]\n", theta1, theta2, d3, theta4);
if (where(theta1, theta2, d3, theta4, q)) {
printf("The End Effector Values are: {%lf, %lf, %lf, %lf]\n", q[0], q[1], q[2], q[3]);
JOINT qWhere = { theta1, theta2, d3, theta4 };
MoveToConfiguration(qWhere, true);
}
else {
printf("One or more of the joint limits provided exceed joint limits.\n");
}
}
else if (ch == '2') {
cout << "SOLVE\nPlease input the values for x (m), y (m), z (m), and phi (deg)\nx : ";
cin >> x;
cout << "y : ";
cin >> y;
cout << "z : ";
cin >> z;
cout << "phi : ";
cin >> phi;
phi = phi * PI / 180;
if (!solve(x, y, z, phi, q)) {
printf("The coordinates entered are outside the joint space of the robot.\n\n");
}
else {
MoveToConfiguration(q, true);
}
}
else if (ch == '3') {
printf("Please input 1 to close the gripper, 2 to open the gripper.\n");
ch = _getch();
if (ch == '1') {
bool closed = Grasp(true);
printf("Closed = %d\n", closed);
}
else if (ch == '2') {
bool opened = Grasp(false);
printf("Opened = %d\n", opened);
}
}
printf("Press any key to continue \n\n");
printf("Press ESC to exit \n\n");
c = _getch();
}
else break;
}
return 0;
}
bool where(double theta1, double theta2, double d3, double theta4, JOINT &conf) {
if (!kin(theta1, theta2, d3, theta4, conf)) {
return false;
}
else {
return true;
}
}
bool kin(double theta1, double theta2, double d3, double theta4, JOINT &conf) {
//static double jointVectors[4] = {};
if (theta1 < -150.0 || theta1 > 150.0 ||
theta2 < -100.0 || theta2 > 100.0 ||
d3 < -200.0 || d3 > -100.0 ||
theta4 < -160.0 || theta4 > 160.0) {
return false;
}
theta1 = DEG2RAD(theta1);
theta2 = DEG2RAD(theta2);
theta4 = DEG2RAD(theta4);
d3 = d3 / 1000.0;
conf[0] = a2 * cos(theta1 + theta2) + a1 * cos(theta1);
conf[1] = a2 * sin(theta1 + theta2) + a1 * sin(theta1);
conf[2] = (d1 + d2 - d3 - d4 - .410) * 1000.0;
conf[3] = theta1 + theta2 - theta4;
return true;
//double T01[4][4] = { //Translation Matrix
// {valueRounding(cos(theta1)), valueRounding(-sin(theta1)), 0, 0},
// {valueRounding(sin(theta1)), valueRounding(cos(theta1)), 0, 0},
// {0, 0, 1, d1},
// {0, 0, 0, 1}
//};
//double T12[4][4] = { //Translation Matrix
// {valueRounding(cos(theta2)), valueRounding(-sin(theta2)), 0, a1},
// {valueRounding(sin(theta2)), valueRounding(cos(theta2)), 0, 0},
// {0, 0, 1, d2},
// {0, 0, 0, 1}
//};
//double T23[4][4] = { //Translation Matrix
// {1, 0, 0, a2},
// {0, -1, 0, 0},
// {0, 0, -1, -d2},
// {0, 0, 0, 1}
//};
//double T34[4][4] = { //Translation Matrix
// {valueRounding(cos(theta4)), valueRounding(-sin(theta4)), 0, 0},
// {valueRounding(sin(theta4)), valueRounding(cos(theta4)), 0, 0},
// {0, 0, 1, d3},
// {0, 0, 0, 1}
//};
//double T45[4][4] = {//Translation Matrix
// {1, 0, 0, 0},
// {0, 1, 0, 0},
// {0, 0, 1, 0},
// {0, 0, 0, 1}
//};
////T05 = T01 * T12 * T23 * T34 * T45;
//T T1 = matrixMul(T01, T12);
//T T2 = matrixMul(T1.result, T23);
//T T3 = matrixMul(T2.result, T34);
//T T4 = matrixMul(T3.result, T45);
//// phi calculation
//double phi = theta1 + theta2 + theta4;
//phi = RAD2DEG(phi);
//jointVectors[0] = T4.result[0][3];
//jointVectors[1] = T4.result[1][3];
//jointVectors[2] = T4.result[2][3];
//jointVectors[3] = phi;
//return jointVectors;
}
double valueRounding(double value) {
return round(value * 1000.0) / 1000.0;
}
T matrixMul(double T1[4][4], double T2[4][4]) {
T result;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k) {
result.result[i][j] += T1[i][k] * T2[k][j];
}
return result;
}
bool solve(double x, double y, double z, double phi, JOINT &conf) {
bool q1Valid = true;
bool q2Valid = true;
JOINT q1;
JOINT q2;
if (!invkin(x, y, z, phi, q1, q2)) {
return false;
}
else {
double theta1_1 = q1[0];
double theta2_1 = q1[1];
double d3 = q1[2];
double theta4_1 = q1[3];
double theta1_2 = q2[0];
double theta2_2 = q2[1];
double theta4_2 = q2[3];
if (theta1_1 < -150.0 || theta1_1 > 150.0 ||
theta2_1 < -100.0 || theta2_1 > 100.0 ||
d3 < -200.0 || d3 > -100.0 ||
theta4_1 < -160.0 || theta4_1 > 160.0) {
printf("q1 is invalid\n");
q1Valid = false;
}
else if (theta1_2 < -150.0 || theta1_2 > 150.0 ||
theta2_2 < -100.0 || theta2_2 > 100.0 ||
d3 < -200.0 || d3 > -100.0 ||
theta4_2 < -160.0 || theta4_2 > 160.0) {
printf("q2 is invalid\n");
q2Valid = false;
}
if (q1Valid && q2Valid) {
JOINT curr;
GetConfiguration(curr);
double dif1_1 = abs(curr[0] - q1[0]);
double dif2_1 = abs(curr[1] - q1[1]);
double dif3_1 = abs(curr[3] - q1[3]);
double dif1_2 = abs(curr[0] - q2[0]);
double dif2_2 = abs(curr[1] - q2[1]);
double dif3_2 = abs(curr[3] - q2[3]);
double difTot1 = dif1_1 + dif2_1 + dif3_1;
double difTot2 = dif1_2 + dif2_2 + dif3_2;
if (difTot1 > difTot2) {
conf[0] = q2[0];
conf[1] = q2[1];
conf[2] = q2[2];
conf[3] = q2[3];
}
else {
conf[0] = q1[0];
conf[1] = q1[1];
conf[2] = q1[2];
conf[3] = q1[3];
}
}
else if (!q1Valid && !q2Valid) {
return false;
}
else if (!q1Valid) {
conf[0] = q2[0];
conf[1] = q2[1];
conf[2] = q2[2];
conf[3] = q2[3];
}
else {
conf[0] = q1[0];
conf[1] = q1[1];
conf[2] = q1[2];
conf[3] = q1[3];
}
return true;
}
}
bool invkin(double x, double y, double z, double phi, JOINT &q1, JOINT &q2) {
double c2 = (pow(x, 2) + pow(y, 2) - pow(a1, 2) - pow(a2, 2)) / (2 * a1 * a2);
double s2 = sqrt(1 - pow(c2, 2));
if (c2 > 1 || c2 < -1 || isnan(c2) || s2 > 1 || s2 < -1 || isnan(s2)) {
return false;
}
else {
double theta2_1 = atan2(s2, c2);
double theta2_2 = atan2(-s2, c2);
double theta1_1 = atan2(y, x) - atan2(a2 * s2, a1 + a2 * c2);
double theta1_2 = atan2(y, x) - atan2(a2 * -s2, a1 + a2 * c2);
double theta4_1 = theta1_1 + theta2_1 - phi;
double theta4_2 = theta1_2 + theta2_2 - phi;
double d3 = valueRounding(d1 + d2 - z - d4 - 0.410);
printf("Joint vector 1: [%lf, %lf, %lf, %lf]\n",
theta1_1 * 180 / PI, theta2_1 * 180 / PI, d3*1000, theta4_1 * 180 / PI);
printf("Joint vector 2: [%lf, %lf, %fl, %lf]\n",
theta1_2 * 180 / PI, theta2_2 * 180 / PI, d3*1000, theta4_2 * 180 / PI);
q1[0] = theta1_1 * 180 / PI;
q1[1] = theta2_1 * 180 / PI;
q1[2] = d3 * 1000;
q1[3] = theta4_1 * 180 / PI;
q2[0] = theta1_2 * 180 / PI;
q2[1] = theta2_2 * 180 / PI;
q2[2] = d3 * 1000;
q2[3] = theta4_2 * 180 / PI;
return true;
}
}