yarp-devices
Loading...
Searching...
No Matches
exampleRemoteJr3.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/Network.h>
15#include <yarp/os/Property.h>
16#include <yarp/os/ResourceFinder.h>
17#include <yarp/os/Time.h>
18
19#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
20#include <yarp/dev/PolyDriver.h>
21
22int main(int argc, char * argv[])
23{
24 yarp::os::Network yarp;
25
26 if (!yarp::os::Network::checkNetwork())
27 {
28 yError() << "Please start a yarp name server first";
29 return 1;
30 }
31
32 yarp::os::Property options {
33 {"device", yarp::os::Value("multipleanalogsensorsclient")},
34 {"remote", yarp::os::Value("/jr3")},
35 {"local", yarp::os::Value("/exampleRemoteJr3")}
36 };
37
38 yarp::dev::PolyDriver device(options);
39
40 if (!device.isValid())
41 {
42 yError() << "Device not available";
43 return 1;
44 }
45
46 yarp::dev::ISixAxisForceTorqueSensors * sensor;
47
48 if (!device.view(sensor))
49 {
50 yError() << "Unable to acquire interface";
51 return 1;
52 }
53
54 int channels = sensor->getNrOfSixAxisForceTorqueSensors();
55 yInfo() << "Channels:" << channels;
56
57 for (auto ch = 0; ch < channels; ch++)
58 {
59 std::string name;
60
61 if (!sensor->getSixAxisForceTorqueSensorName(ch, name))
62 {
63 yError() << "Unable to get name of channel" << ch;
64 return 1;
65 }
66
67 yInfo() << "Channel" << ch << "has name:" << name;
68 }
69
70 int status;
71 int retry = 0;
72 constexpr auto MAX_RETRIES = 10;
73
74 do
75 {
76 status = yarp::dev::MAS_OK; // = 0
77
78 for (auto ch = 0; ch < channels; ch++)
79 {
80 status += sensor->getSixAxisForceTorqueSensorStatus(ch);
81 }
82
83 yInfo() << "Waiting for sensor to be ready... retry" << ++retry;
84
85 if (retry >= MAX_RETRIES)
86 {
87 yError() << "Sensor initialization failure, max number of retries exceeded";
88 return 1;
89 }
90
91 yarp::os::SystemClock::delaySystem(0.1);
92 }
93 while (status != yarp::dev::MAS_OK);
94
95 int n = 0;
96 constexpr auto MAX_ITERS = 500;
97
98 yInfo() << "Performing" << MAX_ITERS << "read iterations";
99
100 yarp::sig::Vector out;
101 double timestamp;
102
103 while (n++ < MAX_ITERS)
104 {
105 for (auto ch = 0; ch < channels; ch++)
106 {
107 if (!sensor->getSixAxisForceTorqueSensorMeasure(ch, out, timestamp))
108 {
109 yError() << "Unable to read channel" << ch;
110 return 1;
111 }
112
113 yInfo("[%d] [%f] Channel %d: %s", n, timestamp, ch, out.toString().c_str());
114 }
115
116 yarp::os::SystemClock::delaySystem(0.01);
117 }
118
119 yInfo() << "Done";
120 return 0;
121}