Robotic arm programming
Example code to use joysticks for controlling
Arduino library
You don’t need to install additional library to control the robotic arm (they are simply servos), but this library provides Inverse Kinematics (IK) that makes it easy for you to program robotic motion.
Menu Tools > Manage libraries… > Library Manager, search for “mearm” and install MeArm-Robot-Arm by Ben Gray.
The library is also on GitHub MeArm / MeArm-Arduino if you want to just look at the code.
Using library examples
There is a built-in IK example which lets you control the end-effector position in X, Y, Z using the joysticks.
Menu Files > Examples > MeArm-Robot-Arm > JoystickIK
There is a bug in the code, fix it before compiling: Change #include "MeArm.h" to #include "meArm.h" with a lowercase m.
You also need to change the pin definitions to match our motor shield:
int basePin = 9;
int shoulderPin = 7;
int elbowPin = 8;
int clawPin = 6;
Note that we’ve changed the claw into a cube.
A hack in MeArm-Robot-Arm library
Since we’ve changed the end effector to a rotatable cube, we want to control the rotation angle, but the library only contains functions openClaw() and closeClaw() which open turns servo the 0 or 180.
Let’s add a new function to the library!
- Find the library folder in the Arduino library directory. Go to Files > Preferences, and you can see Sketchbook location, open this location in the File Explorer/Finder.
- Go into libraries, you should see MeArm-Robot-Arm folder. This is where the library is installed.
- Open MeArm-Robot-Arm folder, you see a bunch of C++ code. These code are compiled everytime when you click on compile/upload button in Arduino. Thus you can simply change the libary code here for any customization.
- We want to add a new function called
controlClaw()which can be passed in an integer that represents the angle (in the library we use radians rather than degrees). - Open
meArm.h, aftervoid closeClaw();, add a linevoid controlClaw(float theta);. - Open
meArm.cpp, insert a new function aftervoid MeArm::openClaw():
void MeArm::controlClaw(float theta) {
_claw.write(angle2pwm(_svoClaw,theta));
delay(100);
}
That’s it, now you can use the new function in Arduino! You can do a conversion for convenience:
arm.controlClaw(float(degrees)*PI/180);
Using Processing to control Arduino robotic arm
You can use any program to read and write Arduino using Serial.
Here is an example using Processing.
- Arduino: Download code (.zip)
- This requires MeArm-Robot-Arm library and our hack
- Select Aruino Uno and the correct port, Upload this code to the board
- Processing: Download code (.zip)
- Make sure Arduino is connected through USB
- Befire running Processing, you need to change the port name