Bot balancing without gyros … for a few seconds at least
No gyros, no accelerometers – only a sonar ranger and lots of code.
The motors used are standard servos modified for continuous rotation, sonar sensor is a HC-SR04 as detailed in a previous post
Code ? Well here’s a simplified portion of the PID loop:
int Drive = 0; // PWM value sent to Servos
int Error[5]; // array of 5 Error elements
int P = 0; // proportional term
int I = 0; // integral term
int D = 0; // derivative term
#define Current 0
#define Sum 1
#define Last 2
#define SecondToLast 3
#define Delta 4
int PID() {
Error[Current] = SetPoint – Ptime;
P = Error[Current] * Kp;
Error[Sum] = Error[Current] + Error[Last] + Error[SecondToLast];
I = Error[Sum] * Ki;
Error[Delta] = Error[Current] – Error[Last];
D = Error[Delta] * Kd;
Drive = P + I + D;
Error[SecondToLast] = Error[Last];
Error[Last] = Error[Current];
Hooked an SR04 ultrasonic sensor to my arduino board – robot eyes !
Ranger sensor uses sonar to give accurate distance measurement to objects or obstacles placed in front of it. They’re surprisingly sensitive / accurate – I was able to reliably measure to a millimeter at 15cm range.
These sensors are also now VERY cheap £2.37 (4$) delivered from HK, and accurate to a few mm.
Added a servo for visual feedback:
Arduino code is basically sending a trigger on one pin, receiving a timing value on another,
which can then be converted into a distance:
Arduino is an open-source platform for microprocessor projects, or as the Arduino website puts it:
“Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.”
Basically its a little CPU that you can program in C, and easily attach servos and sensors to. The open-source nature means there’s a wealth of example code and libraries out there to modify for your own use.
The manual goes through 11 projects, from a simple blinking LED to using shift registers and relays, as well as giving a brief overview of how to program for the board and some more complicated tasks after you’ve built each of the individual projects (such as making the blinking LED fade instead).