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/RobotNodeSet.h>
34 #include <VirtualRobot/Robot.h>
35 #include <VirtualRobot/IK/GenericIKSolver.h>
36 #include <VirtualRobot/IK/DifferentialIK.h>
37 #include "VirtualRobot/IK/AdvancedIKSolver.h"
38 #include "MotionPlanning/CSpace/CSpacePath.h"
39 
40 ///STANDARD-INCLUDES
41 #include <memory>
42 #include <map>
43 
44 namespace armarx
45 {
46  /**
47  * \class KinematicSolver
48  * \brief Realizes the Singleton-Pattern, Provides Methods to solve Kinematic Problems (forward and inverse)
49  *
50  */
52  {
53 
54  /**
55  * @brief The IKCalibration struct
56  * Represents the settings for the Inverse-Jacobi-IK-Approach(IKIterations, stepSize) for three different IK-Solvers.
57  * Small: Recursive IK Multiple steps
58  * Medium: Recursive IK single step
59  * Large: solveIK
60  */
61  struct IKCalibration
62  {
63  public:
64  int smallIterations;
65  double smallStepSize;
66  int mediumIterations;
67  double mediumStepSize;
68  int largeIterations;
69  double largeStepSize;
70  static IKCalibration createIKCalibration(int smallIterations, double smallStepSize, int mediumIterations, double mediumStepSize, int largeIterations, double largeStepSize);
71  };
72 
73  public:
74 
75  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76  ///SINGLETON-FEATURES////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78  /**
79  * @brief getInstance returns an instance of a new KinematicSolver with the attributes scenario and robot if no KinematicSolver is initialized yet
80  * if a KinematicSolver has already been initialized the method returns that one (see Singleton-Pattern)
81  * @param scenario the objects surronding the robot that have to be regarded when calculating an IKSolution
82  * @param robot the robot for which the Kinematic Solution is calculated
83  * @return the single instance of KinematicSolver
84  */
85  static std::shared_ptr<KinematicSolver> getInstance(VirtualRobot::ScenePtr scenario, VirtualRobot::RobotPtr robot);
86 
87  /**
88  * @brief reset
89  * Destroys the current instance of KinematicSolver that a new one can be created via call of getInstance
90  */
91  static void reset();
92 
93  /**
94  * @brief syncRobotPrx updates the robotProxy with the jointAngles of the "real" when the rns is different to the current rns
95  * @param rns the newly selected rns
96  */
97  void syncRobotPrx(VirtualRobot::RobotNodeSetPtr rns);
98  /**
99  * @brief syncRobotPrx always updates the robotProxy with the jointAngles of the "real" robot
100  */
101  void syncRobotPrx();
102 
103  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104  ///IK////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106  /**
107  * @brief solveIK returns a random solution for an inverse Kinematic problem with no consideration of the current pose of the robot
108  * @param kc the kinematic chain for which the IK solution is calculated
109  * @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
110  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
111  * @return an mostly random IK solution for the given PoseBase and kinematic Chain
112  */
113  std::vector<double> solveIK(VirtualRobot::RobotNodeSetPtr kc, armarx::PoseBasePtr pose, VirtualRobot::IKSolver::CartesianSelection selection, int iterations);
114 
115  /**
116  * @brief solveIK returns a random solution for an inverse Kinematic problem with no consideration of the current pose of the robot
117  * @param kc the kinematic chain for which the IK solution is calculated
118  * @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
119  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
120  * @return an mostly random IK solution for the given PoseBase and kinematic Chain RELATIVE TO THE ROBOTS KINEMATIC ROOT
121  */
122  std::vector<double> solveIKRelative(VirtualRobot::RobotNodeSetPtr kc, armarx::PoseBasePtr framedPose, VirtualRobot::IKSolver::CartesianSelection selection);
123 
124  /**
125  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
126  * @param kc the selected kinematic chain, for which the IK Solution should be found
127  * @param startingPoint the jointAngle values of the starting Pose of the robot
128  * @param destinations the GLOBAL Pose that should be reached
129  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
130  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
131  */
132  std::vector<float> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc, std::vector<float> startingPoint, PoseBasePtr globalDestination, VirtualRobot::IKSolver::CartesianSelection selection);
133 
134  /**
135  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
136  * @param kc the selected kinematic chain, for which the IK Solution should be found
137  * @param startingPoint the jointAngle values of the starting Pose of the robot
138  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
139  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
140  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
141  */
142  std::vector<float> solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector<float> startingPoint, PoseBasePtr framedDestination, VirtualRobot::IKSolver::CartesianSelection selection);
143 
144 
145  /**
146  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
147  * @param kc the selected kinematic chain, for which the IK Solution should be found
148  * @param startingPoint the jointAngle values of the starting Pose of the robot
149  * @param destinations the GLOBAL Pose that should be reached
150  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
151  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
152  */
153  std::vector<double> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, PoseBasePtr globalDestination, VirtualRobot::IKSolver::CartesianSelection selection);
154 
155  /**
156  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
157  * @param kc the selected kinematic chain, for which the IK Solution should be found
158  * @param startingPoint the jointAngle values of the starting Pose of the robot
159  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
160  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
161  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
162  */
163  std::vector<double> solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, PoseBasePtr framedDestination, VirtualRobot::IKSolver::CartesianSelection selection);
164 
165  /**
166  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
167  * @param kc the selected kinematic chain, for which the IK Solution should be found
168  * @param startingPoint the jointAngle values of the starting Pose of the robot
169  * @param destinations the GLOBAL Pose that should be reached
170  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
171  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
172  */
173  std::vector<std::vector<double>> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, std::vector<PoseBasePtr> globalDestinations, std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
174 
175  std::vector<std::vector<double>> solveRecursiveIKNoMotionPlanning(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, std::vector<PoseBasePtr> globalDestinations, std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
176 
177  std::vector<std::vector<double>> solveRecursiveIK(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, std::vector<PoseBasePtr> globalDestinations, VirtualRobot::IKSolver::CartesianSelection selection);
178 
179  /**
180  * @brief solveRecursiveIK solves IK based on a starting point and an End point based on one step
181  * @param kc the selected kinematic chain, for which the IK Solution should be found
182  * @param startingPoint the jointAngle values of the starting Pose of the robot
183  * @param destinations the FRAMED POSE RELATIVE TO THE ROBOTS KINEMATIC ROOT that should be reached
184  * @param selection the attributes of the pose that are considered when solving the IK problem, e.g. position, orientation or all
185  * @return an mostly random IK solution for the given PoseBase and kinematic Chain in form joint angle configuration
186  */
187  std::vector<std::vector<double>> solveRecursiveIKRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> startingPoint, std::vector<PoseBasePtr> framedDestinations, std::vector<VirtualRobot::IKSolver::CartesianSelection> selections);
188 
189  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190  ///FORWARD KINEMATIC & REACHABILITY///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
191  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
192  /**
193  * @brief doForwardKinematic executes the given jointAngles on the loaded Robot
194  * @param kc the kinematic Chain on which the joint angles are executed on
195  * @param jointAngles the joint angles which the robot should have
196  * @return a vector of joint angle configurations to reach destination 1,..., n in exactly the same order as the destinations
197  */
198  PoseBasePtr doForwardKinematic(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> jointAngles);
199 
200  /**
201  * @brief doForwardKinematic executes the given jointAngles on the loaded Robot
202  * @param kc the kinematic Chain on which the joint angles are executed on
203  * @param jointAngles the joint angles which the robot should have
204  * @return a vector of joint angle configurations to reach destination 1,..., n in exactly the same order as the destinations
205  */
206  PoseBasePtr doForwardKinematicRelative(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> jointAngles);
207 
208  /**
209  * @brief isReachable calculates whether the PoseBase pose is reachable by the loaded robot or not
210  * @param pose the pose that should be reachable by the robot
211  * @return returns true if the pose is reachable, false otherwise
212  */
213  bool isReachable(VirtualRobot::RobotNodeSetPtr rns, PoseBasePtr globalPose, VirtualRobot::IKSolver::CartesianSelection cs);
214 
215 
216  private:
217  static std::shared_ptr<KinematicSolver> singleton;
219  VirtualRobot::RobotPtr robotProxy;
220  std::map<std::string, IKCalibration> IKCalibrationMap;
221  //std::map<std::string, VirtualRobot::ReachabilityPtr> reachabilityMap;
223  VirtualRobot::GenericIKSolverPtr IKSetup(double jacStepSize, int IKIterations, double vectorError, double orientationError, VirtualRobot::RobotNodeSetPtr kc);
224  VirtualRobot::RobotNodeSetPtr setupProxy(VirtualRobot::RobotNodeSetPtr kc, std::vector<double> jointAngles);
225  std::string currentRns;
226  IKCalibration calibrate();
227  double getMaxDistance(Eigen::Vector3f destination, std::vector<std::vector<double>> jointAngles, VirtualRobot::RobotNodeSetPtr rns);
228  static int getFurthestNode(VirtualRobot::RobotNodeSetPtr rns, Eigen::VectorXf startConfig, Eigen::VectorXf endConfig);
229 
230  //Saba::RrtPtr getMotionPlanning(Eigen::VectorXf start, Eigen::VectorXf end, VirtualRobot::RobotNodeSetPtr rns);
231 
232 
233  };
234 
235  using KinematicSolverPtr = std::shared_ptr<KinematicSolver>;
236 }
237 
238 
239 #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:103
armarx::KinematicSolver
Realizes the Singleton-Pattern, Provides Methods to solve Kinematic Problems (forward and inverse)
Definition: KinematicSolver.h:51
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:115
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:150
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:474
FramedPose.h
armarx::KinematicSolver::doForwardKinematic
PoseBasePtr doForwardKinematic(VirtualRobot::RobotNodeSetPtr kc, std::vector< double > jointAngles)
FORWARD KINEMATIC & REACHABILITY/////////////////////////////////////////////////////////////////////...
Definition: KinematicSolver.cpp:459
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:466
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:379
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:92
armarx::KinematicSolverPtr
std::shared_ptr< KinematicSolver > KinematicSolverPtr
Definition: KinematicSolver.h:235
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::KinematicSolver::syncRobotPrx
void syncRobotPrx()
syncRobotPrx always updates the robotProxy with the jointAngles of the "real" robot
Definition: KinematicSolver.cpp:505
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18