Skip to content

Commit 72df013

Browse files
authored
Merge pull request #265 from robotology/fix/getRefPosition
ControlBoardPositionDirectControlTest: add check of getRefPositions
2 parents 66feb53 + 80a7a8a commit 72df013

7 files changed

Lines changed: 124 additions & 47 deletions

tests/controlboard/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ set(TESTS
99

1010
foreach(TEST ${TESTS})
1111
add_executable(${TEST} ${TEST}.cc)
12-
12+
if (GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN)
13+
target_compile_definitions(${TEST} PRIVATE GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN)
14+
endif()
1315
target_link_libraries(${TEST}
1416
PRIVATE GTest::gtest_main
1517
test-helpers

tests/controlboard/ControlBoardPositionControlTest.cc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,16 @@ TEST_F(ControlBoardPositionFixture, CheckPositionTrackingWithTrajectoryGeneratio
254254
ASSERT_LT(jointPosError, acceptedTolerance);
255255
}
256256

257+
#if defined GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN
258+
257259
TEST_F(ControlBoardPositionCoupledPendulumFixture, CheckPositionTrackingWithTrajectoryGenerationUsingPendulumModel)
258260
{
259261
auto refPosition{90.0};
260262
bool motionDone0{false};
261263
bool motionDone1{false};
262-
yarp::sig::Vector jointPosition{0.0, 0.0};
263-
yarp::sig::Vector jointPosError{0.0, 0.0};
264+
bool motionDone2{false};
265+
yarp::sig::Vector jointPosition{0.0, 0.0, 0.0};
266+
yarp::sig::Vector jointPosError{0.0, 0.0, 0.0};
264267

265268
testFixture
266269
.OnPostUpdate(
@@ -270,6 +273,7 @@ TEST_F(ControlBoardPositionCoupledPendulumFixture, CheckPositionTrackingWithTraj
270273
iEncoders->getEncoders(jointPosition.data());
271274
iPositionControl->checkMotionDone(0, &motionDone0);
272275
iPositionControl->checkMotionDone(1, &motionDone1);
276+
iPositionControl->checkMotionDone(2, &motionDone2);
273277

274278
// std::cerr << "ref position: " << refTrajectory[iterations] << std::endl;
275279
// std::cerr << "joint position: " << jointPosition << std::endl;
@@ -282,45 +286,52 @@ TEST_F(ControlBoardPositionCoupledPendulumFixture, CheckPositionTrackingWithTraj
282286

283287
int modeSet0{};
284288
int modeSet1{};
289+
int modeSet2{};
285290
iControlMode->getControlMode(0, &modeSet0);
286291
iControlMode->getControlMode(1, &modeSet1);
292+
iControlMode->getControlMode(2, &modeSet2);
287293
ASSERT_TRUE(modeSet0 == VOCAB_CM_POSITION);
288294
ASSERT_TRUE(modeSet1 == VOCAB_CM_POSITION);
295+
ASSERT_TRUE(modeSet2 == VOCAB_CM_POSITION);
289296

290297
// Set reference position
291-
iPositionControl->positionMove(0, refPosition);
298+
iPositionControl->positionMove(1, refPosition);
292299

293300
// Setup simulation server, this will call the post-update callbacks.
294301
// It also calls pre-update and update callbacks if those are being used.
295-
while (!motionDone0)
302+
while (!motionDone1)
296303
{
297304
std::cerr << "Running server" << std::endl;
298305
testFixture.Server()->Run(true, plannedIterations, false);
299-
jointPosError[0] = abs(refPosition - jointPosition[0]);
300-
std::cerr << "Joint 0 position error: " << jointPosError[0] << std::endl;
306+
jointPosError[1] = abs(refPosition - jointPosition[1]);
307+
std::cerr << "Joint 0 position error: " << jointPosError[1] << std::endl;
301308
}
302309

303310
std::cerr << "Final tracking error for joint 0: " << jointPosError[0] << std::endl;
304311
ASSERT_LT(jointPosError[0], acceptedTolerance);
305312

306-
iPositionControl->positionMove(1, refPosition);
307-
while (!motionDone1)
313+
iPositionControl->positionMove(2, refPosition);
314+
while (!motionDone2)
308315
{
309316
std::cerr << "Running server" << std::endl;
310317
testFixture.Server()->Run(true, plannedIterations, false);
311-
jointPosError[1] = abs(refPosition - jointPosition[1]);
312-
std::cerr << "Joint 1 position error: " << jointPosError[1] << std::endl;
318+
jointPosError[1] = abs(refPosition - jointPosition[2]);
319+
std::cerr << "Joint 1 position error: " << jointPosError[2] << std::endl;
313320
}
314321

315322
// Final assertions
316323
ASSERT_TRUE(configured);
317324
ASSERT_TRUE(motionDone0);
318325
ASSERT_TRUE(motionDone1);
326+
ASSERT_TRUE(motionDone2);
319327

320328
// Verify that the final error is within the accepted tolerance
321329
std::cerr << "Final tracking error for joint 1: " << jointPosError[1] << std::endl;
322330
ASSERT_LT(jointPosError[1], acceptedTolerance);
331+
std::cerr << "Final tracking error for joint 2: " << jointPosError[2] << std::endl;
332+
ASSERT_LT(jointPosError[2], acceptedTolerance);
323333
}
334+
#endif // GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN
324335

325336
} // namespace test
326337
} // namespace gzyarp

tests/controlboard/ControlBoardPositionDirectControlTest.cc

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ class ControlBoardPositionDirectCoupledPendulumFixture : public ::testing::Test
127127
protected:
128128
// void SetUp() override
129129
ControlBoardPositionDirectCoupledPendulumFixture()
130-
: testFixture{(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR)
131-
/ "coupled_pendulum_two_joints_coupled.sdf")
132-
.string()}
133130
{
131+
yarp::os::NetworkBase::setLocalMode(true);
132+
testFixture = std::make_unique<gz::sim::TestFixture>(
133+
(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "coupled_pendulum_two_joints_coupled.sdf").string());
134134
gz::common::Console::SetVerbosity(4);
135-
136-
testFixture.
137-
// Use configure callback to get values at startup
138-
OnConfigure([&](const gz::sim::Entity& _worldEntity,
135+
// Use configure callback to get values at startup
136+
testFixture->OnConfigure([&](const gz::sim::Entity& _worldEntity,
139137
const std::shared_ptr<const sdf::Element>& /*_sdf*/,
140138
gz::sim::EntityComponentManager& _ecm,
141139
gz::sim::EventManager& /*_eventMgr*/) {
@@ -162,6 +160,25 @@ class ControlBoardPositionDirectCoupledPendulumFixture : public ::testing::Test
162160
iEncoders = nullptr;
163161
ASSERT_TRUE(driver->view(iEncoders));
164162

163+
yarp::os::Property prop;
164+
prop.put("device", "remotecontrolboardremapper");
165+
166+
prop.addGroup("axesNames");
167+
auto& botAxesList = prop.findGroup("axesNames").addList();
168+
botAxesList.addString("fixed_base");
169+
botAxesList.addString("upper_joint");
170+
botAxesList.addString("lower_joint");
171+
172+
prop.addGroup("remoteControlBoards");
173+
auto& botRCBList = prop.findGroup("remoteControlBoards").addList();
174+
botRCBList.addString("/coupledPendulumGazebo/body");
175+
176+
prop.put("localPortPrefix","/gz-sim-posdir-test");
177+
178+
ASSERT_TRUE(remote_controlboard_remapper_driver.open(prop));
179+
ASSERT_TRUE(remote_controlboard_remapper_driver.view(posDirect_remapper));
180+
ASSERT_TRUE(posDirect_remapper != nullptr);
181+
165182
// Get joint1
166183
auto jointEntity0 = model.JointByName(_ecm, "upper_joint");
167184
EXPECT_NE(gz::sim::kNullEntity, jointEntity0);
@@ -175,6 +192,7 @@ class ControlBoardPositionDirectCoupledPendulumFixture : public ::testing::Test
175192
// Set joint in torque control mode
176193
ASSERT_TRUE(iControlMode->setControlMode(0, VOCAB_CM_POSITION_DIRECT));
177194
ASSERT_TRUE(iControlMode->setControlMode(1, VOCAB_CM_POSITION_DIRECT));
195+
ASSERT_TRUE(iControlMode->setControlMode(2, VOCAB_CM_POSITION_DIRECT));
178196

179197
// Print number of joint configured
180198
int nJointsConfigured{};
@@ -187,21 +205,23 @@ class ControlBoardPositionDirectCoupledPendulumFixture : public ::testing::Test
187205
}
188206

189207
// Get SDF model name from test parameter
190-
gz::sim::TestFixture testFixture;
208+
std::unique_ptr<gz::sim::TestFixture> testFixture;
191209
double linkMass{1};
192210
double linkLength{1.0};
193211
double linkInertiaAtLinkEnd{0.3352}; // Computed with parallel axis theorem
194212
int plannedIterations{5000};
195213
int iterations{0};
196214
std::vector<std::vector<double>> trackingErrors{2};
197-
double acceptedTolerance{5e-2};
215+
double acceptedTolerance{5e-1};
198216
bool configured{false};
199217
gz::math::Vector3d gravity;
200218
gz::sim::Entity modelEntity;
201219
gz::sim::Model model;
202220
gz::sim::Joint joint0;
203221
gz::sim::Joint joint1;
204222
yarp::dev::PolyDriver* driver;
223+
yarp::dev::PolyDriver remote_controlboard_remapper_driver;
224+
yarp::dev::IPositionDirect* posDirect_remapper = nullptr;
205225
yarp::dev::IPositionDirect* iPositionDirectControl = nullptr;
206226
yarp::dev::IControlMode* iControlMode = nullptr;
207227
yarp::dev::IEncoders* iEncoders = nullptr;
@@ -219,19 +239,21 @@ TEST_F(ControlBoardPositionDirectFixture, CheckPositionTrackingUsingPendulumMode
219239
refTrajectory[i] = value;
220240
}
221241

222-
double jointPosition;
242+
double jointPosition, jointRefPosition;
223243

224244
testFixture
225245
.OnPreUpdate([&](const gz::sim::UpdateInfo& _info, gz::sim::EntityComponentManager& _ecm) {
226246
// Set ref position
227247
iPositionDirectControl->setPosition(0, refTrajectory[iterations]);
248+
iPositionDirectControl->getRefPosition(0, &jointRefPosition);
249+
EXPECT_EQ(refTrajectory[iterations], jointRefPosition);
250+
228251
})
229252
.OnPostUpdate(
230253
[&](const gz::sim::UpdateInfo& _info, const gz::sim::EntityComponentManager& _ecm) {
231254
// std::cerr << "========== Iteration: " << iterations << std::endl;
232255

233256
iEncoders->getEncoder(0, &jointPosition);
234-
235257
// std::cerr << "ref position: " << refTrajectory[iterations] << std::endl;
236258
// std::cerr << "joint position: " << jointPosition << std::endl;
237259

@@ -264,7 +286,7 @@ TEST_F(ControlBoardPositionDirectFixture, CheckPositionTrackingUsingPendulumMode
264286
std::cerr << "Average tracking error: " << avgTrackgingError << std::endl;
265287
EXPECT_LT(avgTrackgingError, acceptedTolerance);
266288
}
267-
289+
#if defined GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN
268290
TEST_F(ControlBoardPositionDirectCoupledPendulumFixture, CheckPositionTrackingUsingCoupledPendulumModel)
269291
{
270292
// Generate ref trajectory
@@ -277,27 +299,32 @@ TEST_F(ControlBoardPositionDirectCoupledPendulumFixture, CheckPositionTrackingUs
277299
refTrajectory[i] = value;
278300
}
279301

280-
yarp::sig::Vector jointPositions{0.0, 0.0};
302+
yarp::sig::Vector jointPositions{0.0, 0.0, 0.0}, jointRefPositions{0.0, 0.0, 0.0}, jointRefPositionsRemapper{0.0, 0.0, 0.0};
281303

282-
testFixture
283-
.OnPreUpdate([&](const gz::sim::UpdateInfo& _info, gz::sim::EntityComponentManager& _ecm) {
304+
testFixture->OnPreUpdate([&](const gz::sim::UpdateInfo& _info, gz::sim::EntityComponentManager& _ecm) {
284305
// Set ref position
285-
iPositionDirectControl->setPosition(0, refTrajectory[iterations]);
286306
iPositionDirectControl->setPosition(1, refTrajectory[iterations]);
307+
iPositionDirectControl->setPosition(2, refTrajectory[iterations]);
308+
iPositionDirectControl->getRefPositions(jointRefPositions.data());
309+
posDirect_remapper->getRefPositions(jointRefPositionsRemapper.data());
310+
EXPECT_EQ(refTrajectory[iterations], jointRefPositions[1]);
311+
EXPECT_EQ(refTrajectory[iterations], jointRefPositions[2]);
312+
EXPECT_EQ(refTrajectory[iterations], jointRefPositionsRemapper[1]);
313+
EXPECT_EQ(refTrajectory[iterations], jointRefPositionsRemapper[2]);
287314
})
288315
.OnPostUpdate(
289316
[&](const gz::sim::UpdateInfo& _info, const gz::sim::EntityComponentManager& _ecm) {
290317
// std::cerr << "========== Iteration: " << iterations << std::endl;
291318

292319
iEncoders->getEncoders(jointPositions.data());
293-
320+
294321
// std::cerr << "ref position: " << refTrajectory[iterations] << std::endl;
295322
// std::cerr << "joint position: " << jointPosition << std::endl;
296323

297324
// Tracking error
298325
// EXPECT_NEAR(jointPosition, refTrajectory[iterations], acceptedTolerance);
299-
trackingErrors[0].push_back(abs(refTrajectory[iterations] - jointPositions[0]));
300-
trackingErrors[1].push_back(abs(refTrajectory[iterations] - jointPositions[1]));
326+
trackingErrors[0].push_back(abs(refTrajectory[iterations] - jointPositions[1]));
327+
trackingErrors[1].push_back(abs(refTrajectory[iterations] - jointPositions[2]));
301328

302329
iterations++;
303330
})
@@ -314,7 +341,7 @@ TEST_F(ControlBoardPositionDirectCoupledPendulumFixture, CheckPositionTrackingUs
314341

315342
// Setup simulation server, this will call the post-update callbacks.
316343
// It also calls pre-update and update callbacks if those are being used.
317-
testFixture.Server()->Run(true, plannedIterations, false);
344+
testFixture->Server()->Run(true, plannedIterations, false);
318345
std::cerr << "Simulation completed" << std::endl;
319346
// Final assertions
320347
EXPECT_TRUE(configured);
@@ -332,5 +359,7 @@ TEST_F(ControlBoardPositionDirectCoupledPendulumFixture, CheckPositionTrackingUs
332359
EXPECT_LT(avgTrackgingError1, acceptedTolerance);
333360
}
334361

362+
#endif // GZ_SIM_YARP_PLUGINS_ENABLE_TESTS_WITH_ICUB_MAIN
363+
335364
} // namespace test
336365
} // namespace gzyarp
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE robot PUBLIC "-//YARP//DTD yarprobotinterface 3.0//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV3.0.dtd">
3+
4+
<robot name="coupled_pendulum" portprefix="coupled_pendulum" build="0" xmlns:xi="http://www.w3.org/2001/XInclude">
5+
<devices>
6+
<device name="coupled_pendulum_nws_yarp" type="controlBoard_nws_yarp">
7+
<!-- See https://www.yarp.it/latest/classControlBoard__nws__yarp.html for parameter documentation -->
8+
<param name="name"> /coupledPendulumGazebo/body </param>
9+
<param name="period"> 0.01 </param>
10+
<action phase="startup" level="5" type="attach">
11+
<!-- This is the same name that we passed with the yarpDeviceName to the gazebo_controlboard plugin -->
12+
<param name="device"> controlboard_plugin_device </param>
13+
</action>
14+
<action phase="shutdown" level="5" type="detach" />
15+
</device>
16+
</devices>
17+
</robot>

tests/controlboard/conf/gazebo_controlboard_multiple_joints.ini

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ stictionDwn (0.0 0.0)
2020
controlUnits metric_units
2121
controlLaw joint_pid_gazebo_v1
2222
velocityControlImplementationType integrator_and_position_pid
23-
kp (500.0)
24-
kd (2.0)
25-
ki (0.1)
26-
maxInt (9999)
27-
maxOutput (9999)
28-
shift (0.0)
29-
ko (0.0)
30-
stictionUp (0.0)
31-
stictionDwn (0.0)
23+
kp (500.0 500.0)
24+
kd (2.0 2.0)
25+
ki (0.1 0.1)
26+
maxInt (9999 9999)
27+
maxOutput (9999 9999)
28+
shift (0.0 0.0)
29+
ko (0.0 0.0)
30+
stictionUp (0.0 0.0)
31+
stictionDwn (0.0 0.0)
3232

3333
[LIMITS]
3434
jntPosMax (200.0 10.0)

tests/controlboard/coupled_pendulum_two_controlboards.sdf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@
171171
</yarpConfigurationFile>
172172
<initialConfiguration>0.0</initialConfiguration>
173173
</plugin>
174-
<!-- <plugin name='robotinterface' filename='libgazebo_yarp_robotinterface.so'>
175-
<yarpRobotInterfaceConfigurationFile>model://coupled_pendulum/conf/coupled_pendulum_nws.xml</yarpRobotInterfaceConfigurationFile>
176-
</plugin> -->
174+
<plugin name='robotinterface' filename='libgazebo_yarp_robotinterface.so'>
175+
<yarpRobotInterfaceConfigurationFile>model://coupled_pendulum/conf/coupled_pendulum_nwsdafsads.xml</yarpRobotInterfaceConfigurationFile>
176+
</plugin>
177177

178178
<frame name='fake_base_fixed_joint' attached_to='base_link'>
179179
<pose>0 0 0 0 0 0</pose>

tests/controlboard/coupled_pendulum_two_joints_coupled.sdf

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@
4646
</model>
4747

4848
<model name="coupled_pendulum">
49+
<joint name="fixed_base" type="revolute">
50+
<parent>world</parent>
51+
<child>base_link</child>
52+
<axis>
53+
<xyz>1 0 0</xyz>
54+
<limit>
55+
<lower>-5</lower>
56+
<upper>5</upper>
57+
<effort>100</effort>
58+
<velocity>100</velocity>
59+
</limit>
60+
<dynamics>
61+
<damping>0.0</damping>
62+
<spring_reference>0</spring_reference>
63+
<spring_stiffness>0.0</spring_stiffness>
64+
</dynamics>
65+
</axis>
66+
</joint>
4967
<link name='base_link'>
5068
<inertial>
5169
<pose>0 0 0 0 0 0</pose>
@@ -161,13 +179,13 @@
161179

162180
<plugin name="gzyarp::ControlBoard" filename="gz-sim-yarp-controlboard-system">
163181
<yarpConfigurationFile>
164-
model://conf/gazebo_controlboard_multiple_joints.ini
182+
model://conf/gazebo_controlboard_coupling.ini
165183
</yarpConfigurationFile>
166184
<initialConfiguration>0.0</initialConfiguration>
167185
</plugin>
168-
<!-- <plugin name='robotinterface' filename='libgazebo_yarp_robotinterface.so'>
169-
<yarpRobotInterfaceConfigurationFile>model://coupled_pendulum/conf/coupled_pendulum_nws.xml</yarpRobotInterfaceConfigurationFile>
170-
</plugin> -->
186+
<plugin name="gzyarp::RobotInterface" filename="gz-sim-yarp-robotinterface-system">
187+
<yarpRobotInterfaceConfigurationFile>model://conf/coupled_pendulum_nws.xml</yarpRobotInterfaceConfigurationFile>
188+
</plugin>
171189

172190
<frame name='fake_base_fixed_joint' attached_to='base_link'>
173191
<pose>0 0 0 0 0 0</pose>

0 commit comments

Comments
 (0)