Having finished building all parts of the robotic arm kit I bought (see part 1), it is now time to start programming the arm to make it do stuff. SainSmart provides a file with basic code, so I'll start with that.
SainSmart provides this large zip file (162 MB), which includes a video showing arm movement, a C++ script of basic arm commands, and a text file documenting the basics of the C++ script.
The C++ script provides a basic idea for how to interact with the arm's servos. This code has some documentation, however, it's all in Mandarin.
The text file seems promising at first glance, but it assumes you are using an Arduino Uno, not the Mega the kit includes. It also only mentions the how of working with some of the code, not the why of the code structure.
Seeing that there is no hand holding from SainSmart, I had to debug the script line by line to learn its mysteries.
After scanning the code, I realized that the code was not very good. I rewrote sections to provide better functionality with lots of documentation. All of the code below has been tested on my arm, however, many of the servo angles that work for my arm may not work for your arm due to the way you setup the servo horns when assembling the arm.
The first section of the script is normal C++ code for including the Servo library, and for defining all variables.
The setup function attaches all servos to the servo objects specified, along with moving all servos to their default angles. These default angles aren't necessarily their neutral angles. A few of them are offset to provide better arm balance.
The loop function runs the main processing of the script. It identifies any commands that are entered into the serial monitor (changing modes and specifying joint angles in computer control mode), along with executing the computer control commands and auto run commands. The auto run commands cycle through a set range of joint angles.
To run this script, first open the file within the Arduino IDE. You can view and edit the code within this window. Next specify the correct board you are using within Tools > Board > Arduino Mega 2560. Select the correct device within Tools > Serial Port > /dev/ttyACM0.
Now you can upload the script to the Arduino by pressing the Upload button on the toolbar or select File > Upload.
Note that once uploaded, the Arduino will execute the program immediately. If you are using the Arduino servo shield, the servos will not start moving until the shield is connected to a 12 volt DC power input, and the power button is pressed down.
Open the serial monitor by selecting Tools > Serial Monitor, or pressing Ctrl+Shift+m. You will be able to send commands to the Arduino via the serial monitor. To make the arm use auto control, type #auto. To use the computer control, type #com.
When using computer control, you can specify specific joint angles by typing the angle (integer between 0 and 180) and joint identifier (a - f) into the serial monitor. For example, typing 90b will move the second joint (shoulder) to 90 degrees. Typing 90f will close the gripper while typing 30f will open the gripper as wide as it can go. Again, these angles work with my servos, but you may have angled you servos at different orientations, thus make sure to test the limits of each servo before moving them to extreme angles.
This basic script does a good job at showing how to work with the arm. I think it illustrates just how easy it is to work with these servos.
Next I want to expand the capability of what the file can do. I also want to explore how to integrate the remote controller with the arm, so that I can control the arm using the joysticks on the controller. That will require programming the controller's Arduino Uno to talk with the arm's Arduino using the 2.4 Ghz radios. Stay tuned for that!