test_fk_1.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <gtest/gtest.h>
4 
5 #include <thread>
6 
7 #include "hebiros.h"
8 #include "hebiros/AddModelFromURDFSrv.h"
9 #include "hebiros/ModelFkSrv.h"
10 
11 class FKTests6DOF : public ::testing::Test {
12  public:
14  }
16 
17  static void AddModel() {
19 
20  ros::ServiceClient add_model_client = n.serviceClient<hebiros::AddModelFromURDFSrv>(
21  "/hebiros/add_model_from_urdf");
22 
23  hebiros::AddModelFromURDFSrv add_model_srv;
24 
25  add_model_srv.request.model_name = model_name;
26  add_model_srv.request.description_param = "6_dof_robot_description";
27 
28  add_model_client.call(add_model_srv);
29  }
30  protected:
31  // 6 DOF tests have 6 modules!!!
32  const size_t num_modules = 6;
33 
34  static const std::string model_name;
35 
37 
38  // Compare the results to those generated from MATLAB ground truth
39  void CompareResults(double* matlab, double* ros) {
40  // Note: we ignore duplicated "actuator internal" frames from the ROS API
41  std::vector<size_t> ignored_frames = { 0, 1, 4, 7, 10, 13, 16 };
42  auto search_start = ignored_frames.begin();
43  size_t matlab_frame = 0;
44  size_t ros_frame = 0;
45 
46  while (matlab_frame < 11 && ros_frame < 17) {
47  auto found = std::find(search_start, ignored_frames.end(), ros_frame);
48  if (found != ignored_frames.end()) {
49  search_start = found; // A tiny bit more efficient -- don't look through the starting elements
50  ++ros_frame;
51  continue;
52  }
53 
54  // Note: ML data is column major, ROS is row major
55  for (size_t i = 0; i < 3; ++i)
56  for (size_t j = 0; j < 3; ++j)
57  ASSERT_NEAR(matlab[matlab_frame*16 + i*4 + j], ros[ros_frame*16 + j*4 + i], 0.001);
58  ++ros_frame;
59  ++matlab_frame;
60  }
61  }
62 };
63 
64 const std::string FKTests6DOF::model_name { "6DOF" };
65 
66 // Note: the MATLAB ground truth is from the following script.
67 // unfortunately, reshape gives column-major reordering :(
68 //
69 // kin = HebiKinematics
70 // kin.addBody('X8-9');
71 // kin.addBody('X5-HeavyBracket', 'mount', 'right-outside');
72 // kin.addBody('X8-9', 'PosLim', [-0.6 1.2]); % gas spring limits
73 // kin.addBody('X5-Link', 'extension', 0.325, 'twist', pi);
74 // kin.addBody('X8-9');
75 // kin.addBody('X5-Link', 'extension', 0.325, 'twist', pi);
76 // kin.addBody('X5-1');
77 // kin.addBody('X5-LightBracket', 'mount', 'right');
78 // kin.addBody('X5-1');
79 // kin.addBody('X5-LightBracket', 'mount', 'right');
80 // kin.addBody('X5-1');
81 // reshape(kin.getFK('Output', <angle vector goes here>), 1, 11 * 16);
82 TEST_F(FKTests6DOF, GetOutputFrames) {
83  ros::ServiceClient model_fk_client = n.serviceClient<hebiros::ModelFkSrv>(
84  std::string("/hebiros/") + model_name + std::string("/fk"));
85 
86  hebiros::ModelFkSrv model_fk_srv;
87 
88  model_fk_srv.request.positions.resize(num_modules);
89 
90  // First, try with a vector of zeros
91  for (size_t i = 0; i < num_modules; ++i)
92  model_fk_srv.request.positions[i] = 0;
93  model_fk_srv.request.frame_type = hebiros::ModelFkSrv::Request::FrameTypeOutput;
94  ASSERT_TRUE(model_fk_client.call(model_fk_srv));
95  double ml[176] = {
96  1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0.0451, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,-1, 0, 0, 0,-0.0375, 0.11, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,-1, 0, 0, 0,-0.0826, 0.11, 1, 1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0.3250,-0.0826, 0.11, 1, 1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0.3250,-0.0375, 0.11, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,-1, 0, 0, 0.6500,-0.0375, 0.11, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,-1, 0, 0, 0.6500,-0.0685, 0.11, 1, 1, 0, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0.6500,-0.1086, 0.0571, 1, 1, 0, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0.6500,-0.1086, 0.0261, 1, 1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0.6500,-0.0656,-0.01, 1, 1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0.6500,-0.0345,-0.01, 1 };
97  CompareResults(ml, model_fk_srv.response.frames.data());
98 
99  // Some random angles:
100  model_fk_srv.request.positions = {.325, 12.22, 42.4, .033, .2352, 31.34};
101  ASSERT_TRUE(model_fk_client.call(model_fk_srv));
102  double ml2[176] = {
103  0.9477, 0.3193, 0, 0,-0.3193, 0.9477, 0, 0, 0, 0, 1, 0, 0, 0, 0.0451, 1, 0.9477, 0.3193, 0, 0,-0, 0, 1, 0, 0.3193,-0.9477, 0, 0, 0.0120,-0.0355, 0.1001, 1, 0.8914, 0.3003,-0.3395, 0, 0.3217, 0.1084, 0.9406, 0, 0.3193,-0.9477, 0, 0, 0.0264,-0.0783, 0.1001, 1, 0.8914, 0.3003,-0.3395, 0,-0.3217,-0.1084,-0.9406, 0,-0.3193, 0.9477,-0, 0, 0.3161, 0.0193,-0.0102, 1, 0.3114, 0.1049, 0.9445, 0, 0.8950, 0.3016,-0.3286, 0,-0.3193, 0.9477,-0, 0, 0.3017, 0.0621,-0.0102, 1, 0.3114, 0.1049, 0.9445, 0,-0.8950,-0.3016, 0.3286, 0, 0.3193,-0.9477, 0, 0, 0.4029, 0.0962, 0.2967, 1, 0.2817, 0.0949, 0.9548, 0,-0.9048,-0.3049, 0.2973, 0, 0.3193,-0.9477, 0, 0, 0.4128, 0.0668, 0.2967, 1, 0.2817, 0.0949, 0.9548, 0, 0.3193,-0.9477, 0, 0, 0.9048, 0.3049,-0.2973, 0, 0.4645, 0.0420, 0.2839, 1, 0.3484,-0.1285, 0.9285, 0, 0.2449,-0.9437,-0.2225, 0, 0.9048, 0.3049,-0.2973, 0, 0.4926, 0.0514, 0.2747, 1, 0.3484,-0.1285, 0.9285, 0, 0.9048, 0.3049,-0.2973, 0,-0.2449, 0.9437, 0.2225, 0, 0.5182, 0.1042, 0.2724, 1, 0.2788,-0.1513, 0.9484, 0, 0.9286, 0.2942,-0.2260, 0,-0.2449, 0.9437, 0.2225, 0, 0.5106, 0.1335, 0.2793, 1 };
104  CompareResults(ml2, model_fk_srv.response.frames.data());
105 }
106 
107 int main(int argc, char** argv) {
108  ros::init(argc, argv, "FKTests6DOFNode");
109 
110  testing::InitGoogleTest(&argc, argv);
111 
112  std::thread t([]{while(ros::ok()) ros::spin();});
113 
114  // This should only be run once, as it changes the state of the node itself
115  // (we should probably test the add model code separately, fwiw)
117 
118  auto res = RUN_ALL_TESTS();
119 
120  ros::shutdown();
121 
122  return res;
123 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
static void AddModel()
Definition: test_fk_1.cpp:17
int main(int argc, char **argv)
Definition: test_fk_1.cpp:107
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void CompareResults(double *matlab, double *ros)
Definition: test_fk_1.cpp:39
TEST_F(FKTests6DOF, GetOutputFrames)
Definition: test_fk_1.cpp:82
ROSCPP_DECL void spin(Spinner &spinner)
ROSCPP_DECL bool ok()
static const std::string model_name
Definition: test_fk_1.cpp:34
ROSCPP_DECL void shutdown()
ros::NodeHandle n
Definition: test_fk_1.cpp:36
const size_t num_modules
Definition: test_fk_1.cpp:32


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:09:39