00001 #include <gtest/gtest.h>
00002 #include <boost/shared_ptr.hpp>
00003 #include <boost/make_shared.hpp>
00004 #include "robodyn_mechanisms/TubeTareMechanism.h"
00005 #include "robot_instance/RobotInstanceFactory.h"
00006 #include "robot_instance/CoeffMapLoader.h"
00007 #include "ros/package.h"
00008 #include "ros/time.h"
00009
00010
00011 using namespace std;
00012
00013 std::map<std::string, float> testFloats;
00014 std::map<std::string, int16_t> testInt16;
00015 std::map<std::string, uint16_t> testUint16;
00016 CoeffMapPtr motorCoeffMap;
00017 CoeffMapPtr brainstemCoeffMap;
00018 std::map<std::string, float> testLiveCoeffs;
00019 RobotInstancePtr instance;
00020
00021
00022 float getFloat(std::string item)
00023 {
00024 return testFloats[item];
00025 }
00026
00027 float getInt16(std::string item)
00028 {
00029 return testInt16[item];
00030 }
00031
00032 float getUint16(std::string item)
00033 {
00034 return testUint16[item];
00035 }
00036
00037 void setUint16(std::string item, uint16_t value)
00038 {
00039 testUint16[item] = value;
00040 }
00041
00042 void setInt16(std::string item, int16_t value)
00043 {
00044 testInt16[item] = value;
00045 }
00046
00047 float getMotorCoeff(std::string item)
00048 {
00049 return motorCoeffMap->operator[](item);
00050 }
00051
00052 bool hasLiveCoeff(std::string coeff)
00053 {
00054 return testLiveCoeffs.count(coeff) > 0;
00055 }
00056
00057 float getLiveCoeff(std::string coeff)
00058 {
00059 return testLiveCoeffs[coeff];
00060 }
00061
00062 void setLiveCoeff(std::string coeff, float value)
00063 {
00064 testLiveCoeffs[coeff] = value;
00065 }
00066
00067 std::vector<std::string> getJointNames(std::string mechanism)
00068 {
00069 return instance->mechanismJointsMap[mechanism];
00070 }
00071
00072 std::vector<std::string> getActuatorNames(std::string mechanism)
00073 {
00074 return instance->mechanismActuatorsMap[mechanism];
00075 }
00076
00077 std::string getCommandFile(std::string mechanism)
00078 {
00079 return instance->configurationSafetyBasePath + instance->APIMAP_PATH + instance->mechanismCommandMap[mechanism];
00080 }
00081
00082 class TubeTareMechanismTest : public ::testing::Test
00083 {
00084 protected:
00085 virtual void SetUp()
00086 {
00087 packagePath = ros::package::getPath("robot_instance");
00088 configPath = packagePath + "/test/config";
00089 configSafetyPath = packagePath + "/test/configSafety";
00090 fileName = "TestRobotInstanceFinger.xml";
00091 instance = RobotInstanceFactory::createRobotInstanceFromFile(configPath, configSafetyPath, fileName);
00092
00093 motorCoeffMap = boost::make_shared<CoeffMap>();
00094 brainstemCoeffMap = boost::make_shared<CoeffMap>();
00095 CoeffMapLoader::loadElements(instance, motorCoeffMap, brainstemCoeffMap);
00096
00097
00098 testFloats.clear();
00099 testInt16.clear();
00100 testUint16.clear();
00101 testLiveCoeffs.clear();
00102
00103 io.getFloat = getFloat;
00104 io.setInt16 = setInt16;
00105 io.getInt16 = getInt16;
00106 io.getUInt16 = getUint16;
00107 io.setUInt16 = setUint16;
00108 io.getMotorCoeff = getMotorCoeff;
00109 io.hasLiveCoeff = hasLiveCoeff;
00110 io.getLiveCoeff = getLiveCoeff;
00111 io.setLiveCoeff = setLiveCoeff;
00112 io.getJointNames = getJointNames;
00113 io.getActuatorNames = getActuatorNames;
00114 io.getCommandFile = getCommandFile;
00115
00116 ros::Time::init();
00117 }
00118
00119 virtual void TearDown()
00120 {
00121 }
00122
00123 string packagePath, configPath, configSafetyPath, fileName;
00124 TubeTareInterface::IoFunctions io;
00125 };
00126
00127 TEST_F(TubeTareMechanismTest, Constructor)
00128 {
00129 ASSERT_NO_THROW(TubeTareMechanism<4> thumbJoint("r2/right_arm/hand/thumb", io, 500));
00130 ASSERT_NO_THROW(TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500));
00131 ASSERT_NO_THROW(TubeTareMechanism<2> ringlittleJoint("r2/right_arm/hand/ringlittle", io, 500));
00132
00133
00134 io.getInt16 = 0;
00135 ASSERT_THROW(TubeTareMechanism<4> thumbJoint("r2/right_arm/hand/thumb", io, 500), std::invalid_argument);
00136 ASSERT_THROW(TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500), std::invalid_argument);
00137 ASSERT_THROW(TubeTareMechanism<2> ringlittleJoint("r2/right_arm/hand/ringlittle", io, 500), std::invalid_argument);
00138 }
00139
00140 TEST_F(TubeTareMechanismTest, setStop)
00141 {
00142 TubeTareMechanism<4> thumbJoint("r2/right_arm/hand/thumb", io, 500);
00143
00144 setInt16("r2/right_arm/hand/thumb/proximal_extensor/MotCom", 0);
00145 setInt16("r2/right_arm/hand/thumb/medial_extensor/MotCom", 1);
00146 setInt16("r2/right_arm/hand/thumb/medial_flexor/MotCom", 2);
00147 setInt16("r2/right_arm/hand/thumb/distal_extensor/MotCom", 3);
00148 setInt16("r2/right_arm/hand/thumb/distal_flexor/MotCom", 4);
00149
00150 thumbJoint.setStop(std::vector<std::string>());
00151
00152 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/thumb/proximal_extensor/MotCom"));
00153 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/thumb/medial_extensor/MotCom"));
00154 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/thumb/medial_flexor/MotCom"));
00155 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/thumb/distal_extensor/MotCom"));
00156 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/thumb/distal_flexor/MotCom"));
00157
00158 TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500);
00159
00160 setInt16("r2/right_arm/hand/index/proximal_extensor/MotCom", 0);
00161 setInt16("r2/right_arm/hand/index/proximal_flexor/MotCom", 1);
00162 setInt16("r2/right_arm/hand/index/medial_extensor/MotCom", 2);
00163 setInt16("r2/right_arm/hand/index/medial_flexor/MotCom", 3);
00164
00165 fingerJoint.setStop(std::vector<std::string>());
00166
00167 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/index/proximal_extensor/MotCom"));
00168 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/index/proximal_flexor/MotCom"));
00169 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/index/medial_extensor/MotCom"));
00170 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/index/medial_flexor/MotCom"));
00171
00172 TubeTareMechanism<2> ringlittleJoint("r2/right_arm/hand/ringlittle", io, 500);
00173
00174 setInt16("r2/right_arm/hand/ringlittle/ringlittle_extensor/MotCom", 0);
00175 setInt16("r2/right_arm/hand/ringlittle/ring_flexor/MotCom", 1);
00176 setInt16("r2/right_arm/hand/ringlittle/little_flexor/MotCom", 2);
00177
00178 ringlittleJoint.setStop(std::vector<std::string>());
00179
00180 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/ringlittle/ringlittle_extensor/MotCom"));
00181 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/ringlittle/ring_flexor/MotCom"));
00182 EXPECT_FLOAT_EQ(0, getInt16("r2/right_arm/hand/ringlittle/little_flexor/MotCom"));
00183
00184 }
00185
00186 TEST_F(TubeTareMechanismTest, isMoving)
00187 {
00188 TubeTareMechanism<4> thumbJoint("r2/right_arm/hand/thumb", io, 500);
00189
00190 for (int i = 0; i < 20; ++i)
00191 {
00192 setInt16("r2/right_arm/hand/thumb/proximal_extensor/IncEnc", i);
00193 bool moving = thumbJoint.isMoving();
00194
00195 if (i > 3)
00196 {
00197 EXPECT_TRUE(moving);
00198 }
00199 }
00200
00201 for (int i = 0; i > -20; --i)
00202 {
00203 setInt16("r2/right_arm/hand/thumb/proximal_extensor/IncEnc", i);
00204 bool moving = thumbJoint.isMoving();
00205
00206 if (i > 3)
00207 {
00208 EXPECT_TRUE(moving);
00209 }
00210 }
00211
00212 }
00213
00214 TEST_F(TubeTareMechanismTest, setRelease)
00215 {
00216 TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500);
00217
00218 setInt16("r2/right_arm/hand/index/proximal_extensor/MotCom", 10);
00219 setInt16("r2/right_arm/hand/index/proximal_flexor/MotCom", 20);
00220 setInt16("r2/right_arm/hand/index/medial_extensor/MotCom", 30);
00221 setInt16("r2/right_arm/hand/index/medial_flexor/MotCom", 40);
00222
00223 float releaseVal = io.getMotorCoeff("r2/right_arm/hand/index/MotComReleaseProximalExt");
00224
00225 fingerJoint.setRelease(std::vector<std::string>());
00226
00227 EXPECT_FLOAT_EQ(releaseVal, getInt16("r2/right_arm/hand/index/proximal_extensor/MotCom"));
00228 }
00229
00230 TEST_F(TubeTareMechanismTest, getEncTarePos)
00231 {
00232
00233 TubeTareMechanism<2> ringlittleJoint("r2/right_arm/hand/ringlittle", io, 500);
00234
00235 setInt16("r2/right_arm/hand/ringlittle/ringlittle_extensor/IncEnc", 0);
00236 setInt16("r2/right_arm/hand/ringlittle/ring_flexor/IncEnc", 1);
00237 setInt16("r2/right_arm/hand/ringlittle/little_flexor/IncEnc", 2);
00238
00239 ringlittleJoint.getEncoderTarePos();
00240
00241 EXPECT_FLOAT_EQ(0, getLiveCoeff("r2/right_arm/hand/ringlittle/ringlittle_extensor/EncoderOffset"));
00242 EXPECT_FLOAT_EQ(1, getLiveCoeff("r2/right_arm/hand/ringlittle/ring_flexor/EncoderOffset"));
00243 EXPECT_FLOAT_EQ(2, getLiveCoeff("r2/right_arm/hand/ringlittle/little_flexor/EncoderOffset"));
00244 }
00245
00246 TEST_F(TubeTareMechanismTest, setTighten)
00247 {
00248 TubeTareMechanism<4> thumbJoint("r2/right_arm/hand/thumb", io, 500);
00249
00250 setInt16("r2/right_arm/hand/thumb/proximal_extensor/MotCom", 10);
00251
00252 float tightenVal = io.getMotorCoeff("r2/right_arm/hand/thumb/MotComTightenMainExt");
00253
00254 thumbJoint.setTighten(std::vector<std::string>());
00255
00256 EXPECT_FLOAT_EQ(tightenVal, getInt16("r2/right_arm/hand/thumb/proximal_extensor/MotCom"));
00257
00258 }
00259
00260 TEST_F(TubeTareMechanismTest, goodPosition)
00261 {
00262 TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500);
00263
00264 float minTravel = io.getMotorCoeff("r2/right_arm/hand/index/EncoderMinTravel");
00265 float maxTravel = io.getMotorCoeff("r2/right_arm/hand/index/EncoderMaxTravel");
00266
00267 setInt16("r2/right_arm/hand/index/proximal_extensor/IncEnc", minTravel - 1);
00268 setInt16("r2/right_arm/hand/index/proximal_flexor/IncEnc", minTravel - 1);
00269 setInt16("r2/right_arm/hand/index/medial_extensor/IncEnc", minTravel - 1);
00270 setInt16("r2/right_arm/hand/index/medial_flexor/IncEnc", minTravel - 1);
00271
00272 EXPECT_FLOAT_EQ(-1, fingerJoint.goodPosition());
00273
00274 setInt16("r2/right_arm/hand/index/proximal_extensor/IncEnc", maxTravel - 1);
00275 setInt16("r2/right_arm/hand/index/proximal_flexor/IncEnc", maxTravel - 1);
00276 setInt16("r2/right_arm/hand/index/medial_extensor/IncEnc", maxTravel - 1);
00277 setInt16("r2/right_arm/hand/index/medial_flexor/IncEnc", maxTravel - 1);
00278
00279 EXPECT_FLOAT_EQ(0, fingerJoint.goodPosition());
00280
00281 setInt16("r2/right_arm/hand/index/proximal_extensor/IncEnc", maxTravel + 1);
00282 setInt16("r2/right_arm/hand/index/proximal_flexor/IncEnc", maxTravel + 1);
00283 setInt16("r2/right_arm/hand/index/medial_extensor/IncEnc", maxTravel + 1);
00284 setInt16("r2/right_arm/hand/index/medial_flexor/IncEnc", maxTravel + 1);
00285
00286 EXPECT_FLOAT_EQ(1, fingerJoint.goodPosition());
00287
00288 }
00289
00290 TEST_F(TubeTareMechanismTest, getSliderTarePos)
00291 {
00292 TubeTareMechanism<2> ringlittleJoint("r2/right_arm/hand/ringlittle", io, 500);
00293
00294 setInt16("r2/right_arm/hand/ringlittle/ringlittle_extensor/IncEnc", 0);
00295 setInt16("r2/right_arm/hand/ringlittle/ring_flexor/IncEnc", 1);
00296 setInt16("r2/right_arm/hand/ringlittle/little_flexor/IncEnc", 2);
00297
00298 setUint16("r2/right_arm/hand/ringlittle/ring/Hall", 30);
00299 setUint16("r2/right_arm/hand/ringlittle/little/Hall", 30);
00300
00301 ringlittleJoint.getSliderTarePos();
00302
00303 EXPECT_FLOAT_EQ(0, getLiveCoeff("r2/right_arm/hand/ringlittle/ringlittle_extensor/SliderOffset"));
00304 EXPECT_FLOAT_EQ(1, getLiveCoeff("r2/right_arm/hand/ringlittle/ring_flexor/SliderOffset"));
00305 EXPECT_FLOAT_EQ(2, getLiveCoeff("r2/right_arm/hand/ringlittle/little_flexor/SliderOffset"));
00306
00307
00308
00309
00310
00311 }
00312
00313 TEST_F(TubeTareMechanismTest, getTensionTareValue)
00314 {
00315 TubeTareMechanism<3> fingerJoint("r2/right_arm/hand/index", io, 500);
00316
00317 setUint16("r2/right_arm/hand/index/proximal_extensor/TensionA", 0);
00318 setUint16("r2/right_arm/hand/index/proximal_flexor/TensionA", 1);
00319 setUint16("r2/right_arm/hand/index/medial_extensor/TensionA", 2);
00320 setUint16("r2/right_arm/hand/index/medial_flexor/TensionA", 3);
00321 setUint16("r2/right_arm/hand/index/proximal_extensor/TensionB", 0);
00322 setUint16("r2/right_arm/hand/index/proximal_flexor/TensionB", 1);
00323 setUint16("r2/right_arm/hand/index/medial_extensor/TensionB", 2);
00324 setUint16("r2/right_arm/hand/index/medial_flexor/TensionB", 3);
00325
00326 fingerJoint.getTensionTareValue();
00327
00328 EXPECT_FLOAT_EQ(650, getLiveCoeff("r2/right_arm/hand/index/proximal_extensor/TensionOffset"));
00329 EXPECT_FLOAT_EQ(650.02, getLiveCoeff("r2/right_arm/hand/index/proximal_flexor/TensionOffset"));
00330 EXPECT_FLOAT_EQ(650.04, getLiveCoeff("r2/right_arm/hand/index/medial_extensor/TensionOffset"));
00331 EXPECT_FLOAT_EQ(650.06, getLiveCoeff("r2/right_arm/hand/index/medial_flexor/TensionOffset"));
00332
00333 setUint16("r2/right_arm/hand/index/proximal_extensor/TensionA", 40);
00334 setUint16("r2/right_arm/hand/index/proximal_flexor/TensionA", 10);
00335 setUint16("r2/right_arm/hand/index/medial_extensor/TensionA", 20);
00336 setUint16("r2/right_arm/hand/index/medial_flexor/TensionA", 30);
00337 setUint16("r2/right_arm/hand/index/proximal_extensor/TensionB", 40);
00338 setUint16("r2/right_arm/hand/index/proximal_flexor/TensionB", 10);
00339 setUint16("r2/right_arm/hand/index/medial_extensor/TensionB", 20);
00340 setUint16("r2/right_arm/hand/index/medial_flexor/TensionB", 30);
00341
00342 fingerJoint.getTensionTareValue();
00343
00344 EXPECT_FLOAT_EQ(650.4, getLiveCoeff("r2/right_arm/hand/index/proximal_extensor/TensionOffset"));
00345 EXPECT_FLOAT_EQ(650.11, getLiveCoeff("r2/right_arm/hand/index/proximal_flexor/TensionOffset"));
00346 EXPECT_FLOAT_EQ(650.22, getLiveCoeff("r2/right_arm/hand/index/medial_extensor/TensionOffset"));
00347 EXPECT_FLOAT_EQ(650.33, getLiveCoeff("r2/right_arm/hand/index/medial_flexor/TensionOffset"));
00348 }
00349
00350 int main(int argc, char** argv)
00351 {
00352 testing::InitGoogleTest(&argc, argv);
00353 return RUN_ALL_TESTS();
00354 }