| ClusterBot, my first Arduino robot. |
See the ClusterBot build videos here.
ClusterBot specs:
Arduino Uno microcontroller board
Pololu round robot chassis
Tamiya twin-motor gearbox
Toshiba TB6612FNG motor driver on carrier board providing PWM motor control.
9V battery pack for Arduino power
2 AA battery pack for motor power
Various K'NEX parts for mounting parts
The robot is performing fairly well, with some limitations. First of all, the robot is stupid. No active sensors at this time, ClusterBot will simply go exactly where you tell it, with no obstacle avoidance in place.
I had to spend a little time getting ClusterBot to track straight. I've got no physical way to do a front-end alignment on the robot, so I tweaked the goForward() and goBackward() functions by changing the PWM values until the bot tracked relatively straight. It's not crucial that it's perfect with this robot, but when I first ran ClusterBot, he would move in arcs instead of lines. I'm not sure if the robot is not tracking straight because it is physically crooked, the weight is unevenly distributed, the PWM signals are different from the two pins on the Arduino, or if it's simply a matter of different performance characteristics of the two cheap-o motors. Probably a combination of all of the above.
I also took some rough measurements of the angular distance travelled when rotating left and right so I could easily make the robot turn roughly 90 degrees, 180 degrees, etc. The problem with this is that as the batteries discharge, the angular travel will decrease also. It's just a rough estimate. It might not be on this bot, but I will have to have encoders on my wheels at some point, or just use stepper motors. Encoded wheels will probably be the answer though.
ClusterBot has been a qualified success so far, and I've learned a ton about the mechanical construction, Arduino code and motor drivers. The next order of business is to get some sensors on this bot, starting with the HC-SR04 ultrasonic sensor. I'll also put some microswitches tied to bump sensors. Eventually I'd like to make ClusterBot have line following or avoiding capabilites also.
The code: Please excuse my lack of commenting. It should be fairly straightforward and easy to read though.
#define PWMA 3
#define AIN1 0
#define AIN2 1
#define PWMB 5
#define BIN1 2
#define BIN2 4
#define STBY 6
/* Robot does 27 rotations in one minute and
10 feet in 25 seconds. Current straight tracking is 233 left, 255 right. */
void setup() {
// put your setup code here, to run once:
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(STBY,OUTPUT);
}
void loop() {
startUp();
goForward();
delay(5500);
turnAround();
goForward();
delay(5500);
turnAround();
goBackward();
delay(5500);
rotateLeft();
delay(560);
rotateRight();
delay(560);
goForward();
delay(3000);
applyBrakes();
delay(2000);
}
void goForward ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,234);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void goBackward ()
{
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,233);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void rotateRight ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,255);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void rotateLeft ()
{
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void veerLeft ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,190);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void veerRight ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,190);
}
void applyBrakes ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void startUp ()
{
digitalWrite(STBY,HIGH);
}
void turnAround()
{
rotateLeft();
delay(1370);
}
void shutDown ()
{
digitalWrite(STBY,LOW);
}