GithubHelp home page GithubHelp logo

hiroishida / tinyfk Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 2.0 415 KB

cache-utilized fast forward kinematics solver (pose & jacobian) for c++ and python.

License: MIT License

CMake 1.25% C++ 89.56% C 1.03% Python 7.03% Common Lisp 0.35% Dockerfile 0.51% Shell 0.26%
robotics forward-kinematics jacobian python inverse-kinematics cpp

tinyfk's Introduction

tinyfk CI PyPI version

A tiny fast forward-kinematics solver written in c++ and its python wrapper

Installation by downloading wheel from PyPI (only linux):

pip install tinyfk

or, building locally from source (for developer):

sudo apt-get install libeigen3-dev
git clone https://github.com/HiroIshida/tinyfk.git
cd tinyfk
git submodule update --init
pip install . 

Usage

import numpy as np

import tinyfk
from tinyfk import BaseType, KinematicModel, RotationType

urdf_model_path = tinyfk.pr2_urdfpath()
kin = KinematicModel(urdf_model_path)
joint_names = [
    "r_shoulder_pan_joint",
    "r_shoulder_lift_joint",
    "r_upper_arm_roll_joint",
    "r_elbow_flex_joint",
    "r_forearm_roll_joint",
    "r_wrist_flex_joint",
    "r_wrist_roll_joint",
]

joint_ids = kin.get_joint_ids(joint_names)
end_link_id = kin.get_link_ids(["r_gripper_tool_frame"])[0]

# first 7 elements are for joints and the last 3 elements are for x, y, yaw of base.
q = np.array([0.564, 0.35, -0.74, -0.7, -0.7, -0.17, -0.63, 0.1, 0.2, 0.3])

poses, jacobians = kin.solve_fk(
    q,
    end_link_id,
    joint_ids,
    rot_type=RotationType.RPY,
    base_type=BaseType.PLANER,
    with_jacobian=True,
)

For debugging

For debugging or developing, it's better using cmake directly rather than using pip. In this case, please use

sudo apt-get install libeigen3-dev
git clone https://github.com/HiroIshida/tinyfk.git
cd tinyfk
git submodule update --init
mkdir build
cd build
cmake --DCMAKE_BUILD_TYPE=Debug --DINSTALL_VIA_PIP=OFF ..
make -j4
# make install # please read the CMakeLists.txt before doing this

Note that when you build the python wrapper using cmake (not using pip), the package name for the wrapper will be _tinyfk. So, to make it compatible with the pip-installed one, please insert import _tinyfk as tinyfk in the beginning of the python script.

tinyfk's People

Contributors

hiroishida avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tubbz-alt rajkgu

tinyfk's Issues

readability

link_ids -> link_name_id_map
joint_ids -> joint_name_id_map
joints -> movable_joints

An example of scikit robot wrapper

import tinyfk
import skrobot
from skrobot.coordinates.math import rpy_angle
@dataclass(frozen=True)
class KinematicsSolver:  # a wrapper of tinyfk
    # assume using python 3.6 or higher. and use cpython.
    kinsol: tinyfk.RobotModel
    control_joint_names: List[str]
    endeffector_link_names: List[str]
    joint_name_id_table: Dict[str, int]
    link_name_id_table: Dict[str, int]

    @classmethod
    def construct(cls, robot_model: skrobot.model.RobotModel, control_joint_names: List[str], endeffector_names: List[str]) -> 'KinematicsSolver':
        kinsol = tinyfk.RobotModel(robot_model.urdf_path)

        joint_names = [j.name for j in robot_model.joint_list]
        joint_ids = kinsol.get_joint_ids(joint_names)
        joint_name_id_table = {name: idd for name, idd in zip(joint_names, joint_ids)}

        link_names = [j.name for j in robot_model.link_list]
        link_ids = kinsol.get_link_ids(link_names)
        link_name_id_table = {name: idd for name, idd in zip(link_names, link_ids)}
        return cls(kinsol, control_joint_names, endeffector_names, joint_name_id_table, link_name_id_table)

    def get_control_joint_ids(self) -> List[int]:
        return self.kinsol.get_joint_ids(self.control_joint_names)

    def get_endeffector_link_ids(self) -> List[int]:
        return self.kinsol.get_link_ids(self.endeffector_link_names)

    def solve_ik(self, coords: Coordinates, robot_model: skrobot.model.RobotModel) -> Optional[np.ndarray]:

        if robot_model is not None:
            av = robot_model.angle_vector()
            assert len(av) == len(self.joint_name_id_table)
            self.kinsol.set_joint_angles(list(self.joint_name_id_table.values()), av)

        init_angle = np.array([robot_model.__dict__[jname].joint_angle() for jname in self.control_joint_names])

        xyz = coords.worldpos()
        ypr = np.flip(rpy_angle(coords.worldrot()))[1]
        #from IPython import embed; embed()
        xyzrpy = np.hstack([xyz, ypr])
        elink_id = self.get_endeffector_link_ids()[0]
        joint_ids = self.get_control_joint_ids()
        angles = self.kinsol.solve_inverse_kinematics(xyzrpy, init_angle, elink_id, joint_ids)

        for joint_name, angle in zip(self.control_joint_names, angles):
            robot_model.__dict__[joint_name].joint_angle(angle)
        return robot_model.angle_vector()

remove console_bridge dependency

find_package(console_bridge REQUIRED)

-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at urdf_parser/CMakeLists.txt:13 (find_package):
  By not providing "Findconsole_bridge.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "console_bridge", but CMake did not find one.

  Could not find a package configuration file provided by "console_bridge"
  with any of the following names:

    console_bridgeConfig.cmake
    console_bridge-config.cmake

  Add the installation prefix of "console_bridge" to CMAKE_PREFIX_PATH or set
  "console_bridge_DIR" to a directory containing one of the above files.  If
  "console_bridge" provides a separate development package or SDK, be sure it
  has been installed.

GLIBCXX version is too new

on older system (ubuntu16.04):

root@a8e8891acba9:/# python
Python 2.7.12 (default, Mar  1 2021, 11:38:31) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tinyfk 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tinyfk/__init__.py", line 4, in <module>
    from . import _tinyfk
ImportError: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /usr/local/lib/python2.7/dist-packages/tinyfk/_tinyfk.so)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.