# Programming Inverse Kinematics in C++

I want to write my own kinematics library for my project in C++. I do understand that there are a handful of libraries like RL (Robotics Library) and ROS with inverse kinematics solvers. But for my dismay, these libraries DO NOT support MacOS platform. I have already written the Forward Kinematics part, which was quite straight forward. But for the Inverse Kinematics part, I am quite skeptical since the solution to a IK problem involves solving sets of non-linear simultaneous equation. I found out the Eigen/Unsupported 3.3 module has a APIs to non-linear equations. But before I begin on this uncertain path, I want to get some insight from you guys on the plausibility and practicality of writing my IK library. My manipulator design is rather simple with 4 DoF and the library will not be used for other designs of manipulators. So what I am trying to achieve is taylor-made IK library for my particular manipulator design than a rather a 'Universal' library.

So,

Am I simply trying to reinvent the wheel here by not exploring the already available libraries? If yes, please suggest examples of IK libraries for MacOS platform.

Has anyone written their own IK library? Is it a practical solution? Or is it rather complex problem which is not worth solving for a particular manipulator design?

Or should I just migrate all my project code (OpenCV) to a Linux environment and develop the code for IK in Linux using existing libraries?

• Love the Taylor-made pun
– Ben
Feb 4, 2017 at 1:35
• @Ben, not everyone's first language is English. I doubt the pun was intended Feb 20, 2017 at 0:52

## 5 Answers

It is rather straightforward to implement inverse kinematics for a particular manipulator in C++. Of course, you need to begin with the inverse kinematic equations themselves. Putting those into code will only involve a few trigonometric functions such as acos, asin, and atan2 (use atan2 instead of atan), and probably a couple of square and square root terms.

You will learn a lot by doing this.

I agree with SteveO that there is nothing wrong with reinventing the wheel if you want to learn about wheels. And for a single application, 4 DoF arm, the IK is probably not too hard.

But I feel like I should mention that most of the kinematics libraries out there are mostly targeted towards Linux. And as such, probably not too hard to compile from source for MacOS.

Here are some common libraries:

I did several implementations of IK with different setups (up to 8-DOF) Of course you can just sit down and write the algorithms on your own. (Assuming you know what are you doing) For this approach I suggest using the Eigenlib and some basic maths. Anywas the more elegant approach ( from my point of view) is using matlab: implement the algorithm as you like and let matlab generate the code for you. The matlab code generation feature is pretty nice tool and its reliable (haven't found any errors in the generated code so far). After you have this generated code write any wrapper for any os you like. These are just my two cents

I suggest adopting an existing library.

Each can also be integrated with V-REP or gazebo fairly easily for simulation and visualization. I have also created a homebrew-robotics tap for installing various robotics related tools on OS X via homebrew and a set of Linux setup scripts in robotics_setup.

I also have the same paper and I am going to do following

1. Simulate angles in excel by putting in equations.
2. Make an autocad or solidworks 3d model of the robot and test the resultant angles from excel file on this model.

This I will be doing because I do not want to install Linux on my PC for running just a simulation software. Of course, we can use Virtual machine on windows but time is a precious commodity for doing all this.