Sunday 10 February 2013

Servo Offset

Hello everyone,

In this article I will like to describe a method for offsetting the servos.

When working with servos I realised that each servo is different in terms of achieved angle when a command is applied. What I mean is if I apply a command to turn a servo to let's say 90 degrees this will unlikely go exactly to 90 degrees. Almost any servo will go to slightly different angle. I imagine this is due to the assembly of the gears carried by the manufactures.

I tried to move the gears mechanically to overcome this but I didn't succeed so I've decided to correct this on software.

For each servo I need to find its offsetting value and then apply it to my code.

The easiest is to choose an angle easy to measure like 90 degrees. To turn a servo to this position we need to apply a 1500 microsecond pulse. In my code this corresponds to line like:

Serial.println (#0 P1500 T800);    //Move servo 0 to 90 degrees (1500 microseconds) and this movement will take 0.8 seconds

Then increase or decrease the value 1500 until the servo reaches exactly 90 degrees.

Note for Hitec servos: To turn a servo 0 degrees you need to apply around 500 microsecond pulse and to turn a servo 180 degrees you need to apply around 2500 microseconds pulse.

To make this task easy I created a code that increases and decreases the pulse value by typing in my computer keybord the caracters "+" and "-".

This code is also interesting because it shows how to interact with your servos using your computer keyboard.

Code:


int val = 0;
int angle = 1500;
int servo = 24;
int resolution = 10;                                   // This decrements or increments the microseconds value in 10 units.

void setup() {

Serial.begin(115200);
Serial.print ("#");
Serial.print (servo);
Serial.println (" P1500 T800");

}

void loop() {
  val = Serial.read();                                //Reads the value entered in the serial monitor after is sent
  if(val == '+'){

  Serial.print ("#");
  Serial.print (servo);
  Serial.print (" P");
  angle = angle + resolution;
  Serial.print (angle);
  Serial.print (" T");
  Serial.println (800);
  delay(1000);  
  }
  if(val == '-'){
  Serial.print ("#");
  Serial.print (servo);
  Serial.print (" P");
  angle = angle - resolution;
  Serial.print (angle);
  Serial.print (" T");
  Serial.println (800);
  delay(1000);
}}

As you can see in the code I applied 10 units of resolution. If you apply less then you almost will not see the servo move and if you increase the value too much the servo will make bigger jumps and you may not be able to achieve the desired angle.

In this example the right value to bring my servo exactly to 90 degrees was to apply 1440 microseconds.

See the Serial Monitor...


The values we obtain here are still a bit subjective... In my case I stopped at 1440 microseconds but another person may would stopped at 1430. Due to this reason I recommend to do this process with 3 angles... 0, 90 and 180 degrees so you will get 3 pair of values (a pair of values consists on an angle and its equivalent microsecond value).

Then you can apply the offset by using the equation for non-vertical lines:

                                                                 y = mx + c \,

I will explain how to apply the offset in my next post. I promise it will not take long to be published.

Monday 19 November 2012

Published Chapter II on Inverse Kinematics

Good news everyone,

The second chapter on Inverse Kinematics is now published. In this one I show a simple code using the equations from chapter one to make the tetrapod alive...


I hope you find it interesting and useful.

Tuesday 6 November 2012

Inverse Kinematics - Chapter I

Good news everyone,

I've published the first chapter regarding the Inverse Kinematics. This will allow us to set the legs to a desired position.

The Inverse Kinematics will provide the angles that the servos need to rotate to achieve the position we want.

I've tried to be simple and clear in my explanations but any questions or improvement will be much appreciate it. I hope this is useful and see you in the next post.


Sunday 21 October 2012

Pressure sensor

Hello everyone,

I came across to a nice tutorial about how to control a servomotor's position using the value returned from an analog sensor and I've decided to try it but adapting it using the SSC 32 controller as I will use it in my tetrapod.
 
I'm using a variable resistance pressure sensor as a analog input. The sensor operates by decreasing resistance the harder it is pressed. When applying different pressure levels to the sensor the servo will move accordingly.
 
The sensor is connected in series with a fixed resistor across a 5v supply (from Arduino) forming a voltage divider. The pressure dependent voltage is then read by the microcontroller and then the value is mapped to move the servo in a range between 0 and 180 degrees.
 
Let's see the connexions sketch. Note I couldn't find in Fritzing an SSC 32 controller so I've used a PWM shield to show connections:




I've used a 820 ohms reistor instead of 10k because I couldn't find one in my toolbox and the resistor is as I said before to act as a voltage divider. So I play with the one I had and it worked as you can see in the little video.
I've used the Serial Monitor from Arduino IDE to visualise the readings from analog input to be able to map the values.

Code:

int potpin = 0;                                                // analog pin used to connect the potentiometer
int val;                                                           // variable to read the value from the analog pin

void setup()
{
  Serial.begin(115200);                                 //initiate serial communication with SSC 32

}
void loop()
{
  val = analogRead(potpin);                         // reads the value of the pressure sensor (value between 323 and 330)
  val = map(val, 323, 330, 100, 500);          // scale it to use it with the servo (value between 0 and 180)
  Serial.print ("#");
  Serial.print (0);
  Serial.print (" P");
  Serial.print (val);                                       // sets the servo position according to the scaled value
  Serial.print (" T");
  Serial.println (800);                
  delay(15);                                                 // waits for small amount of time before making another reading
}
 




Maybe I can use this type of sensor to make my tetrapod react in different ways. We will see. 

Wednesday 17 October 2012

Arduino + SSC 32 + Servo


Hello everyone,

In this post I want to show how to use the SSC32 controller with Arduino for moving few servos and compare it with a direct connexion of servos to Arduino without using a servo controller.

I will start with the later...

We need:
  • Arduino
  • Servos
  • Breadboard
  • Wires
  • Power cable for Arduino
I've made the connections layout below using fritzing

Arduino receives power and data from USB. Arduino is powerful enough to give power to 3 servos so we can connect red and black wire from 5v and ground in Arduino to breadboard. Servos will take power from the breadboard lines set by Arduino. We also need to connect yellow wire to a PWM input pin. I select pins 2, 3 and 4. If we want to connect more servos probably we will need to add an external battery to power them.

Code:
#include <Servo.h>
Servo servoMain1;                                // Define our 1st Servo
Servo servoMain2;                                // Define our 2nd Servo
Servo servoMain3;                                // Define our 3rd Servo
void setup()
{
   servoMain1.attach(2);                        // servo1 on digital pin 2
   servoMain2.attach(3);                        // servo2 on digital pin 3
   servoMain3.attach(4);                        // servo3 on digital pin 4
}
void loop()
{
   servoMain1.write(0);                          // Turn Servo1 to 0 degrees
   servoMain2.write(90);                        // Turn Servo2 to center position (90 degrees)
   servoMain3.write(180);                      // Turn Servo3 180 degrees
   delay(1000);                                        // Wait 1 second
   servoMain1.write(180);                      // Turn Servo1 to 180 degrees
   servoMain2.write(135);                      // Turn Servo2 135 degrees
   servoMain3.write(90);                        // Turn Servo3 to center position (90 degrees)
   delay(1000);                                       // Wait 1 second
}
Using SSC32
  • Arduino
  • Controller SSC32
  • Servos
  • Wires
  • External battery
  • Power cable for Arduino and SSC32
We need to connect the SSC32 controller to Arduino like below:

See also how to connect the 3 wires of each servo and where to connect an external power supply (5v) for the servos. The picture below shows the controller being powered by USB cable to the computer.


Also it is important to set the baud rate the same as in the code ("serial.begin" parameter)


Code
void setup() {
Serial.begin(115200);                              //Start serial communication at 115200 baud rate
}
void loop() {
 
  Serial.println("#0 P750 #4 P750 #11 P750 T500");         //Moves 3 servos at same time
  delay(1000);                                                                     //Wait 1 second
  Serial.println("#0 P2200 #4 P1500 #11 P2200 T500");
  delay(1000);
}

Using the SSC 32 controller the connexions are simpler and neater. Also is really powerful to be able to move many servos at same time using the multi-servo command (the one used in the code above). Notice in the video that the servo with yellow target starts and finish movement at the same time as the others even though rotates smaller angle, so movement is slower than the other 2.
 

Sunday 30 September 2012

Good news everyone


Components selection

A tetrapod robot is a mechanical vehicle that walks on 4 legs.
After some research I've selected the following components to achieve my walking robot. I don't want to say too much information at same time as many of the topics below will be developed in future posts:
 
Microcontroller (Brain)
 
ATmega1280 (datasheet) mounted on a Arduino Mega microcontroller board.

Why? Lots of inputs/outputs (to talk to motors, sensors and other devices). Open source and extensible software and hardware (lots of info in the web), cost effective, clear programming environment. The fact to have the microcontroller on a board like the Arduino Mega simplifies the connexions and also allows us to interact with sensors and other devices (like distance sensor, mobile phones & Wii nunchuck). Has 4 serialcommunications.

Motors

RC Servos. Not decided model yet until a torque study is done. This study will provide size (torque) needed. I will purchase a couple of standard servos for first tests.

Why? Provide precise control of motor output (in terms of an angle). We will give sequences of angles to the legs in order to create walking motion.



 
SSC-32 Servo Controller (link)

This servo controller board can communicate with a microcontroller like Arduino via serial communication and control up to 32 servos.

Why? Why not... Joking. My tetrapod has 4 legs with 3 servos in each leg to give nice movements. To control 12 servos using the Arduino Mega is feasible but because each servo has 3 cables to be connected this can become quite messy. The SSC-32 has header pins that facilitates the connexion a lot. I purchased mine here.

On top of that the SSC-32 can control motion on speed, time or a combination. Also has "Group Move" commands that allow any combination of servos to begin and end motion at the same time, even if the servos have to move different distances. This is a very powerful feature for creating complex walking gaits for multi servo walking robots. It has also 4 digital inputs that can be handy.
 
 

Physical structure (legs and body)

Legs and body will carry motors, sensors , batteries and electric boards. I will use aluminium or plastic if possible to keep weight down. The physical size (leg length) has to be as small as possible again to keep weight down but also to keep torque as low as possible.

Why? The overall weight and length of parts attached to servos is a key factor when sizing the servos. A heavier and long legged robot requires bigger (more expensive) servos. We will see this when discussing the "Torque Study".



Power

We need to give power to the Arduino, SSC-32 and servos. The 2 boards will be powered with a 9V battery and servos need 4V-7.4V (depending on type).
For servos I will use 7.4V Li-Po battery. Arduino can give power to other devices like sensors and Wii nunchuck.

Why? Because I have a 7.4V Li-Po battery from our RC helicopter. Although, even if I select servos rated to 6V I'm planning to use 7.4V battery as people has been using them with no issues. I invite you to contradict this (and the sooner the better as I don't want to burn any servo). The weight of this battery is also a lot less than 4AA batteries (this give us 6V (1.5Vx4)) and as I said before weight is critical for sizing cheaper servos.



Software

We need to tell (program) the microcontroller what to do and when to do thinks. I will use the Arduino IDE. This is cross-platform application that "controls" your software to make sure the microcontroller will understand. Also will allow us to send the software to microcontroller so our brain knows what to do. The Arduino IDE comes with a C/C++library called "Wiring" (from the project of the same name), which makes many common input/output operations much easier

Why? Quite simple and easy language with lots of info in the net. And it's designed to talk to Arduino.
Recently I've heard that has some limitations but I will see if as I develop the project.