Robot Arm

Can Machine Learning Improve Robot Kinematics?

I’ve tried several times to hand-code inverse kinematics for robot industrial arms in Robot Overlord.  To make a long story short, there are a lot of complicated edge cases that break my brain.  Many modern methods involve a lot of guess work in the path planning.  I know that a well trained Machine Learning agent could do the job much better, but to date there are none I can download and install in my robot.  So I’m going to try and do it myself.  Join me!

The problem I’m trying to solve with Machine Learning

I have a 3D model of my arm in the Java app called Robot Overlord.  The 3D model is fully posable.  At any given pose I can calculate the angle of every joint and the exact location and orientation of the finger tip.  I have Forward Kinematics (FK) which is a tool to translate joint angles into finger tip.  I have Inverse Kinematics (IK) which is a tool to translate the reverse.

A robot arm is programmed with a series of poses.  Go to pose A, close the gripper, Go to pose B, insert the part, Go to pose C, etc… The robot software has to calculate the movement of the arm between poses and then adjust every motor simultaneously to drive the finger tip along the path between the two poses.  I’ve already solved the firmware part to drive six motors given sets of joint angles.

The problem is that one IK solution there might be many combinations of joint angles – sometimes infinite solutions.  To illustrate this, hold a finger on the table and move your elbow.  Your finger tip didn’t move and you had lots of possible wrist/shoulder changes.  As the arm moves through space it can cross a singularity – one of the spots with infinite solutions – and when it comes out the other side the hand-written solution flips the some or all of the arm 180 degrees around.  A smarter system would have recognized the problem and (for instance) kept the elbow to the side.  I have tried to write better IK code but have not had any success.

My Machine Learning Plan of Attack

My plan is to use a Deep Learning Neural Network.  The DNN is a bit of a black box: on one side there is a layer of Inputs, on the other there is a layer of Outputs, and in between there are one or more hidden layers.  Inputs filter through the layers and come out as Outputs.  The magic is in the filtration process!  The filter can be trained with gradient descent using a cost function – if I can score every input/output combination I can let the DNN play with the virtual arm while the cost function watches and says “good, bad, better, worse” until the two work out all the best possible movements.

My Machine Learning Network design

I believe my inputs should be:

  • Arm starting pose: 6 random angle values.  Because DNN inputs are values in the range 0…1 I’ll say 0 is 0 degrees and 1 is 360 degrees.
  • Arm ending pose: 3 random position values and 3 random angle values.  Position values are scaled over the total movement range of the robot.  So if the robot can move on the X axis from -50 to +50, (x/100+0.5) would give a value 0…1.
  • Interpolation between both poses: 1 decimal number.  0 means at the start pose and 1 means at the end pose.

I want my outputs to be:

  • Arm joint angles: 6 angle values.
  • confidence flag: 1 number.  0 means “I definitely can’t reach that pose” and 1 means “I can reach that pose”.

The cost function should work in two steps:

  1. make sure there is no error in the joint value – that is to say, the finger is actually on the path where it should be, and
  2. seek to reduce joint acceleration.  Adjust the elbow and the wrist ahead of time to avoid the need to suddenly twist.

I’m going to try first with two hidden layers, then experiment from there.  I intuitively guess it will take at least two layers because there are two parts to the cost function.

My Machine Learning code setup

Robot Overlord source code is already written in Java so I’ve added TensorFlow and DL4J.  Currently I’m still walking through the MNIST quickstart tutorials and asking the DL4J chat room for help.  They already solved a few head scratching differences between the DL4J quickstart tutorials and the DL4J up-to-date examples.  You can find my first test in Robot Overlord’s code at /src/main/java/com/marginallyclever/robotOverlord/


I hope that I’ve described my challenge thoroughly.  Please feel free to look at the code and make pull requests, or comment below or in the forums with any tips and advice you might have.  If you’re feeling helpful but not sure how, please share this far and wide so that I can reach the people who have the DNN know-how.

Stay awesome!

News Robot Arm

Robot Arm Study 5 in Robot Overlord, Tool mounting design

I’ve got Study 5 working in Robot Overlord.  The STL files are heavy in detail so the load time is long, but it works.  Forward and Inverse kinematics as good as any other arm in the system (which already includes the 7bot, MANTIS, and others).  I still don’t have enough time to rip apart the inner workings and rebuild it with a record & play back mechanism.  That would be very nice…

Also here is the hole pattern if you want to design a tool that fits on the wrist of the robot.  I would love to see someone design an Automatic Tool Changer (ATC) so the robot could put down one tool, pick up another, and use any tool it is currently holding.

Robot Arm Tutorials

How would I create a robot arm from scratch?

Reddit user singdawg asked a great question:

I’m currently starting a project to [create a robot arm]. Anybody know good resources? I’m a capable programmer, have experience with microelectronics and microcontrollers. Have some advanced maths to understand the depth of material (jacobian matrices etc) but I’ve limited experience with servo motors.


If your goal is specifically to DIY a robot arm from scratch, I’d start by figuring out what are my design constraints. I’d choose a carrying weight limit, a reach distance, the number of DOF, and the maximum mass of each joint. from that I could build a kinematic chain to find my torque limits, and match those to available motors and gearboxes. Personally, I choose stepper motors. In Fusion360 I’d create models of those motors, then start placing them at their desired locations. The gaps between parts would be filled with my custom designs, which would then have to be manufactured. The custom design part is very iterative and very slow (for me). Some of the things I ask myself are: Where do the bearings and fasteners fit? How do I plan to make this custom part and how does that affect the design? Where do I run the wiring so it doesn’t catch and break? Have I remembered to put in *every* part, not just waved my hand and said “figure it out later”? Can I design this in smaller pieces for easier testing of each piece?

I should mention here that a robot built with stepper motors can’t tell where it is from moment to moment the way you or I can. Mostly they are built by having limit switches. The robot moves to touch the switches at startup. Since it knows where the switches are it can count steps as it moves from then on. It is crucial from that point on to never miss a step. One day we’ll have better encoders for DIY robot arms, but not yet!

Once I’ve reached a design I like, it’s time to use the bill of materials from Fusion360 to place my order for the parts, and get to making the custom bits while the orders are in the mail. Once they arrive I can put it together and figure out what I did wrong, then go back to the fusion custom part step 🙂

Once I get something that doesn’t fail on assembly, I take each major section of the arm in Fusion360 and save them out as STL files. I bring those into Robot Overlord and make it move virtually. I can then modify some Arduino CNC firmware to follow the same kinematic model that I used in RO, and now I have a GUI to drive it. There are several arms already in RO, feel free to branch it and add your own.

Robot Arm Torque Calculator

I find calculating forces boring and I love to code. So I wrote a Processing sketch that can simulate a robot arm enough to calculate some masses and torque values. My thinking is that I can use this to set an upper limit on the weight of each joint, then see the torque values and find the motors that will be under-weight and over-torque.

Robot arm torque calculator

The arm can be moved by clicking on a joint and pressing Q/E.  The values at the bottom are the joint number, the direction of rotation, the maximum weight, the distance from the previous joint, the current angle, and the current torque.  In a 6DOF arm there are joints 0-5 and joint 6 represents the weight of the tool or the payload carried by the arm.

Get and run the Arm torque calculator.

Final thoughts

I’ve done this with 3dof and 5dof arms. I’m currently working on a 6dof robot arm.  I like to design from the wrist backwards, because the payload is the most important part, and each motion after that depends on the ones that come before it.

I’m constantly distracted by the work of assembling my other machines. Ironic! If I had the arms they would do the work for me. Soon, soon!

Next in part 2 I will show some of my work designing the arm based on the calculated constraints.