-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
552 lines (401 loc) · 15.9 KB
/
mainwindow.cpp
File metadata and controls
552 lines (401 loc) · 15.9 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include "mainwindow.h"
#include "ui_mainwindow.h"
const int buttonPin=4;
const int one_motor_rotate=16384;
const int one_body_degree=7281;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
wiringPiSetup();
pinMode(buttonPin,INPUT);
pullUpDnControl(buttonPin,PUD_UP);
ch_serialport = new CHSerialPort(nullptr);
//get data from ch_serialport class
// connect(ch_serialport, SIGNAL(errorOpenPort()), this, SLOT(geterrorOpenPort()));
// connect(ch_serialport, SIGNAL(sigPortOpened()), this, SLOT(getsigPortOpened()));
// connect(ch_serialport, SIGNAL(sigPortClosed()), this, SLOT(getsigPortClosed()));
connect(ch_serialport, SIGNAL(sigSendIMU(receive_imusol_packet_t)),
this, SLOT(getIMUData(receive_imusol_packet_t)), Qt::QueuedConnection);
//link imu
ch_serialport->linkCHdevices("/dev/ttyUSB0",115200);
//motor
QString str_error;
enc_pos=new int();
m_motor=new MaxonMotor();
bool motor_connected = m_motor->openDevice(&str_error);
bool motor_set = m_motor->setmotor_Operationmode(&str_error);
bool enable_state = m_motor->setEnabled(&str_error);
ui->spinBox->setRange(-one_motor_rotate*1000,one_motor_rotate*1000);
encoder_timer = new QTimer(this);
connect(encoder_timer, SIGNAL(timeout()) ,this, SLOT(getpos()));
encoder_timer->setInterval(10);
encoder_timer->start();
//svm
m_IMUsvm=new IMUsvm();
svm_trainer_timer = new QTimer(this);
connect(svm_trainer_timer, SIGNAL(timeout()) ,this, SLOT(collectTrainingData()));
svm_trainer_timer->setInterval(10);
//start au assist
AIassistant_timer = new QTimer(this);
connect(AIassistant_timer, SIGNAL(timeout()) ,this, SLOT(controlMachine()));
AIassistant_timer->setInterval(10);
}
MainWindow::~MainWindow()
{
//close imu port and thread
ch_serialport->closePort();
QString str_error;
bool disable_state = m_motor->setDisabled(&str_error);
m_motor->closeAllDevice();
delete ui;
}
void MainWindow::getIMUData(receive_imusol_packet_t imu_data)
{
static QList<float> imu_roll_3mean;
static QList<float> imu_gyrX_3mean;
imu_roll_3mean.append(imu_data.eul[0]);
imu_gyrX_3mean.append(imu_data.acc[0]);
if(imu_roll_3mean.length()>3){
imu_roll_3mean.pop_front();
imu_gyrX_3mean.pop_front();
}
imu_roll=(imu_roll_3mean.at(0)+imu_roll_3mean.at(1)+imu_roll_3mean.at(2))/3.0f;
imu_gyrX=(imu_gyrX_3mean.at(0)+imu_gyrX_3mean.at(1)+imu_gyrX_3mean.at(2))/3.0f;
raw_imu_data=imu_data;
}
void MainWindow::collectTrainingData()
{
static QStringList motion_data;
static int windowsize_counter=0;
static std::vector<double> vec_accX,vec_accY,vec_accZ,
vec_gyrX,vec_gyrY,vec_roll,vec_pitch;
const uint training_amount=12;
uint num_motion=motion_data.length();
//define bending area of angles
bool bending_area=(imu_roll<75 &&imu_roll>0);
if(bending_area){
//true until motion.data.length() >= training amount
if(windowsize_counter>=0 && windowsize_counter<=100){
vec_accX.push_back(raw_imu_data.acc[0]);
vec_accY.push_back(raw_imu_data.acc[1]);
vec_accZ.push_back(raw_imu_data.acc[2]);
vec_gyrX.push_back(raw_imu_data.gyr[0]);
vec_gyrY.push_back(raw_imu_data.gyr[1]);
vec_roll.push_back(raw_imu_data.eul[0]);
vec_pitch.push_back(raw_imu_data.eul[1]);
windowsize_counter++;
}else if(windowsize_counter>100){
int label=0;
if(num_motion<training_amount/2)
label=1;//fast motion
else if(num_motion>=training_amount/2 && num_motion<training_amount)
label=0;//lifting motion
double std_accX=stddev(vec_accX);
double std_accY=stddev(vec_accY);
double std_accZ=stddev(vec_accZ);
double std_gyrX=stddev(vec_gyrX);
double std_gyrY=stddev(vec_gyrY);
double std_roll=stddev(vec_roll);
double std_pitch=stddev(vec_pitch);
QString a_row_data=QString("%1,%2,%3,%4,%5,%6,%7,%8\n").arg(label).arg(QString::number(std_accX, 'f', 3))
.arg(QString::number(std_accY, 'f', 3)).arg(QString::number(std_accZ, 'f', 3))
.arg(QString::number(std_gyrX, 'f', 3)).arg(QString::number(std_gyrY, 'f', 3))
.arg(QString::number(std_roll, 'f', 3)).arg(QString::number(std_pitch, 'f', 3));
motion_data.append(a_row_data);
ui->textBrowser->append(tr("A row %1 has been recorded: %2").arg(num_motion).arg(a_row_data));
//stop pushing new data until leaving bending area;
windowsize_counter=-1;}
}else{//not in bending area
//restart collect a window of data
if(windowsize_counter!=0){
windowsize_counter=0;
vec_accX.clear();
vec_accY.clear();
vec_accZ.clear();
vec_gyrX.clear();
vec_gyrY.clear();
vec_roll.clear();
vec_pitch.clear();}}
if(num_motion>=training_amount){
bool err=saveData2CSV(motion_data, person_name);
if(err){
ui->textBrowser->append(person_name+".csv can't be opened");
}else{
bool err1=m_IMUsvm->trainSVM(person_name);
if(err1){
ui->textBrowser->append("train svm error!");
}else{
ui->textBrowser->append(person_name+".model"+" is trained");}
}
//stop training
windowsize_counter=0;
vec_accX.clear();
vec_accY.clear();
vec_accZ.clear();
vec_gyrX.clear();
vec_gyrY.clear();
vec_roll.clear();
vec_pitch.clear();
motion_data.clear();
svm_trainer_timer->stop();}
}
bool MainWindow::saveData2CSV(QStringList data, QString person_profile)
{
QFile file(person_profile+".csv");
if (!file.open(QFile::WriteOnly | QFile::Truncate |QIODevice::Append)) {
qDebug() << file.errorString();
return 1;
}else{
QTextStream stream(&file);
for (int i=0; i<data.length(); i++) {
stream << data.at(i);
}}
file.close();
return 0;
}
int MainWindow::collectPredictionData()
{
static int windowsize_counter=0;
static std::vector<double> vec_accX,vec_accY,vec_accZ,
vec_gyrX,vec_gyrY,vec_roll,vec_pitch;
//define bending area of angles
bool bending_area=(imu_roll<75 &&imu_roll>-90);
if(bending_area){
if(windowsize_counter>=0 && windowsize_counter<=100){
vec_accX.push_back(raw_imu_data.acc[0]);
vec_accY.push_back(raw_imu_data.acc[1]);
vec_accZ.push_back(raw_imu_data.acc[2]);
vec_gyrX.push_back(raw_imu_data.gyr[0]);
vec_gyrY.push_back(raw_imu_data.gyr[1]);
vec_roll.push_back(raw_imu_data.eul[0]);
vec_pitch.push_back(raw_imu_data.eul[1]);
windowsize_counter++;
}else if(windowsize_counter>100){
double std_accX=stddev(vec_accX);
double std_accY=stddev(vec_accY);
double std_accZ=stddev(vec_accZ);
double std_gyrX=stddev(vec_gyrX);
double std_gyrY=stddev(vec_gyrY);
double std_roll=stddev(vec_roll);
double std_pitch=stddev(vec_pitch);
QString a_row_data=QString("%1,%2,%3,%4,%5,%6,%7").arg(QString::number(std_accX, 'f', 3))
.arg(QString::number(std_accY, 'f', 3)).arg(QString::number(std_accZ, 'f', 3))
.arg(QString::number(std_gyrX, 'f', 3)).arg(QString::number(std_gyrY, 'f', 3))
.arg(QString::number(std_roll, 'f', 3)).arg(QString::number(std_pitch, 'f', 3));
int rst=m_IMUsvm->svmPredict(a_row_data, ui->line_person_name->text());
windowsize_counter=-1;
return rst;}
}else{//not in bending area
//restart collect a window of data
if(windowsize_counter!=0){
windowsize_counter=0;
vec_accX.clear();
vec_accY.clear();
vec_accZ.clear();
vec_gyrX.clear();
vec_gyrY.clear();
vec_roll.clear();
vec_pitch.clear();}}
return -2;//normally ignoring
}
void MainWindow::controlMachine()
{
/*
* timer run this loop every 10ms
* (while bend to lowest)-90< imu_roll <90(while standing)
* imu_gyrX>0 while lifting, imu_gyrX<0 while bending
*/
//define bending area of angles
bool bending_area=(imu_roll<75 &&imu_roll>-90);
ui->label_imuroll->setText(tr("Roll=%1").arg(imu_roll));
static int counter_for_lockmode;
switch (this->control_state) {
case 0:{
/*rst=-2 while not in bending area or haven't receive a window of datas(1000ms)
*When user enter into bending area "<75 degrees", it'll collect a window of datas(1000ms) to predict motion.
*rst=-1 if couldn't find model
*When a window of datas(1000ms) is collected, rst will ==0 or 1, which is the prediction result.
*
*/
int rst=collectPredictionData();
if(rst==-1){
ui->textBrowser->append(ui->line_person_name->text()+".model is not exists");
}else if(rst==0){//change to lifting motion
ui->textBrowser->append(tr("go to lifting"));
control_state=1;
}else if(rst==1){//user picking fast, doesn't need motor assistant, so it change to lock detection mode.
ui->textBrowser->append(tr("go to lock detection"));
control_state=2;
}else{
control_state=0;}
break;}
case 1:{//enter into motor lifting state
if(bending_area){
//send signal once. It'll shorten the wire, help lifting
static short insideloop=0;
//wait 1 sec to start assistant
if(insideloop>100){
//only rotate while body up
if(imu_gyrX>10)
rotateToBodyDegree(imu_roll);
else{
insideloop=0;
this->control_state=2;
}
} else {insideloop++;
}
//change to lock detection mode.
}else{
//release all wire to free mode
rotateToBodyDegree(-100);
this->control_state=0;
counter_for_lockmode=0;}
break;}
case 2:{//lock detection mode. check if body is static.
if(bending_area){
//while user keep body at a posture for more than 2000ms, will change state into lock mode.
if(abs(imu_gyrX)<10){
counter_for_lockmode++;
}else{
counter_for_lockmode=0;}
if(counter_for_lockmode>500){
this->control_state=3;}
}else{//not in bending area
//release all wire to free mode
rotateToBodyDegree(-100);
this->control_state=0;
counter_for_lockmode=0;}
break;}
case 3:{//enter into lock mode
static bool islocked=false;
if(bending_area){
if(!islocked){
rotateToBodyDegree(imu_roll);
islocked=true;
ui->textBrowser->append(tr("lock? %1").arg(islocked));
}
}else{
//release all wire to free mode
islocked=false;
rotateToBodyDegree(-100);
this->control_state=0;
counter_for_lockmode=0;
ui->textBrowser->append(tr("lock? %1").arg(islocked));}
break;}
default:{
//release all wire to free mode
rotateToBodyDegree(-100);
this->control_state=0;
counter_for_lockmode=0;
break;}}
}
int MainWindow::rotateToBodyDegree(int degree)
{
//((standing angle)-imu_roll)/360 *(one_motor_rotate of encoder)/(reduction ratio=1/160)))
int imu_pos=round(max_pos_limit-(75-degree)*one_body_degree);
if(imu_pos>max_pos_limit){
imu_pos=max_pos_limit-one_motor_rotate;
}else if(imu_pos<min_pos_limit){
imu_pos=min_pos_limit+one_motor_rotate;
}
//*pos=imu_pos;
ui->spinBox->setValue(imu_pos);
m_motor->moveToPosition(imu_pos);
ui->textBrowser->append(tr("move to %1").arg(degree));
return 0;
}
double MainWindow::stddev(std::vector<double> const & func)
{
double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
[](double const & x, double const & y) { return x + y; },
[mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
return sqrt(sq_sum / func.size());
}
void MainWindow::getpos()
{
bool err_handle; //1 if success, 0 if error
int btn_state=2;
btn_state=digitalRead(buttonPin);
//qDebug()<<btn_state;
ui->label_limitbtn->setNum(btn_state);
err_handle=m_motor->getPosition(enc_pos);
ui->label_enc_pos->setText(tr("Current Pos= %1").arg(*enc_pos));
if(err_handle){
//ui->label_limitbtn->setText("Motor Reach Up Limitation!");
if(max_pos_limit<*enc_pos){
// if(m_motor->haltMotor()){
// qDebug()<<"halt success";
// }
//ui->textBrowser->append("too big");
//m_motor->moveToPosition(max_pos_limit-one_motor_rotate);
}
if(min_pos_limit>*enc_pos){
//ui->textBrowser->append("too small");
//m_motor->moveToPosition(min_pos_limit+one_motor_rotate);
}
}
}
void MainWindow::motorCalibrate()
{
while(true){
bool err_handle; //1 if success, 0 if error
err_handle=m_motor->getPosition(enc_pos);
int btn_state=2;
btn_state=digitalRead(buttonPin);
if(err_handle){
if(btn_state!=1){
//ui->label_limitbtn->setText("Motor Reach Up Limitation!");
if(m_motor->haltMotor()){
qDebug()<<"halt success";}
//max_pos_limit:the wire is shortest, min_pos_limit:the wire is released
//while reach max, reset the max_pos_limit, and move it to see if it's still touch limitation btn
min_pos_limit=*enc_pos+one_body_degree*15;
//the free position is: a degree=16384*160(reduction ratio)/360, we set to 200 bending degrees of body.
max_pos_limit=min_pos_limit+one_motor_rotate*160.0f/360.0f*160.0f;
ui->label_pos_limitation->setText(tr("MaxPos=%1,MinPos=%2").arg(max_pos_limit).arg(min_pos_limit));
m_motor->moveToPosition(min_pos_limit+one_motor_rotate);
break;
}else{
//ui->label_limitbtn->setText("Motor is In Save Area");
*enc_pos-=one_motor_rotate;
ui->spinBox->setValue(*enc_pos);
m_motor->moveToPosition(*enc_pos);}}}
}
void MainWindow::on_btn_train_clicked()
{
person_name=ui->line_person_name->text();
svm_trainer_timer->start();
}
void MainWindow::on_btn_AIassistant_clicked()
{
if(!AIassistant_timer->isActive()){
AIassistant_timer->start();
ui->btn_AIassistant->setText("AI is working");
}else{
AIassistant_timer->stop();
//release wire
m_motor->moveToPosition(min_pos_limit);
ui->btn_AIassistant->setText("AI is sleeping");}
}
void MainWindow::on_btn_manual_setpos_clicked()
{
long pos = ui->spinBox->value();
// pos = 12800*(10/360)*160*(72/102);
m_motor->moveToPosition(pos);
}
void MainWindow::on_btn_up_clicked()
{
ui->spinBox->setValue(ui->spinBox->value()+one_motor_rotate);
}
void MainWindow::on_btn_down_clicked()
{
ui->spinBox->setValue(ui->spinBox->value()-one_motor_rotate);
}
void MainWindow::on_btn_calimotor_clicked()
{
motorCalibrate();
//rotateToBodyDegree(-100);
}