Thursday, May 22, 2014

Tank code

Here's the Tank code, from when I used the Tamiya Tracked Vehicle Chassis as my platform.  (The Blink comments at the top just mean I recycled the code from an earlier LED test and forgot to remove the comment...)

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

void setup()
{
  int i;  
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);
  
  // Right motor (with gearbox on rear)
  pinMode(3, OUTPUT);     // PWM, HIGH=fast, LOW=slow or off
  pinMode(9, OUTPUT);     // Brake, HIGH=brake, LOW=coast when PWM low
  pinMode(12, OUTPUT);    // Direction, HIGH=forward, LOW=reverse

  pinMode(8, OUTPUT);     // Brake
  pinMode(11, OUTPUT);    // PWM
  pinMode(13, OUTPUT);    // Direction

  //run_tank;  
  digitalWrite(9,HIGH);
  digitalWrite(8,HIGH);
  

  digitalWrite(12,LOW);
  delay(1000);
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(3,HIGH);
  delay(5000);
  digitalWrite(9,HIGH);
  delay(1000);
  
  digitalWrite(12,HIGH);
  delay(1000);
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(3,HIGH);
  delay(5000);
  digitalWrite(9,HIGH);
  delay(1000);


  digitalWrite(13,LOW);
  delay(1000);
  digitalWrite(8,LOW);
  delay(1000);
  digitalWrite(11,HIGH);
  delay(5000);
  digitalWrite(8,HIGH);
  delay(1000);
  
  digitalWrite(13,HIGH);
  delay(1000);
  digitalWrite(8,LOW);
  delay(1000);
  digitalWrite(11,HIGH);
  delay(5000);
  digitalWrite(8,HIGH);
  delay(1000);
  
  digitalWrite(9,HIGH);
  digitalWrite(8,HIGH);
  
}

void loop()
{
}

Nothing special, really.  With the digital pinMode settings to output, I set up the Arduino Uno and Motor shield to control the outputs to the motors.  After //run_tank, I turn on both brakes.  Then the right motor is set to reverse, the brake is released, the right motor is run (in reverse), and then the brake is applied.

Next, the right motor is set to forward, the brake is released, the right motor is run (forward), and then the brake is reapplied.

And then I do the two steps on the left motor, before finally setting both brakes.

I suppose I could have put the same code in loop().  Mainly I put it in setup() out of habit.

However, I found this approach pretty unsatisfactory.  Without sensors providing any feedback, there's no way for the robot to find its way around a room or other obstacle course, so all of the motion has to be preprogrammed.  With the hardware I had available at the time, with no wireless communication to the Arduino, that writing a new program, picking up the tank and attaching the Arduino to the computer, and uploading the program before setting the tank back on the floor and pressing the button to restart the device.

There simply was no remote control or even limited robot intelligence and autonomy.

I needed something better.

(I'll put up a schematic of the wiring for this tank robot later, when I get around to it.)