I recently got some MeArms to play with. I even got a special MeBrain preview board.
The MeArm is a wonderful little robot arm designed by Ben Gray.
I wanted to use the excellent MeArm IK library written by Nick from York Hackspace. I noticed a lack of documentation which slowed my progress a little and so decided to explain in detail how to calibrate your MeArm and get the IK library working.
For those who don't know; IK stands for Inverse Kinematics. The servo angles do not relate directly to any meaningful coordinate space and so it is awkward to control the MeArm by just thinking about servo angles. The IK library solves this problem by converting from a useful coordinate space (usually cartesian X, Y and Z) to the angles required for the servos.
So here I have written three steps to follow to get things set up. You can click on any of the images in this post to get the original size.
Record which pins are used for which servos.
It seems like the obvious first step. (And it is.) I had somehow got the shoulder and the elbow servos mixed up and spent a while wondering what I had done wrong. Make sure you check which pin controls each servo, write it down, then check it and check it again.
Measure the servo PWM values you need for each servo.
This step is much trickier and will take a little while.
The first thing we need to understand is how the angles are measured. Some are obvious, some are not.
Let's start with the base. It's a fairly simple one.
The base servo controls the rotation of the entire arm around the Z axis, the IK library considers zero degrees to be when the arm is facing forwards. To rotate to the right is to increase the angle and to rotate to the left is to decrease the angle. The image shows that the base servo should be able to reach 90 degrees either side of the zero point. 90 degrees is pi/2 radians.
You need to make sure that when you build your MeArm you get the servos into positions that allow an appropriate range of angles. This is usually simple. In this case, just rotate the servo as far left as it can go and then attach the arm to the base so that it is pointing left. The servo can do at least 180 degrees and so will be able to reach both sides.
Now lets look at the shoulder and the elbow. if you are standing above the arm and looking down at the top of it, you will see the shoulder servo on the right and the elbow servo on the left.
Let's look at the shoulder first, it's the simpler of the two.
The shoulder is considered to be in the zero position when the main body of the arm (that big lever in the middle) is pointing directly forwards. This is about the limit of the arm's reach. The image shows the main body of the arm in a 45 degree position. this is a more natural resting position for the arm.
Finally we have the elbow. The elbow is a little odd. The best way to imaging the elbow is by imagining that the lever that is connected to the servo actually extends in the other direction, opposite to the actual lever. It is the angle of the imaginary extension that you want to measure. As a result, the angle of the actual lever is usually negative. See the image below, where the lever is at -45 degrees, for more clarity.
I almost forgot the gripper, it's dead simple though, 0 degrees is gripper closed, as the angle increases, the gripper opens. 90 degrees is a sensible gripper open limit.
So, how do we match angles to PWM values?
To do this, I connected a slider board to my MeBrain and used one of the sliders to allow me to control one servo at a time. I was able to select which servo to change using the serial console in the Arduino IDE. The current PWM value for the servo is simply printed to the console. To find the limits of my MeArm, I took each servo to a sensible minimum and maximum end point and wrote down the PWM value I got at each angle. It is important to record both the angle and the PWM value because the MeArm IK library needs to know how each angle matches to each PWM value.
The Arduino Sketch I used to do this is here:
In the serial console you can send the letters 'b', 'g', 'e' and 's' to select the base, gripper, elbow and shoulder servos respectively.
So, load this on to your MeBrain and find the numbers you need to calibrate your MeArm. Once you have found these numbers, you can put them into the constructor call for the MeArm instance in your Arduino sketch. For each servo, the library needs to know the minimum PWM, maximum PWM, minimum angle and maximum angle in that order. These min and max pairs aren't constraints, they are just calibration points really. The PWM values will correspond to the angles. The order they are specified in is: Base, Shoulder, Elbow, Gripper. See example below.
Phew, step two was lots of effort, step three is simpler. Now we just need to put some code on the MeBrain.
The library comes with some great examples and I will shortly be adding a MeBrain example to it. Until then you can grab any one of the examples and adapt it to your needs being sure to call the constructor with your own calibrations values. If you don't then you will be using the default values which are unlikely to work properly with your MeArm. The calibration procedure needs to be done on each MeArm you use and may need repeating if you replace a servo or otherwise change it's alignment.
When you write your code for your MeArm, you will need to decide which coordinate space to use. The library supports both cartesian and cylindrical-polar coordinates. (If you get the right version.) For the cylindrical polar coordinates you need to specify 'theta' (the angle of the base), 'r' (the distance from the Z axis to the grabber tip) and 'z' (the usual cartesian z axis position or height).
The following two images show the two available coordinate spaces.
You simply choose between these four functions:
The 'Cylinder' versions are for cylindrical polar coordinates and the others are cartesian. The 'goDirectlyTo' functions will move as fast as possible to the target. The 'gotoPoint' functions move through a smooth path to their target at a more controlled speed.
Cartesian space is better for automated control and is what the library uses internally to remember it's current position. The polar space is easier for a human to use because it is more intuitive to control theta directly.
The zero point on each axis is at the centre of rotation of the base servo, at the point where the servo joins to the under side of the MeArm.
It took me a little while to figure this all out, I even had to ask Nick about how the angles are interpreted. Hopefully this will be a useful guide in getting others to have success with the IK library.
The MeArm is super fun to play with once you have it working. I recommend you go buy at least one here.