yarp-devices
Loading...
Searching...
No Matches
exampleJr3Pci.cpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
13#include <yarp/os/LogStream.h>
14#include <yarp/os/Property.h>
15#include <yarp/os/SystemClock.h>
16
17#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
18#include <yarp/dev/PolyDriver.h>
19
20int main(int argc, char * argv[])
21{
22 yarp::os::Property options {{"device", yarp::os::Value("Jr3Pci")}};
23
24 yarp::dev::PolyDriver device(options);
25
26 if (!device.isValid())
27 {
28 yError() << "Device not available";
29 return 1;
30 }
31
32 yarp::dev::ISixAxisForceTorqueSensors * sensor;
33
34 if (!device.view(sensor))
35 {
36 yError() << "Unable to acquire interface";
37 return 1;
38 }
39
40 int channels = sensor->getNrOfSixAxisForceTorqueSensors();
41 yInfo() << "Channels:" << channels;
42
43 for (auto ch = 0; ch < channels; ch++)
44 {
45 std::string name;
46 sensor->getSixAxisForceTorqueSensorName(ch, name);
47 yInfo() << "Channel" << ch << "has name:" << name;
48 }
49
50 int status;
51 int retry = 0;
52 constexpr auto MAX_RETRIES = 10;
53
54 do
55 {
56 status = yarp::dev::MAS_OK; // = 0
57
58 for (auto ch = 0; ch < channels; ch++)
59 {
60 status += sensor->getSixAxisForceTorqueSensorStatus(ch);
61 }
62
63 yInfo() << "Waiting for sensor to be ready... retry" << ++retry;
64
65 if (retry >= MAX_RETRIES)
66 {
67 yError() << "Sensor initialization failure, max number of retries exceeded";
68 return 1;
69 }
70
71 yarp::os::SystemClock::delaySystem(0.1);
72 }
73 while (status != yarp::dev::MAS_OK);
74
75 int n = 0;
76 constexpr auto MAX_ITERS = 500;
77
78 yInfo() << "Performing" << MAX_ITERS << "read iterations";
79
80 yarp::sig::Vector out;
81 double timestamp;
82
83 while (n++ < MAX_ITERS)
84 {
85 for (auto ch = 0; ch < channels; ch++)
86 {
87 if (!sensor->getSixAxisForceTorqueSensorMeasure(ch, out, timestamp))
88 {
89 yError() << "Unable to read channel" << ch;
90 return 1;
91 }
92
93 yInfo("[%d] [%f] Channel %d: %s", n, timestamp, ch, out.toString().c_str());
94 }
95
96 yarp::os::SystemClock::delaySystem(0.01);
97 }
98
99 yInfo() << "Done";
100 return 0;
101}