bio_ik
MoveIt kinematics_base plugin based on particle optimization & GA
ik_test.cpp
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016-2017, Philipp Sebastian Ruppel
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include "ik_base.h"
36 
37 namespace bio_ik
38 {
39 
40 struct IKTest : IKBase
41 {
42 
43  RobotFK_MoveIt fkref;
44 
45  std::vector<double> temp;
46 
47  double d_rot_sum, d_pos_sum, d_div;
48 
49  IKTest(const IKParams& params)
50  : IKBase(params)
51  , fkref(params.robot_model)
52  {
53  d_rot_sum = d_pos_sum = d_div = 0;
54  }
55 
56  /*double tipdiff(const std::vector<Frame>& fa, const std::vector<Frame>& fb)
57  {
58  double diff = 0.0;
59  for(size_t i = 0; i < problem.tip_link_indices.size(); i++)
60  {
61  //LOG_VAR(fa[i]);
62  //LOG_VAR(fb[i]);
63  diff += fa[i].rot.angleShortestPath(fb[i].rot);
64  diff += fa[i].pos.distance(fb[i].pos);
65  }
66  return diff;
67  }*/
68 
69  void initialize(const Problem& problem)
70  {
71  IKBase::initialize(problem);
72 
73  fkref.initialize(problem.tip_link_indices);
74  model.initialize(problem.tip_link_indices);
75 
76  fkref.applyConfiguration(problem.initial_guess);
77  model.applyConfiguration(problem.initial_guess);
78 
79  // double diff = tipdiff(fkref.getTipFrames(), model.getTipFrames());
80  // LOG_VAR(diff);
81 
82  /*{
83  auto& fa = fkref.getTipFrames();
84  auto& fb = model.getTipFrames();
85  for(size_t i = 0; i < problem.tip_link_indices.size(); i++)
86  {
87  LOG("d rot", i, fa[i].rot.angleShortestPath(fb[i].rot));
88  LOG("d pos", i, fa[i].pos.distance(fb[i].pos));
89  }
90  }*/
91 
92  {
93  temp = problem.initial_guess;
94  for(size_t ivar : problem.active_variables)
95  if(modelInfo.isRevolute(ivar) || modelInfo.isPrismatic(ivar)) temp[ivar] = modelInfo.clip(temp[ivar] + random(-0.1, 0.1), ivar);
96 
97  fkref.applyConfiguration(temp);
98  auto& fa = fkref.getTipFrames();
99 
100  model.applyConfiguration(problem.initial_guess);
101  model.initializeMutationApproximator(problem.active_variables);
102 
103  std::vector<aligned_vector<Frame>> fbm;
104 
105  std::vector<double> mutation_values;
106  for(size_t ivar : problem.active_variables)
107  mutation_values.push_back(temp[ivar]);
108  const double* mutation_ptr = mutation_values.data();
109 
110  model.computeApproximateMutations(1, &mutation_ptr, fbm);
111 
112  auto& fb = fbm[0];
113 
114  // auto& fb = model.getTipFrames();
115 
116  for(size_t i = 0; i < problem.tip_link_indices.size(); i++)
117  {
118  // LOG("d rot", i, fa[i].rot.angleShortestPath(fb[i].rot));
119  // LOG("d pos", i, fa[i].pos.distance(fb[i].pos));
120 
121  d_rot_sum += fa[i].rot.angleShortestPath(fb[i].rot);
122  d_pos_sum += fa[i].pos.distance(fb[i].pos);
123  d_div += 1;
124  }
125  }
126 
127  LOG("d rot avg", d_rot_sum / d_div);
128  LOG("d pos avg", d_pos_sum / d_div);
129  }
130 
131  void step() {}
132 
133  const std::vector<double>& getSolution() const { return problem.initial_guess; }
134 };
135 
136 static IKFactory::Class<IKTest> test("test");
137 }