ur_kin_py.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Python wrapper for UR kinematics
4  * Author: Kelsey Hawkins (kphawkins@gatech.edu)
5  *
6  * Software License Agreement (BSD License)
7  *
8  * Copyright (c) 2013, Georgia Institute of Technology
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above
18  * copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided
20  * with the distribution.
21  * * Neither the name of the Georgia Institute of Technology nor the names of
22  * its contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *********************************************************************/
38 
39 #include <boost/numpy.hpp>
40 #include <boost/scoped_array.hpp>
41 
42 #include <ur_kinematics/ur_kin.h>
43 
44 namespace p = boost::python;
45 namespace np = boost::numpy;
46 
47 np::ndarray forward_wrapper(np::ndarray const & q_arr) {
48  if(q_arr.get_dtype() != np::dtype::get_builtin<double>()) {
49  PyErr_SetString(PyExc_TypeError, "Incorrect array data type");
50  p::throw_error_already_set();
51  }
52  if(q_arr.get_nd() != 1) {
53  PyErr_SetString(PyExc_TypeError, "Incorrect number of dimensions");
54  p::throw_error_already_set();
55  }
56  if(q_arr.shape(0) != 6) {
57  PyErr_SetString(PyExc_TypeError, "Incorrect shape (should be 6)");
58  p::throw_error_already_set();
59  }
60  Py_intptr_t shape[2] = { 4, 4 };
61  np::ndarray result = np::zeros(2,shape,np::dtype::get_builtin<double>());
62  ur_kinematics::forward(reinterpret_cast<double*>(q_arr.get_data()),
63  reinterpret_cast<double*>(result.get_data()));
64  return result;
65 }
66 
67 np::ndarray inverse_wrapper(np::ndarray const & array, PyObject * q6_des_py) {
68  if(array.get_dtype() != np::dtype::get_builtin<double>()) {
69  PyErr_SetString(PyExc_TypeError, "Incorrect array data type");
70  p::throw_error_already_set();
71  }
72  if(array.get_nd() != 2) {
73  PyErr_SetString(PyExc_TypeError, "Incorrect number of dimensions");
74  p::throw_error_already_set();
75  }
76  if(array.shape(0) != 4 || array.shape(1) != 4) {
77  PyErr_SetString(PyExc_TypeError, "Incorrect shape (should be 4x4)");
78  p::throw_error_already_set();
79  }
80  double* T = reinterpret_cast<double*>(array.get_data());
81  double* q_sols = (double*) malloc(8*6*sizeof(double));
82  double q6_des = PyFloat_AsDouble(q6_des_py);
83  int num_sols = ur_kinematics::inverse(T, q_sols, q6_des);
84  q_sols = (double*) realloc(q_sols, num_sols*6*sizeof(double));
85  return np::from_data(q_sols, np::dtype::get_builtin<double>() , p::make_tuple(num_sols, 6), p::make_tuple(6*sizeof(double), sizeof(double)), p::object());
86 }
87 
88 BOOST_PYTHON_MODULE(ur_kin_py) {
89  np::initialize(); // have to put this in any module that uses Boost.NumPy
90  p::def("forward", forward_wrapper);
91  p::def("inverse", inverse_wrapper);
92 }
np::ndarray forward_wrapper(np::ndarray const &q_arr)
Definition: ur_kin_py.cpp:47
np::ndarray inverse_wrapper(np::ndarray const &array, PyObject *q6_des_py)
Definition: ur_kin_py.cpp:67
int inverse(const double *T, double *q_sols, double q6_des=0.0)
Definition: ur_kin.cpp:197
BOOST_PYTHON_MODULE(ur_kin_py)
Definition: ur_kin_py.cpp:88
void forward(const double *q, double *T)
Definition: ur_kin.cpp:47


ur_kinematics
Author(s): Kelsey Hawkins
autogenerated on Sun Nov 24 2019 03:36:27