pr2-ikfast 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ Metadata-Version: 2.1
2
+ Name: pr2_ikfast
3
+ Version: 0.0.1
4
+ Summary: Python bindings for the PR2 IKFast library
5
+ Requires-Python: >=3.8
6
+ Requires-Dist: numpy
@@ -0,0 +1,20 @@
1
+ # PR2 IKFast Python Bindings
2
+ cpp codes are fetched from https://github.com/caelan/pybullet-planning (MIT License), where the cpp code themselves (Apache License 2.0) are generated by IKFast. This repo just provides simple `pip installable` python package using setuptools.
3
+
4
+ ## Installation
5
+ ```bash
6
+ pip install .
7
+ ```
8
+
9
+ ## Usage
10
+ ```python
11
+ import numpy as np
12
+ from pr2_ikfast import solve_left_ik, solve_right_ik
13
+
14
+ trans = [0.5, -0.2, 0.8]
15
+ rot = np.eye(3).tolist()
16
+ free = [0.1, 0.2]
17
+
18
+ sol_left = solve_left_ik(trans, rot, free)
19
+ sol_right = solve_right_ik(trans, rot, free)
20
+ ```
@@ -0,0 +1,16 @@
1
+ [build-system]
2
+ requires = ["setuptools>=75", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pr2_ikfast"
7
+ version = "0.0.1"
8
+ description = "Python bindings for the PR2 IKFast library"
9
+ requires-python = ">=3.8"
10
+ dependencies = ["numpy"]
11
+
12
+ [tool.setuptools]
13
+ ext-modules = [
14
+ {name = "pr2_ikfast.ikLeft", sources = ["src/cpp/left_arm_ik.cpp"], include-dirs = ["src/cpp"]},
15
+ {name = "pr2_ikfast.ikRight", sources = ["src/cpp/right_arm_ik.cpp"], include-dirs = ["src/cpp"]}
16
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,340 @@
1
+ // -*- coding: utf-8 -*-
2
+ // Copyright (C) 2012-2014 Rosen Diankov <rosen.diankov@gmail.com>
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /** \brief Header file for all ikfast c++ files/shared objects.
15
+
16
+ The ikfast inverse kinematics compiler is part of OpenRAVE.
17
+
18
+ The file is divided into two sections:
19
+ - <b>Common</b> - the abstract classes section that all ikfast share regardless of their settings
20
+ - <b>Library Specific</b> - the library-specific definitions, which depends on the precision/settings that the library was compiled with
21
+
22
+ The defines are as follows, they are also used for the ikfast C++ class:
23
+
24
+ - IKFAST_HEADER_COMMON - common classes
25
+ - IKFAST_HAS_LIBRARY - if defined, will include library-specific functions. by default this is off
26
+ - IKFAST_CLIBRARY - Define this linking statically or dynamically to get correct visibility.
27
+ - IKFAST_NO_MAIN - Remove the ``main`` function, usually used with IKFAST_CLIBRARY
28
+ - IKFAST_ASSERT - Define in order to get a custom assert called when NaNs, divides by zero, and other invalid conditions are detected.
29
+ - IKFAST_REAL - Use to force a custom real number type for IkReal.
30
+ - IKFAST_NAMESPACE - Enclose all functions and classes in this namespace, the ``main`` function is excluded.
31
+
32
+ */
33
+ #include <vector>
34
+ #include <list>
35
+ #include <stdexcept>
36
+ #include <cmath>
37
+
38
+ #ifndef IKFAST_HEADER_COMMON
39
+ #define IKFAST_HEADER_COMMON
40
+
41
+ /// should be the same as ikfast.__version__
42
+ /// if 0x10000000 bit is set, then the iksolver assumes 6D transforms are done without the manipulator offset taken into account (allows to reuse IK when manipulator offset changes)
43
+ #define IKFAST_VERSION 0x10000048
44
+
45
+ namespace ikfast {
46
+
47
+ /// \brief holds the solution for a single dof
48
+ template <typename T>
49
+ class IkSingleDOFSolutionBase
50
+ {
51
+ public:
52
+ IkSingleDOFSolutionBase() : fmul(0), foffset(0), freeind(-1), maxsolutions(1) {
53
+ indices[0] = indices[1] = indices[2] = indices[3] = indices[4] = -1;
54
+ }
55
+ T fmul, foffset; ///< joint value is fmul*sol[freeind]+foffset
56
+ signed char freeind; ///< if >= 0, mimics another joint
57
+ unsigned char jointtype; ///< joint type, 0x01 is revolute, 0x11 is slider
58
+ unsigned char maxsolutions; ///< max possible indices, 0 if controlled by free index or a free joint itself
59
+ unsigned char indices[5]; ///< unique index of the solution used to keep track on what part it came from. sometimes a solution can be repeated for different indices. store at least another repeated root
60
+ };
61
+
62
+ /// \brief The discrete solutions are returned in this structure.
63
+ ///
64
+ /// Sometimes the joint axes of the robot can align allowing an infinite number of solutions.
65
+ /// Stores all these solutions in the form of free variables that the user has to set when querying the solution. Its prototype is:
66
+ template <typename T>
67
+ class IkSolutionBase
68
+ {
69
+ public:
70
+ virtual ~IkSolutionBase() {
71
+ }
72
+ /// \brief gets a concrete solution
73
+ ///
74
+ /// \param[out] solution the result
75
+ /// \param[in] freevalues values for the free parameters \se GetFree
76
+ virtual void GetSolution(T* solution, const T* freevalues) const = 0;
77
+
78
+ /// \brief std::vector version of \ref GetSolution
79
+ virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const {
80
+ solution.resize(GetDOF());
81
+ GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
82
+ }
83
+
84
+ /// \brief Gets the indices of the configuration space that have to be preset before a full solution can be returned
85
+ ///
86
+ /// 0 always points to the first value accepted by the ik function.
87
+ /// \return vector of indices indicating the free parameters
88
+ virtual const std::vector<int>& GetFree() const = 0;
89
+
90
+ /// \brief the dof of the solution
91
+ virtual const int GetDOF() const = 0;
92
+ };
93
+
94
+ /// \brief manages all the solutions
95
+ template <typename T>
96
+ class IkSolutionListBase
97
+ {
98
+ public:
99
+ virtual ~IkSolutionListBase() {
100
+ }
101
+
102
+ /// \brief add one solution and return its index for later retrieval
103
+ ///
104
+ /// \param vinfos Solution data for each degree of freedom of the manipulator
105
+ /// \param vfree If the solution represents an infinite space, holds free parameters of the solution that users can freely set. The indices are of the configuration that the IK solver accepts rather than the entire robot, ie 0 points to the first value accepted.
106
+ virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) = 0;
107
+
108
+ /// \brief returns the solution pointer
109
+ virtual const IkSolutionBase<T>& GetSolution(size_t index) const = 0;
110
+
111
+ /// \brief returns the number of solutions stored
112
+ virtual size_t GetNumSolutions() const = 0;
113
+
114
+ /// \brief clears all current solutions, note that any memory addresses returned from \ref GetSolution will be invalidated.
115
+ virtual void Clear() = 0;
116
+ };
117
+
118
+ /// \brief holds function pointers for all the exported functions of ikfast
119
+ template <typename T>
120
+ class IkFastFunctions
121
+ {
122
+ public:
123
+ IkFastFunctions() : _ComputeIk(NULL), _ComputeIk2(NULL), _ComputeFk(NULL), _GetNumFreeParameters(NULL), _GetFreeParameters(NULL), _GetNumJoints(NULL), _GetIkRealSize(NULL), _GetIkFastVersion(NULL), _GetIkType(NULL), _GetKinematicsHash(NULL) {
124
+ }
125
+ virtual ~IkFastFunctions() {
126
+ }
127
+ typedef bool (*ComputeIkFn)(const T*, const T*, const T*, IkSolutionListBase<T>&);
128
+ ComputeIkFn _ComputeIk;
129
+ typedef bool (*ComputeIk2Fn)(const T*, const T*, const T*, IkSolutionListBase<T>&, void*);
130
+ ComputeIk2Fn _ComputeIk2;
131
+ typedef void (*ComputeFkFn)(const T*, T*, T*);
132
+ ComputeFkFn _ComputeFk;
133
+ typedef int (*GetNumFreeParametersFn)();
134
+ GetNumFreeParametersFn _GetNumFreeParameters;
135
+ typedef int* (*GetFreeParametersFn)();
136
+ GetFreeParametersFn _GetFreeParameters;
137
+ typedef int (*GetNumJointsFn)();
138
+ GetNumJointsFn _GetNumJoints;
139
+ typedef int (*GetIkRealSizeFn)();
140
+ GetIkRealSizeFn _GetIkRealSize;
141
+ typedef const char* (*GetIkFastVersionFn)();
142
+ GetIkFastVersionFn _GetIkFastVersion;
143
+ typedef int (*GetIkTypeFn)();
144
+ GetIkTypeFn _GetIkType;
145
+ typedef const char* (*GetKinematicsHashFn)();
146
+ GetKinematicsHashFn _GetKinematicsHash;
147
+ };
148
+
149
+ // Implementations of the abstract classes, user doesn't need to use them
150
+
151
+ /// \brief Default implementation of \ref IkSolutionBase
152
+ template <typename T>
153
+ class IkSolution : public IkSolutionBase<T>
154
+ {
155
+ public:
156
+ IkSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) {
157
+ _vbasesol = vinfos;
158
+ _vfree = vfree;
159
+ }
160
+
161
+ virtual void GetSolution(T* solution, const T* freevalues) const {
162
+ for(std::size_t i = 0; i < _vbasesol.size(); ++i) {
163
+ if( _vbasesol[i].freeind < 0 )
164
+ solution[i] = _vbasesol[i].foffset;
165
+ else {
166
+ solution[i] = freevalues[_vbasesol[i].freeind]*_vbasesol[i].fmul + _vbasesol[i].foffset;
167
+ if( solution[i] > T(3.14159265358979) ) {
168
+ solution[i] -= T(6.28318530717959);
169
+ }
170
+ else if( solution[i] < T(-3.14159265358979) ) {
171
+ solution[i] += T(6.28318530717959);
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const {
178
+ solution.resize(GetDOF());
179
+ GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
180
+ }
181
+
182
+ virtual const std::vector<int>& GetFree() const {
183
+ return _vfree;
184
+ }
185
+ virtual const int GetDOF() const {
186
+ return static_cast<int>(_vbasesol.size());
187
+ }
188
+
189
+ virtual void Validate() const {
190
+ for(size_t i = 0; i < _vbasesol.size(); ++i) {
191
+ if( _vbasesol[i].maxsolutions == (unsigned char)-1) {
192
+ throw std::runtime_error("max solutions for joint not initialized");
193
+ }
194
+ if( _vbasesol[i].maxsolutions > 0 ) {
195
+ if( _vbasesol[i].indices[0] >= _vbasesol[i].maxsolutions ) {
196
+ throw std::runtime_error("index >= max solutions for joint");
197
+ }
198
+ if( _vbasesol[i].indices[1] != (unsigned char)-1 && _vbasesol[i].indices[1] >= _vbasesol[i].maxsolutions ) {
199
+ throw std::runtime_error("2nd index >= max solutions for joint");
200
+ }
201
+ }
202
+ if( !std::isfinite(_vbasesol[i].foffset) ) {
203
+ throw std::runtime_error("foffset was not finite");
204
+ }
205
+ }
206
+ }
207
+
208
+ virtual void GetSolutionIndices(std::vector<unsigned int>& v) const {
209
+ v.resize(0);
210
+ v.push_back(0);
211
+ for(int i = (int)_vbasesol.size()-1; i >= 0; --i) {
212
+ if( _vbasesol[i].maxsolutions != (unsigned char)-1 && _vbasesol[i].maxsolutions > 1 ) {
213
+ for(size_t j = 0; j < v.size(); ++j) {
214
+ v[j] *= _vbasesol[i].maxsolutions;
215
+ }
216
+ size_t orgsize=v.size();
217
+ if( _vbasesol[i].indices[1] != (unsigned char)-1 ) {
218
+ for(size_t j = 0; j < orgsize; ++j) {
219
+ v.push_back(v[j]+_vbasesol[i].indices[1]);
220
+ }
221
+ }
222
+ if( _vbasesol[i].indices[0] != (unsigned char)-1 ) {
223
+ for(size_t j = 0; j < orgsize; ++j) {
224
+ v[j] += _vbasesol[i].indices[0];
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+
231
+ std::vector< IkSingleDOFSolutionBase<T> > _vbasesol; ///< solution and their offsets if joints are mimiced
232
+ std::vector<int> _vfree;
233
+ };
234
+
235
+ /// \brief Default implementation of \ref IkSolutionListBase
236
+ template <typename T>
237
+ class IkSolutionList : public IkSolutionListBase<T>
238
+ {
239
+ public:
240
+ virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
241
+ {
242
+ size_t index = _listsolutions.size();
243
+ _listsolutions.push_back(IkSolution<T>(vinfos,vfree));
244
+ return index;
245
+ }
246
+
247
+ virtual const IkSolutionBase<T>& GetSolution(size_t index) const
248
+ {
249
+ if( index >= _listsolutions.size() ) {
250
+ throw std::runtime_error("GetSolution index is invalid");
251
+ }
252
+ typename std::list< IkSolution<T> >::const_iterator it = _listsolutions.begin();
253
+ std::advance(it,index);
254
+ return *it;
255
+ }
256
+
257
+ virtual size_t GetNumSolutions() const {
258
+ return _listsolutions.size();
259
+ }
260
+
261
+ virtual void Clear() {
262
+ _listsolutions.clear();
263
+ }
264
+
265
+ protected:
266
+ std::list< IkSolution<T> > _listsolutions;
267
+ };
268
+
269
+ }
270
+
271
+ #endif // OPENRAVE_IKFAST_HEADER
272
+
273
+ // The following code is dependent on the C++ library linking with.
274
+ #ifdef IKFAST_HAS_LIBRARY
275
+
276
+ // defined when creating a shared object/dll
277
+ #ifdef IKFAST_CLIBRARY
278
+ #ifdef _MSC_VER
279
+ #define IKFAST_API extern "C" __declspec(dllexport)
280
+ #else
281
+ #define IKFAST_API extern "C"
282
+ #endif
283
+ #else
284
+ #define IKFAST_API
285
+ #endif
286
+
287
+ #ifdef IKFAST_NAMESPACE
288
+ namespace IKFAST_NAMESPACE {
289
+ #endif
290
+
291
+ #ifdef IKFAST_REAL
292
+ typedef IKFAST_REAL IkReal;
293
+ #else
294
+ typedef double IkReal;
295
+ #endif
296
+
297
+ /** \brief Computes all IK solutions given a end effector coordinates and the free joints.
298
+
299
+ - ``eetrans`` - 3 translation values. For iktype **TranslationXYOrientation3D**, the z-axis is the orientation.
300
+ - ``eerot``
301
+ - For **Transform6D** it is 9 values for the 3x3 rotation matrix.
302
+ - For **Direction3D**, **Ray4D**, and **TranslationDirection5D**, the first 3 values represent the target direction.
303
+ - For **TranslationXAxisAngle4D**, **TranslationYAxisAngle4D**, and **TranslationZAxisAngle4D** the first value represents the angle.
304
+ - For **TranslationLocalGlobal6D**, the diagonal elements ([0],[4],[8]) are the local translation inside the end effector coordinate system.
305
+ */
306
+ IKFAST_API bool ComputeIk(const IkReal* eetrans, const IkReal* eerot, const IkReal* pfree, ikfast::IkSolutionListBase<IkReal>& solutions);
307
+
308
+ /** \brief Similar to ComputeIk except takes OpenRAVE boost::shared_ptr<RobotBase::Manipulator>*
309
+ */
310
+ IKFAST_API bool ComputeIk2(const IkReal* eetrans, const IkReal* eerot, const IkReal* pfree, ikfast::IkSolutionListBase<IkReal>& solutions, void* pOpenRAVEManip);
311
+
312
+ /// \brief Computes the end effector coordinates given the joint values. This function is used to double check ik.
313
+ IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
314
+
315
+ /// \brief returns the number of free parameters users has to set apriori
316
+ IKFAST_API int GetNumFreeParameters();
317
+
318
+ /// \brief the indices of the free parameters indexed by the chain joints
319
+ IKFAST_API int* GetFreeParameters();
320
+
321
+ /// \brief the total number of indices of the chain
322
+ IKFAST_API int GetNumJoints();
323
+
324
+ /// \brief the size in bytes of the configured number type
325
+ IKFAST_API int GetIkRealSize();
326
+
327
+ /// \brief the ikfast version used to generate this file
328
+ IKFAST_API const char* GetIkFastVersion();
329
+
330
+ /// \brief the ik type ID
331
+ IKFAST_API int GetIkType();
332
+
333
+ /// \brief a hash of all the chain values used for double checking that the correct IK is used.
334
+ IKFAST_API const char* GetKinematicsHash();
335
+
336
+ #ifdef IKFAST_NAMESPACE
337
+ }
338
+ #endif
339
+
340
+ #endif // IKFAST_HAS_LIBRARY