test_timedAction.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include "testUtils.h"
3 
4 #include <ros/ros.h>
5 
12 
13 namespace {
14 
15 class testTimedAction: public ::testing::Test {
16 
17 
18 protected:
19 
20  testTimedAction() : timedAction("TestTimedAction") {
21  }
22 
23  virtual ~testTimedAction() {
24  }
25 
26  virtual void SetUp() override {
27 
28  std::shared_ptr <ROSEE::ParserMoveIt> parserMoveIt = std::make_shared <ROSEE::ParserMoveIt> ();
29 
30  //if return false, models are not found and it is useless to continue the test
31  ASSERT_TRUE(parserMoveIt->init ("robot_description", false)) ;
32 
33  ROSEE::FindActions actionsFinder (parserMoveIt);
34 
35  std::string folderForActions = "ROSEE/actions/" + parserMoveIt->getHandName();
36 
37  trigMap = actionsFinder.findTrig(ROSEE::ActionPrimitive::Type::Trig, folderForActions + "/primitives/") ;
38  auto pinches = actionsFinder.findPinch(folderForActions + "/primitives/") ;
39 
40  pinchTightMap = pinches.first;
41  pinchLooseMap = pinches.second;
42 
43  std::shared_ptr<ROSEE::ActionComposed> actionComposed = std::make_shared<ROSEE::ActionComposed> ("degrasp");
44 
45  if (trigMap.size() > 0){
46  for (auto trig : trigMap) {
47  std::shared_ptr <ROSEE::ActionPrimitive> pointer =
48  std::make_shared <ROSEE::ActionTrig> ( trig.second );
49 
50  actionComposed->sumAction(pointer, 0);
51 
52  //different name for each trig inserted
53  timedAction.insertAction(pointer, 0.54, 0.2, 0, 0.8, "trig_" + *(pointer->getKeyElements().begin()) );
54  }
55  }
56 
57  //lets add a pinch
58  if (pinchTightMap.size()>0) {
59 
60  std::shared_ptr <ROSEE::ActionPrimitive> pointer =
61  std::make_shared <ROSEE::ActionPinchTight> ( pinchTightMap.begin()->second );
62 
63  timedAction.insertAction(pointer, 0, 0.45);
64 
65  } else if (pinchLooseMap.size()>0) {
66 
67  std::shared_ptr <ROSEE::ActionPrimitive> pointer =
68  std::make_shared <ROSEE::ActionPinchLoose> ( pinchLooseMap.begin()->second );
69 
70  timedAction.insertAction(pointer, 0, 0.45);
71  }
72 
73  //now lets add a reset grasp pos (trig all zero)
74  if (actionComposed->numberOfInnerActions( ) > 0) {
75  std::shared_ptr <ROSEE::Action> pointer = actionComposed ;
76  timedAction.insertAction(pointer);
77  }
78 
79  ROSEE::YamlWorker yamlWorker;
80  yamlWorker.createYamlFile (&timedAction, folderForActions + "/timeds/");
81 
82  //Parsing
83  timedActionParsed = yamlWorker.parseYamlTimed (folderForActions + "/timeds/TestTimedAction.yaml");
84 
85  }
86 
87  virtual void TearDown() override{
88  }
89 
90  ROSEE::ActionTrig::Map trigMap;
91  ROSEE::ActionPinchTight::Map pinchTightMap;
92  ROSEE::ActionPinchLoose::Map pinchLooseMap;
93 
94  ROSEE::ActionTimed timedAction;
95  std::shared_ptr<ROSEE::ActionTimed> timedActionParsed;
96 
97 
98 };
99 
100 TEST_F ( testTimedAction, checkMembersSizeConsistency ) {
101 
102  EXPECT_EQ ( timedAction.getInnerActionsNames().size(), timedAction.getAllActionMargins().size() );
103  EXPECT_EQ ( timedAction.getAllActionMargins().size(), timedAction.getAllJointCountAction().size() );
104  EXPECT_EQ ( timedAction.getAllJointCountAction().size(), timedAction.getAllJointPos().size() );
105 
106  EXPECT_EQ ( timedActionParsed->getInnerActionsNames().size(), timedActionParsed->getAllActionMargins().size() );
107  EXPECT_EQ ( timedActionParsed->getAllActionMargins().size(), timedActionParsed->getAllJointCountAction().size() );
108  EXPECT_EQ ( timedActionParsed->getAllJointCountAction().size(), timedActionParsed->getAllJointPos().size() );
109 
110 }
111 
112 TEST_F ( testTimedAction, checkEmitParse ) {
113 
114  if (timedAction.getInnerActionsNames().size() > 0) {
115 
116  EXPECT_EQ (timedAction.getName(), timedActionParsed->getName() );
117  EXPECT_EQ (timedAction.getType(), timedActionParsed->getType() );
118  EXPECT_EQ (timedAction.getFingersInvolved(), timedActionParsed->getFingersInvolved() );
119 
120  for (auto jointCount : timedAction.getJointsInvolvedCount()) {
121  EXPECT_EQ ( jointCount.second, timedActionParsed->getJointsInvolvedCount().at(jointCount.first) );
122  }
123 
124  for (auto joint: timedAction.getJointPos() ) {
125 
126  //compare size of joint (number of dofs)
127  ASSERT_EQ (joint.second.size(), timedActionParsed->getJointPos().at(joint.first).size() );
128  //loop the eventually multiple joint pos (when dofs > 1)
129  for (int j = 0; j < joint.second.size(); ++j ){
130  EXPECT_DOUBLE_EQ ( joint.second.at(j), timedActionParsed->getJointPos().at(joint.first).at(j) );
131  }
132  }
133 
134  unsigned int k = 0;
135  for (auto innerActName : timedAction.getInnerActionsNames()) {
136  EXPECT_EQ ( innerActName, timedActionParsed->getInnerActionsNames().at(k) );
137  k++;
138  }
139 
140  k = 0;
141  for (auto timeMargins : timedAction.getAllActionMargins() ) {
142 
143  EXPECT_DOUBLE_EQ (timeMargins.first, timedActionParsed->getAllActionMargins().at(k).first);
144  EXPECT_DOUBLE_EQ (timeMargins.second, timedActionParsed->getAllActionMargins().at(k).second);
145  k++;
146  }
147 
148  auto jpvector = timedAction.getAllJointPos();
149  for (int i=0; i < jpvector.size(); i++) {
150 
151  for (auto joint: jpvector.at(i) ) {
152 
153  auto otherjointPos = timedActionParsed->getAllJointPos().at(i).at(joint.first);
154  //compare size of joint (number of dofs)
155  ASSERT_EQ (joint.second.size(), otherjointPos.size() );
156 
157  //loop the eventually multiple joint pos (when dofs > 1)
158  for (int j = 0; j < joint.second.size(); ++j ){
159  EXPECT_DOUBLE_EQ ( joint.second.at(j), otherjointPos.at(j) );
160  }
161  }
162  }
163 
164  auto jpcvector = timedAction.getAllJointCountAction();
165  for (int i=0; i < jpcvector.size(); i++) {
166 
167  for (auto joint: jpcvector.at(i) ) {
168 
169  EXPECT_EQ ( joint.second, timedActionParsed->getAllJointCountAction().at(i).at(joint.first) );
170 
171  }
172  }
173 
174  }
175 }
176 
177 } //namespace
178 
179 int main ( int argc, char **argv ) {
180 
181  if (argc < 2 ){
182 
183  std::cout << "[TEST ERROR] Insert hand name as argument" << std::endl;
184  return -1;
185  }
186 
187  /* Run tests on an isolated roscore */
188  if(setenv("ROS_MASTER_URI", "http://localhost:11322", 1) == -1)
189  {
190  perror("setenv");
191  return 1;
192  }
193 
194  //run roscore
195  std::unique_ptr<ROSEE::TestUtils::Process> roscore;
196  roscore.reset(new ROSEE::TestUtils::Process({"roscore", "-p", "11322"}));
197 
198  if ( ROSEE::TestUtils::prepareROSForTests ( argc, argv, "testTimedAction" ) != 0 ) {
199 
200  std::cout << "[TEST ERROR] Prepare Function failed" << std::endl;
201  return -1;
202  }
203 
204 
205  ::testing::InitGoogleTest ( &argc, argv );
206  return RUN_ALL_TESTS();
207 }
std::map< std::string, ActionTrig > Map
Definition: ActionTrig.h:69
std::shared_ptr< ROSEE::ActionTimed > parseYamlTimed(std::string fileWithPath)
Parse a timed Action.
Definition: YamlWorker.cpp:300
std::map< std::pair< std::string, std::string >, ActionPinchTight > Map
#define EXPECT_EQ(a, b)
std::map< std::pair< std::string, std::string >, ActionPinchLoose > Map
int prepareROSForTests(int argc, char **argv, std::string testName)
Function to be called in the main of each test, it runs roscore and fill parameter server with robot ...
Definition: testUtils.h:95
int main(int argc, char **argv)
An action composed by other ones that must be executed one after other with some wait time (also 0) i...
Definition: ActionTimed.h:39
Class to check which fingertips collide (for the pinch action at the moment)
Definition: FindActions.h:36
TEST_F(TestRunner, threaded_test)
std::string createYamlFile(const std::map< std::set< std::string >, ActionPrimitive * > mapOfActions, const std::string actionName, std::string pathFolder)
Create/overwrite yaml file and emit info on it about each ActionPrimitive inside the given mapOfActio...
Definition: YamlWorker.cpp:39


end-effector
Author(s): Luca Muratore , Davide Torielli , Liana Bertoni
autogenerated on Tue Apr 5 2022 02:57:53