icub-test
TorqueControlGravityConsistency.cpp
1 /*
2  * iCub Robot Unit Tests (Robot Testing Framework)
3  *
4  * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include <math.h>
23 #include <robottestingframework/TestAssert.h>
24 #include <robottestingframework/dll/Plugin.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/os/Property.h>
27 #include <yarp/sig/Vector.h>
28 
29 #include "TorqueControlGravityConsistency.h"
30 
31 using namespace robottestingframework;
32 using namespace yarp::os;
33 using namespace yarp::dev;
34 using namespace yarp::sig;
35 
36 // prepare the plugin
37 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(TorqueControlGravityConsistency)
38 
39 TorqueControlGravityConsistency::TorqueControlGravityConsistency() : yarp::robottestingframework::TestCase("TorqueControlGravityConsistency"),
40  yarpRobot(0)
41 {
42 
43 }
44 
45 TorqueControlGravityConsistency::~TorqueControlGravityConsistency()
46 {
47 }
48 
49 bool TorqueControlGravityConsistency::setup(yarp::os::Property& property)
50 {
51  yarp::os::ResourceFinder & rf = yarp::os::ResourceFinder::getResourceFinderSingleton();
52 
53  yarp::os::Property yarpWbiConfiguration;
54  std::string yarpWbiConfigurationFile;
55  if(property.check("wbi_conf_file") && property.find("wbi_conf_file").isString())
56  {
57  yarpWbiConfigurationFile = rf.findFileByName(property.find("wbi_conf_file").asString());
58  }
59  else
60  {
61  yarpWbiConfigurationFile = rf.findFileByName("yarpWholeBodyInterface.ini");
62  }
63 
64  //It may be convenient to overload some option of the configuration file,
65  // so we load in the yarpWbiConfiguration also the option passed in the command line
66  yarpWbiConfiguration.fromConfigFile(yarpWbiConfigurationFile);
67 
68  yarpWbiConfiguration.fromString(property.toString().c_str(),false);
69 
70  // Create yarpWholeBodyInterface
71  std::string localName = "wbiTest";
72  if( property.check("local") )
73  {
74  localName = property.find("local").asString();
75  }
76 
77  wbi::wholeBodyInterface *yarpRobot = new yarpWbi::yarpWholeBodyInterface (localName.c_str(), yarpWbiConfiguration);
78 
79  wbi::IDList RobotMainJoints;
80  std::string RobotMainJointsListName = "ROBOT_TORQUE_CONTROL_JOINTS";
81  if( !yarpWbi::loadIdListFromConfig(RobotMainJointsListName,yarpWbiConfiguration,RobotMainJoints) )
82  {
83  fprintf(stderr, "[ERR] yarpWholeBodyInterface: impossible to load wbiId joint list with name %s\n",RobotMainJointsListName.c_str());
84  return false;
85  }
86 
87  yarpRobot->addJoints(RobotMainJoints);
88 
89  std::cout << "Joints added, calling init method" << std::endl;
90 
91  if(!yarpRobot->init())
92  {
93  std::cout << "Error: init() method failed" << std::endl;
94  return false;
95  }
96 
97  Time::delay(0.5);
98 
99  return true;
100 }
101 
102 void TorqueControlGravityConsistency::tearDown()
103 {
104  yarpRobot->close();
105  delete yarpRobot;
106  yarpRobot = 0;
107 }
108 
109 void TorqueControlGravityConsistency::run()
110 {
111  //Get the number of controlled degrees of freedom of the robot
112  int dof = yarpRobot->getDoFs();
113  printf("Number of (internal) controlled DoFs: %d\n", dof);
114 
115  //Allocate yarp vector of the right dimensions
116  Vector q(dof), trqMeasured(dof), trqGravity(dof), generalizedBiasForces(dof+6), dq(dof);
117 
118  //Read position and torque estimates
119  yarpRobot->getEstimates(wbi::ESTIMATE_JOINT_POS, q.data());
120  yarpRobot->getEstimates(wbi::ESTIMATE_JOINT_VEL, dq.data());
121  yarpRobot->getEstimates(wbi::ESTIMATE_JOINT_TORQUE, trqMeasured.data());
122 
123  // Compute gravity compensation torques
124  wbi::Frame world2base;
125  world2base.identity();
126  Vector baseTwist(6);
127  baseTwist.zero();
128 
129  double m_gravity[3];
130  m_gravity[0] = 0;
131  m_gravity[1] = 0;
132  m_gravity[2] = -9.81;
133 
134  yarpRobot->computeGeneralizedBiasForces(q.data(),world2base,dq.data(),baseTwist.data(),m_gravity,generalizedBiasForces.data());
135 
136  // We extract the joint torques from the generalized bias forces
137  trqGravity = generalizedBiasForces.subVector(6,6+dof-1);
138 
139  for(size_t i=0; i < dof; i++)
140  {
141  wbi::ID wbiID;
142  yarpRobot->getJointList().indexToID(i,wbiID);
143  std::cerr << "Joint " << wbiID.toString() << " : " << std::endl;
144  std::cerr << "\t Measured torque: " << trqMeasured[i] << "Nm" << std::endl;
145  std::cerr << "\t Gravity torque: " << trqGravity[i] << "Nm" << std::endl;
146  std::cerr << "\t Difference : " << trqMeasured[i]-trqGravity[i] << "Nm" << std::endl;
147  }
148 }
The test is supposed to be run with the iCub fixed to the pole, with the pole leveled with respect to...