Instructions – November 2015

There are two stages to creating your RoboSlam robot:

  • Stage 1 is the physical construction
  • Stage 2 is programming

Note: If you’re reading this in Internet Explorer, please switch to Firefox or Chrome now. Copying and pasting the example programs from Internet Explorer will not work correctly.

Stage 1: Construction

The instructions for constructing the RoboSlam robot are in this PDF document:

We have pre-programmed your MSP430 microcontroller with a simple example program, so once you insert batteries into your completed robot it should spring into life.

Stage 2: Programming

This stage of the workshop is where you really begin to take control of the robot. We will work through three example programs, each of which illustrates how to control a different feature of the robot.

Step 2.1: The RoboSlam folder

To write programs that control your robot, you need a text editor called Notepad++ and a C compiler called mspgcc. Check if there is a folder called “C:\RoboSlam” on your PC. If it’s there, it probably already contains all the software you need to program your robot so you can skip to Step 2.2.

In case the folder “C:\RoboSlam” isn’t already on your PC, we have created a zip file called "roboslam.zip" which contains all the software you need.

If you don’t have the “C:\RoboSlam” folder, please download “roboslam.zip” and extract its contents to "C:\RoboSlam\".

WARNING: If you decide to place your RoboSlam folder in a different location, the path to the folder must not contain any spaces. For example, "C:\Documents and Settings\users\Joe\RoboSlam" won’t work because there are spaces in the folder name "Documents and Settings".

Step 2.2: Connecting the robot to the MSP430 Launchpad

To update the software on the robot, you need to connect it to the PC using the MSP430 Launchpad. The Launchpad connects to the PC via a USB socket. It connects to the robot via three colour wires that you’ll find in your kit – black, green and yellow.

  • The black wire connects GND on the Launchpad to hole H3 on the breadboard.
  • The yellow wire connects TEST on the Launchpad to hole H6 on the breadboard.
  • The green wire connects RST (short for “reset”) on the Launchpad to hole H7 on the breadboard.

The three programming wires are shown from various angles in the photos below. To make the programming wires clearly visible in the photos, the colour sensor module was temporarily disconnected from the breadboard. However, you should leave yours connected.

Step 2.3: Blinking LED example program

It’s time to compile your first program to control your robot.

  • Create a folder called "C:\RoboSlam\blink\".
  • Open the Notepad++ text editor by double clicking on the "Run Notepad++" shortcut in the RoboSlam folder.
  • Copy and paste the C code below into Notepad++ and save it as the filename "main.c" in the "blink" folder.
  • Copy and paste the file "build_for_g2452.bat" from the "RoboSlam" folder into the "blink" folder.
  • Before switching on the power on your robot, make sure it won’t drive off the table!
  • Switch on the power on the battery pack.
  • Double click the file "build_for_g2452.bat" to compile the blinking LED example and copy it onto the microcontroller, where it should start running immediately, causing the LED to blink on and off!
Don’t Panic! If the program doesn’t work straight away, don’t worry. It’s probably something quite simple. Just ask one of the workshop facilitators to help you find the problem.
//
// RoboSlam example: Blink the LED
// Written 14-11-2015
// Code is for MSP430G2452 or MSP430G2553
//
 
#include <msp430.h>
  
void main()
{
    // Stop watchdog timer to prevent timeout reset
    WDTCTL = WDTPW + WDTHOLD;
     
    // Configure pin P1.0 as an output to control the LED
    P1DIR = 0b00000001;
    
    // This while loop repeats the four statements it contains
    // over and over again to blink the LED on and off forever
    while(1)
    {
        P1OUT = 0b00000001;      // Set P1.0 high to switch on LED
        __delay_cycles(1000000); // 1000000 cycles = 1 second delay
        P1OUT = 0b00000000;      // Set P1.0 low to switch off LED
        __delay_cycles(1000000); // 1000000 cycles = 1 second delay
    }
}
Challenge: Modify the program so that the LED flashes in the following sequence over and over again:
blinking_led

  1. LED on for 0.2 seconds
  2. LED off for 0.4 seconds
  3. LED on for 0.2 seconds
  4. LED off for 2.2 seconds

Explanation of Ports 1 and 2 on the MSP430

The MSP430G2452 has 20 pins in total:

  • Two pins – pin 1 (VCC) and pin 20 (GND) – are used to supply the MSP430 with power.
  • Two pins – pin 16 (RESET) and pin 17 (TEST) – are used for programming.
  • The remaining 16 pins are divided into “ports”, each with 8 pins.
  • The 8 pins in Port 1 are P1.0, P1.1, P1.2, P1.3, P1.4, P1.5, P1.6, P1.7
  • The 8 pins in Port 2 are P2.0, P2.1, P2.2, P2.3, P2.4, P2.5, P2.6, P2.7

The following diagram shows which pins on the MSP430 are in each port.

msp430_ports

Step 2.4: Zigzag motor control example

  • Create another new folder called "C:\RoboSlam\zigzag\".
  • Copy and paste the code below into a new file in Notepad++ and save it as filename "main.c" in the "zigzag" folder.
  • Copy and paste the file "build_for_g2452.bat" from the "RoboSlam" folder into the "zigzag" folder.
  • Before switching on the power on your robot, make sure it won’t drive off the table!
  • Switch on the power on the battery pack.
  • Double click the file "build_for_g2452.bat" to compile the zigzag example program and copy it onto the microcontroller where it should immediately start running, causing the motors to move.
  • Disconnect the robot from the LaunchPad and it should drive along the floor in a zigzag pattern.
//
// RoboSlam example: ZigZag Motor Control
// Written 14-11-2015
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

// These functions make it simpler to control the LED
// and the motors and to read from the colour sensor
void led(int light);
void motors(int left, int right);
int colour();

void main()
{
    // stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
     
    // P2.0 and P2.1 control right motor
    // P2.4 and P2.5 control left motor
    // P1.0 controls the green LED
    P2DIR = 0b00110011;
    P1DIR = 0b00000001;
     
    while(1)
    {
        led(0);                  // Turn LED off
        motors(1,1);             // Go forward
        __delay_cycles(1000000); // 1 second delay
        
        led(1);                  // Turn LED on
        motors(-1,1);            // Spin left
        __delay_cycles(500000);  // 0.5 second delay
        
        led(0);                  // Turn LED off
        motors(1,1);             // Go forward
        __delay_cycles(1000000); // 1 second delay
        
        led(1);                  // Turn LED on
        motors(1,-1);            // Spin right
        __delay_cycles(500000);  // 0.5 second delay
    }
}

// This function controls the motors.
// For each motor, 1=forward, 0=stop and -1=reverse.
//
// motors( 1, 1) goes forward.
// motors(-1, 1) spins left.
// motors( 1,-1) spins right.
// motors(-1,-1) reverses.
//
void motors(int left, int right)
{
    int p2out_value = 0;
    
    if (left  > 0) p2out_value += BIT4;
    if (left  < 0) p2out_value += BIT5;
    if (right > 0) p2out_value += BIT0;
    if (right < 0) p2out_value += BIT1;
    
    P2OUT = p2out_value;
}

// This function controls the LED.
//
// led(1) switches the LED on.
// led(0) switches the LED off.
//
void led(int on_off)
{
    if (on_off == 1) P1OUT |= BIT0;  // LED on
    if (on_off == 0) P1OUT &= ~BIT0; // LED off
}

// This function reads the colour sensor input.
// The output value is 1 for white and 0 for black.
int colour()
{
    if (P1IN & BIT7) return 1; // Return 1 if P1.7 is high
    else return 0;             // Return 0 if P1.7 is low
}
Challenge: Modify the zigzag program to make the robot drive in a square pattern. Place an object in the centre of your table and see how many times you can make your robot drive around it without either touching the object or falling off the table.

Step 2.5: Colour sensor navigation example

  • Create another new folder called "C:\RoboSlam\sensor\".
  • Copy and paste the code below into a new file in Notepad++ and save it as filename "main.c" in the "sensor" folder.
  • Copy and paste the file "build_for_g2452.bat" from the "RoboSlam" folder into the "sensor" folder.
  • Before switching on the power on your robot, make sure it won’t drive off the table!
  • Switch on the power on the battery pack.
  • Double click the file "build_for_g2452.bat" to compile the colour sensor example program and copy it onto the microcontroller, where it should immediately start running, causing the motors to move.
  • Disconnect the robot from the LaunchPad. It should drive forward on a black or dark coloured surface but turn on the spot when the sensor detects white or another light colour.
//
// RoboSlam example: Colour Sensor Navigation
// Written 14-11-2015
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

// These functions make it simpler to control the LED
// and the motors and to read from the colour sensor
void led(int light);
void motors(int left, int right);
int colour();
  
void main()
{
    // stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
     
    // P2.0 and P2.1 control right motor
    // P2.4 and P2.5 control left motor
    // P1.0 controls the green LED
    P2DIR = 0b00110011;    
    P1DIR = 0b00000001;
    
    // Declare a variable to store colour sensor reading
    int c;
    
    while(1)
    {
        // Read the colour sensor and store the value in c
        c = colour();

        if (c == 1)
        {
            // On white...
            led(1);       // LED on
            motors(1,-1); // Turn right
        }
        
        if (c == 0)
        {
            // On black...
            led(0);       // LED off
            motors(1,1);  // Turn left
        }
    }
}

// This function controls the motors.
// For each motor, 1=forward, 0=stop and -1=reverse.
//
// motors( 1, 1) goes forward.
// motors(-1, 1) spins left.
// motors( 1,-1) spins right.
// motors(-1,-1) reverses.
//
void motors(int left, int right)
{
    int p2out_value = 0;
    
    if (left  > 0) p2out_value += BIT4;
    if (left  < 0) p2out_value += BIT5;
    if (right > 0) p2out_value += BIT0;
    if (right < 0) p2out_value += BIT1;
    
    P2OUT = p2out_value;
}

// This function controls the LED.
//
// led(1) switches the LED on.
// led(0) switches the LED off.
//
void led(int on_off)
{
    if (on_off == 1) P1OUT |= BIT0;  // LED on
    if (on_off == 0) P1OUT &= ~BIT0; // LED off
}

// This function reads the colour sensor input.
// The output value is 1 for white and 0 for black.
int colour()
{
    if (P1IN & BIT7) return 1; // Return 1 if P1.7 is high
    else return 0;             // Return 0 if P1.7 is low
}
Challenge: Can you make your robot turn through 180 degrees every time its sensor detects white?
Advertisements