Programming examples from today’s lecture

The following examples illustrate various useful techniques you might choose to use when you program your sumo robot.

Motor control functions:

// Example of a motor control function

// function prototypes
void forward();
void reverse();

void setup()
{
  // Configure pins
  pinMode(2, OUTPUT); // Left motor foward
  pinMode(3, OUTPUT); // Left motor reverse
  pinMode(4, OUTPUT); // Right motor foward
  pinMode(5, OUTPUT); // Right motor reverse
}

void loop()
{
  forward();
  delay(1000);
  reverse();
  delay(1000);
}

void forward()
{
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
}

void reverse()
{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
}

An example of a function with arguments (input values):

// Example of a motor control function

// function prototypes
void forward();
void reverse();
void motors(int, int, int, int);

void setup()
{
  // Configure pins
  pinMode(2, OUTPUT); // Left motor foward
  pinMode(3, OUTPUT); // Left motor reverse
  pinMode(4, OUTPUT); // Right motor foward
  pinMode(5, OUTPUT); // Right motor reverse
}

void loop()
{
  forward();
  delay(1000);
  
  reverse();
  delay(1000);
  
  motors(1,0,0,0); // left motor forward, right motor stop
  delay(2000);
}

void motors(int lmf, int lmr, int rmf, int rmr)
{
  digitalWrite(2, lmf);
  digitalWrite(3, lmr);
  digitalWrite(4, rmf);
  digitalWrite(5, rmr);
}

void forward()
{
  motors(1, 0, 1, 0);
}

void reverse()
{
  motors(0, 1, 0, 1);
}

An example of a function with a return value:

// Example of a function with a return value

// function prototypes
void forward();
void reverse();
void motors(int, int, int, int);
float distance();

void setup()
{
  // Motor pins
  pinMode(2, OUTPUT); // Left motor foward
  pinMode(3, OUTPUT); // Left motor reverse
  pinMode(4, OUTPUT); // Right motor foward
  pinMode(5, OUTPUT); // Right motor reverse
  
  pinMode(6, OUTPUT); // trigger pin for rangefinder
}

void loop()
{
  float d;
  
  d = distance();
  
  if (d < 0.5)
  {
    reverse();
  }
  else
  {
    forward();
  }
}

float distance()
{
  int duration;
  float dist;
  
  // Send trigger pulse
  digitalWrite(6, HIGH);
  delayMicroseconds(20);
  digitalWrite(6, LOW);
 
  // Wait for start of echo pulse
  while (digitalRead(7) == 0);
 
  duration = 0;
  while (digitalRead(7) == 1)
  {
    duration = duration + 10;
    delayMicroseconds(10);
  }
 
  dist = 0.5 * 1e-6 * duration * 340.0;
  
  return dist;
}

void motors(int lmf, int lmr, int rmf, int rmr)
{
  digitalWrite(2, lmf);
  digitalWrite(3, lmr);
  digitalWrite(4, rmf);
  digitalWrite(5, rmr);
}

void forward()
{
  motors(1, 0, 1, 0);
}

void reverse()
{
  motors(0, 1, 0, 1);
}

An example showing how to use the Arduino millis() function:

// Example of the Arduino millis() function

// function prototypes
void forward();
void reverse();
void motors(int, int, int, int);
float distance();

void setup()
{
  // Motor pins
  pinMode(2, OUTPUT); // Left motor foward
  pinMode(3, OUTPUT); // Left motor reverse
  pinMode(4, OUTPUT); // Right motor foward
  pinMode(5, OUTPUT); // Right motor reverse
  
  pinMode(6, OUTPUT); // trigger pin for rangefinder
}

void loop()
{
  unsigned long t, duration;
  
  // Remember what time we started driving forward
  t = millis();
  
  // Drive forward until distance is less than 10cm
  while(distance() > 0.1)
  {
    forward();
  }
  
  // Calculate how long we drove forward for
  duration = millis() - t;
  
  // Reverse to start position
  reverse();
  delay(duration);
}

float distance()
{
  int duration;
  float dist;
  
  // Send trigger pulse
  digitalWrite(6, HIGH);
  delayMicroseconds(20);
  digitalWrite(6, LOW);
 
  // Wait for start of echo pulse
  while (digitalRead(7) == 0);
 
  duration = 0;
  while (digitalRead(7) == 1)
  {
    duration = duration + 10;
    delayMicroseconds(10);
  }
 
  dist = 0.5 * 1e-6 * duration * 340.0;
  
  return dist;
}

void motors(int lmf, int lmr, int rmf, int rmr)
{
  digitalWrite(2, lmf);
  digitalWrite(3, lmr);
  digitalWrite(4, rmf);
  digitalWrite(5, rmr);
}

void forward()
{
  motors(1, 0, 1, 0);
}

void reverse()
{
  motors(0, 1, 0, 1);
}

A partial example of a state machine:

TO BE ADDED!

Advertisements
Posted in Uncategorized | Leave a comment

Last Chance to record a Tip the Can time – now in KEG-036

For any teams still wishing to record a Tip the Can time, today’s class is the last chance. The Can abides!

Teams who have already recorded a green light result will presumably already have moved on to thinking about the RoboSumo final tournament, which (subject to confirmation) is expected to take place during the normal class time in week 12 – i.e. Wednesday December 5th 2018. We will discuss this is more detail during next week’s lecture.

Posted in Uncategorized | Leave a comment

Tip the Can extra session: 4-5pm, Friday 26th October in KEG-036

There will be a final opportunity to record a week 6 time in this semester’s Tip the Can competition tomorrow, Friday 26th October between 4-5pm in room KEG-036 (where Ted and Catherine normally hold their RoboSumo lab on Wednesday afternoons).

This session is not for robot development or repair – it’s just for teams who now have a working robot and want to record a time. The session is primarily aimed at teams who have not yet achieved a green light time, but have now resolved whatever technical difficulties they had on Wednesday. However, teams who have already recorded a green light time are permitted to take part if they want to try to reduce their time.

Teams who do not achieve a week 6 green light time by 5pm tomorrow (Friday) will have another opportunity to try for one next Wednesday (week 7) during the normal timetabled RoboSumo lab (3-6pm). However, please note that all week 6 green light results will rank above all week 7 green light results.

Posted in Uncategorized | Leave a comment

Tip the Can – Live results spreadsheet

Tip the Can live results spreadsheet

Posted in Uncategorized | Leave a comment

Yes we can! Code and circuit for smart can (RoboSumo Tip the Can competition)

//
// Tip the Can code for Arduino Nano
// Written by Ted Burke - 24-10-2018
//
// State machine code for the smart can in the RoboSumo Tip the Can
// competition.
//
// Tip the can and withdraw to achieve success (green LED on for 5 seconds).
// After tipping the can, you have a 2 second grace period during which the
// can may remai tipped (or be tipped again), but during the subsequent 2
// seconds, tipping the can will result in a failed attempt (red LED for 5
// seconds).
//

// Pin numbers
#define RED 3
#define GREEN 5
#define BLUE 9
#define ARDUINO_LED 13
#define CAN 12

// Global variables
int state = 0;
int counter = 0;

void setup()
{
  // Indicator LEDs
  pinMode(RED,   OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE,  OUTPUT);
  pinMode(ARDUINO_LED, OUTPUT);

  // D7 provides ground for blue and green LEDs
  digitalWrite(7, LOW);
  pinMode(7, OUTPUT);
  
  // D12 is a pull-up input for the can (tipping can shorts D12 to ground)
  pinMode(CAN, INPUT_PULLUP);

  // D11 provides ground for can switch
  digitalWrite(11, LOW);
  pinMode(11, OUTPUT);
}

// Used to change state in state machine
void change_state(int n)
{
  state = n;
  counter = 0;
}

void loop()
{
  int touch;

  touch = !digitalRead(CAN);  
  digitalWrite(BLUE, touch);

  counter++;

  if (state == 0) // Initialisation - light all LEDs for 2 seconds
  {
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
    digitalWrite(ARDUINO_LED, HIGH);
    
    if (counter >= 200) change_state(1);
  }
  else if (state == 1) // Waiting for can to be tipped
  {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(ARDUINO_LED, counter/10 % 2);

    if (touch) change_state(2);
  }
  else if (state == 2) // Tip detected (two second grace period)
  {
    digitalWrite(RED, counter/10 % 2);
    digitalWrite(GREEN, counter/10 % 2);
    digitalWrite(ARDUINO_LED, LOW);

    if (counter >= 200) change_state(3);
  }
  else if (state == 3) // Tipping forbidden for 2 seconds
  {
    digitalWrite(RED, (counter/10 + 1) % 2);
    digitalWrite(GREEN, counter/10 % 2);
    digitalWrite(ARDUINO_LED, LOW);

    if (counter >= 200) change_state(4);
    if (touch) change_state(5);
  }
  else if (state == 4) // No tipping after grace period - SUCCESS!
  {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(ARDUINO_LED, LOW);

    if (counter >= 1000) change_state(0); // Reset after 10 seconds
  }
  else if (state == 5) // Tip was detected after grace period - FAILURE
  {
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
    digitalWrite(ARDUINO_LED, LOW);

    if (counter >= 1000) change_state(0); // Reset after 10 seconds
  }

  // Each iteration of the loop is approximately 10 ms
  delay(10);
}
Posted in Uncategorized | Leave a comment

Whiteboard snapshots from today’s lecture on Tip the Can competition

Posted in Uncategorized | Leave a comment

Switch input example for Arduino

This is a simple switch input example for the Arduino Nano. The circuit diagram, photos of the breadboard circuit, and two example Arduino programs are shown below.

This version switches the LED on when the switch is pressed and switches the LED off when the switch is not pressed.

//
// Arduino switch input example
// Written by Ted Burke - 10-10-2018
//

void setup()
{
  pinMode(3, OUTPUT); // indicator LED
}

void loop()
{
  int x;

  x = digitalRead(2); // switch input
  
  if (x == 1)
  {
    digitalWrite(3, HIGH); // LED on
  }
  else
  {
    digitalWrite(3, LOW); // LED off
  }
}

This version toggles the state of the LED each time the switch is pressed.

//
// Arduino switch input example
// Written by Ted Burke - 10-10-2018
//

// This variable remembers if the LED is on or off.
// Each time the switch on D2 is pressed, this
// variable switches from 1 to 0 or vice versa.
int led_on = 0;

void setup()
{
  pinMode(3, OUTPUT); // indicator LED on D3
}

void loop()
{
  int is_pressed;

  // Read the switch input and store value in variable is_pressed
  is_pressed = digitalRead(2);
  
  if (is_pressed == 1)
  {
    // The switch is pressed so toggle the led_on variable
    led_on = 1 - led_on;

    // Wait 500ms to allow switch to be released
    delay(500);
  }

  // Set the LED pin (D3) to match the led_on variable
  digitalWrite(3, led_on);
}
Posted in Uncategorized | Leave a comment