-
Notifications
You must be signed in to change notification settings - Fork 33
/
YarpSensorBridgeImpl.h
1708 lines (1504 loc) · 65.4 KB
/
YarpSensorBridgeImpl.h
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
/**
* @file YarpSensorBridgeImpl.h
* @authors Prashanth Ramadoss
* @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/
#ifndef BIPEDAL_LOCOMOTION_ROBOT_INTERFACE_YARP_SENSOR_BRIDGE_IMPL_H
#define BIPEDAL_LOCOMOTION_ROBOT_INTERFACE_YARP_SENSOR_BRIDGE_IMPL_H
#include <BipedalLocomotion/RobotInterface/YarpSensorBridge.h>
#include <BipedalLocomotion/GenericContainer/Vector.h>
// YARP os
#include <yarp/os/Time.h>
// YARP sig
#include <yarp/sig/Vector.h>
#include <yarp/sig/Image.h>
// YARP Sensor Interfaces
#include <yarp/dev/IAnalogSensor.h>
#include <yarp/dev/IGenericSensor.h>
#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
// YARP Camera Interfaces
#include <yarp/dev/FrameGrabberInterfaces.h>
#include <yarp/dev/IRGBDSensor.h>
// YARP Control Board Interfaces
#include <yarp/dev/IEncodersTimed.h>
#include <yarp/dev/IAxisInfo.h>
// std
#include <cmath>
#include <set>
#include <algorithm>
namespace BipedalLocomotion
{
namespace RobotInterface
{
struct YarpSensorBridge::Impl
{
using StampedYARPVector = std::pair<yarp::sig::Vector, double>;
using StampedYARPImage = std::pair<yarp::sig::Image, double>;
/**
* Struct holding remapped remote control board interfaces
*/
struct ControlBoardRemapperInterfaces
{
yarp::dev::IEncodersTimed* encoders;
yarp::dev::IAxisInfo* axis;
};
ControlBoardRemapperInterfaces controlBoardRemapperInterfaces;
/**
* Struct holding remapped MAS interfaces -inertial sensors related
*/
struct WholeBodyMASInertialsInterface
{
yarp::dev::IThreeAxisLinearAccelerometers* accelerometers;
yarp::dev::IThreeAxisGyroscopes* gyroscopes;
yarp::dev::IThreeAxisMagnetometers* magnetometers;
yarp::dev::IOrientationSensors* orientationSensors;
};
WholeBodyMASInertialsInterface wholeBodyMASInertialsInterface;
/**
* Struct holding remapped MAS interfaces - FT sensors related
*/
struct WholeBodyMASForceTorquesInterface
{
yarp::dev::ISixAxisForceTorqueSensors* sixAxisFTSensors;
};
WholeBodyMASForceTorquesInterface wholeBodyMASForceTorquesInterface;
struct MASSensorIndexMaps
{
std::unordered_map<std::string, std::size_t> accelerometer;
std::unordered_map<std::string, std::size_t> gyroscopes;
std::unordered_map<std::string, std::size_t> magnetometers;
std::unordered_map<std::string, std::size_t> orientationSensors;
std::unordered_map<std::string, std::size_t> sixAxisFTSensors;
};
MASSensorIndexMaps masSensorIndexMaps;
/**
* Struct holding measurements polled from remapped remote control board interfaces
*/
struct ControlBoardRemapperMeasures
{
yarp::sig::Vector remappedJointIndices;
yarp::sig::Vector jointPositions;
yarp::sig::Vector jointVelocities;
double receivedTimeInSeconds;
};
ControlBoardRemapperMeasures controlBoardRemapperMeasures;
std::unordered_map<std::string, yarp::dev::IGenericSensor*> wholeBodyAnalogIMUInterface; /** < map of IMU sensors attached through generic sensor interfaces */
std::unordered_map<std::string, yarp::dev::IGenericSensor*> wholeBodyCartesianWrenchInterface; /** < map of cartesian wrench streams attached through generic sensor interfaces */
std::unordered_map<std::string, yarp::dev::IAnalogSensor*> wholeBodyAnalogSixAxisFTSensorsInterface; /** < map of six axis force torque sensors attached through analog sensor interfaces */
std::unordered_map<std::string, yarp::dev::IFrameGrabberImage*> wholeBodyFrameGrabberInterface; /** < map of cameras attached through frame grabber interfaces */
std::unordered_map<std::string, yarp::dev::IRGBDSensor*> wholeBodyRGBDInterface; /** < map of cameras attached through RGBD interfaces */
std::unordered_map<std::string, StampedYARPVector> wholeBodyIMUMeasures; /** < map holding analog IMU sensor measurements */
std::unordered_map<std::string, StampedYARPVector> wholeBodyFTMeasures; /** < map holding six axis force torque measures */
std::unordered_map<std::string, StampedYARPVector> wholeBodyInertialMeasures; /** < map holding three axis inertial sensor measures */
std::unordered_map<std::string, StampedYARPVector> wholeBodyCartesianWrenchMeasures; /** < map holding cartesian wrench measures */
std::unordered_map<std::string, StampedYARPImage> wholeBodyCameraRGBImages; /** < map holding images **/
std::unordered_map<std::string, StampedYARPImage> wholeBodyCameraDepthImages; /** < map holding images **/
const int nrChannelsInYARPGenericIMUSensor{12};
const int nrChannelsInYARPGenericCartesianWrench{6};
const int nrChannelsInYARPAnalogSixAxisFTSensor{6};
std::vector<std::string> failedSensorReads;
SensorBridgeMetaData metaData; /**< struct holding meta data **/
bool bridgeInitialized{false}; /**< flag set to true if the bridge is successfully initialized */
bool driversAttached{false}; /**< flag set to true if the bridge is successfully attached to required device drivers */
bool checkForNAN{false}; /**< flag to enable binary search for NANs in the incoming measurement buffers */
using SubConfigLoader = bool (YarpSensorBridge::Impl::*)(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler>,
SensorBridgeMetaData&);
/**
* Utility function to get index from vector
*/
bool getIndexFromVector(const std::vector<std::string>& vec,
const std::string& query,
int& index)
{
auto iter{std::lower_bound(vec.begin(), vec.end(), query)};
if (iter == vec.end())
{
return false;
}
index = iter - vec.begin();
return true;
}
/**
* Checks is a stream is enabled in configuration and
* loads the relevant stream group from configuration
*/
bool subConfigLoader(const std::string& enableStreamString,
const std::string& streamGroupString,
const SubConfigLoader loader,
std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData,
bool& enableStreamFlag)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::subConfigLoader] ";
auto ptr = handler.lock();
if (ptr == nullptr)
{
std::cerr << logPrefix << "The handler is not pointing to an already initialized memory."
<< std ::endl;
return false;
}
int success;
if (ptr->getParameter(enableStreamString, success))
{
enableStreamFlag = static_cast<bool>(success);
if (enableStreamFlag)
{
auto groupHandler = ptr->getGroup(streamGroupString);
if (! (this->*loader)(groupHandler, metaData) )
{
std::cerr << logPrefix << streamGroupString << " group could not be initialized from the configuration file."
<< std ::endl;
return false;
}
}
}
return true;
}
/**
* Configure remote control board remapper meta data
* Related to kinematics and other joint/motor relevant quantities
*/
bool configureRemoteControlBoardRemapper(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::configureRemoteControlBoardRemapper] ";
auto ptr = handler.lock();
if (ptr == nullptr) { return false; }
if (!ptr->getParameter("joints_list", metaData.sensorsList.jointsList))
{
std::cerr << logPrefix << " Required parameter \"joints_list\" not available in the configuration"
<< std::endl;
return false;
}
metaData.bridgeOptions.nrJoints = metaData.sensorsList.jointsList.size();
return true;
}
/**
* Configure inertial sensors meta data
*/
bool configureInertialSensors(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::configureInertialSensors] ";
auto ptr = handler.lock();
if (ptr == nullptr) { return false; }
if (ptr->getParameter("imu_list", metaData.sensorsList.IMUsList))
{
metaData.bridgeOptions.isIMUEnabled = true;
}
if (ptr->getParameter("accelerometers_list", metaData.sensorsList.linearAccelerometersList))
{
metaData.bridgeOptions.isLinearAccelerometerEnabled = true;
}
if (ptr->getParameter("gyroscopes_list", metaData.sensorsList.gyroscopesList))
{
metaData.bridgeOptions.isGyroscopeEnabled = true;
}
if (ptr->getParameter("orientation_sensors_list", metaData.sensorsList.orientationSensorsList))
{
metaData.bridgeOptions.isOrientationSensorEnabled = true;
}
if (ptr->getParameter("magnetometers_list", metaData.sensorsList.magnetometersList))
{
metaData.bridgeOptions.isMagnetometerEnabled = true;
}
return true;
}
/**
* Configure six axis force torque sensors meta data
*/
bool configureSixAxisForceTorqueSensors(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::configureSixAxisForceTorqueSensors] ";
auto ptr = handler.lock();
if (ptr == nullptr)
{
return false;
}
if (!ptr->getParameter("sixaxis_forcetorque_sensors_list", metaData.sensorsList.sixAxisForceTorqueSensorsList))
{
std::cerr << logPrefix << " Required parameter \"sixaxis_forcetorque_sensors_list\" not available in the configuration"
<< std::endl;
return false;
}
return true;
}
/**
* Configure cartesian wrenches meta data
*/
bool configureCartesianWrenches(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::configureCartesianWrenchSensors] ";
auto ptr = handler.lock();
if (ptr == nullptr)
{
return false;
}
if (!ptr->getParameter("cartesian_wrenches_list", metaData.sensorsList.cartesianWrenchesList))
{
std::cerr << logPrefix << " Required parameter \"cartesian_wrenches_list\" not available in the configuration"
<< std::endl;
return false;
}
return true;
}
/**
* Configure cameras meta data
*/
bool configureCameras(std::weak_ptr<BipedalLocomotion::ParametersHandler::IParametersHandler> handler,
SensorBridgeMetaData& metaData)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::configureCameras] ";
auto ptr = handler.lock();
if (ptr == nullptr) { return false; }
if (ptr->getParameter("rgb_cameras_list", metaData.sensorsList.rgbCamerasList))
{
metaData.bridgeOptions.isCameraEnabled = true;
std::vector<int> rgbWidth, rgbHeight;
if (ptr->getParameter("rgb_image_width", rgbWidth))
{
std::cerr << logPrefix << " Required parameter \"rgb_image_width\" not available in the configuration"
<< std::endl;
return false;
}
if (ptr->getParameter("rgb_image_height", rgbHeight))
{
std::cerr << logPrefix << " Required parameter \"rgb_image_height\" not available in the configuration"
<< std::endl;
return false;
}
if ( (rgbWidth.size() != metaData.sensorsList.rgbCamerasList.size()) ||
(rgbHeight.size() != metaData.sensorsList.rgbCamerasList.size()) )
{
std::cerr << logPrefix << " Parameters list size mismatch" << std::endl;
return false;
}
for (int idx = 0; idx < rgbHeight.size(); idx++)
{
std::pair<int, int> imgDimensions(rgbWidth[idx], rgbHeight[idx]);
auto cameraName{metaData.sensorsList.rgbCamerasList[idx]};
metaData.bridgeOptions.rgbImgDimensions[cameraName] = imgDimensions;
}
}
if (ptr->getParameter("rgbd_cameras_list", metaData.sensorsList.rgbdCamerasList))
{
metaData.bridgeOptions.isCameraEnabled = true;
std::vector<int> rgbdCamWidth, rgbdCamHeight;
if (ptr->getParameter("rgbd_image_width", rgbdCamWidth))
{
std::cerr << logPrefix << " Required parameter \"rgbd_image_width\" not available in the configuration"
<< std::endl;
return false;
}
if (ptr->getParameter("rgbd_image_height", rgbdCamHeight))
{
std::cerr << logPrefix << " Required parameter \"rgbd_image_height\" not available in the configuration"
<< std::endl;
return false;
}
if ( (rgbdCamWidth.size() != metaData.sensorsList.rgbdCamerasList.size()) ||
(rgbdCamHeight.size() != metaData.sensorsList.rgbdCamerasList.size()) )
{
std::cerr << logPrefix << " Parameters list size mismatch" << std::endl;
return false;
}
for (int idx = 0; idx < rgbdCamHeight.size(); idx++)
{
std::pair<int, int> imgDimensions(rgbdCamWidth[idx], rgbdCamHeight[idx]);
auto cameraName{metaData.sensorsList.rgbdCamerasList[idx]};
metaData.bridgeOptions.rgbdImgDimensions[cameraName] = imgDimensions;
}
}
return true;
}
/**
* Attach device with IGenericSensor or IAnalogSensor interfaces
* Important assumptions here,
* - Any generic sensor with 12 channels is a IMU sensor
* - Any generic sensor with 6 channels is a cartesian wrench sensor
* - Any analog sensor with 6 channels is a six axis force torque sensor
*/
template <typename SensorType>
bool attachGenericOrAnalogSensor(const yarp::dev::PolyDriverList& devList,
const std::string& sensorName,
const int& nrChannelsInSensor,
std::unordered_map<std::string, SensorType*>& sensorMap)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachGenericOrAnalogSensor] ";
for (int devIdx = 0; devIdx < devList.size(); devIdx++)
{
if (sensorName != devList[devIdx]->key)
{
continue;
}
SensorType* sensorInterface{nullptr};
if (devList[devIdx]->poly->view(sensorInterface))
{
if (sensorInterface == nullptr)
{
std::cerr << logPrefix << sensorName << " Could not view interface." << std::endl;
return false;
}
int nrChannels;
if constexpr (std::is_same_v<SensorType, yarp::dev::IAnalogSensor>)
{
nrChannels = sensorInterface->getChannels();
}
else if constexpr (std::is_same_v<SensorType, yarp::dev::IGenericSensor>)
{
sensorInterface->getChannels(&nrChannels);
}
if (nrChannels != nrChannelsInSensor)
{
std::cerr << logPrefix << sensorName << " Mismatch in the expected number of channels in the sensor stream." << std::endl;
return false;
}
sensorMap[devList[devIdx]->key] = sensorInterface;
}
else
{
std::cerr << logPrefix << sensorName << " Could not view interface." << std::endl;
return false;
}
}
return true;
}
template <typename SensorType>
bool attachAllGenericOrAnalogSensors(const yarp::dev::PolyDriverList& devList,
std::unordered_map<std::string, SensorType*>& sensorMap,
const int& nrChannelsInSensor,
const std::vector<std::string>& sensorList,
std::string_view interfaceType)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachAllGenericOrAnalogSensors] ";
bool allSensorsAttachedSuccesful{true};
for (auto genericSensorCartesianWrench : sensorList)
{
if (!attachGenericOrAnalogSensor(devList,
genericSensorCartesianWrench,
nrChannelsInSensor,
sensorMap))
{
allSensorsAttachedSuccesful = false;
break;
}
}
if (!allSensorsAttachedSuccesful ||
(sensorMap.size() != sensorList.size()) )
{
std::cerr << logPrefix << " Could not attach all desired " << interfaceType << " ." << std::endl;
return false;
}
return true;
}
/**
* Attach Remapped Multiple Analog Sensor Interfaces and check available sensors
*/
template <typename MASSensorType>
bool attachAndCheckMASSensors(const yarp::dev::PolyDriverList& devList,
MASSensorType* sensorInterface,
const std::vector<std::string>& sensorList,
const std::string_view interfaceName)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachAndCheckMASSensors] ";
if (!attachRemappedMASSensor(devList, sensorInterface))
{
std::cerr << logPrefix << " Could not find " << interfaceName << " interface." << std::endl;
return false;
}
if (!checkAttachedMASSensors(devList, sensorInterface, sensorList))
{
std::cerr << logPrefix << " Could not find atleast one of the required sensors." << std::endl;
return false;
}
return true;
}
/**
* Attach Remapped Multiple Analog Sensor Interfaces
* Looks for a specific MAS Sensor interface in the attached MAS Remapper
*/
template <typename MASSensorType>
bool attachRemappedMASSensor(const yarp::dev::PolyDriverList& devList,
MASSensorType* masSensorInterface)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachRemappedMASSensor] ";
bool broken{false};
for (int devIdx = 0; devIdx < devList.size(); devIdx++)
{
MASSensorType* sensorInterface{nullptr};
devList[devIdx]->poly->view(sensorInterface);
if (sensorInterface == nullptr)
{
continue;
}
// break out of the loop if we find relevant MAS interface
masSensorInterface = sensorInterface;
broken = true;
break;
}
if (!broken || masSensorInterface == nullptr)
{
std::cerr << logPrefix << " Could not view interface." << std::endl;
return false;
}
return true;
}
/**
* Check if all the desired MAS sensors are availabled in the attached MAS interface
*/
template <typename MASSensorType>
bool checkAttachedMASSensors(const yarp::dev::PolyDriverList& devList,
MASSensorType* sensorInterface,
const std::vector<std::string>& sensorList)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::checkAttachedMASSensors] ";
auto nrSensors{getNumberOfMASSensors(sensorInterface)};
if (nrSensors != sensorList.size())
{
std::cerr << logPrefix << "Expecting the same number of MAS sensors attached to the Bridge as many mentioned in the initialization step" << std::endl;
return false;
}
for (const auto& masSensorName : sensorList)
{
bool sensorFound{false};
for (std::size_t attachedIdx = 0; attachedIdx < nrSensors; attachedIdx++)
{
std::string attachedSensorName;
if (!getMASSensorName(sensorInterface, attachedIdx, attachedSensorName))
{
continue;
}
if (attachedSensorName == masSensorName)
{
sensorFound = true;
// update mas sensor index map for lookup
if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisGyroscopes>)
{
masSensorIndexMaps.gyroscopes[masSensorName] = attachedIdx;
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisLinearAccelerometers>)
{
masSensorIndexMaps.accelerometer[masSensorName] = attachedIdx;
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisMagnetometers>)
{
masSensorIndexMaps.magnetometers[masSensorName] = attachedIdx;
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IOrientationSensors>)
{
masSensorIndexMaps.orientationSensors[masSensorName] = attachedIdx;
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::ISixAxisForceTorqueSensors>)
{
masSensorIndexMaps.sixAxisFTSensors[masSensorName] = attachedIdx;
}
break;
}
}
if (!sensorFound)
{
std::cerr << logPrefix << "Bridge could not attach to MAS Sensor " << masSensorName << "." << std::endl;
}
}
return true;
}
/**
* Get number of MAS Sensors
*/
template <typename MASSensorType>
std::size_t getNumberOfMASSensors(MASSensorType* sensorInterface)
{
if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisGyroscopes>)
{
return sensorInterface->getNrOfThreeAxisGyroscopes();
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisLinearAccelerometers>)
{
return sensorInterface->getNrOfThreeAxisLinearAccelerometers();
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisMagnetometers>)
{
return sensorInterface->getNrOfThreeAxisMagnetometers();
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IOrientationSensors>)
{
return sensorInterface->getNrOfOrientationSensors();
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::ISixAxisForceTorqueSensors>)
{
return sensorInterface->getNrOfSixAxisForceTorqueSensors();
}
return 0;
}
/**
* Get name of MAS Sensors
*/
template <typename MASSensorType>
bool getMASSensorName(MASSensorType* sensorInterface,
const std::size_t& sensIdx,
std::string& sensorName)
{
if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisGyroscopes>)
{
return sensorInterface->getThreeAxisGyroscopeName(sensIdx, sensorName);
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisLinearAccelerometers>)
{
return sensorInterface->getThreeAxisLinearAccelerometerName(sensIdx, sensorName);
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisMagnetometers>)
{
return sensorInterface->getThreeAxisMagnetometerName(sensIdx, sensorName);
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IOrientationSensors>)
{
return sensorInterface->getOrientationSensorName(sensIdx, sensorName);
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::ISixAxisForceTorqueSensors>)
{
return sensorInterface->getSixAxisForceTorqueSensorName(sensIdx, sensorName);
}
return true;
}
/**
* Get all sensor names in a MAS Inerface
*/
template <typename MASSensorType>
std::vector<std::string> getAllSensorsInMASInterface(MASSensorType* sensorInterface)
{
std::vector<std::string> availableSensorNames;
if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisGyroscopes>)
{
auto nrSensors = sensorInterface->getNrOfThreeAxisGyroscopes();
for (std::size_t sensIdx = 0; sensIdx < nrSensors; sensIdx++)
{
std::string sensName;
sensorInterface->getThreeAxisGyroscopeName(sensIdx, sensName);
availableSensorNames.push_back(sensName);
}
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisLinearAccelerometers>)
{
auto nrSensors = sensorInterface->getNrOfThreeAxisLinearAccelerometers();
for (std::size_t sensIdx = 0; sensIdx < nrSensors; sensIdx++)
{
std::string sensName;
sensorInterface->getThreeAxisLinearAccelerometerName(sensIdx, sensName);
availableSensorNames.push_back(sensName);
}
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IThreeAxisMagnetometers>)
{
auto nrSensors = sensorInterface->getNrOfThreeAxisMagnetometers();
for (std::size_t sensIdx = 0; sensIdx < nrSensors; sensIdx++)
{
std::string sensName;
sensorInterface->getThreeAxisMagnetometerName(sensIdx, sensName);
availableSensorNames.push_back(sensName);
}
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::IOrientationSensors>)
{
auto nrSensors = sensorInterface->getNrOfOrientationSensors();
for (std::size_t sensIdx = 0; sensIdx < nrSensors; sensIdx++)
{
std::string sensName;
sensorInterface->getOrientationSensorName(sensIdx, sensName);
availableSensorNames.push_back(sensName);
}
}
else if constexpr (std::is_same_v<MASSensorType, yarp::dev::ISixAxisForceTorqueSensors>)
{
auto nrSensors = sensorInterface->getNrOfSixAxisForceTorqueSensors();
for (std::size_t sensIdx = 0; sensIdx < nrSensors; sensIdx++)
{
std::string sensName;
sensorInterface->getSixAxisForceTorqueSensorName(sensIdx, sensName);
availableSensorNames.push_back(sensName);
}
}
return availableSensorNames;
}
/**
* Attach cameras
*/
template <typename CameraType>
bool attachCamera(const yarp::dev::PolyDriverList& devList,
const std::string sensorName,
std::unordered_map<std::string, CameraType* >& sensorMap)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachCamera] ";
for (int devIdx = 0; devIdx < devList.size(); devIdx++)
{
if (sensorName != devList[devIdx]->key)
{
continue;
}
CameraType* cameraInterface{nullptr};
if (devList[devIdx]->poly->view(cameraInterface))
{
if (cameraInterface == nullptr)
{
std::cerr << logPrefix << " Could not view interface." << std::endl;
return false;
}
sensorMap[devList[devIdx]->key] = cameraInterface;
}
}
return true;
}
/**
* Check if sensor is available in the relevant sensor map
*/
template <typename SensorType>
bool checkSensor(const std::unordered_map<std::string, SensorType* >& sensorMap,
const std::string& sensorName)
{
if (sensorMap.find(sensorName) == sensorMap.end())
{
return false;
}
return true;
}
/**
* Check if sensor is available in the relevant sensor measurement map
*/
template <typename YARPDataType>
bool checkValidSensorMeasure(std::string_view logPrefix,
const std::unordered_map<std::string, YARPDataType >& sensorMap,
const std::string& sensorName)
{
if (!checkValid(logPrefix))
{
return false;
}
if (sensorMap.find(sensorName) == sensorMap.end())
{
std::cerr << logPrefix << sensorName << " sensor unavailable in the measurement map" << std::endl;
return false;
}
return true;
}
/**
* Check if the bridge is successfully initialized and attached to required device drivers
*/
bool checkValid(const std::string_view methodName)
{
if (!(bridgeInitialized && driversAttached))
{
std::cerr << methodName << " SensorBridge is not ready. Please initialize and set drivers list.";
return false;
}
return true;
}
/**
* Attach generic IMU sensor types and MAS inertials
*/
bool attachAllInertials(const yarp::dev::PolyDriverList& devList)
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachAllInertials] ";
if (metaData.bridgeOptions.isIMUEnabled)
{
// check if a generic sensor has 12 channels implying it is a IMU sensor through a GenericSensor Interfaces
std::string_view interfaceType{"Generic IMU Interface"};
if (!attachAllGenericOrAnalogSensors(devList,
wholeBodyAnalogIMUInterface,
nrChannelsInYARPGenericIMUSensor,
metaData.sensorsList.IMUsList,
interfaceType))
{
return false;
}
}
if (metaData.bridgeOptions.isLinearAccelerometerEnabled)
{
std::string_view interfaceType{"IThreeAxisLinearAccelerometers"};
if (!attachAndCheckMASSensors(devList, wholeBodyMASInertialsInterface.accelerometers,
metaData.sensorsList.linearAccelerometersList, interfaceType))
{
return false;
}
}
if (metaData.bridgeOptions.isGyroscopeEnabled)
{
std::string_view interfaceType{"IThreeAxisGyroscopes"};
if (!attachAndCheckMASSensors(devList, wholeBodyMASInertialsInterface.gyroscopes,
metaData.sensorsList.gyroscopesList, interfaceType))
{
return false;
}
}
if (metaData.bridgeOptions.isOrientationSensorEnabled)
{
std::string_view interfaceType{"IOrientationSensors"};
if (!attachAndCheckMASSensors(devList, wholeBodyMASInertialsInterface.orientationSensors,
metaData.sensorsList.orientationSensorsList, interfaceType))
{
return false;
}
}
if (metaData.bridgeOptions.isMagnetometerEnabled)
{
std::string_view interfaceType{"IThreeAxisMagnetometers"};
if (!attachAndCheckMASSensors(devList, wholeBodyMASInertialsInterface.magnetometers,
metaData.sensorsList.magnetometersList, interfaceType))
{
return false;
}
}
return true;
}
/**
* Attach a remapped control board and check the availability of desired interface
* Further, resize joint data buffers and check if
* the control board joints list and the desired joints list match
* Also, maintain a remapping index buffer for adapting to arbitrary joint list serializations
*/
bool attachRemappedRemoteControlBoard(const yarp::dev::PolyDriverList& devList)
{
if (!metaData.bridgeOptions.isKinematicsEnabled)
{
// do nothing
return true;
}
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::attachRemappedRemoteControlBoard] ";
// expects only one remotecontrolboard device attached to it, if found break!
// if there multiple remote control boards, then use a remapper to create a single remotecontrolboard
bool ok{true};
for (int devIdx = 0; devIdx < devList.size(); devIdx++)
{
ok = ok && devList[devIdx]->poly->view(controlBoardRemapperInterfaces.axis);
ok = ok && devList[devIdx]->poly->view(controlBoardRemapperInterfaces.encoders);
if (ok)
{
break;
}
}
if (!ok)
{
std::cerr << logPrefix << " Could not find a remapped remote control board with the desired interfaces" << std::endl;
return false;
}
resetControlBoardBuffers();
if (!compareControlBoardJointsList())
{
std::cerr << logPrefix << " Could not attach to remapped control board interface" << std::endl;
return false;
}
return true;
}
/**
* resize and set control board buffers to zero
*/
void resetControlBoardBuffers()
{
// firstly resize all the controlboard data buffers
controlBoardRemapperMeasures.remappedJointIndices.resize(metaData.bridgeOptions.nrJoints);
controlBoardRemapperMeasures.jointPositions.resize(metaData.bridgeOptions.nrJoints);
controlBoardRemapperMeasures.jointVelocities.resize(metaData.bridgeOptions.nrJoints);
// zero buffers
controlBoardRemapperMeasures.remappedJointIndices.zero();
controlBoardRemapperMeasures.jointPositions.zero();
controlBoardRemapperMeasures.jointVelocities.zero();
}
/**
* check and match control board joints with the sensorbridge joints list
*/
bool compareControlBoardJointsList()
{
constexpr std::string_view logPrefix = "[YarpSensorBridge::Impl::compareControlBoardJointsList] ";
// get the names of all the joints available in the attached remote control board remapper
std::vector<std::string> controlBoardJoints;
int controlBoardDOFs;
controlBoardRemapperInterfaces.encoders->getAxes(&controlBoardDOFs);
for (int DOF = 0; DOF < controlBoardDOFs; DOF++)
{
std::string joint;
controlBoardRemapperInterfaces.axis->getAxisName(DOF, joint);
controlBoardJoints.push_back(joint);
}
// check if the joints in the desired joint list are available in the controlboard joints list
// if available get the control board index at which the desired joint is available
// this is required in order to remap the control board joints on to the desired joints
// this needs to be optimized with better STL algorithms
bool jointFound{false};
for (int desiredDOF = 0; desiredDOF < metaData.sensorsList.jointsList.size(); desiredDOF++)
{
jointFound = false;
for (int DOF = 0; DOF < controlBoardDOFs; DOF++)
{
if (metaData.sensorsList.jointsList[desiredDOF] == controlBoardJoints[DOF])
{
controlBoardRemapperMeasures.remappedJointIndices[desiredDOF] = DOF;
jointFound = true;
break;
}
}
if (!jointFound)
{
std::cerr << logPrefix << " Could not find a desired joint from the configuration in the attached control board remapper." << std::endl;
return false;
}
}
if (!jointFound)
{
return false;
}
std::cout << logPrefix << "Found all joints with the remapped index " << std::endl;
for (int idx =0; idx < controlBoardRemapperMeasures.remappedJointIndices.size(); idx++)
{
std::cout << logPrefix << "Remapped Index: " << controlBoardRemapperMeasures.remappedJointIndices[idx]
<< " , Joint name: " << controlBoardJoints[controlBoardRemapperMeasures.remappedJointIndices[idx]]
<< std::endl;
}
return true;
}
bool attachAllSixAxisForceTorqueSensors(const yarp::dev::PolyDriverList& devList)
{
if (!metaData.bridgeOptions.isSixAxisForceTorqueSensorEnabled)
{
// do nothing
return true;
}
std::vector<std::string> analogFTSensors;
// attach MAS sensors
if (attachRemappedMASSensor(devList,
wholeBodyMASForceTorquesInterface.sixAxisFTSensors))
{
// get MAS FT sensor names
auto MASFTs = getAllSensorsInMASInterface(wholeBodyMASForceTorquesInterface.sixAxisFTSensors);
auto copySensorsList = metaData.sensorsList.sixAxisForceTorqueSensorsList;
// compare with sensorList - those not available in the MAS interface list
// are assumed to be analog FT sensors
std::sort(MASFTs.begin(), MASFTs.end());
std::sort(copySensorsList.begin(), copySensorsList.end());
std::set_intersection(MASFTs.begin(), MASFTs.end(),
copySensorsList.begin(), copySensorsList.end(),
std::back_inserter(analogFTSensors));
}
else
{
// if there are no MAS FT sensors then all the FT sensors in the configuration
// are analog FT sensors
analogFTSensors = metaData.sensorsList.sixAxisForceTorqueSensorsList;