Hexapod Coding Preferences
By Chau To
- Introduction:
This blog post will discuss some issues of the servo library (Servo.h) and how coding of the Hexapod was built including functions and subfuctions as well as some tips and troubleshooting.
Note: Hexapod team drives 19 servos directly from the Arduino board (digital I/O pins), and it uses the built-in Servo.h library functions such as “write” and “attach” as the main code to rotate the servo shaft. If your hexapod uses an additional servo controller boards such as the Adafruit etc, some of the materials discussed in the post would not apply in that situation.
- How to create object servo in the Servo.h library:
Since the coding will require “for” loops, creating individual servo object such as servo1, servo2, etc.. Similar to the example in the Ardunino websites is impossible. The only way is to create an array of servo object. How can we do it? We can define a number of servo then call the servo library to create and array of servo object:
#define number_of_servos 19
Servo servos[number_of_servos];
So, we will have an array of servos; in this example, we will have array of 20 servos. Each servo will be appearing like this servo[1], servo[2], etc… NOW! We can put these into for loop. For example: servo[i].write(180).
- Array:
Array is very important. As mentioned above, when we run “for” loop we need to use array because it will optimize our coding. An array of digital I/O pin is a great example since we need to attach each servo to a specific pin on the Arduino. Here is a sample code:
int pins[] = {30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48};
void set_servos() {
for (int i=0; i<number_of_servos; i++) {
servos[i+1].attach(pins[i]);
}}
The above example is a simple function I wrote to attach the pin to the servo. My digital pins will be from pin 30 to pin 48 of the ADK board.
The same concept applies for the servo’s shaft angle rotation. If user wants a specific servo to rotate at a specific angle, they can make an array of angles and run it in a for loop. More detail about array of C++ can be found at: http://www.cplusplus.com/doc/tutorial/arrays/
- Subfunctions:
Subfunctions are smaller functions that will be called in a main or larger function. Breaking a long and complex function into smaller subfunctions is very useful because it’s easier for the user to double-check and debug the code. For example, one of the main function of the hexapod is run which includes 2 stages; first stage is moving the femur, tibia, and shoulder to a new position, and second stage is to bring them back to the initial position. So, we can break these 2 stages into 2 subfunctions. And, even smaller subfunctions for controlling and debugging.
- Serial Monitor:
Serial Monitor can be turned on by Ctrl+Shift+M after uploading the code into the Arduino board. The serial monitor is extremely useful when testing the code. It’s used to keep track of the codes and fix all the bugs before uploading to a real model. This is the sample of the serial monitor added in my set_servo function that was discussed in the previous part:
void set_servos() {
for (int i=0; i<number_of_servos; i++) {
servos[i+1].attach(pins[i]);
Serial.println(“attach pin”);
Serial.println(i+1,DEC);
Serial.println(pins[i],DEC);
}}
With these serial function added in, I can monitor for my loop as well as to see which servo had attached to which pins. The serial monitor is available at: http://arduino.cc/en/reference/serial
Note: In order to run the serial monitor, in the setup you have to call the serial.begin
void setup()
{
Serial.begin(9600);
Figure 2