-
Notifications
You must be signed in to change notification settings - Fork 193
/
partitem.cpp
2157 lines (1832 loc) · 68.9 KB
/
partitem.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
* Copyright (C) 2015 iCub Facility - Istituto Italiano di Tecnologia
* Authors: Marco Randazzo <marco.randazzo@iit.it>
* Francesco Nori <francesco.nori@iit.it>
* Davide Perrone <dperrone@aitek.it>
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include "partitem.h"
#include "log.h"
#include <yarp/os/all.h>
#include <yarp/dev/Drivers.h>
#include <yarp/dev/PolyDriver.h>
#include <QDebug>
#include <QEvent>
#include <QResizeEvent>
#include <QFileDialog>
#include <QXmlStreamWriter>
#include <QXmlStreamAttribute>
#include <QMessageBox>
#include <QSettings>
#include <cmath>
PartItem::PartItem(QString robotName, int id, QString partName, ResourceFinder *finder,
bool debug_param_enabled,
bool speedview_param_enabled,
bool enable_calib_all, QWidget *parent) : QWidget(parent)
{
layout = new FlowLayout();
setLayout(layout);
slow_k = 0;
partId = id;
this->finder = NULL;
node = NULL;
currentPidDlg = NULL;
sequenceWindow = NULL;
this->finder = finder;
this->partName = partName;
mixedEnabled = false;
positionDirectEnabled = false;
openloopEnabled = false;
//PolyDriver *cartesiandd[MAX_NUMBER_ACTIVATED];
QString robotPartPort = QString("/%1/%2").arg(robotName).arg(partName);
QString robotPartDebugPort = QString("/%1/debug/%2").arg(robotName).arg(partName);
//checking existence of the port
int ind = 0;
QString portLocalName = QString("/%1/yarpmotorgui%2/%3").arg(robotName).arg(ind).arg(partName);
QString nameToCheck = portLocalName;
nameToCheck += "/rpc:o";
// NameClient &nic=NameClient::getNameClient();
yDebug("Checking the existence of: %s \n", nameToCheck.toLatin1().data());
// Address adr=nic.queryName(nameToCheck.c_str());
Contact adr=Network::queryName(nameToCheck.toLatin1().data());
//Contact c = yarp::os::Network::queryName(portLocalName.c_str());
yDebug("ADDRESS is: %s \n", adr.toString().c_str());
while(adr.isValid()){
ind++;
portLocalName = QString("/%1/yarpmotorgui%2/%3").arg(robotName).arg(ind).arg(partName);;
nameToCheck = portLocalName;
nameToCheck += "/rpc:o";
// adr=nic.queryName(nameToCheck.c_str());
adr=Network::queryName(nameToCheck.toLatin1().data());
}
interfaceError = false;
// Initializing the polydriver options and instantiating the polydrivers
partOptions.put( "local", portLocalName.toLatin1().data());
partOptions.put( "device", "remote_controlboard");
partOptions.put( "remote", robotPartPort.toLatin1().data());
partOptions.put("carrier", "udp");
partsdd = new PolyDriver();
// Opening the drivers
interfaceError = !openPolyDrivers();
if (interfaceError==true)
{
yError("Opening PolyDriver for part %s failed...", robotPartPort.toLatin1().data());
QMessageBox::critical(0,"Error opening a device", QString("Error while opening device for part ").append(robotPartPort.toLatin1().data()));
}
/*********************************************************************/
/**************** PartMover Content **********************************/
if (!finder->isNull()){
yDebug("Setting a valid finder \n");
}
QString sequence_portname = QString("/yarpmotorgui/%1/sequence:o").arg(partName);
sequence_port.open(sequence_portname.toLatin1().data());
initInterfaces();
openInterfaces();
// COPY_STORED_POS=0;
// COPY_STORED_VEL=0;
// COPY_SEQUENCE=0;
// COPY_TIMING=0;
if (interfaceError == false)
{
// yDebug("Allocating memory \n");
// STORED_POS = new double* [NUMBER_OF_STORED];
// STORED_VEL = new double* [NUMBER_OF_STORED];
// CURRENT_POS_UPDATE = new bool [NUMBER_OF_STORED];
// int j,k;
// for (j = 0; j < NUMBER_OF_STORED; j++){
// CURRENT_POS_UPDATE[j] = true;
// STORED_POS[j] = new double [MAX_NUMBER_OF_JOINTS];
// STORED_VEL[j] = new double [MAX_NUMBER_OF_JOINTS];
// }
// SEQUENCE = new int [NUMBER_OF_STORED];
// INV_SEQUENCE = new int [NUMBER_OF_STORED];
// TIMING = new double [NUMBER_OF_STORED];
// index = new int [MAX_NUMBER_OF_JOINTS];
// SEQUENCE_ITERATOR = new int [0];
// timeout_seqeunce_id = new uint [0];
// entry_id = new uint [0];
// *entry_id = -1;
// timeout_seqeunce_rate = new uint [0];
// for (j = 0; j < NUMBER_OF_STORED; j++){
// SEQUENCE[j] = -1;
// TIMING[j] = -0.1;
// }
double positions[MAX_NUMBER_OF_JOINTS];
bool ret=false;
Time::delay(0.050);
do {
ret=iencs->getEncoders(positions);
if (!ret) {
yError("%s iencs->getEncoders() failed, retrying...\n", partName.toLatin1().data());
Time::delay(0.050);
}
}
while (!ret);
yInfo("%s iencs->getEncoders() ok!\n", partName.toLatin1().data());
double min_pos = 0;
double max_pos = 100;
double min_vel = 0;
double max_vel = 100;
yarp::os::ConstString jointname;
//char buffer[40] = {'i', 'n', 'i', 't'};
int NUMBER_OF_JOINTS;
iPos->getAxes(&NUMBER_OF_JOINTS);
for (int k = 0; k<NUMBER_OF_JOINTS; k++)
{
// //init stored
// for(j = 0; j < NUMBER_OF_STORED; j++){
// STORED_POS[j][k] = 0.0;
// }
//index[k]=k;
bool bpl = iLim->getLimits(k, &min_pos, &max_pos);
bool bvl = iLim->getVelLimits(k, &min_vel, &max_vel);
if (bpl == false)
{
yError() << "Error while getting position limits, part " << partName.toStdString() << " joint " << k;
}
if (bvl == false || (min_vel == 0 && max_vel == 0))
{
yError() << "Error while getting velocity limits, part " << partName.toStdString() << " joint " << k;
}
QSettings settings("YARP", "yarpmotorgui");
double max_slider_vel = settings.value("velocity_slider_limit", 100.0).toDouble();
if (max_vel > max_slider_vel) max_vel = max_slider_vel;
iinfo->getAxisName(k, jointname);
yarp::dev::JointTypeEnum jtype = yarp::dev::VOCAB_JOINTTYPE_REVOLUTE;
bool bjt = iinfo->getJointType(k, jtype);
Pid myPid(0,0,0,0,0,0);
yarp::os::Time::delay(0.005);
iPid->getPid(k, &myPid);
JointItem *joint = new JointItem(k);
joint->setJointName(jointname.c_str());
joint->setOpenLoopRange(-myPid.max_output,myPid.max_output);
layout->addWidget(joint);
joint->setPositionRange(min_pos, max_pos);
joint->setVelocityRange(min_vel, max_vel);
joint->setTrajectoryVelocityRange(max_vel);
joint->setTorqueRange(5.0);
joint->setUnits(jtype);
joint->enableControlPositionDirect(positionDirectEnabled);
joint->enableControlMixed(mixedEnabled);
joint->enableControlOpenloop(openloopEnabled);
int val_pos_choice = settings.value("val_pos_choice", 0).toInt();
int val_trq_choice = settings.value("val_trq_choice", 0).toInt();
int val_vel_choice = settings.value("val_vel_choice", 0).toInt();
double val_pos_custom_step = settings.value("val_pos_custom_step", 1.0).toDouble();
double val_trq_custom_step = settings.value("val_trq_custom_step", 1.0).toDouble();
double val_vel_custom_step = settings.value("val_vel_custom_step", 1.0).toDouble();
onSetPosSliderOptionPI(val_pos_choice, val_pos_custom_step);
onSetVelSliderOptionPI(val_vel_choice, val_vel_custom_step);
onSetTrqSliderOptionPI(val_trq_choice, val_trq_custom_step);
joint->setEnabledOptions(debug_param_enabled,
speedview_param_enabled,
enable_calib_all);
connect(joint, SIGNAL(changeMode(int,JointItem*)), this, SLOT(onJointChangeMode(int,JointItem*)));
connect(joint, SIGNAL(changeInteraction(int,JointItem*)), this, SLOT(onJointInteraction(int,JointItem*)));
connect(joint, SIGNAL(sliderTrajectoryPositionCommand(double, int)), this, SLOT(onSliderTrajectoryPositionCommand(double, int)));
connect(joint, SIGNAL(sliderTrajectoryVelocityCommand(double, int)), this, SLOT(onSliderTrajectoryVelocityCommand(double, int)));
connect(joint, SIGNAL(sliderMixedPositionCommand(double, int)), this, SLOT(onSliderMixedPositionCommand(double, int)));
connect(joint, SIGNAL(sliderMixedVelocityCommand(double, int)), this, SLOT(onSliderMixedVelocityCommand(double, int)));
connect(joint, SIGNAL(sliderTorqueCommand(double, int)), this, SLOT(onSliderTorqueCommand(double, int)));
connect(joint, SIGNAL(sliderDirectPositionCommand(double, int)), this, SLOT(onSliderDirectPositionCommand(double, int)));
connect(joint, SIGNAL(sliderOpenloopCommand(double, int)), this, SLOT(onSliderOpenloopCommand(double, int)));
connect(joint, SIGNAL(sliderVelocityCommand(double, int)), this, SLOT(onSliderVelocityCommand(double, int)));
connect(joint, SIGNAL(homeClicked(JointItem*)),this,SLOT(onHomeClicked(JointItem*)));
connect(joint, SIGNAL(idleClicked(JointItem*)),this,SLOT(onIdleClicked(JointItem*)));
connect(joint, SIGNAL(runClicked(JointItem*)),this,SLOT(onRunClicked(JointItem*)));
connect(joint, SIGNAL(pidClicked(JointItem*)),this,SLOT(onPidClicked(JointItem*)));
connect(joint, SIGNAL(calibClicked(JointItem*)),this,SLOT(onCalibClicked(JointItem*)));
}
}
/*********************************************************************/
/*********************************************************************/
cycleTimer.setSingleShot(true);
cycleTimer.setTimerType(Qt::PreciseTimer);
connect(&cycleTimer,SIGNAL(timeout()),this,SLOT(onCycleTimerTimeout()),Qt::QueuedConnection);
cycleTimeTimer.setSingleShot(true);
cycleTimeTimer.setTimerType(Qt::PreciseTimer);
connect(&cycleTimeTimer,SIGNAL(timeout()),this,SLOT(onCycleTimeTimerTimeout()),Qt::QueuedConnection);
runTimeTimer.setSingleShot(true);
runTimeTimer.setTimerType(Qt::PreciseTimer);
connect(&runTimeTimer,SIGNAL(timeout()),this,SLOT(onRunTimerTimeout()),Qt::QueuedConnection);
runTimer.setSingleShot(true);
runTimer.setTimerType(Qt::PreciseTimer);
connect(&runTimer,SIGNAL(timeout()),this,SLOT(onRunTimeout()),Qt::QueuedConnection);
}
PartItem::~PartItem()
{
disconnect(&runTimer,SIGNAL(timeout()),this,SLOT(onRunTimeout()));
runTimer.stop();
disconnect(&runTimeTimer,SIGNAL(timeout()),this,SLOT(onRunTimerTimeout()));
runTimeTimer.stop();
disconnect(&cycleTimer,SIGNAL(timeout()),this,SLOT(onCycleTimerTimeout()));
cycleTimer.stop();
disconnect(&cycleTimeTimer,SIGNAL(timeout()),this,SLOT(onCycleTimeTimerTimeout()));
cycleTimeTimer.stop();
if(sequenceWindow){
sequenceWindow->hide();
delete sequenceWindow;
}
for(int i=0;i<layout->count();i++){
JointItem *joint = (JointItem *)layout->itemAt(i)->widget();
if(joint){
disconnect(joint,SIGNAL(changeMode(int,JointItem*)), this, SLOT(onJointChangeMode(int,JointItem*)));
disconnect(joint,SIGNAL(changeInteraction(int,JointItem*)), this, SLOT(onJointInteraction(int,JointItem*)));
disconnect(joint,SIGNAL(homeClicked(JointItem*)),this,SLOT(onHomeClicked(JointItem*)));
disconnect(joint,SIGNAL(idleClicked(JointItem*)),this,SLOT(onIdleClicked(JointItem*)));
disconnect(joint,SIGNAL(runClicked(JointItem*)),this,SLOT(onRunClicked(JointItem*)));
disconnect(joint,SIGNAL(pidClicked(JointItem*)),this,SLOT(onPidClicked(JointItem*)));
disconnect(joint,SIGNAL(calibClicked(JointItem*)),this,SLOT(onCalibClicked(JointItem*)));
delete joint;
}
}
if(partsdd){
partsdd->close();
}
}
bool PartItem::openPolyDrivers()
{
partsdd->open(partOptions);
if (!partsdd->isValid()) {
return false;
}
#ifdef DEBUG_INTERFACE
if (debug_param_enabled)
{
debugdd->open(debugOptions);
if(!debugdd->isValid()){
yError("Problems opening the debug client!");
}
} else {
debugdd = NULL;
}
#endif
return true;
}
void PartItem::initInterfaces()
{
yDebug("Initializing interfaces...");
//default value for unopened interfaces
iPos = NULL;
iVel = NULL;
iVar = NULL;
iDir = NULL;
iencs = NULL;
iAmp = NULL;
iPid = NULL;
iOpl = NULL;
iTrq = NULL;
iImp = NULL;
iLim = NULL;
cal = NULL;
ctrlmode2 = NULL;
iinteract = NULL;
remCalib = NULL;
}
bool PartItem::openInterfaces()
{
yDebug("Opening interfaces...");
bool ok = false;
if (partsdd->isValid()) {
ok = partsdd->view(iPid);
if(!ok){
yError("...iPid was not ok...");
}
ok &= partsdd->view(iAmp);
if(!ok){
yError("...iAmp was not ok...");
}
ok &= partsdd->view(iPos);
if(!ok){
yError("...iPos was not ok...");
}
ok &= partsdd->view(iDir);
if(!ok){
yError("...posDirect was not ok...");
}
ok &= partsdd->view(iVel);
if(!ok){
yError("...iVel was not ok...");
}
ok &= partsdd->view(iLim);
if(!ok){
yError("...iLim was not ok...");
}
ok &= partsdd->view(iencs);
if(!ok){
yError("...enc was not ok...");
}
ok &= partsdd->view(cal);
if(!ok){
yError("...cal was not ok...");
}
ok &= partsdd->view(iTrq);
if(!ok){
yError("...trq was not ok...");
}
ok = partsdd->view(iOpl);
if(!ok){
yError("...opl was not ok...");
}
ok &= partsdd->view(iImp);
if(!ok){
yError("...imp was not ok...");
}
ok &= partsdd->view(ctrlmode2);
if(!ok){
yError("...ctrlmode2 was not ok...");
}
ok &= partsdd->view(iinteract);
if(!ok){
yError("...iinteract was not ok...");
}
//optional interfaces
if (!partsdd->view(iVar))
{
yError("...iVar was not ok...");
}
if (!partsdd->view(remCalib))
{
yError("...remCalib was not ok...");
}
if (!partsdd->view(iinfo))
{
yError("...axisInfo was not ok...");
}
if (!ok) {
yError("Error while acquiring interfaces!");
QMessageBox::critical(0,"Problems acquiring interfaces.","Check if interface is running");
interfaceError=true;
}
}
else
{
yError("Device driver was not valid!");
interfaceError=true;
}
return !interfaceError;
}
bool PartItem::getInterfaceError()
{
return interfaceError;
}
QString PartItem::getPartName()
{
return partName;
}
void PartItem::onSliderOpenloopCommand(double torqueVal, int index)
{
iOpl->setRefOutput(index, torqueVal);
}
void PartItem::onSliderVelocityCommand(double speedVal, int index)
{
iVel->velocityMove(index,speedVal);
}
void PartItem::onSliderTorqueCommand(double torqueVal, int index)
{
iTrq->setRefTorque(index, torqueVal);
}
void PartItem::onSliderTrajectoryVelocityCommand(double trajspeedVal, int index)
{
iPos->setRefSpeed(index, trajspeedVal);
}
void PartItem::onSliderDirectPositionCommand(double dirpos, int index)
{
int mode;
ctrlmode2->getControlMode(index, &mode);
if (mode == VOCAB_CM_POSITION_DIRECT)
{
iDir->setPosition(index, dirpos);
}
else
{
yWarning("Joint not in position direct mode so cannot send references");
}
}
void PartItem::onSliderTrajectoryPositionCommand(double posVal, int index)
{
int mode;
ctrlmode2->getControlMode(index, &mode);
if ( mode == VOCAB_CM_POSITION)
{
iPos->positionMove(index, posVal);
}
else
{
yWarning("Joint not in position mode so cannot send references");
}
}
void PartItem::onSliderMixedPositionCommand(double posVal, int index)
{
int mode;
ctrlmode2->getControlMode(index, &mode);
if ( mode == VOCAB_CM_MIXED)
{
iPos->positionMove(index, posVal);
}
else
{
LOG_ERROR("Joint not in mixed mode so cannot send references");
}
}
void PartItem::onSliderMixedVelocityCommand( double vel, int index)
{
int mode;
ctrlmode2->getControlMode(index, &mode);
if (mode == VOCAB_CM_MIXED)
{
iVel->velocityMove(index, vel);
}
else
{
LOG_ERROR("Joint not in mixed mode so cannot send references");
}
}
void PartItem::onJointInteraction(int interaction,JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
switch (interaction) {
case JointItem::Compliant:
yInfo("interaction mode of joint %d set to COMPLIANT", jointIndex);
iinteract->setInteractionMode(jointIndex, (yarp::dev::InteractionModeEnum) VOCAB_IM_COMPLIANT);
break;
case JointItem::Stiff:
yInfo("interaction mode of joint %d set to STIFF", jointIndex);
iinteract->setInteractionMode(jointIndex, (yarp::dev::InteractionModeEnum) VOCAB_IM_STIFF);
break;
default:
break;
}
}
void PartItem::onSendOpenLoop(int jointIndex, int openLoopVal)
{
double openloop_reference = 0;
double openloop_current_pwm = 0;
iOpl->setRefOutput(jointIndex, openLoopVal);
yarp::os::Time::delay(0.010);
iOpl->getRefOutput(jointIndex, &openloop_reference); //This is the reference reference
yarp::os::Time::delay(0.010);
iOpl->getOutput(jointIndex, &openloop_current_pwm); //This is the reak PWM output
if(currentPidDlg){
currentPidDlg->initOpenLoop(openloop_reference,openloop_current_pwm);
}
}
void PartItem::onSendStiffness(int jointIdex,double stiff,double damp,double force)
{
Q_UNUSED(force);
double stiff_val=0;
double damp_val=0;
double offset_val=0;
iImp->setImpedance(jointIdex, stiff, damp);
//imp->setImpedanceOffset(jointIdex, force);
yarp::os::Time::delay(0.005);
iImp->getImpedance(jointIdex, &stiff_val, &damp_val);
iImp->getImpedanceOffset(jointIdex, &offset_val);
//update the impedance limits
double stiff_max=0.0;
double stiff_min=0.0;
double damp_max=0.0;
double damp_min=0.0;
double off_max=0.0;
double off_min=0.0;
iImp->getCurrentImpedanceLimit(jointIdex, &stiff_min, &stiff_max, &damp_min, &damp_max);
iTrq->getTorqueRange(jointIdex, &off_min, &off_max);
if(currentPidDlg){
currentPidDlg->initStiffness(stiff_val,stiff_min,stiff_max,
damp_val,damp_min,damp_max,
offset_val,off_min,off_max);
}
}
void PartItem::onSendTorquePid(int jointIndex,Pid newPid,MotorTorqueParameters newTrqParam)
{
Pid myTrqPid(0,0,0,0,0,0);
yarp::dev::MotorTorqueParameters TrqParam;
iTrq->setTorquePid(jointIndex, newPid);
iTrq->setMotorTorqueParams(jointIndex, newTrqParam);
yarp::os::Time::delay(0.005);
iTrq->getTorquePid(jointIndex, &myTrqPid);
iTrq->getMotorTorqueParams(jointIndex, &TrqParam);
if(currentPidDlg){
currentPidDlg->initTorque(myTrqPid,TrqParam);
}
}
void PartItem::onSendPositionPid(int jointIndex,Pid newPid)
{
Pid myPosPid(0,0,0,0,0,0);
iPid->setPid(jointIndex, newPid);
yarp::os::Time::delay(0.005);
iPid->getPid(jointIndex, &myPosPid);
if(currentPidDlg){
currentPidDlg->initPosition(myPosPid);
}
}
void PartItem::onSendVelocityPid(int jointIndex, Pid newPid)
{
Pid myVelPid(0, 0, 0, 0, 0, 0);
iVel->setVelPid(jointIndex, newPid);
yarp::os::Time::delay(0.005);
iVel->getVelPid(jointIndex, &myVelPid);
if (currentPidDlg){
currentPidDlg->initVelocity(myVelPid);
}
}
void PartItem::onRefreshPids(int jointIndex)
{
Pid myPosPid(0, 0, 0, 0, 0, 0);
Pid myTrqPid(0, 0, 0, 0, 0, 0);
Pid myVelPid(0, 0, 0, 0, 0, 0);
Pid myCurPid(0, 0, 0, 0, 0, 0);
MotorTorqueParameters motorTorqueParams;
double stiff_val = 0;
double damp_val = 0;
double stiff_max = 0;
double damp_max = 0;
double off_max = 0;
double stiff_min = 0;
double damp_min = 0;
double off_min = 0;
double impedance_offset_val = 0;
double openloop_reference = 0;
double openloop_current_pwm = 0;
iImp->getCurrentImpedanceLimit(jointIndex, &stiff_min, &stiff_max, &damp_min, &damp_max);
iTrq->getTorqueRange(jointIndex, &off_min, &off_max);
// Position
iPid->getPid(jointIndex, &myPosPid);
yarp::os::Time::delay(0.005);
// Velocity
iVel->getVelPid(jointIndex, &myVelPid);
yarp::os::Time::delay(0.005);
// Current
//???????iCur->getCurPid(jointIndex, &myCurPid);
yarp::os::Time::delay(0.005);
// Torque
iTrq->getTorquePid(jointIndex, &myTrqPid);
iTrq->getMotorTorqueParams(jointIndex, &motorTorqueParams);
yarp::os::Time::delay(0.005);
//Stiff
iImp->getImpedance(jointIndex, &stiff_val, &damp_val);
iImp->getImpedanceOffset(jointIndex, &impedance_offset_val);
yarp::os::Time::delay(0.005);
// Openloop
iOpl->getRefOutput(jointIndex, &openloop_reference);
iOpl->getOutput(jointIndex, &openloop_current_pwm);
if (currentPidDlg)
{
currentPidDlg->initPosition(myPosPid);
currentPidDlg->initTorque(myTrqPid, motorTorqueParams);
currentPidDlg->initVelocity(myVelPid);
currentPidDlg->initCurrent(myCurPid);
currentPidDlg->initStiffness(stiff_val, stiff_min, stiff_max, damp_val, damp_min, damp_max, impedance_offset_val, off_min, off_max);
currentPidDlg->initOpenLoop(openloop_reference, openloop_current_pwm);
currentPidDlg->initRemoteVariables(iVar);
}
}
void PartItem::onSendCurrentPid(int jointIndex, Pid newPid)
{
Pid myCurPid(0, 0, 0, 0, 0, 0);
//????iCur->setCurPid(jointIndex, newPid);
yarp::os::Time::delay(0.005);
//????iCur->getCurPid(jointIndex, &myCurPid);
if (currentPidDlg){
currentPidDlg->initCurrent(myCurPid);
}
}
void PartItem::onSendSingleRemoteVariable(std::string key, yarp::os::Bottle val)
{
iVar->setRemoteVariable(key,val);
yarp::os::Time::delay(0.005);
}
void PartItem::onUpdateAllRemoteVariables()
{
if (currentPidDlg){
currentPidDlg->initRemoteVariables(iVar);
}
}
void PartItem::onCalibClicked(JointItem *joint)
{
if(!remCalib)
{
QMessageBox::critical(this,"Operation not supported", QString("The IRemoteCalibrator interface was not found on this application"));
return;
}
if(QMessageBox::question(this,"Question","Do you want really to recalibrate the joint?") != QMessageBox::Yes){
return;
}
if(!remCalib->calibrateSingleJoint(joint->getJointIndex()) )
{
// provide better feedback to user by verifying if the calibrator device was set or not
bool isCalib = false;
remCalib->isCalibratorDevicePresent(&isCalib);
if(!isCalib)
QMessageBox::critical(this,"Calibration failed", QString("No calibrator device was configured to perform this action, please verify that the wrapper config file has the 'Calibrator' keyword in the attach phase"));
else
QMessageBox::critical(this,"Calibration failed", QString("The remote calibrator reported that something went wrong during the calibration procedure"));
}
}
void PartItem::onPidClicked(JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
currentPidDlg = new PidDlg(partName,jointIndex);
connect(currentPidDlg, SIGNAL(sendPositionPid(int,Pid)),this,SLOT(onSendPositionPid(int,Pid)));
connect(currentPidDlg, SIGNAL(sendVelocityPid(int, Pid)), this, SLOT(onSendVelocityPid(int, Pid)));
connect(currentPidDlg, SIGNAL(sendCurrentPid(int, Pid)), this, SLOT(onSendCurrentPid(int, Pid)));
connect(currentPidDlg, SIGNAL(sendSingleRemoteVariable(std::string, yarp::os::Bottle)), this, SLOT(onSendSingleRemoteVariable(std::string, yarp::os::Bottle)));
connect(currentPidDlg, SIGNAL(updateAllRemoteVariables()), this, SLOT(onUpdateAllRemoteVariables()));
connect(currentPidDlg, SIGNAL(sendTorquePid(int,Pid,MotorTorqueParameters)),this,SLOT(onSendTorquePid(int,Pid,MotorTorqueParameters)));
connect(currentPidDlg, SIGNAL(sendStiffness(int,double,double,double)),this,SLOT(onSendStiffness(int,double,double,double)));
connect(currentPidDlg, SIGNAL(sendOpenLoop(int,int)),this,SLOT(onSendOpenLoop(int,int)));
connect(currentPidDlg, SIGNAL(refreshPids(int)), this, SLOT(onRefreshPids(int)));
this->onRefreshPids(jointIndex);
currentPidDlg->exec();
delete currentPidDlg;
currentPidDlg = NULL;
}
void PartItem::onRunClicked(JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
double posJoint;
while (!iencs->getEncoder(jointIndex, &posJoint)){
Time::delay(0.001);
}
ctrlmode2->setControlMode(jointIndex,VOCAB_CM_POSITION);
//gtk_range_set_value ((GtkRange *) (sliderAry[*joint]), posJoint);
}
void PartItem::onIdleClicked(JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
ctrlmode2->setControlMode(jointIndex,VOCAB_CM_FORCE_IDLE);
}
void PartItem::onHomeClicked(JointItem *joint)
{
int NUMBER_OF_JOINTS;
const int jointIndex = joint->getJointIndex();
iPos->getAxes(&NUMBER_OF_JOINTS);
QString zero = QString("%1_zero").arg(partName);
if (!finder->isNull() && !finder->findGroup(zero.toLatin1().data()).isNull()){
bool ok = true;
Bottle xtmp;
xtmp = finder->findGroup(zero.toLatin1().data()).findGroup("PositionZero");
ok = ok && (xtmp.size() == NUMBER_OF_JOINTS+1);
double positionZero = xtmp.get(jointIndex+1).asDouble();
//fprintf(stderr, "%f\n", positionZero);
xtmp = finder->findGroup(zero.toLatin1().data()).findGroup("VelocityZero");
//fprintf(stderr, "VALUE VEL is %d \n", fnd->findGroup(buffer2).find("VelocityZero").toString().c_str());
ok = ok && (xtmp.size() == NUMBER_OF_JOINTS+1);
double velocityZero = xtmp.get(jointIndex+1).asDouble();
//fprintf(stderr, "%f\n", velocityZero);
if(!ok){
QMessageBox::critical(this,"Error", QString("Check the number of entries in the group %1").arg(zero));
} else {
iPos->setRefSpeed(jointIndex, velocityZero);
iPos->positionMove(jointIndex, positionZero);
}
}
else
{
if(!remCalib)
{
QMessageBox::critical(this,"Operation not supported", QString("The IRemoteCalibrator interface was not found on this application"));
return;
}
QMessageBox::information(this,"Info", QString("Asking the yarprobotinterface to homing part %1 through the remoteCalibrator interface, since no custom zero group found in the supplied file").arg(partName));
if(!remCalib->homingSingleJoint(jointIndex) )
{
// provide better feedback to user by verifying if the calibrator device was set or not
bool isCalib = false;
remCalib->isCalibratorDevicePresent(&isCalib);
if(!isCalib)
QMessageBox::critical(this,"Calibration failed", QString("No calibrator device was configured to perform this action, please verify that the wrapper config file for part %1 has the 'Calibrator' keyword in the attach phase").arg(partName));
else
QMessageBox::critical(this,"Calibration failed", QString("The remote calibrator reported that something went wrong during the calibration procedure"));
}
}
}
void PartItem::onJointChangeMode(int mode,JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
switch (mode) {
case JointItem::Idle:{
yInfo("joint: %d in IDLE mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_IDLE);
} else {
yError("ERROR: cannot do!");
}
break;
}
case JointItem::Position:{
yInfo("joint: %d in POSITION mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_POSITION);
joint->resetTarget();
} else {
yError("ERROR: cannot do!");
}
break;
}
case JointItem::PositionDirect:{
//if(positionDirectEnabled){
yInfo("joint: %d in POSITION DIRECT mode", jointIndex);
if(ctrlmode2){
joint->resetTarget();
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_POSITION_DIRECT);
} else {
yError("ERROR: cannot do!");
}
break;
/*}else{
LOG_ERROR("joint: %d in MIXED mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_MIXED);
} else {
yError("ERROR: cannot do!");
}
break;
}*/
}
case JointItem::Mixed:{
//if(positionDirectEnabled){
yInfo("joint: %d in MIXED mode", jointIndex);
if(ctrlmode2){
joint->resetTarget();
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_MIXED);
} else {
yError("ERROR: cannot do!");
}
break;
/*}else{
LOG_ERROR("joint: %d in VELOCITY mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setVelocityMode(jointIndex);
} else {
LOG_ERROR("ERROR: cannot do!");
}
break;
}*/
}
case JointItem::Velocity:{
//if(positionDirectEnabled){
yInfo("joint: %d in VELOCITY mode", jointIndex);
if(ctrlmode2)
{
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_VELOCITY);
yInfo() << "Changing reference acceleration of joint " << jointIndex << " to 100000";
iVel->setRefAcceleration(jointIndex, 100000);
} else {
yError("ERROR: cannot do!");
}
break;
// } else {
// LOG_ERROR("joint: %d in TORQUE mode", jointIndex);
// if(ctrlmode2){
// ctrlmode2->setTorqueMode(jointIndex);
// } else {
// LOG_ERROR("ERROR: cannot do!");
// }
// break;
// }
}
case JointItem::Torque:{
//if(positionDirectEnabled){
yInfo("joint: %d in TORQUE mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_TORQUE);
} else {
yError("ERROR: cannot do!");
}
break;
// } else {
// LOG_ERROR("joint: %d in OPENLOOP mode", jointIndex);
// if(ctrlmode2){
// ctrlmode2->setOpenLoopMode(jointIndex);
// } else {
// LOG_ERROR("ERROR: cannot do!");
// }
// break;
// }
}
case JointItem::OpenLoop:{
yInfo("joint: %d in OPENLOOP mode", jointIndex);
if(ctrlmode2){
ctrlmode2->setControlMode(jointIndex, VOCAB_CM_OPENLOOP);
} else {
yError("ERROR: cannot do!");
}
break;
}
default:
break;
}
}
void PartItem::resizeWidget(int w)
{
int count = layout->count();
int jointPerLineCount = (w - 20) / (MAX_WIDTH_JOINT + 10);
if(jointPerLineCount > count){
jointPerLineCount = count;
}
if(jointPerLineCount <= 0){
return;
}
int extraSpace = (w - 20) - jointPerLineCount * (MAX_WIDTH_JOINT + 10);
for(int i=0;i<count;i++){
QWidget *widget = layout->itemAt(i)->widget();
if(widget){
widget->setMaximumWidth(MAX_WIDTH_JOINT + (extraSpace/jointPerLineCount));
widget->setMinimumWidth(MAX_WIDTH_JOINT + (extraSpace/jointPerLineCount));
}
}
}
void PartItem::resizeEvent(QResizeEvent *event)
{
if(!isVisible()){
return;
}
resizeWidget(event->size().width());
}
void PartItem::changeEvent( QEvent *event )
{
if(event->type() == QEvent::WindowStateChange ){
qDebug() << "State Changed " << width();
int count = layout->count();
int jointPerLineCount = (width() - 20) / (MAX_WIDTH_JOINT + 10);