Week 5: Prep for Race to the Wall

This weeks class was cancelled so we met up in the lab anyway as we had a good amount of work we needed doing by next week as it was “Race to the Wall” challenge, so like before we set about making tasks for everyone, I would go about writing the code for the race, Sean and David would be tackle the task of designing the bot and figuring out the best way about building the bot, Nathan was sick this week so he wasn’t in.
The program I had to write would have the robot race forward to the wall when the switch was pressed and when the bot hit the wall it would reverse until it gets back to the starting position and wait there until the switch is pressed again. So I decided that for each operation of the robot I would write a “state” so the program only checks one or two things at a time instead of it getting confused by checking everything all the time. When I had the code written I tested it and it didn’t work, wasn’t sure if the chip wasn’t registering the switch being on or that the code was stuck so I used two LEDs to tell which state I was in. After I tested it again I was seeing that it was stuck in state one, which was waiting for the switch to be pressed to start, I tried troubleshooting with the multi-meter and the switch was giving a HIGH value to the pin but the chip wasn’t registering it so I wasn’t sure why it wasn’t working. I then changed around the code and moved the switch to a different pin on the chip and tested it and it worked so I’m assuming that the input on that pin wasn’t working as using it as an output did. The working code is shown below:

//
// Race to the Wall
// Written by Ronan Byrne - 21-02-2015
//
 
#include <msp430.h>
#define REVERSE 0b00100010
#define FORWARD 0b00010001
#define STOP 0b00000000

// Function prototypes
unsigned int read_analog_channel(unsigned int);

int main( void )
{
    // Watchdog timer
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
     
    // Digital i/o
    P2DIR = 0b00110011; // P2.0,1,4,5 are outputs, the rest are inputs
    P1DIR = 0b00000110; // P1.1 and 2 are outputs, the rest are inputs
 
    // Analog inputs
    ADC10AE0 = 0b00111000; // A3,A4,A5 (pins 5-7) are analog inputs
    ADC10CTL0 = ADC10ON;   // Turn on the ADC
     
    // Define integers for colour sensor, white value and robot state 
    int colour;
    int state = 1;
    int white_threshold = 512;
    
    while(1)
    {
        // Read voltage from analog input A3 (pin 5)
        colour = read_analog_channel(3);
        
        // Do actions for current state
        if (state == 1)
        {
            // STATE 1: Wait for button to be pressed to start
            P2OUT = STOP; // motors stopped
            P1OUT = 0b00000100; //Second LED on
 
            // Check if switch is pressed
            if ((P1IN & BIT0) == 1) state = 2;
        }
        if (state == 2)
        {
            // STATE 2: Wait for button to be released
            P2OUT = STOP; // motors stopped
            P1OUT = 0b00000010; //First LED on 
 
            // Check if switch is released
            if ((P1IN & BIT0) == 0) state = 3;
        }
        if (state == 3)
        {
            // STATE 3: Drive forward until button is pressed at wall
            P2OUT = FORWARD; // motors forward
            P1OUT = 0b00000110; //Both LEDs on
 
            // Check if switch is pressed
            if ((P1IN & BIT0) == 1) state = 4;
        }
        if (state == 4)
        {
            // STATE 4: Reverse until colour sensor sees white
            P2OUT = REVERSE; // motors reverse
            P1OUT = 0b00000010; // First LED on
            
            // Check if colour sensor sees mid colour to white
            if (colour > white_threshold) state = 5;
        }
        if (state == 5)
        {
            // STATE 5: Drive forward for 100ms to brake
            P2OUT = REVERSE; // motors forward
            P1OUT = 0b00000100; // Second LED on
            __delay_cycles(10000); // Wait 100ms
            state = 1; // Return to state 1
        }
    }
    return 0;
}

//
// This function performs a single analog to digital conversion,
// converting the voltage on analog input pin ANx into a 10-bit
// unsigned integer. Execution time for this function is of the
// order of 100us.
//
unsigned int read_analog_channel(unsigned int x)
{
    ADC10CTL0 &= ~ENC;            // disable conversion
    ADC10CTL1 = x << 12;          // select channel
    ADC10CTL0 |= ENC;             // enable conversion
    ADC10CTL0 |= ADC10SC;         // start conversion
    while(ADC10CTL1 & ADC10BUSY); // wait until complete
    return ADC10MEM;              // return digital value
}

I haven’t actually tested it with the bot as its not built yet so somethings may need to be changed like the white threshold might need to be increased or the braking delay may need to be increased as well.
While I was doing this Sean and David were working on the robot, Sean drew a technical drawing of the the robot we got last week as we were going to use this as a bases for the design of our robot, his drawing is shown below:
DSC_0122[1]
We will be adding wheels to the front to keep it as straight as possible, the robot right now has a ball for the front wheel which we’ll probably use for the tournament but for the race we thing that the wheels will be better. We have to make sure that we stay within the 10cm by 10cm restrictions of the challenge so we are going to get small enough wheels stay safely within the restrictions. David started taking apart the robot so we could use the pieces later for our robot, when testing the circuit on the new motors we salvaged last we we notice that they were much slower than then when just connected to the battery, after testing with the multi-meter the voltage was 3V coming out of the power chip, we’re not sure if its because the batteries weren’t fully charged or something was wrong with the chip, David is going to test the circuit at home with a 9V battery to see if it makes any difference. We decided that we did enough and would work on anything we thought needed working on at home, me improving the code, Sean drawing up more designs and David trying to get the most out of the motors.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s