-
Notifications
You must be signed in to change notification settings - Fork 104
/
embObjMotionControl.cpp
5239 lines (4351 loc) · 172 KB
/
embObjMotionControl.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
// -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
/*
* Copyright (C) 2012 iCub Facility, Istituto Italiano di Tecnologia
* Authors: Alberto Cardellino
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*
*/
//#include <yarp/dev/CanBusInterface.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/Time.h>
#include <string.h>
#include <iostream>
#include "embObjMotionControl.h"
#include <ethManager.h>
#include <FeatureInterface.h>
#include <yarp/conf/environment.h>
#include <yarp/os/LogStream.h>
#include "EoCommon.h"
#include "EOarray.h"
#include "EoProtocol.h"
#include "EoProtocolMN.h"
#include "EoProtocolMC.h"
#include "EoProtocolAS.h"
#include "motionControlDefaultValues.h"
#include <yarp/os/NetType.h>
#include <yarp/dev/ControlBoardHelper.h>
#include "eomcUtils.h"
using namespace yarp::dev;
using namespace yarp::os;
using namespace yarp::os::impl;
using namespace yarp::dev::eomc;
// macros
#define ASK_REFERENCE_TO_FIRMWARE 1
#define PARSER_MOTION_CONTROL_VERSION 6
static inline bool NOT_YET_IMPLEMENTED(const char *txt)
{
yError() << txt << " is not yet implemented for embObjMotionControl";
return true;
}
static inline bool DEPRECATED(const char *txt)
{
yError() << txt << " has been deprecated for embObjMotionControl";
return true;
}
#define NV_NOT_FOUND return nv_not_found();
static bool nv_not_found(void)
{
yError () << " nv_not_found!! This may mean that this variable is not handled by this EMS\n";
return false;
}
std::string embObjMotionControl::getBoardInfo(void)
{
if(nullptr == res)
{
return " BOARD name_unknown (IP unknown) ";
}
else
{
return ("BOARD " + res->getProperties().boardnameString + " (IP " + res->getProperties().ipv4addrString + ") ");
}
}
bool embObjMotionControl::alloc(int nj)
{
_axisMap = allocAndCheck<int>(nj);
_encodersStamp = allocAndCheck<double>(nj);
_gearbox_M2J = allocAndCheck<double>(nj);
_gearbox_E2J = allocAndCheck<double>(nj);
_deadzone = allocAndCheck<double>(nj);
_twofocinfo=allocAndCheck<eomc::twofocSpecificInfo_t>(nj);
_trj_pids= new eomc::PidInfo[nj];
//_dir_pids= new eomc::PidInfo[nj];
_trq_pids= new eomc::TrqPidInfo [nj];
_cur_pids= new eomc::PidInfo[nj];
_spd_pids= new eomc::PidInfo[nj];
_impedance_limits=allocAndCheck<eomc::impedanceLimits_t>(nj);
checking_motiondone=allocAndCheck<bool>(nj);
_last_position_move_time=allocAndCheck<double>(nj);
// Reserve space for data stored locally. values are initialize to 0
_ref_command_positions = allocAndCheck<double>(nj);
_ref_positions = allocAndCheck<double>(nj);
_ref_command_speeds = allocAndCheck<double>(nj);
_ref_speeds = allocAndCheck<double>(nj);
_ref_accs = allocAndCheck<double>(nj);
_enabledAmp = allocAndCheck<bool>(nj);
_enabledPid = allocAndCheck<bool>(nj);
_calibrated = allocAndCheck<bool>(nj);
_cacheImpedance = allocAndCheck<eOmc_impedance_t>(nj);
_rotorsLimits.resize(nj);
_jointsLimits.resize(nj);
_currentLimits.resize(nj);
_jsets.resize(nj);
_joint2set.resize(nj);
_timeouts.resize(nj);
_impedance_params.resize(nj);
_axesInfo.resize(nj);
_jointEncs.resize(nj);
_motorEncs.resize(nj);
//debug purpose
return true;
}
bool embObjMotionControl::dealloc()
{
checkAndDestroy(_axisMap);
checkAndDestroy(_encodersStamp);
checkAndDestroy(_gearbox_M2J);
checkAndDestroy(_gearbox_E2J);
checkAndDestroy(_deadzone);
checkAndDestroy(_impedance_limits);
checkAndDestroy(checking_motiondone);
checkAndDestroy(_ref_command_positions);
checkAndDestroy(_ref_positions);
checkAndDestroy(_ref_command_speeds);
checkAndDestroy(_ref_speeds);
checkAndDestroy(_ref_accs);
checkAndDestroy(_enabledAmp);
checkAndDestroy(_enabledPid);
checkAndDestroy(_calibrated);
checkAndDestroy(_twofocinfo);
if(_trj_pids)
delete [] _trj_pids;
//if(_dir_pids)
// delete [] _dir_pids;
if(_trq_pids)
delete [] _trq_pids;
if(_cur_pids)
delete [] _cur_pids;
if (_spd_pids)
delete[] _spd_pids;
return true;
}
embObjMotionControl::embObjMotionControl() :
ImplementControlCalibration(this),
ImplementAmplifierControl(this),
ImplementPidControl(this),
ImplementEncodersTimed(this),
ImplementPositionControl(this),
ImplementVelocityControl(this),
ImplementControlMode(this),
ImplementImpedanceControl(this),
ImplementMotorEncoders(this),
#ifdef IMPLEMENT_DEBUG_INTERFACE
ImplementDebugInterface(this),
#endif
ImplementTorqueControl(this),
ImplementControlLimits(this),
ImplementPositionDirect(this),
ImplementInteractionMode(this),
ImplementMotor(this),
ImplementRemoteVariables(this),
ImplementAxisInfo(this),
ImplementPWMControl(this),
ImplementCurrentControl(this),
SAFETY_THRESHOLD(2.0),
_rotorsLimits(0),
_jointsLimits(0),
_currentLimits(0),
_jsets(0),
_joint2set(0),
_timeouts(0),
_impedance_params(0),
_axesInfo(0),
_jointEncs(0),
_motorEncs(0)
{
_gearbox_M2J = 0;
_gearbox_E2J = 0;
_deadzone = 0;
opened = 0;
_trj_pids = NULL;
//_dir_pids = NULL;
_trq_pids = NULL;
_cur_pids = NULL;
_spd_pids = NULL;
res = NULL;
_njoints = 0;
_axisMap = NULL;
_encodersStamp = NULL;
_twofocinfo = NULL;
_cacheImpedance = NULL;
_impedance_limits = NULL;
_ref_accs = NULL;
_ref_command_speeds = NULL;
_ref_command_positions= NULL;
_ref_positions = NULL;
_ref_speeds = NULL;
_measureConverter = NULL;
checking_motiondone = NULL;
// debug connection
//tot_packet_recv = 0;
//errors = 0;
//start = 0;
//end = 0;
// Check status of joints
_enabledPid = NULL;
_enabledAmp = NULL;
_calibrated = NULL;
_last_position_move_time = NULL;
behFlags.useRawEncoderData = false;
behFlags.pwmIsLimited = false;
std::string tmp = yarp::conf::environment::getEnvironment("ETH_VERBOSEWHENOK");
if (tmp != "")
{
behFlags.verbosewhenok = (bool)NetType::toInt(tmp);
}
else
{
behFlags.verbosewhenok = false;
}
parser = NULL;
_mcparser = NULL;
#ifdef NETWORK_PERFORMANCE_BENCHMARK
/* We would like to verify if the round trimp of request and answer from embedded board is about 3 milliseconds, with a tollerance 0f 0.250 milliseconds.
The m_responseTimingVerifier object, after 3 seconds, prints an istogram with values from 1 to 10 millisec with a step of 0.5 millisec
*/
m_responseTimingVerifier.init(0.003, 0.00025, 0.001, 0.01, 0.0005, 30);
#endif
}
embObjMotionControl::~embObjMotionControl()
{
yTrace() << "embObjMotionControl::~embObjMotionControl()";
if(NULL != parser)
{
delete parser;
parser = NULL;
}
if(NULL != _mcparser)
{
delete _mcparser;
_mcparser = NULL;
}
dealloc();
}
bool embObjMotionControl::initialised()
{
return opened;
}
bool embObjMotionControl::initializeInterfaces(measureConvFactors &f)
{
ImplementControlCalibration::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementAmplifierControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL,f.ampsToSensor);
ImplementEncodersTimed::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementMotorEncoders::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementPositionControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementPidControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL, f.newtonsToSensor, f.ampsToSensor, f.dutycycleToPWM);
ImplementControlMode::initialize(_njoints, _axisMap);
ImplementVelocityControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementControlLimits::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementImpedanceControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL, f.newtonsToSensor);
ImplementTorqueControl::initialize(_njoints, _axisMap, f.angleToEncoder, NULL, f.newtonsToSensor, f.ampsToSensor, f.dutycycleToPWM, f.bemf2raw, f.ktau2raw);
ImplementPositionDirect::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementInteractionMode::initialize(_njoints, _axisMap, f.angleToEncoder, NULL);
ImplementMotor::initialize(_njoints, _axisMap);
ImplementRemoteVariables::initialize(_njoints, _axisMap);
ImplementAxisInfo::initialize(_njoints, _axisMap);
ImplementCurrentControl::initialize(_njoints, _axisMap, f.ampsToSensor);
ImplementPWMControl::initialize(_njoints, _axisMap, f.dutycycleToPWM);
return true;
}
bool embObjMotionControl::open(yarp::os::Searchable &config)
{
// - first thing to do is verify if the eth manager is available. then i parse info about the eth board.
ethManager = eth::TheEthManager::instance();
if(NULL == ethManager)
{
yFatal() << "embObjMotionControl::open() fails to instantiate ethManager";
return false;
}
eOipv4addr_t ipv4addr;
string boardIPstring;
string boardName;
if(false == ethManager->verifyEthBoardInfo(config, ipv4addr, boardIPstring, boardName))
{
yError() << "embObjMotionControl::open(): object TheEthManager fails in parsing ETH propertiex from xml file";
return false;
}
// add specific info about this device ...
if(NULL == parser)
{
parser = new ServiceParser;
}
// - now all other things
// -- instantiate EthResource etc.
res = ethManager->requestResource2(this, config);
if(NULL == res)
{
yError() << "embObjMotionControl::open() fails because could not instantiate the ethResource for " << getBoardInfo() << " ... unable to continue";
return false;
}
// READ CONFIGURATION
if(!fromConfig(config))
{
yError() << getBoardInfo() << "Missing motion control parameters in config file";
return false;
}
if(!res->verifyEPprotocol(eoprot_endpoint_motioncontrol))
{
yError() << "embObjMotionControl: failed verifyEPprotocol. Cannot continue!";
cleanup();
return false;
}
const eOmn_serv_parameter_t* servparam = &serviceConfig.ethservice;
if(eomn_serv_MC_generic == serviceConfig.ethservice.configuration.type)
{
servparam = NULL;
}
// in here ...we open ports where to print AMO data
mcdiagnostics.config.mode = serviceConfig.ethservice.configuration.diagnosticsmode;
mcdiagnostics.config.par16 = serviceConfig.ethservice.configuration.diagnosticsparam;
if(eomn_serv_diagn_mode_MC_AMOyarp == mcdiagnostics.config.mode)
{
// prepare the ports
mcdiagnostics.ports.resize(2);
for(size_t i=0; i<mcdiagnostics.ports.size(); i++)
{
mcdiagnostics.ports[i] = new BufferedPort<Bottle>;
mcdiagnostics.ports[i]->open("/amo/" + res->getProperties().boardnameString + "/j" + std::to_string(i));
}
}
if(false == res->serviceVerifyActivate(eomn_serv_category_mc, servparam))
{
yError() << "embObjMotionControl::open() has an error in call of ethResources::serviceVerifyActivate() for" << getBoardInfo();
cleanup();
return false;
}
yDebug() << "embObjMotionControl:serviceVerifyActivate OK!";
if(!init() )
{
yError() << "embObjMotionControl::open() has an error in call of embObjMotionControl::init() for" << getBoardInfo();
return false;
}
else
{
if(behFlags.verbosewhenok)
{
yDebug() << "embObjMotionControl::init() has succesfully initted" << getBoardInfo();
}
}
if(false == res->serviceStart(eomn_serv_category_mc))
{
yError() << "embObjMotionControl::open() fails to start mc service for" << getBoardInfo() << ": cannot continue";
cleanup();
return false;
}
else
{
if(behFlags.verbosewhenok)
{
yDebug() << "embObjMotionControl::open() correctly starts mc service of" << getBoardInfo();
}
}
opened = true;
if(eomn_serv_diagn_mode_MC_AMOyarp == mcdiagnostics.config.mode)
{
// marco.accame on 10 june 2011.
// in here we wait some time so that yarprobotinterface may receive amo streaming before any
// further module (e.g., the calibrator) sends an active control mode and starts moving the motor.
//
// how does it work?
//
// when the ETH board receives command from res->serviceStart() it enters the control loop and it
// waits for a non IDLE control mode etc. but it also starts streaming AMO data. It is the thread
// inside EthReceiver whuch process UDP pckets, so a wait inside this current thread will do the job.
//
// moreover, if mcdiagnostics.config.par16 is > 0, then we also have a mechanism such that the AMO
// are streamed in a conditio of the motor not initiaized yet. in such way we can observe what is
// the effect of the configuration of the motor on the AMO reading.
//
// how does it work?
//
// it is the function embObjMotionControl::init() which configures the motors by sending messages
// with tag eoprot_tag_mc_joint_config.
// the ETH board, if it sees eomn_serv_diagn_mode_MC_AMOyarp and par16 > 0, it applies the config of
// the motor with a delay of mcdiagnostics.config.par16 milliseconds. so, in here if we wait
// the same amount of time, we:
// - can read amo values with the motors off,
// - we are sure that no other module (e.g., the calibrator) will attempt to move a motor which is
// not yet configured.
// I KNOW: IT WOULD BE MUCH BETTER to wait in here until the ETH board tells us that it has configured
// the motor (rather then just applying a delay and hoping the ETH boards works fine). but that would
// require some effort which for now i prefer to postpone because "l'ottimo e' il nemico del bene"
// and we need a quick solution to test icub3.
SystemClock::delaySystem(0.001*mcdiagnostics.config.par16);
}
return true;
}
int embObjMotionControl::fromConfig_NumOfJoints(yarp::os::Searchable &config)
{
//
// Read Configuration params from file
//
int jn = config.findGroup("GENERAL").check("Joints", Value(1), "Number of degrees of freedom").asInt();
return(jn);
}
void embObjMotionControl::debugUtil_printJointsetInfo(void)
{
yError() << "****** DEBUG PRINTS **********";
yError() << "joint to set:";
for(int x=0; x< _njoints; x++)
yError() << " /t j " << x << ": set " <<_joint2set[x];
yError() << "jointmap:";
yError() << " number of sets" << _jsets.size();
for(size_t x=0; x< _jsets.size(); x++)
{
yError() << "set " << x<< "has size " <<_jsets[x].getNumberofJoints();
for(int y=0; y<_jsets[x].getNumberofJoints(); y++)
yError() << "set " << x << ": " << _jsets[x].joints[y];
}
yError() << "********* END ****************";
}
bool embObjMotionControl::verifyUserControlLawConsistencyInJointSet(eomc::PidInfo *pidInfo)
{
for(size_t s=0; s<_jsets.size(); s++)
{
int numofjoints = _jsets[s].getNumberofJoints();
if(numofjoints== 0 )
{
yError() << "embObjMC" << getBoardInfo() << "Jointsset " << s << "hasn't joints!!! I should be never stay here!!!";
return false;
}
int firstjoint = _jsets[s].joints[0];//get firts joint of set s
for(int k=1; k<numofjoints; k++)
{
int otherjoint = _jsets[s].joints[k];
if(pidInfo[firstjoint].usernamePidSelected != pidInfo[otherjoint].usernamePidSelected)
{
yError() << "embObjMC "<< getBoardInfo() << "Joints beloning to same set must be have same control law. Joint " << otherjoint << " differs from " << firstjoint << "Set num " << s ;
yError() << pidInfo[firstjoint].usernamePidSelected << "***" << pidInfo[otherjoint].usernamePidSelected;
return false;
}
}
}
return true;
}
bool embObjMotionControl::verifyUserControlLawConsistencyInJointSet(eomc::TrqPidInfo *pidInfo)
{
for(size_t s=0; s<_jsets.size(); s++)
{
int numofjoints = _jsets[s].getNumberofJoints();
if(numofjoints== 0 )
{
yError() << "embObjMC "<< getBoardInfo() << "Jointsset " << s << "hasn't joints!!! I should be never stay here!!!";
return false;
}
int firstjoint = _jsets[s].joints[0];//get firts joint of set s
for(int k=1; k<numofjoints; k++)
{
int otherjoint = _jsets[s].joints[k];
if(pidInfo[firstjoint].usernamePidSelected != pidInfo[otherjoint].usernamePidSelected)
{
yError() << "embObjMC"<< getBoardInfo() << "Joints beloning to same set must be have same control law. Joint " << otherjoint << " differs from " << firstjoint << "Set num " << s ;
yError() << pidInfo[firstjoint].usernamePidSelected << "***" << pidInfo[otherjoint].usernamePidSelected;
return false;
}
}
}
return true;
}
bool embObjMotionControl::updatedJointsetsCfgWithControlInfo()
{
//#error ALE
for(size_t s=0; s<_jsets.size(); s++)
{
if(_jsets[s].getNumberofJoints() == 0)
{
yError() << "embObjMC"<< getBoardInfo() << "Jointsset " << s << "hasn't joints!!! I should be never stay here!!!";
return false;
}
int joint = _jsets[s].joints[0];
//eOmc_pidoutputtype_t pid_out_type = pidOutputTypeConver_eomc2fw(_trj_pids[joint].controlLaw);
//if(eomc_pidoutputtype_unknown == pid_out_type)
//{
// yError() << "embObjMC"<< getBoardInfo() << "pid output type is unknown for joint " << joint;
// return false;
//}
//_jsets[s].setPidOutputType(pid_out_type);
//_jsets[s].setCanDoTorqueControl(isTorqueControlEnabled(joint));
_jsets[s].cfg.pid_output_types.postrj_ctrl_out_type = _trj_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.veltrj_ctrl_out_type = _trj_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.mixtrj_ctrl_out_type = _trj_pids[joint].out_type;
//_jsets[s].cfg.pid_output_types.posdir_ctrl_out_type = _dir_pids[joint].out_type;
//_jsets[s].cfg.pid_output_types.veldir_ctrl_out_type = _dir_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.posdir_ctrl_out_type = _trj_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.veldir_ctrl_out_type = _trj_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.torque_ctrl_out_type = _trq_pids[joint].out_type;
_jsets[s].cfg.pid_output_types.pwm_ctrl_out_type = eomc_ctrl_out_type_pwm;
if (_cur_pids[joint].enabled)
{
_jsets[s].cfg.pid_output_types.cur_ctrl_out_type = eomc_ctrl_out_type_cur;
}
else
{
_jsets[s].cfg.pid_output_types.cur_ctrl_out_type = eomc_ctrl_out_type_n_a;
}
}
return true;
}
bool embObjMotionControl::saveCouplingsData(void)
{
eOmc_4jomo_coupling_t *jc_dest;
static eOmc_4jomo_coupling_t dummyjomocoupling = {};
switch(serviceConfig.ethservice.configuration.type)
{
case eomn_serv_MC_foc:
{
jc_dest = &(serviceConfig.ethservice.configuration.data.mc.foc_based.jomocoupling);
} break;
case eomn_serv_MC_mc4plus:
{
jc_dest = &(serviceConfig.ethservice.configuration.data.mc.mc4plus_based.jomocoupling);
} break;
case eomn_serv_MC_mc4plusmais:
{
jc_dest = &(serviceConfig.ethservice.configuration.data.mc.mc4plusmais_based.jomocoupling);
} break;
case eomn_serv_MC_mc2pluspsc:
{
jc_dest = &(serviceConfig.ethservice.configuration.data.mc.mc2pluspsc.jomocoupling);
} break;
case eomn_serv_MC_mc4plusfaps:
{
jc_dest = &(serviceConfig.ethservice.configuration.data.mc.mc4plusfaps.jomocoupling);
} break;
case eomn_serv_MC_mc4pluspmc:
{
jc_dest = &dummyjomocoupling; // this mode does not have a coupling as it is w/ 7 independent joints
} break;
case eomn_serv_MC_mc4:
{
return true;
} break;
case eomn_serv_MC_generic:
{
return true;
} break;
default:
{
return false;
}
}
// this mode does not use jointsets but only eOmc_jointset_configuration_t
if(eomn_serv_MC_mc4pluspmc == serviceConfig.ethservice.configuration.type)
{
eOmc_arrayof_7jointsetconfig_t *arrayof7jset = &(serviceConfig.ethservice.configuration.data.mc.mc4pluspmc.arrayof7jointsets);
EOarray *array = eo_array_New(7, sizeof(eOmc_jointset_configuration_t), arrayof7jset);
// must initialise each entry of array
//for(size_t s=0; s< _jsets.size(); s++)
for(size_t s=0; s<4; s++)
{
eOmc_jointset_configuration_t* cfg_ptr = _jsets[s].getConfiguration();
eo_array_PushBack(array, cfg_ptr);
}
for(size_t e=0; e<3; e++)
{
eOmc_jointset_configuration_t cfg = {0};
eo_array_PushBack(array, &cfg);
}
return true;
}
memset(jc_dest, 0, sizeof(eOmc_4jomo_coupling_t));
//I need to initialize all elements of joint2set with "eomc_jointSetNum_none": it is used by fw to get num of setBemfParamRaw
//4 is teh satic dimension of joint2set. see definition of type eOmc_4jomo_coupling_t
for(int i=0; i<4; i++)
{
jc_dest->joint2set[i] = eomc_jointSetNum_none;
}
if(_joint2set.size() > 4 )
{
yError() << "embObjMC "<< getBoardInfo() << "Jointsset size is bigger than 4. I can't send jointset information to fw.";
return false;
}
for(size_t i=0; i<_joint2set.size(); i++)
{
jc_dest->joint2set[i] = _joint2set[i];
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
jc_dest->joint2motor[i][j] = eo_common_float_to_Q17_14((float)_couplingInfo.matrixJ2M[4*i+j]);
jc_dest->motor2joint[i][j] = eo_common_float_to_Q17_14((float)_couplingInfo.matrixM2J[4*i+j]);
}
}
for(int r=0; r<4; r++)
{
for(int c=0; c<6; c++)
{
jc_dest->encoder2joint[r][c] = eo_common_float_to_Q17_14((float)_couplingInfo.matrixE2J[6*r+c]);
}
}
for(size_t s=0; s< _jsets.size(); s++)
{
eOmc_jointset_configuration_t* cfg_ptr = _jsets[s].getConfiguration();
memcpy(&(jc_dest->jsetcfg[s]), cfg_ptr, sizeof(eOmc_jointset_configuration_t));
}
return true;
}
bool embObjMotionControl::fromConfig_Step2(yarp::os::Searchable &config)
{
Bottle xtmp;
int i,j;
measureConvFactors measConvFactors (_njoints);
if(iNeedCouplingsInfo())
{
////// COUPLINGS
if(!_mcparser->parseCouplingInfo(config, _couplingInfo))
return false;
////// JOINTSET_CFG
if(!_mcparser->parseJointsetCfgGroup(config, _jsets, _joint2set))
return false;
//debugUtil_printJointsetInfo();
}
///////// GENERAL MECHANICAL INFO
{
if(!_mcparser->parseAxisInfo(config, _axisMap, _axesInfo))
return false;
////// measures conversion factors
if(behFlags.useRawEncoderData)
{
for (i = 0; i < _njoints; i++)
{
measConvFactors.angleToEncoder[i] = 1;
}
}
else
{
if(!_mcparser->parseEncoderFactor(config, measConvFactors.angleToEncoder))
return false;
}
if (!_mcparser->parsefullscalePWM(config, measConvFactors.dutycycleToPWM))
return false;
if (!_mcparser->parseAmpsToSensor(config, measConvFactors.ampsToSensor))
return false;
//VALE: i have to parse GeneralMecGroup after parsing jointsetcfg, because inside generalmec group there is useMotorSpeedFbk that needs jointset info.
if(!_mcparser->parseGearboxValues(config, _gearbox_M2J, _gearbox_E2J))
return false;
// useMotorSpeedFbk
if(eomn_serv_MC_mc4 != (eOmn_serv_type_t)serviceConfig.ethservice.configuration.type)
{
int* useMotorSpeedFbk = 0;
useMotorSpeedFbk = new int[_njoints];
if (!_mcparser->parseMechanicalsFlags(config, useMotorSpeedFbk))
{
delete[] useMotorSpeedFbk;
return false;
}
//Note: currently in eth protocol this parameter belongs to jointset configuration. So
// i need to check that every joint belong to same set has the same value
if (!verifyUseMotorSpeedFbkInJointSet(useMotorSpeedFbk))
{
delete[] useMotorSpeedFbk;
return false;
}
delete[] useMotorSpeedFbk;
}
bool deadzoneIsAvailable;
if(!_mcparser->parseDeadzoneValue(config, _deadzone, &deadzoneIsAvailable))
return false;
if(!deadzoneIsAvailable) // if parameter is not written in configuration files then use default values
{
updateDeadZoneWithDefaultValues();
}
}
///// CONTROLS AND PID GROUPS
{
bool lowLevPidisMandatory = false;
if(iMange2focBoards())
lowLevPidisMandatory = true;
if(!_mcparser->parsePids(config, _trj_pids/*, _dir_pids*/, _trq_pids, _cur_pids, _spd_pids, lowLevPidisMandatory))
return false;
// 1) verify joint belonging to same set has same control law
//if(!verifyUserControlLawConsistencyInJointSet(_ppids))
// return false;
//if(!verifyUserControlLawConsistencyInJointSet(_vpids))
// return false;
//if(!verifyUserControlLawConsistencyInJointSet(_tpids))
// return false;
//yarp::dev::PidFeedbackUnitsEnum fbk_TrqPidUnits;
//yarp::dev::PidOutputUnitsEnum out_TrqPidUnits;
//if(!verifyTorquePidshasSameUnitTypes(fbk_TrqPidUnits, out_TrqPidUnits))
// return false;
//2) since some joint sets configuration info is in control and ids group, get that info and save them in jointset data struct.
updatedJointsetsCfgWithControlInfo();
}
for (i = 0; i < _njoints; i++)
{
measConvFactors.newtonsToSensor[i] = 1000000.0f; // conversion from Nm into microNm
measConvFactors.bemf2raw[i] = measConvFactors.newtonsToSensor[i] / measConvFactors.angleToEncoder[i];
if (_trq_pids->out_PidUnits == yarp::dev::PidOutputUnitsEnum::DUTYCYCLE_PWM_PERCENT)
{
measConvFactors.ktau2raw[i] = measConvFactors.dutycycleToPWM[i] / measConvFactors.newtonsToSensor[i];
}
else if (_trq_pids->out_PidUnits == yarp::dev::PidOutputUnitsEnum::RAW_MACHINE_UNITS)
{
measConvFactors.ktau2raw[i] = 1.0 / measConvFactors.newtonsToSensor[i];
}
else
{
yError() << "Invalid ktau units"; return false;
}
}
///////////////INIT INTERFACES
_measureConverter = new ControlBoardHelper(_njoints, _axisMap, measConvFactors.angleToEncoder, NULL, measConvFactors.newtonsToSensor, measConvFactors.ampsToSensor, nullptr, measConvFactors.dutycycleToPWM , measConvFactors.bemf2raw, measConvFactors.ktau2raw);
_measureConverter->set_pid_conversion_units(PidControlTypeEnum::VOCAB_PIDTYPE_POSITION, _trj_pids->fbk_PidUnits, _trj_pids->out_PidUnits);
//_measureConverter->set_pid_conversion_units(PidControlTypeEnum::VOCAB_PIDTYPE_DIRECT, _dir_pids->fbk_PidUnits, _dir_pids->out_PidUnits);
_measureConverter->set_pid_conversion_units(PidControlTypeEnum::VOCAB_PIDTYPE_TORQUE, _trq_pids->fbk_PidUnits, _trq_pids->out_PidUnits);
_measureConverter->set_pid_conversion_units(PidControlTypeEnum::VOCAB_PIDTYPE_CURRENT, _cur_pids->fbk_PidUnits, _cur_pids->out_PidUnits);
_measureConverter->set_pid_conversion_units(PidControlTypeEnum::VOCAB_PIDTYPE_VELOCITY, _spd_pids->fbk_PidUnits, _spd_pids->out_PidUnits);
/*
void ControlBoardHelper::set_pid_conversion_units(const PidControlTypeEnum& pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
{
ControlBoardHelper* cb_helper = this;
int nj = cb_helper->axes();
for (int i = 0; i < nj; i++)
{
mPriv->pid_units[pidtype][i].fbk_units = fbk_conv_units;
mPriv->pid_units[pidtype][i].out_units = out_conv_units;
}
}
*/
initializeInterfaces(measConvFactors);
ImplementPidControl::setConversionUnits(PidControlTypeEnum::VOCAB_PIDTYPE_POSITION, _trj_pids->fbk_PidUnits, _trj_pids->out_PidUnits);
//ImplementPidControl::setConversionUnits(PidControlTypeEnum::VOCAB_PIDTYPE_DIRECT, _dir_pids->fbk_PidUnits, _dir_pids->out_PidUnits);
ImplementPidControl::setConversionUnits(PidControlTypeEnum::VOCAB_PIDTYPE_TORQUE, _trq_pids->fbk_PidUnits, _trq_pids->out_PidUnits);
ImplementPidControl::setConversionUnits(PidControlTypeEnum::VOCAB_PIDTYPE_CURRENT, _cur_pids->fbk_PidUnits, _cur_pids->out_PidUnits);
ImplementPidControl::setConversionUnits(PidControlTypeEnum::VOCAB_PIDTYPE_VELOCITY, _spd_pids->fbk_PidUnits, _spd_pids->out_PidUnits);
//Now save in data in structures EmbObj protocol compatible
if(!saveCouplingsData())
return false;
////// IMPEDANCE PARAMETERS
if(! _mcparser->parseImpedanceGroup(config,_impedance_params))
{
yError() << "embObjMC " << getBoardInfo() << "IMPEDANCE section: error detected in parameters syntax";
return false;
}
////// IMPEDANCE LIMITS DEFAULT VALUES (UNDER TESTING)
for(j=0; j<_njoints; j++)
{
// got from canBusMotionControl, ask to Randazzo Marco
_impedance_limits[j].min_damp= 0.001;
_impedance_limits[j].max_damp= 9.888;
_impedance_limits[j].min_stiff= 0.002;
_impedance_limits[j].max_stiff= 9.889;
_impedance_limits[j].param_a= 0.011;
_impedance_limits[j].param_b= 0.012;
_impedance_limits[j].param_c= 0.013;
}
/////// LIMITS
{
if(!_mcparser->parseCurrentLimits(config, _currentLimits))
return false;
if(!_mcparser->parseJointsLimits(config, _jointsLimits))
return false;
if(!_mcparser->parseRotorsLimits(config, _rotorsLimits))
return false;
}
/////// [2FOC]
if(iMange2focBoards())
{
if(!_mcparser->parse2FocGroup(config, _twofocinfo))
return false;
}
/////// [TIMEOUTS]
if(! _mcparser->parseTimeoutsGroup(config, _timeouts, 1000 /*defaultVelocityTimeout*/))
return false;
return true;
}
bool embObjMotionControl::verifyUseMotorSpeedFbkInJointSet(int useMotorSpeedFbk [])
{
for(size_t s=0; s< _jsets.size(); s++)
{
int numofjointsinset = _jsets[s].getNumberofJoints();
if(numofjointsinset == 0 )
{
yError() << "embObjMC " << getBoardInfo() << "Jointsset " << s << "hasn't joints!!! I should be never stay here!!!";
return false;
}
int firstjointofset = _jsets[s].joints[0];
for(int j=1; j<numofjointsinset; j++)
{
int joint = _jsets[s].joints[j];
if(useMotorSpeedFbk[firstjointofset] != useMotorSpeedFbk[joint])
{
yError() << "embObjMC " << getBoardInfo() << ". Param useMotorSpeedFbk should have same value for joints belong same set. See joint " << firstjointofset << " and " << joint;
return false;
}
}
_jsets[s].setUseSpeedFeedbackFromMotors(useMotorSpeedFbk[firstjointofset]);
}
return true;
}
bool embObjMotionControl::verifyTorquePidshasSameUnitTypes(yarp::dev::PidFeedbackUnitsEnum &fbk_pidunits, yarp::dev::PidOutputUnitsEnum& out_pidunits)