KinematicSolver.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXGuiPlugins::RobotTrajectoryDesigner
17  * @author Timo Birr
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #ifndef KINEMATICSOLVER_H
23 #define KINEMATICSOLVER_H
24 
25 ///ARMARX-INCLUDES
27 
28 ///EIGEN-INCLUDES
29 #include <Eigen/Core>
30 #include <Eigen/Geometry>
31 
32 ///VIRTUAL-ROBOT-INCLUDES
33 #include <VirtualRobot/IK/DifferentialIK.h>
34 #include <VirtualRobot/IK/GenericIKSolver.h>
35 #include <VirtualRobot/Robot.h>
36 #include <VirtualRobot/RobotNodeSet.h>
37 
38 #include "MotionPlanning/CSpace/CSpacePath.h"
39 #include "VirtualRobot/IK/AdvancedIKSolver.h"
40 
41 ///STANDARD-INCLUDES
42 #include <map>
43 #include <memory>
44 
45 namespace armarx
46 {
47  /**
48  * \class KinematicSolver
49  * \brief Realizes the Singleton-Pattern, Provides Methods to solve Kinematic Problems (forward and inverse)
50  *
51  */
53  {
54 
55  /**
56  * @brief The IKCalibration struct
57  * Represents the settings for the Inverse-Jacobi-IK-Approach(IKIterations, stepSize) for three different IK-Solvers.
58  * Small: Recursive IK Multiple steps
59  * Medium: Recursive IK single step
60  * Large: solveIK
61  */
62  struct IKCalibration
63  {
64  public:
65  int smallIterations;
66  double smallStepSize;
67  int mediumIterations;
68  double mediumStepSize;
69  int largeIterations;
70  double largeStepSize;
71  static IKCalibration createIKCalibration(int smallIterations,
72  double smallStepSize,
73  int mediumIterations,
74  double mediumStepSize,
75  int largeIterations,
76  double largeStepSize);
77  };
78 
79  public:
80  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81  ///SINGLETON-FEATURES////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83  /**
84  * @brief getInstance returns an instance of a new KinematicSolver with the attributes scenario and robot if no KinematicSolver is initialized yet
85  * if a KinematicSolver has already been initialized the method returns that one (see Singleton-Pattern)
86  * @param scenario the objects surronding the robot that have to be regarded when calculating an IKSolution
87  * @param robot the robot for which the Kinematic Solution is calculated
88  * @return the single instance of KinematicSolver
89  */
90  static std::shared_ptr<KinematicSolver> getInstance(VirtualRobot::ScenePtr scenario,
92 
93  /**
94  * @brief reset
95  * Destroys the current instance of KinematicSolver that a new one can be created via call of getInstance
96  */
97  static void reset();
98 
99  /**
100  * @brief syncRobotPrx updates the robotProxy with the jointAngles of the "real" when the rns is different to the current rns
101  * @param rns the newly selected rns
102  */
103  void syncRobotPrx(VirtualRobot::RobotNodeSetPtr rns);
104  /**
105  * @brief syncRobotPrx always updates the robotProxy with the jointAngles of the "real" robot
106  */
107  void syncRobotPrx();
108 
109  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110  ///IK////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112  /**
113  * @brief solveIK returns a random solution for an inverse Kinematic problem with no consideration of the current pose of the robot
114  * @param kc the kinematic chain for which the IK solution is calculated
115  * @param pose the GLOBAL pose that should be reached by the tcp of the kinematic chain kc when the joint values of the solution are applied to the robot
116  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
117  * @return an mostly random IK solution for the given PoseBase and kinematic Chain
118  */
119  std::vector<double> solveIK(VirtualRobot::RobotNodeSetPtr kc,
120  armarx::PoseBasePtr pose,
122  int iterations);
123 
124  /**
125  * @brief solveIK returns a random solution for an inverse Kinematic problem with no consideration of the current pose of the robot
126  * @param kc the kinematic chain for which the IK solution is calculated
127  * @param pose the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached by the tcp of the kinematic chain kc when the joint values of the solution are applied to the robot
128  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
129  * @return an mostly random IK solution for the given PoseBase and kinematic Chain RELATIVE TO THE ROBOTS KINEMATIC ROOT
130  */
131  std::vector<double> solveIKRelative(VirtualRobot::RobotNodeSetPtr kc,
132  armarx::PoseBasePtr framedPose,
134 
135  /**
136  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
137  * @param kc the selected kinematic chain, for which the IK Solution should be found
138  * @param startingPoint the jointAngle values of the starting Pose of the robot
139  * @param destinations the GLOBAL Pose that should be reached
140  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
141  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
142  */
143  std::vector<float> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc,
144  std::vector<float> startingPoint,
145  PoseBasePtr globalDestination,
147 
148  /**
149  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
150  * @param kc the selected kinematic chain, for which the IK Solution should be found
151  * @param startingPoint the jointAngle values of the starting Pose of the robot
152  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
153  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
154  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
155  */
156  std::vector<float>
157  solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc,
158  std::vector<float> startingPoint,
159  PoseBasePtr framedDestination,
161 
162 
163  /**
164  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
165  * @param kc the selected kinematic chain, for which the IK Solution should be found
166  * @param startingPoint the jointAngle values of the starting Pose of the robot
167  * @param destinations the GLOBAL Pose that should be reached
168  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
169  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
170  */
171  std::vector<double> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc,
172  std::vector<double> startingPoint,
173  PoseBasePtr globalDestination,
175 
176  /**
177  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
178  * @param kc the selected kinematic chain, for which the IK Solution should be found
179  * @param startingPoint the jointAngle values of the starting Pose of the robot
180  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
181  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
182  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
183  */
184  std::vector<double>
185  solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc,
186  std::vector<double> startingPoint,
187  PoseBasePtr framedDestination,
189 
190  /**
191  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
192  * @param kc the selected kinematic chain, for which the IK Solution should be found
193  * @param startingPoint the jointAngle values of the starting Pose of the robot
194  * @param destinations the GLOBAL Pose that should be reached
195  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
196  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
197  */
198  std::vector<std::vector<double>>
199  solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc,
200  std::vector<double> startingPoint,
201  std::vector<PoseBasePtr> globalDestinations,
202  std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
203 
204  std::vector<std::vector<double>> solveRecursiveIKNoMotionPlanning(
205  VirtualRobot::RobotNodeSetPtr kc,
206  std::vector<double> startingPoint,
207  std::vector<PoseBasePtr> globalDestinations,
208  std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
209 
210  std::vector<std::vector<double>>
211  solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc,
212  std::vector<double> startingPoint,
213  std::vector<PoseBasePtr> globalDestinations,
215 
216  /**
217  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
218  * @param kc the selected kinematic chain, for which the IK Solution should be found
219  * @param startingPoint the jointAngle values of the starting Pose of the robot
220  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
221  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
222  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
223  */
224  std::vector<std::vector<double>> solveRecursiveIKRelative(
225  VirtualRobot::RobotNodeSetPtr kc,
226  std::vector<double> startingPoint,
227  std::vector<PoseBasePtr> framedDestinations,
228  std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
229 
230  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231  ///FORWARD KINEMATIC & REACHABILITY///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233  /**
234  * @brief doForwardKinematic executes the given jointAngles on the loaded Robot
235  * @param kc the kinematic Chain on which the joint angles are executed on
236  * @param jointAngles the joint angles which the robot should have
237  * @return a vector of joint angle configurations to reach destination 1,..., n in exactly the same order as the destinations
238  */
239  PoseBasePtr doForwardKinematic(VirtualRobot::RobotNodeSetPtr kc,
240  std::vector<double> jointAngles);
241 
242  /**
243  * @brief doForwardKinematic executes the given jointAngles on the loaded Robot
244  * @param kc the kinematic Chain on which the joint angles are executed on
245  * @param jointAngles the joint angles which the robot should have
246  * @return a vector of joint angle configurations to reach destination 1,..., n in exactly the same order as the destinations
247  */
248  PoseBasePtr doForwardKinematicRelative(VirtualRobot::RobotNodeSetPtr kc,
249  std::vector<double> jointAngles);
250 
251  /**
252  * @brief isReachable calculates whether the PoseBase pose is reachable by the loaded robot or not
253  * @param pose the pose that should be reachable by the robot
254  * @return returns true if the pose is reachable, false otherwise
255  */
256  bool isReachable(VirtualRobot::RobotNodeSetPtr rns,
257  PoseBasePtr globalPose,
259 
260 
261  private:
262  static std::shared_ptr<KinematicSolver> singleton;
264  VirtualRobot::RobotPtr robotProxy;
265  std::map<std::string, IKCalibration> IKCalibrationMap;
266  //std::map<std::string, VirtualRobot::ReachabilityPtr> reachabilityMap;
268  VirtualRobot::GenericIKSolverPtr IKSetup(double jacStepSize,
269  int IKIterations,
270  double vectorError,
271  double orientationError,
272  VirtualRobot::RobotNodeSetPtr kc);
273  VirtualRobot::RobotNodeSetPtr setupProxy(VirtualRobot::RobotNodeSetPtr kc,
274  std::vector<double> jointAngles);
275  std::string currentRns;
276  IKCalibration calibrate();
277  double getMaxDistance(Eigen::Vector3f destination,
278  std::vector<std::vector<double>> jointAngles,
279  VirtualRobot::RobotNodeSetPtr rns);
280  static int getFurthestNode(VirtualRobot::RobotNodeSetPtr rns,
281  Eigen::VectorXf startConfig,
282  Eigen::VectorXf endConfig);
283 
284  //Saba::RrtPtr getMotionPlanning(Eigen::VectorXf start, Eigen::VectorXf end, VirtualRobot::RobotNodeSetPtr rns);
285  };
286 
287  using KinematicSolverPtr = std::shared_ptr<KinematicSolver>;
288 } // namespace armarx
289 
290 
291 #endif
armarx::KinematicSolver::solveRecursiveIKRelative
std::vector< float > solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector< float > startingPoint, PoseBasePtr framedDestination, VirtualRobot::IKSolver::CartesianSelection selection)
solveRecursiveIK solves IK based on a starting point and an End point based on one step
armarx::KinematicSolver::reset
static void reset()
reset Destroys the current instance of KinematicSolver that a new one can be created via call of getI...
Definition: KinematicSolver.cpp:117
armarx::KinematicSolver
Realizes the Singleton-Pattern, Provides Methods to solve Kinematic Problems (forward and inverse)
Definition: KinematicSolver.h:52
scene3D::ScenePtr
std::shared_ptr< Scene > ScenePtr
Definition: PointerDefinitions.h:36
armarx::KinematicSolver::solveIK
std::vector< double > solveIK(VirtualRobot::RobotNodeSetPtr kc, armarx::PoseBasePtr pose, VirtualRobot::IKSolver::CartesianSelection selection, int iterations)
IK///////////////////////////////////////////////////////////////////////////////////////////////////...
Definition: KinematicSolver.cpp:129
armarx::KinematicSolver::solveIKRelative
std::vector< double > solveIKRelative(VirtualRobot::RobotNodeSetPtr kc, armarx::PoseBasePtr framedPose, VirtualRobot::IKSolver::CartesianSelection selection)
solveIK returns a random solution for an inverse Kinematic problem with no consideration of the curre...
Definition: KinematicSolver.cpp:169
armarx::KinematicSolver::isReachable
bool isReachable(VirtualRobot::RobotNodeSetPtr rns, PoseBasePtr globalPose, VirtualRobot::IKSolver::CartesianSelection cs)
isReachable calculates whether the PoseBase pose is reachable by the loaded robot or not
Definition: KinematicSolver.cpp:553
FramedPose.h
armarx::KinematicSolver::doForwardKinematic
PoseBasePtr doForwardKinematic(VirtualRobot::RobotNodeSetPtr kc, std::vector< double > jointAngles)
FORWARD KINEMATIC & REACHABILITY/////////////////////////////////////////////////////////////////////...
Definition: KinematicSolver.cpp:536
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::KinematicSolver::solveRecursiveIK
std::vector< float > solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc, std::vector< float > startingPoint, PoseBasePtr globalDestination, VirtualRobot::IKSolver::CartesianSelection selection)
solveRecursiveIK solves IK based on a starting point and an End point based on one step
armarx::KinematicSolver::doForwardKinematicRelative
PoseBasePtr doForwardKinematicRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector< double > jointAngles)
doForwardKinematic executes the given jointAngles on the loaded Robot
Definition: KinematicSolver.cpp:544
armarx::KinematicSolver::solveRecursiveIKNoMotionPlanning
std::vector< std::vector< double > > solveRecursiveIKNoMotionPlanning(VirtualRobot::RobotNodeSetPtr kc, std::vector< double > startingPoint, std::vector< PoseBasePtr > globalDestinations, std::vector< VirtualRobot::IKSolver::CartesianSelection > selections)
Definition: KinematicSolver.cpp:438
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::KinematicSolver::getInstance
static std::shared_ptr< KinematicSolver > getInstance(VirtualRobot::ScenePtr scenario, VirtualRobot::RobotPtr robot)
SINGLETON-FEATURES///////////////////////////////////////////////////////////////////////////////////...
Definition: KinematicSolver.cpp:105
armarx::KinematicSolverPtr
std::shared_ptr< KinematicSolver > KinematicSolverPtr
Definition: KinematicSolver.h:287
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::KinematicSolver::syncRobotPrx
void syncRobotPrx()
syncRobotPrx always updates the robotProxy with the jointAngles of the "real" robot
Definition: KinematicSolver.cpp:586
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19