-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathcontrolBoardRemapper_t1_test.cpp
326 lines (254 loc) · 12.4 KB
/
controlBoardRemapper_t1_test.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
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <yarp/os/Time.h>
#include <yarp/dev/ControlBoardInterfaces.h>
#include <yarp/dev/IControlMode.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/PolyDriverList.h>
#include <yarp/dev/IMultipleWrapper.h>
#include <vector>
#include <catch2/catch_amalgamated.hpp>
#include <harness.h>
using namespace yarp::os;
using namespace yarp::dev;
const char *fmcA_file_content = "device fakeMotionControl\n"
"[GENERAL]\n"
"Joints 2\n"
"\n"
"AxisName \"axisA1\" \"axisA2\" \n";
const char *fmcB_file_content = "device fakeMotionControl\n"
"[GENERAL]\n"
"Joints 3\n"
"\n"
"AxisName \"axisB1\" \"axisB2\" \"axisB3\"\n";
const char *fmcC_file_content = "device fakeMotionControl\n"
"[GENERAL]\n"
"Joints 4\n"
"\n"
"AxisName \"axisC1\" \"axisC2\" \"axisC3\" \"axisC4\" \n";
const char *wrapperA_file_content = "device controlBoard_nws_yarp\n"
"name /testRemapperRobot/a\n"
"period 0.01\n"
"networks (net_a)\n"
"joints 2\n"
"net_a 0 1 0 1\n";
const char *wrapperB_file_content = "device controlBoard_nws_yarp\n"
"name /testRemapperRobot/b\n"
"period 0.01\n"
"networks (net_b)\n"
"joints 3\n"
"net_b 0 2 0 2\n";
const char *wrapperC_file_content = "device controlBoard_nws_yarp\n"
"name /testRemapperRobot/c\n"
"period 0.01\n"
"networks (net_c)\n"
"joints 4\n"
"net_c 0 3 0 3\n";
static void checkRemapper(yarp::dev::PolyDriver & ddRemapper, int rand, size_t nrOfRemappedAxes)
{
IPositionControl *pos = nullptr;
REQUIRE(ddRemapper.view(pos)); // interface position correctly opened
int axes = 0;
CHECK(pos->getAxes(&axes)); // getAxes returned correctly
CHECK((size_t) axes == nrOfRemappedAxes); // remapper seems functional
IPositionDirect *posdir = nullptr;
REQUIRE(ddRemapper.view(posdir)); // direct position interface correctly opened
IEncoders * encs = nullptr;
REQUIRE(ddRemapper.view(encs)); // encoders interface correctly opened
IControlMode *ctrlmode = nullptr;
REQUIRE(ddRemapper.view(ctrlmode)); // control mode interface correctly opened
// Vector used for setting/getting data from the controlboard
std::vector<double> setPosition(nrOfRemappedAxes,-10),
setRefSpeeds(nrOfRemappedAxes,-15),
readedPosition(nrOfRemappedAxes,-20),
readedEncoders(nrOfRemappedAxes,-30);
for(size_t i=0; i < nrOfRemappedAxes; i++)
{
setPosition[i] = i*100.0+50.0 + rand;
setRefSpeeds[i] = i*10.0+5 + rand;
readedPosition[i] = -100 + rand;
}
// Set the control mode in position direct
std::vector<int> settedControlMode(nrOfRemappedAxes,VOCAB_CM_POSITION_DIRECT);
std::vector<int> readedControlMode(nrOfRemappedAxes,VOCAB_CM_POSITION);
CHECK(ctrlmode->setControlModes(settedControlMode.data())); // setControlModes correctly called
// Check that the readed control mode is actually position direct
// Let's try 10 times because if the remapper is using some remotecontrolboards,
// it is possible that this return false if it is called before the first message
// has been received from the controlboard_nws_yarp
bool getControlModesOk = false;
for(int wait=0; wait < 10 && !getControlModesOk; wait++)
{
getControlModesOk = ctrlmode->getControlModes(readedControlMode.data());
yarp::os::Time::delay(0.001);
}
CHECK(getControlModesOk); // getControlModes correctly called
for(size_t i=0; i < nrOfRemappedAxes; i++)
{
CHECK(settedControlMode[i] == readedControlMode[i]); // Setted control mode and readed control mode match
}
// Test position direct methods
// Set position
CHECK(posdir->setPositions(setPosition.data())); // setPositions correctly called
// Set also the speeds in the mean time, so we are sure that we don't get
// spurios successful set/get of position because of intermediate buffers
CHECK(pos->setRefSpeeds(setRefSpeeds.data())); // setRefSpeeds correctly called
// Wait some time to make sure that the vector has been correctly propagated
// back and forth
yarp::os::Time::delay(0.1);
// Read position
CHECK(posdir->getRefPositions(readedPosition.data())); // getRefPositions correctly called
// Check that the two vector match
for(size_t i=0; i < nrOfRemappedAxes; i++)
{
CHECK(setPosition[i] == readedPosition[i]); // Setted position and readed ref position match
}
// Do a similar test for the encoders
// in fakeMotionControl their value is the one setted with setPosition
CHECK(encs->getEncoders(readedEncoders.data())); // getEncoders correctly called
// Check that the two vector match
for(size_t i=0; i < nrOfRemappedAxes; i++)
{
CHECK(setPosition[i] == readedEncoders[i]); // Setted position and readed encoders match
}
}
TEST_CASE("dev::ControlBoardRemapperTest", "[yarp::dev]")
{
YARP_REQUIRE_PLUGIN("fakeMotionControl", "device");
#if defined(DISABLE_FAILING_TESTS)
YARP_SKIP_TEST("Skipping failing tests")
#endif
Network::setLocalMode(true);
SECTION("Test the controlboard remapper")
{
// We first allocate three fakeMotionControl boards
// and their wrappers that we will remap using the remapper
std::vector<PolyDriver *> fmcbs;
std::vector<PolyDriver *> wrappers;
fmcbs.resize(3);
wrappers.resize(3);
std::vector<int> fmcbsSizes;
fmcbsSizes.push_back(2);
fmcbsSizes.push_back(3);
fmcbsSizes.push_back(4);
std::vector<std::string> fmcbsNames;
fmcbsNames.push_back("fakeControlBoardA");
fmcbsNames.push_back("fakeControlBoardB");
fmcbsNames.push_back("fakeControlBoardC");
std::vector<std::string> wrapperNetworks;
wrapperNetworks.push_back("net_a");
wrapperNetworks.push_back("net_b");
wrapperNetworks.push_back("net_c");
for(int i=0; i < 3; i++)
{
fmcbs[i] = new PolyDriver();
Property p;
if(i==0) { p.fromConfig(fmcA_file_content); }
if(i==1) { p.fromConfig(fmcB_file_content); }
if(i==2) { p.fromConfig(fmcC_file_content); }
REQUIRE(fmcbs[i]->open(p)); // fakeMotionControlBoard open reported successful
IPositionControl *pos = nullptr;
REQUIRE(fmcbs[i]->view(pos)); // interface position correctly opened
int axes = 0;
pos->getAxes(&axes);
CHECK(axes == fmcbsSizes[i]); // fakeMotionControlBoard seems functional
// Open the wrapper
wrappers[i] = new PolyDriver();
if(i==0) { p.fromConfig(wrapperA_file_content); }
if(i==1) { p.fromConfig(wrapperB_file_content); }
if(i==2) { p.fromConfig(wrapperC_file_content); }
REQUIRE(wrappers[i]->open(p)); // controlBoard_nws_yarp open reported successful
yarp::dev::IMultipleWrapper *iwrap = nullptr;
REQUIRE(wrappers[i]->view(iwrap)); // interface for multiple wrapper correctly opened for the controlBoard_nws_yarp
PolyDriverList pdList;
pdList.push(fmcbs[i],wrapperNetworks[i].c_str());
CHECK(iwrap->attachAll(pdList)); // controlBoard_nws_yarp attached successfully to the device
}
// Create a list containing all the fake controlboards
yarp::dev::PolyDriverList fmcList;
for(int i=0; i < 3; i++)
{
fmcList.push(fmcbs[i],fmcbsNames[i].c_str());
}
// Open a controlboardremapper with the wrong axisName,
// and make sure that if fails during attachAll
PolyDriver ddRemapperWN;
Property pRemapperWN;
pRemapperWN.put("device","controlboardremapper");
pRemapperWN.addGroup("axesNames");
Bottle & axesListWN = pRemapperWN.findGroup("axesNames").addList();
axesListWN.addString("axisA1");
axesListWN.addString("axisB1");
axesListWN.addString("axisC1");
axesListWN.addString("axisB3");
axesListWN.addString("axisC3");
axesListWN.addString("axisA2");
axesListWN.addString("thisIsAnAxisNameThatDoNotExist");
REQUIRE(ddRemapperWN.open(pRemapperWN)); // controlboardremapper with wrong names open reported successful
yarp::dev::IMultipleWrapper *imultwrapWN = nullptr;
REQUIRE(ddRemapperWN.view(imultwrapWN)); // interface for multiple wrapper with wrong names correctly opened
CHECK_FALSE(imultwrapWN->attachAll(fmcList)); // attachAll for controlboardremapper with wrong names successful
// Make sure that a controlboard in which attachAll is not successfull
// closes correctly
CHECK(ddRemapperWN.close()); // controlboardremapper with wrong names close was successful
// Open the controlboardremapper
PolyDriver ddRemapper;
Property pRemapper;
pRemapper.put("device","controlboardremapper");
pRemapper.addGroup("axesNames");
Bottle & axesList = pRemapper.findGroup("axesNames").addList();
axesList.addString("axisA1");
axesList.addString("axisB1");
axesList.addString("axisC1");
axesList.addString("axisB3");
axesList.addString("axisC3");
axesList.addString("axisA2");
size_t nrOfRemappedAxes = 6;
REQUIRE(ddRemapper.open(pRemapper)); // controlboardremapper open reported successful
yarp::dev::IMultipleWrapper *imultwrap = nullptr;
REQUIRE(ddRemapper.view(imultwrap)); // interface for multiple wrapper correctly opened
CHECK(imultwrap->attachAll(fmcList)); // attachAll for controlboardremapper successful
// Test the controlboardremapper
checkRemapper(ddRemapper,200,nrOfRemappedAxes);
// Open the remotecontrolboardremapper
PolyDriver ddRemoteRemapper;
Property pRemoteRemapper;
pRemoteRemapper.put("device","remotecontrolboardremapper");
pRemoteRemapper.addGroup("axesNames");
Bottle & remoteAxesList = pRemoteRemapper.findGroup("axesNames").addList();
remoteAxesList.addString("axisA1");
remoteAxesList.addString("axisB1");
remoteAxesList.addString("axisC1");
remoteAxesList.addString("axisB3");
remoteAxesList.addString("axisC3");
remoteAxesList.addString("axisA2");
Bottle remoteControlBoards;
Bottle & remoteControlBoardsList = remoteControlBoards.addList();
remoteControlBoardsList.addString("/testRemapperRobot/a");
remoteControlBoardsList.addString("/testRemapperRobot/b");
remoteControlBoardsList.addString("/testRemapperRobot/c");
pRemoteRemapper.put("remoteControlBoards",remoteControlBoards.get(0));
pRemoteRemapper.put("localPortPrefix","/test/remoteControlBoardRemapper");
Property & opts = pRemoteRemapper.addGroup("REMOTE_CONTROLBOARD_OPTIONS");
opts.put("writeStrict","on");
REQUIRE(ddRemoteRemapper.open(pRemoteRemapper)); // remotecontrolboardremapper open reported successful, testing it
// Test the remotecontrolboardremapper
checkRemapper(ddRemoteRemapper,100,nrOfRemappedAxes);
// Close devices
imultwrap->detachAll();
ddRemapper.close();
ddRemoteRemapper.close();
for(int i=0; i < 3; i++)
{
wrappers[i]->close();
delete wrappers[i];
wrappers[i] = nullptr;
fmcbs[i]->close();
delete fmcbs[i];
fmcbs[i] = nullptr;
}
}
Network::setLocalMode(false);
}