-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathpartitem.cpp
2401 lines (2082 loc) · 76.1 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
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "partitem.h"
#include "log.h"
#include <yarp/os/LogStream.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>
#include <cstdio>
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),
m_sequenceWindow(nullptr),
m_partId(id),
m_mixedEnabled(false),
m_positionDirectEnabled(false),
m_pwmEnabled(false),
m_currentEnabled(false),
m_currentPidDlg(nullptr),
m_controlModes(nullptr),
m_refTrajectorySpeeds(nullptr),
m_refTrajectoryPositions(nullptr),
m_refTorques(nullptr),
m_refVelocitySpeeds(nullptr),
m_torques(nullptr),
m_positions(nullptr),
m_speeds(nullptr),
m_currents(nullptr),
m_motorPositions(nullptr),
m_dutyCycles(nullptr),
m_done(nullptr),
m_part_speedVisible(false),
m_part_motorPositionVisible(false),
m_part_dutyVisible(false),
m_part_currentVisible(false),
m_interactionModes(nullptr),
m_finder(&_finder),
m_iMot(nullptr),
m_iinfo(nullptr),
m_slow_k(0)
{
m_layout = new FlowLayout();
setLayout(m_layout);
//PolyDriver *cartesiandd[MAX_NUMBER_ACTIVATED];
if (robotName.at(0) == '/') {
robotName.remove(0, 1);
}
if (partName.at(0) == '/') {
partName.remove(0, 1);
}
m_robotPartPort = QString("/%1/%2").arg(robotName).arg(partName);
m_partName = partName;
m_robotName = robotName;
//checking existence of the port
int ind = 0;
QString portLocalName = QString("/yarpmotorgui%1/%2").arg(ind).arg(m_robotPartPort);
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("/yarpmotorgui%1/%2").arg(ind).arg(m_robotPartPort);
nameToCheck = portLocalName;
nameToCheck += "/rpc:o";
// adr=nic.queryName(nameToCheck.c_str());
adr=Network::queryName(nameToCheck.toLatin1().data());
}
m_interfaceError = false;
// Initializing the polydriver options and instantiating the polydrivers
m_partOptions.put("local", portLocalName.toLatin1().data());
m_partOptions.put("device", "remote_controlboard");
m_partOptions.put("remote", m_robotPartPort.toLatin1().data());
m_partOptions.put("carrier", "udp");
m_partsdd = new PolyDriver();
// Opening the drivers
m_interfaceError = !openPolyDrivers();
if (m_interfaceError == true)
{
yError("Opening PolyDriver for part %s failed...", m_robotPartPort.toLatin1().data());
QMessageBox::critical(nullptr, "Error opening a device", QString("Error while opening device for part ").append(m_robotPartPort.toLatin1().data()));
}
/*********************************************************************/
/**************** PartMover Content **********************************/
if (!m_finder->isNull()){
yDebug("Setting a valid finder \n");
}
QString sequence_portname = QString("/yarpmotorgui/%1/sequence:o").arg(partName);
m_sequence_port.open(sequence_portname.toLatin1().data());
initInterfaces();
openInterfaces();
if (m_interfaceError == false)
{
int i = 0;
std::string jointname;
int number_of_joints;
m_iPos->getAxes(&number_of_joints);
m_controlModes = new int[number_of_joints]; //for (i = 0; i < number_of_joints; i++) m_controlModes = 0;
m_refTrajectorySpeeds = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_refTrajectorySpeeds[i] = std::nan("");
}
m_refTrajectoryPositions = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_refTrajectoryPositions[i] = std::nan("");
}
m_refTorques = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_refTorques[i] = std::nan("");
}
m_refVelocitySpeeds = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_refVelocitySpeeds[i] = std::nan("");
}
m_torques = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_torques[i] = std::nan("");
}
m_positions = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_positions[i] = std::nan("");
}
m_speeds = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_speeds[i] = std::nan("");
}
m_currents = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_currents[i] = std::nan("");
}
m_motorPositions = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_motorPositions[i] = std::nan("");
}
m_dutyCycles = new double[number_of_joints];
for (i = 0; i < number_of_joints; i++) {
m_dutyCycles[i] = std::nan("");
}
m_done = new bool[number_of_joints];
m_interactionModes = new yarp::dev::InteractionModeEnum[number_of_joints];
bool ret = false;
SystemClock::delaySystem(0.050);
do {
ret = m_iencs->getEncoders(m_positions);
if (!ret) {
yError("%s iencs->getEncoders() failed, retrying...\n", partName.toLatin1().data());
SystemClock::delaySystem(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;
double min_cur = -2.0;
double max_cur = +2.0;
for (int k = 0; k<number_of_joints; k++)
{
bool bpl = m_iLim->getLimits(k, &min_pos, &max_pos);
bool bvl = m_iLim->getVelLimits(k, &min_vel, &max_vel);
bool bcr = m_iCur->getCurrentRange(k, &min_cur, &max_cur);
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;
}
if (bcr == false || (min_cur == 0 && max_cur == 0))
{
yError() << "Error while getting current range, 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;
}
m_iinfo->getAxisName(k, jointname);
yarp::dev::JointTypeEnum jtype = yarp::dev::VOCAB_JOINTTYPE_REVOLUTE;
m_iinfo->getJointType(k, jtype);
Pid myPid(0,0,0,0,0,0);
yarp::os::SystemClock::delaySystem(0.005);
m_iPid->getPid(VOCAB_PIDTYPE_POSITION, k, &myPid);
auto* joint = new JointItem(k);
joint->setJointName(jointname.c_str());
joint->setPWMRange(-100.0, 100.0);
joint->setCurrentRange(min_cur, max_cur);
m_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(m_positionDirectEnabled);
joint->enableControlMixed(m_mixedEnabled);
joint->enableControlPWM(m_pwmEnabled);
joint->enableControlCurrent(m_currentEnabled);
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);
onSetCurSliderOptionPI(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(sliderPWMCommand(double, int)), this, SLOT(onSliderPWMCommand(double, int)));
connect(joint, SIGNAL(sliderCurrentCommand(double, int)), this, SLOT(onSliderCurrentCommand(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*)));
}
}
/*********************************************************************/
/*********************************************************************/
m_cycleTimer.setSingleShot(true);
m_cycleTimer.setTimerType(Qt::PreciseTimer);
connect(&m_cycleTimer, SIGNAL(timeout()), this, SLOT(onCycleTimerTimeout()), Qt::QueuedConnection);
m_cycleTimeTimer.setSingleShot(true);
m_cycleTimeTimer.setTimerType(Qt::PreciseTimer);
connect(&m_cycleTimeTimer, SIGNAL(timeout()), this, SLOT(onCycleTimeTimerTimeout()), Qt::QueuedConnection);
m_runTimeTimer.setSingleShot(true);
m_runTimeTimer.setTimerType(Qt::PreciseTimer);
connect(&m_runTimeTimer, SIGNAL(timeout()), this, SLOT(onRunTimerTimeout()), Qt::QueuedConnection);
m_runTimer.setSingleShot(true);
m_runTimer.setTimerType(Qt::PreciseTimer);
connect(&m_runTimer, SIGNAL(timeout()), this, SLOT(onRunTimeout()), Qt::QueuedConnection);
}
PartItem::~PartItem()
{
disconnect(&m_runTimer, SIGNAL(timeout()), this, SLOT(onRunTimeout()));
m_runTimer.stop();
disconnect(&m_runTimeTimer, SIGNAL(timeout()), this, SLOT(onRunTimerTimeout()));
m_runTimeTimer.stop();
disconnect(&m_cycleTimer, SIGNAL(timeout()), this, SLOT(onCycleTimerTimeout()));
m_cycleTimer.stop();
disconnect(&m_cycleTimeTimer, SIGNAL(timeout()), this, SLOT(onCycleTimeTimerTimeout()));
m_cycleTimeTimer.stop();
if (m_sequenceWindow){
m_sequenceWindow->hide();
delete m_sequenceWindow;
}
for (int i = 0; i<m_layout->count(); i++){
auto* joint = (JointItem *)m_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 (m_partsdd){
m_partsdd->close();
delete m_partsdd;
m_partsdd = nullptr;
}
if (m_controlModes) { delete[] m_controlModes; m_controlModes = nullptr; }
if (m_refTrajectorySpeeds) { delete[] m_refTrajectorySpeeds; m_refTrajectorySpeeds = nullptr; }
if (m_refTrajectoryPositions) { delete[] m_refTrajectoryPositions; m_refTrajectoryPositions = nullptr; }
if (m_refTorques) { delete[] m_refTorques; m_refTorques = nullptr; }
if (m_refVelocitySpeeds) { delete[] m_refVelocitySpeeds; m_refVelocitySpeeds = nullptr; }
if (m_torques) { delete[] m_torques; m_torques = nullptr; }
if (m_positions) { delete[] m_positions; m_positions = nullptr; }
if (m_speeds) { delete[] m_speeds; m_speeds = nullptr; }
if (m_currents) { delete[] m_currents; m_currents = nullptr; }
if (m_motorPositions) { delete[] m_motorPositions; m_motorPositions = nullptr; }
if (m_dutyCycles) { delete[] m_dutyCycles; m_dutyCycles = nullptr; }
if (m_done) { delete[] m_done; m_done = nullptr; }
if (m_interactionModes) { delete [] m_interactionModes; m_interactionModes = nullptr; }
}
bool PartItem::openPolyDrivers()
{
m_partsdd->open(m_partOptions);
if (!m_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
m_iPos = nullptr;
m_iVel = nullptr;
m_iVar = nullptr;
m_iDir = nullptr;
m_iencs = nullptr;
m_iAmp = nullptr;
m_iPid = nullptr;
m_iCur = nullptr;
m_iPWM = nullptr;
m_iTrq = nullptr;
m_iImp = nullptr;
m_iLim = nullptr;
m_ical = nullptr;
m_ictrlmode = nullptr;
m_iinteract = nullptr;
m_iremCalib = nullptr;
m_ijointfault = nullptr;
}
bool PartItem::openInterfaces()
{
yDebug("Opening interfaces...");
bool ok = false;
if (m_partsdd->isValid()) {
ok = m_partsdd->view(m_iPid);
if(!ok){
yError("...iPid was not ok...");
}
ok &= m_partsdd->view(m_iAmp);
if(!ok){
yError("...iAmp was not ok...");
}
ok &= m_partsdd->view(m_iPos);
if(!ok){
yError("...iPos was not ok...");
}
ok &= m_partsdd->view(m_iDir);
if(!ok){
yError("...posDirect was not ok...");
}
ok &= m_partsdd->view(m_iVel);
if(!ok){
yError("...iVel was not ok...");
}
ok &= m_partsdd->view(m_iLim);
if(!ok){
yError("...iLim was not ok...");
}
ok &= m_partsdd->view(m_iencs);
if(!ok){
yError("...enc was not ok...");
}
ok &= m_partsdd->view(m_ical);
if(!ok){
yError("...cal was not ok...");
}
ok &= m_partsdd->view(m_iTrq);
if(!ok){
yError("...trq was not ok...");
}
ok = m_partsdd->view(m_iPWM);
if(!ok){
yError("...opl was not ok...");
}
ok &= m_partsdd->view(m_iImp);
if(!ok){
yError("...imp was not ok...");
}
ok &= m_partsdd->view(m_ictrlmode);
if(!ok){
yError("...ctrlmode2 was not ok...");
}
ok &= m_partsdd->view(m_iinteract);
if(!ok){
yError("...iinteract was not ok...");
}
//optional interfaces
if (!m_partsdd->view(m_ijointfault))
{
yError("...m_iJointFault was not ok...");
}
if (!m_partsdd->view(m_iVar))
{
yError("...iVar was not ok...");
}
if (!m_partsdd->view(m_iMot))
{
yError("...iMot was not ok...");
}
if (!m_partsdd->view(m_iremCalib))
{
yError("...remCalib was not ok...");
}
if (!m_partsdd->view(m_iinfo))
{
yError("...axisInfo was not ok...");
}
if (!m_partsdd->view(m_iCur))
{
yError("...iCur was not ok...");
}
if (!ok) {
yError("Error while acquiring interfaces!");
QMessageBox::critical(nullptr,"Problems acquiring interfaces.","Check if interface is running");
m_interfaceError = true;
}
}
else
{
yError("Device driver was not valid!");
m_interfaceError = true;
}
return !m_interfaceError;
}
bool PartItem::getInterfaceError()
{
return m_interfaceError;
}
QString PartItem::getPartName()
{
return m_partName;
}
void PartItem::onSliderPWMCommand(double pwmVal, int index)
{
m_iPWM->setRefDutyCycle(index, pwmVal);
}
void PartItem::onSliderCurrentCommand(double currentVal, int index)
{
m_iCur->setRefCurrent(index, currentVal);
}
void PartItem::onSliderVelocityCommand(double speedVal, int index)
{
m_iVel->velocityMove(index, speedVal);
}
void PartItem::onSliderTorqueCommand(double torqueVal, int index)
{
m_iTrq->setRefTorque(index, torqueVal);
}
void PartItem::onSliderTrajectoryVelocityCommand(double trajspeedVal, int index)
{
m_iPos->setRefSpeed(index, trajspeedVal);
}
void PartItem::onSliderDirectPositionCommand(double dirpos, int index)
{
int mode;
m_ictrlmode->getControlMode(index, &mode);
if (mode == VOCAB_CM_POSITION_DIRECT)
{
m_iDir->setPosition(index, dirpos);
}
else
{
yWarning("Joint not in position direct mode so cannot send references");
}
}
void PartItem::onDumpAllRemoteVariables()
{
if (m_iVar == nullptr)
{
return;
}
QString fileName = QFileDialog::getSaveFileName(this, QString("Save Dump for Remote Variables as:"), QDir::homePath()+QString("/RemoteVariablesDump.txt"));
FILE* dumpfile = fopen(fileName.toStdString().c_str(), "w");
if (dumpfile != nullptr)
{
yarp::os::Bottle keys;
if (m_iVar->getRemoteVariablesList(&keys))
{
std::string s = keys.toString();
size_t keys_size = keys.size();
for (size_t i = 0; i < keys_size; i++)
{
std::string key_name;
if (keys.get(i).isString())
{
yarp::os::Bottle val;
key_name = keys.get(i).asString();
if (m_iVar->getRemoteVariable(key_name, val))
{
std::string key_value = val.toString();
std::string p_value = std::string(key_name).append(" ").append(key_value).append("\n");
fputs(p_value.c_str(), dumpfile);
}
}
}
}
fclose(dumpfile);
}
}
void PartItem::onSliderTrajectoryPositionCommand(double posVal, int index)
{
int mode;
m_ictrlmode->getControlMode(index, &mode);
if ( mode == VOCAB_CM_POSITION)
{
m_iPos->positionMove(index, posVal);
}
else
{
yWarning("Joint not in position mode so cannot send references");
}
}
void PartItem::onSliderMixedPositionCommand(double posVal, int index)
{
int mode;
m_ictrlmode->getControlMode(index, &mode);
if ( mode == VOCAB_CM_MIXED)
{
m_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;
m_ictrlmode->getControlMode(index, &mode);
if (mode == VOCAB_CM_MIXED)
{
m_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);
m_iinteract->setInteractionMode(jointIndex, (yarp::dev::InteractionModeEnum) VOCAB_IM_COMPLIANT);
break;
case JointItem::Stiff:
yInfo("interaction mode of joint %d set to STIFF", jointIndex);
m_iinteract->setInteractionMode(jointIndex, (yarp::dev::InteractionModeEnum) VOCAB_IM_STIFF);
break;
default:
break;
}
}
void PartItem::onSendPWM(int jointIndex, double pwmVal)
{
double pwm_reference = 0;
double current_pwm = 0;
m_iPWM->setRefDutyCycle(jointIndex, pwmVal);
yarp::os::SystemClock::delaySystem(0.010);
m_iPWM->getRefDutyCycle(jointIndex, &pwm_reference); //This is the reference
yarp::os::SystemClock::delaySystem(0.010);
m_iPWM->getDutyCycle(jointIndex, ¤t_pwm); //This is the real PWM output
if (m_currentPidDlg){
m_currentPidDlg->initPWM(pwm_reference, 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;
m_iImp->setImpedance(jointIdex, stiff, damp);
//imp->setImpedanceOffset(jointIdex, force);
yarp::os::SystemClock::delaySystem(0.005);
m_iImp->getImpedance(jointIdex, &stiff_val, &damp_val);
m_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;
m_iImp->getCurrentImpedanceLimit(jointIdex, &stiff_min, &stiff_max, &damp_min, &damp_max);
m_iTrq->getTorqueRange(jointIdex, &off_min, &off_max);
if (m_currentPidDlg)
{
m_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;
m_iPid->setPid(VOCAB_PIDTYPE_TORQUE, jointIndex, newPid);
m_iTrq->setMotorTorqueParams(jointIndex, newTrqParam);
yarp::os::SystemClock::delaySystem(0.005);
m_iPid->getPid(VOCAB_PIDTYPE_TORQUE,jointIndex, &myTrqPid);
m_iTrq->getMotorTorqueParams(jointIndex, &TrqParam);
if (m_currentPidDlg){
m_currentPidDlg->initTorque(myTrqPid, TrqParam);
}
}
void PartItem::onSendPositionPid(int jointIndex,Pid newPid)
{
Pid myPosPid(0,0,0,0,0,0);
m_iPid->setPid(VOCAB_PIDTYPE_POSITION, jointIndex, newPid);
yarp::os::SystemClock::delaySystem(0.005);
m_iPid->getPid(VOCAB_PIDTYPE_POSITION, jointIndex, &myPosPid);
if (m_currentPidDlg){
m_currentPidDlg->initPosition(myPosPid);
}
}
void PartItem::onSendVelocityPid(int jointIndex, Pid newPid)
{
Pid myVelPid(0, 0, 0, 0, 0, 0);
m_iPid->setPid(VOCAB_PIDTYPE_VELOCITY, jointIndex, newPid);
yarp::os::SystemClock::delaySystem(0.005);
m_iPid->getPid(VOCAB_PIDTYPE_VELOCITY, jointIndex, &myVelPid);
if (m_currentPidDlg){
m_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 pwm_reference = 0;
double current_pwm = 0;
m_iImp->getCurrentImpedanceLimit(jointIndex, &stiff_min, &stiff_max, &damp_min, &damp_max);
m_iTrq->getTorqueRange(jointIndex, &off_min, &off_max);
// Position
m_iPid->getPid(VOCAB_PIDTYPE_POSITION, jointIndex, &myPosPid);
yarp::os::SystemClock::delaySystem(0.005);
// Velocity
m_iPid->getPid(VOCAB_PIDTYPE_VELOCITY, jointIndex, &myVelPid);
yarp::os::SystemClock::delaySystem(0.005);
// Current
if (m_iCur)
{
m_iPid->getPid(VOCAB_PIDTYPE_CURRENT, jointIndex, &myCurPid);
yarp::os::SystemClock::delaySystem(0.005);
}
// Torque
m_iPid->getPid(VOCAB_PIDTYPE_TORQUE, jointIndex, &myTrqPid);
m_iTrq->getMotorTorqueParams(jointIndex, &motorTorqueParams);
yarp::os::SystemClock::delaySystem(0.005);
//Stiff
m_iImp->getImpedance(jointIndex, &stiff_val, &damp_val);
m_iImp->getImpedanceOffset(jointIndex, &impedance_offset_val);
yarp::os::SystemClock::delaySystem(0.005);
// PWM
m_iPWM->getRefDutyCycle(jointIndex, &pwm_reference);
m_iPWM->getDutyCycle(jointIndex, ¤t_pwm);
if (m_currentPidDlg)
{
m_currentPidDlg->initPosition(myPosPid);
m_currentPidDlg->initTorque(myTrqPid, motorTorqueParams);
m_currentPidDlg->initVelocity(myVelPid);
m_currentPidDlg->initCurrent(myCurPid);
m_currentPidDlg->initStiffness(stiff_val, stiff_min, stiff_max, damp_val, damp_min, damp_max, impedance_offset_val, off_min, off_max);
m_currentPidDlg->initPWM(pwm_reference, current_pwm);
m_currentPidDlg->initRemoteVariables(m_iVar);
}
}
void PartItem::onSendCurrentPid(int jointIndex, Pid newPid)
{
if (m_iCur == nullptr)
{
yError() << "iCurrent interface not opened";
return;
}
Pid myCurPid(0, 0, 0, 0, 0, 0);
m_iPid->setPid(VOCAB_PIDTYPE_CURRENT, jointIndex, newPid);
yarp::os::SystemClock::delaySystem(0.005);
m_iPid->getPid(VOCAB_PIDTYPE_CURRENT, jointIndex, &myCurPid);
if (m_currentPidDlg){
m_currentPidDlg->initCurrent(myCurPid);
}
}
void PartItem::onSendSingleRemoteVariable(std::string key, yarp::os::Bottle val)
{
m_iVar->setRemoteVariable(key, val);
yarp::os::SystemClock::delaySystem(0.005);
}
void PartItem::onUpdateAllRemoteVariables()
{
if (m_currentPidDlg){
m_currentPidDlg->initRemoteVariables(m_iVar);
}
}
void PartItem::onCalibClicked(JointItem *joint)
{
if (!m_iremCalib)
{
QMessageBox::critical(this,"Operation not supported", QString("The IRemoteCalibrator interface was not found on this application"));
return;
}
if(QMessageBox::question(this,"Question","Do you really want to recalibrate the joint?") != QMessageBox::Yes){
return;
}
if (!m_iremCalib->calibrateSingleJoint(joint->getJointIndex()))
{
// provide better feedback to user by verifying if the calibrator device was set or not
bool isCalib = false;
m_iremCalib->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();
QString jointName = joint->getJointName();
m_currentPidDlg = new PidDlg(m_partName, jointIndex, jointName);
connect(m_currentPidDlg, SIGNAL(sendPositionPid(int, Pid)), this, SLOT(onSendPositionPid(int, Pid)));
connect(m_currentPidDlg, SIGNAL(sendVelocityPid(int, Pid)), this, SLOT(onSendVelocityPid(int, Pid)));
connect(m_currentPidDlg, SIGNAL(sendCurrentPid(int, Pid)), this, SLOT(onSendCurrentPid(int, Pid)));
connect(m_currentPidDlg, SIGNAL(sendSingleRemoteVariable(std::string, yarp::os::Bottle)), this, SLOT(onSendSingleRemoteVariable(std::string, yarp::os::Bottle)));
connect(m_currentPidDlg, SIGNAL(updateAllRemoteVariables()), this, SLOT(onUpdateAllRemoteVariables()));
connect(m_currentPidDlg, SIGNAL(sendTorquePid(int, Pid, MotorTorqueParameters)), this, SLOT(onSendTorquePid(int, Pid, MotorTorqueParameters)));
connect(m_currentPidDlg, SIGNAL(sendStiffness(int, double, double, double)), this, SLOT(onSendStiffness(int, double, double, double)));
connect(m_currentPidDlg, SIGNAL(sendPWM(int, double)), this, SLOT(onSendPWM(int, double)));
connect(m_currentPidDlg, SIGNAL(refreshPids(int)), this, SLOT(onRefreshPids(int)));
connect(m_currentPidDlg, SIGNAL(dumpRemoteVariables()), this, SLOT(onDumpAllRemoteVariables()));
this->onRefreshPids(jointIndex);
m_currentPidDlg->exec();
delete m_currentPidDlg;
m_currentPidDlg = nullptr;
}
void PartItem::onRunClicked(JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
double posJoint;
while (!m_iencs->getEncoder(jointIndex, &posJoint)){
SystemClock::delaySystem(0.001);
}
m_ictrlmode->setControlMode(jointIndex, VOCAB_CM_POSITION);
}
void PartItem::onIdleClicked(JointItem *joint)
{
const int jointIndex = joint->getJointIndex();
m_ictrlmode->setControlMode(jointIndex, VOCAB_CM_FORCE_IDLE);
}
void PartItem::onHomeClicked(JointItem *joint)
{
int NUMBER_OF_JOINTS;
const int jointIndex = joint->getJointIndex();
m_iPos->getAxes(&NUMBER_OF_JOINTS);
this->homeJoint(jointIndex);
}
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 (m_ictrlmode){
m_ictrlmode->setControlMode(jointIndex, VOCAB_CM_IDLE);
} else {
yError("ERROR: cannot do!");
}
break;
}
case JointItem::Position:{
yInfo("joint: %d in POSITION mode", jointIndex);
if (m_ictrlmode){
m_ictrlmode->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 (m_ictrlmode){
joint->resetTarget();
m_ictrlmode->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 (m_ictrlmode){
joint->resetTarget();
m_ictrlmode->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 (m_ictrlmode)
{
m_ictrlmode->setControlMode(jointIndex, VOCAB_CM_VELOCITY);
yInfo() << "Changing reference acceleration of joint " << jointIndex << " to 100000";
m_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;
// }