-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
472 lines (348 loc) · 13.8 KB
/
mainwindow.cpp
File metadata and controls
472 lines (348 loc) · 13.8 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <Eigen/Dense>
#include <math.h>
#include <array>
#include <fstream> // for excel
#include <iomanip> // for excel
#include <QDebug>
#define rad2deg 180 / M_PI
#define deg2rad M_PI / 180
// Used only for debugging during development
#define watch(x) std::cout << (#x) << ":\n " << (x) << std::endl
using Eigen::MatrixXd;
using Eigen::VectorXd;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->Slider_X, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_X_valueChanged(int)));
connect(ui->Slider_Y, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_Y_valueChanged(int)));
connect(ui->Slider_Z, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_Z_valueChanged(int)));
connect(ui->Slider_PHI, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_PHI_valueChanged(int)));
connect(ui->Slider_THETA, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_THETA_valueChanged(int)));
connect(ui->Slider_PSI, SIGNAL(valueChanged(int)), this, SLOT(on_Slider_PSI_valueChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_3_clicked()
{
ui ->Slider_X->setValue(0);
ui ->Slider_Y->setValue(0);
ui ->Slider_Z->setValue(0);
ui ->Slider_PHI->setValue(0);
ui ->Slider_THETA->setValue(0);
ui ->Slider_PSI->setValue(0);
//Platform geometric parameters
double X, Y, Z, phi, theta, psi;
double servo_arm = 18;
double servo_leg = 140;
double beta[6] = {
M_PI / 3,
-2 * M_PI / 3,
M_PI,
0,
5 * M_PI / 3,
2 * M_PI / 3}; //! Changes home calcs
double height = 155;
int FLAG = 0;
// Platform coordinates (top)
MatrixXd Platform_pos_zero{
{-41.4, 38.3, 0},
{-53.9, 16.7, 0},
{-12.5, -55, 0},
{12.5, -55, 0},
{53.9, 16.7, 0},
{41.4, 38.3, 0}};
Platform_pos_zero.transposeInPlace();
// Servo coordingates (base)
MatrixXd Servo_pos{
{-46.5, 74, 0},
{-90.5, 1.1, 0},
{-43.5, -81, 0},
{43.5, -81, 0},
{90.5, 1.14, 0},
{46.5, 74, 0}};
Servo_pos.transposeInPlace();
// Rotation matrix (R = Rz*Ry*Rx) used to take platform stuff to base frame
MatrixXd R_PB{
{cos(psi) * cos(theta), (-sin(psi) * cos(phi)) + (cos(psi) * sin(theta) * sin(phi)), (sin(psi) * sin(phi)) + (cos(psi) * sin(theta) * cos(phi))},
{sin(psi) * cos(theta), (cos(psi) * cos(phi)) + (sin(psi) * sin(theta) * sin(phi)), (-cos(psi) * sin(phi)) + (sin(psi) * sin(theta) * cos(phi))},
{-sin(theta), cos(theta) * sin(phi), cos(theta) * cos(phi)}
};
// Initialising to home position
while(FLAG == 0)
{
//Parameters (3 translational inputs and 3 rotational inputs)
X = Y = Z = phi = theta = psi = 0;
// Height of the platform when servo arm is perpendicular to leg
double h_0 = sqrt(std::pow(servo_leg,2) + std::pow(servo_arm,2)
- std::pow(Platform_pos_zero(0,0) - Servo_pos(0,0),2)
- std::pow(Platform_pos_zero(1,0) - Servo_pos(1,0),2))
- Platform_pos_zero(2,0);
// Platform points when zero rotation and translation
Eigen::Matrix<double, 3, 1> t_home;
t_home << 0, 0, h_0;
Eigen::Matrix<double, 3, 1> t_input;
t_input << X, Y, Z;
Eigen::Matrix<double, 3, 1> T;
T = t_home + t_input;
// Calculate platform's home position (New_pos)
MatrixXd Rotated_platform = R_PB * Platform_pos_zero;
MatrixXd New_pos = T.replicate<1, 6>().array() + Rotated_platform.array(); // q
// Calculate angle of the servo arm at home position
// First calculating the linear Leg length
MatrixXd lin_leg_lengths = New_pos - Servo_pos;
// The .colwise().norm() method calculates the Euclidean norm of each column,
// which corresponds to the length of each leg vector
MatrixXd virtual_leg_lengths = (lin_leg_lengths).colwise().norm();
// Due to platform symmetry, we can only consider the leg with 0 beta //!Are we symmetric?
double L_home = 2 * std::pow(servo_arm, 2);
double M_home = 2 * servo_arm * (New_pos(0,0) - Servo_pos(0,0));
double N_home = 2 * servo_arm * (h_0 + New_pos(2,3));
double alpha_home = asin(L_home/sqrt(pow(M_home,2)+pow(N_home,2))) - atan(M_home/N_home);
double alpha_home_deg = rad2deg*alpha_home;
// Let's try to workout the servo arm/leg join positions
// For writing simulation outputs to CSV file
std::fstream file;
file << std::fixed << std::setprecision(3); // set precision to 3 decimal places
file.open("data.csv", std::ios::out);
// Let's save it all in a CSV
New_pos.transposeInPlace();
for (size_t i = 0; i<6; i++)
{
for (size_t j = 0; j<3; j++)
{
file << New_pos(i,j);
file << ";";
}
file << "\n";
}
//Update knee position
Eigen::Matrix<double,3,6> Knee_pos_home;
for (size_t j=0; j<6; j++)
{
Knee_pos_home(0,j) = servo_arm*cos(alpha_home)*cos(beta[j]) + Servo_pos(0,j);
Knee_pos_home(1,j) = servo_arm*cos(alpha_home)*sin(beta[j]) + Servo_pos(1,j);
Knee_pos_home(2,j) = servo_arm*sin(alpha_home) + Servo_pos(2,j);
}
// Updating CSV file with new knee position information
MatrixXd Knee_pos_csv = Knee_pos_home;
Knee_pos_csv.transposeInPlace();
for (size_t i = 0; i<6; i++)
{
for (size_t j = 0; j<3; j++)
{
file << Knee_pos_csv(i,j);
file << ";";
}
file << "\n";
}
file.close();
FLAG = 1;
};
}
void MainWindow::on_pushButton_clicked()
{
double servo_arm = 18;
double servo_leg = 140;
double beta[6] = {
M_PI / 3,
-2 * M_PI / 3,
M_PI,
0,
5 * M_PI / 3,
2 * M_PI / 3};
double height = 155;
// Platform coordinates (top)
MatrixXd Platform_pos_zero{
{-41.4, 38.3, 0},
{-53.9, 16.7, 0},
{-12.5, -55, 0},
{12.5, -55, 0},
{53.9, 16.7, 0},
{41.4, 38.3, 0}};
Platform_pos_zero.transposeInPlace();
// SServo coordingates (base)
MatrixXd Servo_pos{
{-46.5, 74, 0},
{-90.5, 1.1, 0},
{-43.5, -81, 0},
{43.5, -81, 0},
{90.5, 1.14, 0},
{46.5, 74, 0}};
Servo_pos.transposeInPlace();
// Rotation matrix (R = Rz*Ry*Rx) used to take platform stuff to base frame
MatrixXd R_PB{
{cos(psi) * cos(theta), (-sin(psi) * cos(phi)) + (cos(psi) * sin(theta) * sin(phi)), (sin(psi) * sin(phi)) + (cos(psi) * sin(theta) * cos(phi))},
{sin(psi) * cos(theta), (cos(psi) * cos(phi)) + (sin(psi) * sin(theta) * sin(phi)), (-cos(psi) * sin(phi)) + (sin(psi) * sin(theta) * cos(phi))},
{-sin(theta), cos(theta) * sin(phi), cos(theta) * cos(phi)}
};
double FLAG = 1;
// When platform is moving
while(FLAG == 1)
{
double h_0 = sqrt(std::pow(servo_leg,2) + std::pow(servo_arm,2)
- std::pow(Platform_pos_zero(0,0) - Servo_pos(0,0),2)
- std::pow(Platform_pos_zero(1,0) - Servo_pos(1,0),2))
- Platform_pos_zero(2,0);
//watch(h_0);//debug
// Platform points when zero rotation and translation
Eigen::Matrix<double, 3, 1> t_home;
t_home << 0, 0, h_0;
Eigen::Matrix<double, 3, 1> t_input;
t_input << X, Y, Z;
qDebug() << "X here is :" << X;
Eigen::Matrix<double, 3, 1> T;
T = t_home + t_input;
// Rotation matrix (R = Rz*Ry*Rx) used to take platform stuff to base frame
MatrixXd R_PB{
{cos(psi) * cos(theta), (-sin(psi) * cos(phi)) + (cos(psi) * sin(theta) * sin(phi)), (sin(psi) * sin(phi)) + (cos(psi) * sin(theta) * cos(phi))},
{sin(psi) * cos(theta), (cos(psi) * cos(phi)) + (sin(psi) * sin(theta) * sin(phi)), (-cos(psi) * sin(phi)) + (sin(psi) * sin(theta) * cos(phi))},
{-sin(theta), cos(theta) * sin(phi), cos(theta) * cos(phi)}
};
// Calculate platform's transformed position(New_pos)
MatrixXd Rotated_platform = R_PB * Platform_pos_zero;
MatrixXd New_pos = T.replicate<1, 6>().array() + Rotated_platform.array(); // q
// Calculate angle of the servo arm at NEW position
// First find the linear Leg length
MatrixXd lin_leg_lengths = New_pos - Servo_pos;
// The .colwise().norm() method calculates the Euclidean norm of each column,
// which corresponds to the length of each leg vector
MatrixXd virtual_leg_lengths = (lin_leg_lengths).colwise().norm();
watch(virtual_leg_lengths);
// Calculating the servo angles for each leg
Eigen::Matrix<double,1,6> L;
for (size_t i=0;i<6;i++)
{
L(0,i) = std::pow(virtual_leg_lengths(0,i),2)
- ((std::pow(servo_leg,2)) - (std::pow(servo_arm,2)));
}
Eigen::Matrix<double,1,6> M;
for (size_t i=0;i<6;i++)
{
M(0,i) = 2*servo_arm*(New_pos(2,i)-Servo_pos(2,i));
}
Eigen::Matrix<double,1,6> N;
for (size_t i=0;i<6;i++)
{
double x_diff = New_pos(0,i) - Servo_pos(0,i); // intermediate calc
double y_diff = New_pos(1,i) - Servo_pos(1,i);
N(0,i) = 2*servo_arm*((cos(beta[i])*x_diff)+(sin(beta[i])*y_diff));
}
// Now we can calculate the servo angles
Eigen::Matrix<double,1,6>alpha;
Eigen::Matrix<double,1,6>servo_deg;
for (size_t i = 0; i <6; i++)
{
alpha(0,i) = asin(L(0,i) / sqrt(std::pow(M(0,i), 2) + std::pow(N(0,i), 2))) - atan(N(0,i) / M(0,i));
servo_deg(0,i) = rad2deg*(alpha[i]);
}
// Updating CSV file
std::fstream file;
file << std::fixed << std::setprecision(3); // set precision to 3 decimal places
file.open("data.csv", std::ios::out);
// Let's save it all in a CSV
New_pos.transposeInPlace();
for (size_t i = 0; i<6; i++)
{
for (size_t j = 0; j<3; j++)
{
file << New_pos(i,j);
file << ";";
}
file << "\n";
}
//Changed knee position
Eigen::Matrix<double,3,6> Knee_pos_new;
for (size_t j=0; j<6; j++)
{
Knee_pos_new(0,j) = servo_arm*cos(alpha[j])*cos(beta[j]) + Servo_pos(0,j);
Knee_pos_new(1,j) = servo_arm*cos(alpha[j])*sin(beta[j]) + Servo_pos(1,j);
Knee_pos_new(2,j) = servo_arm*sin(alpha[j]) + Servo_pos(2,j);
}
// For CSV
MatrixXd Knee_pos_csv = Knee_pos_new;
Knee_pos_csv.transposeInPlace();
for (size_t i = 0; i<6; i++)
{
for (size_t j = 0; j<3; j++)
{
file << Knee_pos_csv(i,j);
file << ";";
}
file << "\n";
}
file.close();
FLAG = 0;
};
}
void MainWindow::on_pushButton_2_clicked()
{
QApplication::quit();
}
void MainWindow::on_Slider_X_valueChanged(int value)
{
double X = 1.0*value;//(ui->Slider_X->value());
qDebug() << "X value changed to" << X;
setX(X);
on_pushButton_clicked();
}
void MainWindow::on_Slider_Y_valueChanged(int value)
{
double Y = 1.0*value;//(ui ->Slider_Y ->value());
setY(Y);
on_pushButton_clicked();
}
void MainWindow::on_Slider_Z_valueChanged(int value)
{
double Z = 1.0*value;//(ui ->Slider_Z -> value());
setZ(Z);
on_pushButton_clicked();
}
void MainWindow::on_Slider_PHI_valueChanged(int value)
{
double phi = deg2rad*value;//(ui->Slider_PHI ->value()) ;
setPHI(phi);
on_pushButton_clicked();
}
void MainWindow::on_Slider_THETA_valueChanged(int value)
{
double theta = deg2rad*value;//(ui ->Slider_THETA ->value());
setTHETA(theta);
on_pushButton_clicked();
}
void MainWindow::on_Slider_PSI_valueChanged(int value)
{
double psi = deg2rad*value;//(ui ->Slider_PSI -> value());
setPSI(psi);
on_pushButton_clicked();
}
void MainWindow::setX(double value)
{
X = value;
}
void MainWindow::setY(double value)
{
Y = value;
}
void MainWindow::setZ(double value)
{
Z = value;
}
void MainWindow::setPHI(double value)
{
phi = value;
}
void MainWindow::setTHETA(double value)
{
theta = value;
}
void MainWindow::setPSI(double value)
{
psi = value;
}