3 #include <gtest/gtest.h> 8 TEST(MotionBuilderModelTest, jointLimitTest)
13 double valid_pos = 0.0;
14 double too_low_pos = -2.0;
15 double too_big_pos = 2.0;
17 EXPECT_TRUE(jm2.
inLimits(valid_pos));
18 EXPECT_FALSE(jm2.
inLimits(too_low_pos));
19 EXPECT_FALSE(jm2.
inLimits(too_big_pos));
22 TEST(MotionBuilderModelTest, keyframeTest)
41 std::map<std::string, bool> used;
42 used[
"Test1"] =
false;
51 std::vector<std::string> joint_list;
52 joint_list.push_back(
"Test1");
56 em << k2.
print(0.0, 1.0, joint_list);
57 EXPECT_EQ(
"time_from_start: 5\npositions: [1]", std::string(em.c_str()));
60 em2 << k2.
print(1.0, 1.0, joint_list);
61 EXPECT_EQ(
"time_from_start: 6\npositions: [1]", std::string(em2.c_str()));
64 em3 << k2.
print(0.0, 2.0, joint_list);
65 EXPECT_EQ(
"time_from_start: 10\npositions: [1]", std::string(em3.c_str()));
68 em4 << k2.
print(1.0, 2.0, joint_list);
69 EXPECT_EQ(
"time_from_start: 11\npositions: [1]", std::string(em4.c_str()));
73 em5 << k2.
print(joint_list);
74 EXPECT_EQ(
"joints: [Test1]\npoints:\n - time_from_start: 0\n " 76 std::string(em5.c_str()));
79 TEST(MotionBuilderModelTest, motionTest)
84 EXPECT_FALSE(m.jointsLoaded());
85 for (
int i = 0; i < 3; ++i)
88 m.addJointModel(
"Test_joint_" + std::to_string(i + 1), jm);
90 EXPECT_FALSE(m.jointsLoaded());
92 for (
int i = 0; i < 3; ++i)
94 m.addJointToGroup(
"TestGroup",
"Test_joint_" + std::to_string(i + 1));
96 EXPECT_TRUE(m.jointsLoaded());
97 m.setCurrentGroup(
"TestGroup");
98 EXPECT_EQ(3, m.getJoints().size());
101 sensor_msgs::JointState js1;
102 js1.name.push_back(
"Test_joint_1");
103 js1.position.push_back(1.1);
104 js1.name.push_back(
"Test_joint_2");
105 js1.position.push_back(1.2);
106 js1.name.push_back(
"Test_joint_3");
107 js1.position.push_back(1.3);
108 js1.name.push_back(
"Test_joint_4");
109 js1.position.push_back(1.4);
110 sensor_msgs::JointStateConstPtr js1c(
new sensor_msgs::JointState(js1));
112 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime()) <<
"Should be 0 as it's first " 114 EXPECT_EQ(1.1, m.getKeyFrame(0).getJointPosition(
"Test_joint_1"));
115 EXPECT_EQ(1.2, m.getKeyFrame(0).getJointPosition(
"Test_joint_2"));
116 EXPECT_EQ(1.3, m.getKeyFrame(0).getJointPosition(
"Test_joint_3"));
117 EXPECT_TRUE(std::isnan(m.getKeyFrame(0).getJointPosition(
"Test_joint_4")))
118 <<
"Should be NaN as it is not on used list";
122 EXPECT_EQ(1.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
123 EXPECT_EQ(1.2, m.getKeyFrame(1).getJointPosition(
"Test_joint_2"));
124 EXPECT_EQ(1.3, m.getKeyFrame(1).getJointPosition(
"Test_joint_3"));
125 EXPECT_TRUE(std::isnan(m.getKeyFrame(1).getJointPosition(
"Test_joint_4")))
126 <<
"Should be NaN as it is not on used list";
129 sensor_msgs::JointState js2;
130 js2.name.push_back(
"Test_joint_1");
131 js2.position.push_back(2.1);
132 js2.name.push_back(
"Test_joint_2");
133 js2.position.push_back(2.2);
134 js2.name.push_back(
"Test_joint_3");
135 js2.position.push_back(2.3);
136 js2.name.push_back(
"Test_joint_4");
137 js2.position.push_back(2.4);
138 sensor_msgs::JointStateConstPtr js2c(
new sensor_msgs::JointState(js2));
140 m.updateKeyFrame(js2c, 1);
141 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime()) <<
"Should be 0 as it's first " 143 EXPECT_EQ(1.1, m.getKeyFrame(0).getJointPosition(
"Test_joint_1"));
144 EXPECT_EQ(1.2, m.getKeyFrame(0).getJointPosition(
"Test_joint_2"));
145 EXPECT_EQ(1.3, m.getKeyFrame(0).getJointPosition(
"Test_joint_3"));
146 EXPECT_TRUE(std::isnan(m.getKeyFrame(0).getJointPosition(
"Test_joint_4")))
147 <<
"Should be NaN as it is not used";
149 EXPECT_EQ(2.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
150 EXPECT_EQ(2.2, m.getKeyFrame(1).getJointPosition(
"Test_joint_2"));
151 EXPECT_EQ(2.3, m.getKeyFrame(1).getJointPosition(
"Test_joint_3"));
152 EXPECT_TRUE(std::isnan(m.getKeyFrame(1).getJointPosition(
"Test_joint_4")))
153 <<
"Should be NaN as it is not used";
156 EXPECT_THROW(m.changeTime(5, 1.0),
ros::Exception) <<
"Trying to modify a non-existing " 157 "frame should result in an Exception";
158 m.changeTime(1, 2.0);
159 EXPECT_EQ(2.0, m.getKeyFrame(1).getTime());
163 <<
"Trying to modify a non-existing frame should result in an Exception";
164 EXPECT_THROW(m.changeJoint(1,
"Non-existing", 1.0),
ros::Exception)
165 <<
"Trying to modify a non-existing joint should result in an Exception";
166 double res = m.changeJoint(1,
"Test_joint_1", 5000.0);
168 EXPECT_EQ(2.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"))
169 <<
"If value not in joint limits no change";
170 res = m.changeJoint(1,
"Test_joint_1", 3.1);
172 EXPECT_EQ(3.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
176 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime()) <<
"Should be 0 as it's first frame";
177 EXPECT_EQ(1.1, m.getKeyFrame(0).getJointPosition(
"Test_joint_1"));
178 EXPECT_EQ(1.2, m.getKeyFrame(0).getJointPosition(
"Test_joint_2"));
179 EXPECT_EQ(1.3, m.getKeyFrame(0).getJointPosition(
"Test_joint_3"));
180 EXPECT_TRUE(std::isnan(m.getKeyFrame(0).getJointPosition(
"Test_joint_4")))
181 <<
"Should be NaN as it is not used";
182 EXPECT_EQ(2.0, m.getKeyFrame(1).getTime());
183 EXPECT_EQ(3.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
184 EXPECT_EQ(2.2, m.getKeyFrame(1).getJointPosition(
"Test_joint_2"));
185 EXPECT_EQ(2.3, m.getKeyFrame(1).getJointPosition(
"Test_joint_3"));
186 EXPECT_TRUE(std::isnan(m.getKeyFrame(1).getJointPosition(
"Test_joint_4")))
187 <<
"Should be NaN as it is not used";
189 EXPECT_EQ(1.1, m.getKeyFrame(2).getJointPosition(
"Test_joint_1"));
190 EXPECT_EQ(1.2, m.getKeyFrame(2).getJointPosition(
"Test_joint_2"));
191 EXPECT_EQ(1.3, m.getKeyFrame(2).getJointPosition(
"Test_joint_3"));
192 EXPECT_TRUE(std::isnan(m.getKeyFrame(2).getJointPosition(
"Test_joint_4")))
193 <<
"Should be NaN as it is not used";
196 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime()) <<
"Should be 0 as it's first frame";
197 EXPECT_EQ(1.1, m.getKeyFrame(0).getJointPosition(
"Test_joint_1"));
198 EXPECT_EQ(1.2, m.getKeyFrame(0).getJointPosition(
"Test_joint_2"));
199 EXPECT_EQ(1.3, m.getKeyFrame(0).getJointPosition(
"Test_joint_3"));
200 EXPECT_TRUE(std::isnan(m.getKeyFrame(0).getJointPosition(
"Test_joint_4")))
201 <<
"Should be NaN as it is not used";
202 EXPECT_EQ(2.0, m.getKeyFrame(1).getTime());
203 EXPECT_EQ(3.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
204 EXPECT_EQ(2.2, m.getKeyFrame(1).getJointPosition(
"Test_joint_2"));
205 EXPECT_EQ(2.3, m.getKeyFrame(1).getJointPosition(
"Test_joint_3"));
206 EXPECT_TRUE(std::isnan(m.getKeyFrame(1).getJointPosition(
"Test_joint_4")))
207 <<
"Should be NaN as it is not used";
208 EXPECT_EQ(2.0, m.getKeyFrame(2).getTime());
209 EXPECT_EQ(3.1, m.getKeyFrame(2).getJointPosition(
"Test_joint_1"));
210 EXPECT_EQ(2.2, m.getKeyFrame(2).getJointPosition(
"Test_joint_2"));
211 EXPECT_EQ(2.3, m.getKeyFrame(2).getJointPosition(
"Test_joint_3"));
212 EXPECT_TRUE(std::isnan(m.getKeyFrame(2).getJointPosition(
"Test_joint_4")))
213 <<
"Should be NaN as it is not used";
215 EXPECT_EQ(1.1, m.getKeyFrame(3).getJointPosition(
"Test_joint_1"));
216 EXPECT_EQ(1.2, m.getKeyFrame(3).getJointPosition(
"Test_joint_2"));
217 EXPECT_EQ(1.3, m.getKeyFrame(3).getJointPosition(
"Test_joint_3"));
218 EXPECT_TRUE(std::isnan(m.getKeyFrame(3).getJointPosition(
"Test_joint_4")))
219 <<
"Should be NaN as it is not used";
222 EXPECT_EQ(4, m.size());
225 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime()) <<
"Should be 0 as it's first frame";
226 EXPECT_EQ(1.1, m.getKeyFrame(0).getJointPosition(
"Test_joint_1"));
227 EXPECT_EQ(1.2, m.getKeyFrame(0).getJointPosition(
"Test_joint_2"));
228 EXPECT_EQ(1.3, m.getKeyFrame(0).getJointPosition(
"Test_joint_3"));
229 EXPECT_TRUE(std::isnan(m.getKeyFrame(0).getJointPosition(
"Test_joint_4")))
230 <<
"Should be NaN as it is not used";
232 EXPECT_EQ(1.1, m.getKeyFrame(1).getJointPosition(
"Test_joint_1"));
233 EXPECT_EQ(1.2, m.getKeyFrame(1).getJointPosition(
"Test_joint_2"));
234 EXPECT_EQ(1.3, m.getKeyFrame(1).getJointPosition(
"Test_joint_3"));
235 EXPECT_TRUE(std::isnan(m.getKeyFrame(1).getJointPosition(
"Test_joint_4")))
236 <<
"Should be NaN as it is not used";
237 EXPECT_EQ(2, m.size());
240 m.changeTime(1, 2.0);
245 EXPECT_EQ(
"joints: [Test_joint_1, Test_joint_2, Test_joint_3]\n" 247 "- time_from_start: 0\n " 248 "positions: [1.1, 1.2, 1.3]\n " 249 "- time_from_start: 2\n " 250 "positions: [1.1, 1.2, 1.3]\n " 251 "- time_from_start: 7\n " 252 "positions: [1.1, 1.2, 1.3]",
253 std::string(em.c_str()));
257 EXPECT_EQ(
"joints: [Test_joint_1, Test_joint_2, Test_joint_3]\n" 259 "- time_from_start: 0\n " 260 "positions: [1.1, 1.2, 1.3]\n " 261 "- time_from_start: 4\n " 262 "positions: [1.1, 1.2, 1.3]\n " 263 "- time_from_start: 14\n " 264 "positions: [1.1, 1.2, 1.3]",
265 std::string(em2.c_str()));
268 template <
typename InputIterator1,
typename InputIterator2>
269 bool range_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
270 InputIterator2 last2)
272 while (first1 != last1 && first2 != last2)
274 if (*first1 != *first2)
279 return (first1 == last1) && (first2 == last2);
282 bool compare_files(
const std::string& filename1,
const std::string& filename2)
284 std::ifstream file1(filename1);
285 std::ifstream file2(filename2);
287 std::istreambuf_iterator<char> begin1(file1);
288 std::istreambuf_iterator<char> begin2(file2);
290 std::istreambuf_iterator<char> end;
295 TEST(MotionBuilderModelTest, motionFromParamTest)
299 nh.
getParam(
"/play_motion/motions/test_motion", param_to_load);
306 "<robot name=\"testbot\"><joint name=\"arm_left_1_joint\" type=\"revolute\"/>" 307 "<joint name=\"arm_left_2_joint\" type=\"revolute\"/>" 308 "<joint name=\"arm_left_3_joint\" type=\"revolute\"/>" 309 "<joint name=\"arm_left_4_joint\" type=\"revolute\"/>" 310 "<joint name=\"arm_left_5_joint\" type=\"revolute\"/>" 311 "<joint name=\"arm_left_6_joint\" type=\"revolute\"/>" 312 "<joint name=\"arm_left_7_joint\" type=\"revolute\"/>" 313 "<joint name=\"arm_right_1_joint\" type=\"revolute\"/>" 314 "<joint name=\"arm_right_2_joint\" type=\"revolute\"/>" 315 "<joint name=\"arm_right_3_joint\" type=\"revolute\"/>" 316 "<joint name=\"arm_right_4_joint\" type=\"revolute\"/>" 317 "<joint name=\"arm_right_5_joint\" type=\"revolute\"/>" 318 "<joint name=\"arm_right_6_joint\" type=\"revolute\"/>" 319 "<joint name=\"arm_right_7_joint\" type=\"revolute\"/>" 320 "<joint name=\"torso_1_joint\" type=\"revolute\"/>" 321 "<joint name=\"torso_2_joint\" type=\"revolute\"/></robot>",
322 "<?xml version=\"1.0\" ?><robot name=\"testbot\"><group name=\"arm_left\">" 323 "<joint name=\"arm_left_1_joint\" /><joint name=\"arm_left_2_joint\" />" 324 "<joint name=\"arm_left_3_joint\"/><joint name=\"arm_left_4_joint\" />" 325 "<joint name=\"arm_left_5_joint\"/><joint name=\"arm_left_6_joint\"/>" 326 "<joint name=\"arm_left_7_joint\"/></group><group name=\"arm_right\">" 327 "<joint name=\"arm_right_1_joint\" /><joint name=\"arm_right_2_joint\" />" 328 "<joint name=\"arm_right_3_joint\" /><joint name=\"arm_right_4_joint\" />" 329 "<joint name=\"arm_right_5_joint\"/><joint name=\"arm_right_6_joint\"/>" 330 "<joint name=\"arm_right_7_joint\"/></group><group name=\"torso\">" 331 "<joint name=\"torso_1_joint\"/><joint name=\"torso_2_joint\"/></group>" 332 "<group name=\"both_arms\"><group name=\"arm_left\" /><group name=\"arm_right\" />" 333 "</group><group name=\"both_arms_torso\"><group name=\"arm_left\" />" 334 "<group name=\"arm_right\" /><group name=\"torso\" /></group></robot>",
335 {
"head_1_joint",
"head_2_joint" });
338 EXPECT_EQ(
"both_arms_torso", m.getCurrentGroup());
342 EXPECT_EQ(7, m.getJoints().size());
343 EXPECT_EQ(
"arm_left_1_joint", m.getJoints()[0]);
344 EXPECT_EQ(
"arm_left_2_joint", m.getJoints()[1]);
345 EXPECT_EQ(
"arm_left_3_joint", m.getJoints()[2]);
346 EXPECT_EQ(
"arm_left_4_joint", m.getJoints()[3]);
347 EXPECT_EQ(
"arm_left_5_joint", m.getJoints()[4]);
348 EXPECT_EQ(
"arm_left_6_joint", m.getJoints()[5]);
349 EXPECT_EQ(
"arm_left_7_joint", m.getJoints()[6]);
351 m.setCurrentGroup(
"arm_right");
352 EXPECT_EQ(7, m.getJoints().size());
353 EXPECT_EQ(
"arm_right_1_joint", m.getJoints()[0]);
354 EXPECT_EQ(
"arm_right_2_joint", m.getJoints()[1]);
355 EXPECT_EQ(
"arm_right_3_joint", m.getJoints()[2]);
356 EXPECT_EQ(
"arm_right_4_joint", m.getJoints()[3]);
357 EXPECT_EQ(
"arm_right_5_joint", m.getJoints()[4]);
358 EXPECT_EQ(
"arm_right_6_joint", m.getJoints()[5]);
359 EXPECT_EQ(
"arm_right_7_joint", m.getJoints()[6]);
361 m.setCurrentGroup(
"torso");
362 EXPECT_EQ(2, m.getJoints().size());
363 EXPECT_EQ(
"torso_1_joint", m.getJoints()[0]);
364 EXPECT_EQ(
"torso_2_joint", m.getJoints()[1]);
366 m.setCurrentGroup(
"both_arms");
367 EXPECT_EQ(14, m.getJoints().size());
368 EXPECT_EQ(
"arm_left_1_joint", m.getJoints()[0]);
369 EXPECT_EQ(
"arm_left_2_joint", m.getJoints()[1]);
370 EXPECT_EQ(
"arm_left_3_joint", m.getJoints()[2]);
371 EXPECT_EQ(
"arm_left_4_joint", m.getJoints()[3]);
372 EXPECT_EQ(
"arm_left_5_joint", m.getJoints()[4]);
373 EXPECT_EQ(
"arm_left_6_joint", m.getJoints()[5]);
374 EXPECT_EQ(
"arm_left_7_joint", m.getJoints()[6]);
375 EXPECT_EQ(
"arm_right_1_joint", m.getJoints()[7]);
376 EXPECT_EQ(
"arm_right_2_joint", m.getJoints()[8]);
377 EXPECT_EQ(
"arm_right_3_joint", m.getJoints()[9]);
378 EXPECT_EQ(
"arm_right_4_joint", m.getJoints()[10]);
379 EXPECT_EQ(
"arm_right_5_joint", m.getJoints()[11]);
380 EXPECT_EQ(
"arm_right_6_joint", m.getJoints()[12]);
381 EXPECT_EQ(
"arm_right_7_joint", m.getJoints()[13]);
383 m.setCurrentGroup(
"both_arms_torso");
384 EXPECT_EQ(16, m.getJoints().size());
385 EXPECT_EQ(
"arm_left_1_joint", m.getJoints()[0]);
386 EXPECT_EQ(
"arm_left_2_joint", m.getJoints()[1]);
387 EXPECT_EQ(
"arm_left_3_joint", m.getJoints()[2]);
388 EXPECT_EQ(
"arm_left_4_joint", m.getJoints()[3]);
389 EXPECT_EQ(
"arm_left_5_joint", m.getJoints()[4]);
390 EXPECT_EQ(
"arm_left_6_joint", m.getJoints()[5]);
391 EXPECT_EQ(
"arm_left_7_joint", m.getJoints()[6]);
392 EXPECT_EQ(
"arm_right_1_joint", m.getJoints()[7]);
393 EXPECT_EQ(
"arm_right_2_joint", m.getJoints()[8]);
394 EXPECT_EQ(
"arm_right_3_joint", m.getJoints()[9]);
395 EXPECT_EQ(
"arm_right_4_joint", m.getJoints()[10]);
396 EXPECT_EQ(
"arm_right_5_joint", m.getJoints()[11]);
397 EXPECT_EQ(
"arm_right_6_joint", m.getJoints()[12]);
398 EXPECT_EQ(
"arm_right_7_joint", m.getJoints()[13]);
399 EXPECT_EQ(
"torso_1_joint", m.getJoints()[14]);
400 EXPECT_EQ(
"torso_2_joint", m.getJoints()[15]);
402 EXPECT_EQ(4, m.size()) <<
"There should be 4 keyframes";
404 EXPECT_EQ(0.0, m.getKeyFrame(0).getTime());
405 EXPECT_EQ(3.0, m.getKeyFrame(1).getTime());
406 EXPECT_EQ(3.0, m.getKeyFrame(2).getTime());
407 EXPECT_EQ(3.0, m.getKeyFrame(3).getTime());
410 sensor_msgs::JointState js;
411 js.name.push_back(
"head_1_joint");
412 js.position.push_back(0.0);
413 js.name.push_back(
"head_2_joint");
414 js.position.push_back(0.0);
415 js.name.push_back(
"");
416 sensor_msgs::JointStateConstPtr jsc(
new sensor_msgs::JointState(js));
419 m.addJointModel(
"head_1_joint", jm);
420 m.addJointModel(
"head_2_joint", jm);
421 EXPECT_FALSE(m.isJointUsed(
"head_1_joint"));
422 EXPECT_FALSE(m.isJointUsed(
"head_2_joint"));
425 EXPECT_FALSE(std::isnan(m.getKeyFrame(0).getJointPosition(
"head_1_joint")));
426 EXPECT_FALSE(std::isnan(m.getKeyFrame(0).getJointPosition(
"head_2_joint")));
427 EXPECT_FALSE(std::isnan(m.getKeyFrame(3).getJointPosition(
"head_1_joint")));
428 EXPECT_FALSE(std::isnan(m.getKeyFrame(3).getJointPosition(
"head_2_joint")));
432 em << YAML::BeginMap << YAML::Key <<
"play_motion" << YAML::Value << YAML::BeginMap
433 << YAML::Key <<
"motions" << YAML::Value << YAML::BeginMap << YAML::Key
434 <<
"test_motion" << YAML::Value
435 << m.print(param_to_load[
"meta"][
"name"], param_to_load[
"meta"][
"usage"],
436 param_to_load[
"meta"][
"description"])
437 << YAML::EndMap << YAML::EndMap << YAML::EndMap;
439 std::ofstream ofile(
"/tmp/tm.yaml");
444 std::string filepath;
445 nh.
getParam(
"test_file_path", filepath);
450 int main(
int argc,
char** argv)
452 ros::init(argc, argv,
"test_motion_model");
454 testing::InitGoogleTest(&argc, argv);
455 return RUN_ALL_TESTS();
void addPosition(const std::string &name, double position)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool compare_files(const std::string &filename1, const std::string &filename2)
bool setCurrentGroup(const std::string &group)
Type const & getType() const
bool getParam(const std::string &key, std::string &s) const
PrintPoint print(double basetime, double downshift, const std::vector< std::string > &names) const
bool range_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
void cleanUnused(const std::map< std::string, bool > &used_joints)
double getJointPosition(const std::string &joint) const
static const float DEFAULT_TIME
bool inLimits(double position)
TEST(MotionBuilderTest, actionNotStartedTest)
int main(int argc, char **argv)