Racing Robots Session

Dependencies:   MbedJSONValue m3pi

This is the library for the Racing Robots session. It supports the M3PI robot of Polulu.

It is based on the "Arduino" principle of the init and loop function.

Just add a main.cpp file which contains:

Racing Robots main file

#include "robot_logic.h"

void init()
{
   //put your initialization logic here
}

void loop()
{
    //put your robot control logic here    
}

Features include:

  1. Controlling the LEDS
  2. Move forward and backward
  3. Turn
  4. Read the sensor values
  5. Use a PID controller

robot_logic.cpp

Committer:
dwini
Date:
2015-02-23
Revision:
0:c0ae66a0ec7a
Child:
2:356bb8d99326

File content as of revision 0:c0ae66a0ec7a:

#include "robot_logic.h"

m3pi m3pi;

#define MAX_SPEED 1.0
#define MAX_TURN_SPEED 1.0

/*
 * Drive the robot forward or backward.
 *
 * @speed The speed percentage with which to drive forward or backward.
 * Can range from -100 (full throttle backward) to +100 (full throttle forward).
 */
void drive(int speed) {
    if (speed > 100 || speed < -100) {
        printf("[ERROR] Drive speed out of range");
        return;
    }

    if (speed == 0) {
        m3pi.stop();
    } else if (speed > 0) {
        m3pi.forward(MAX_SPEED*speed/100.0);
    } else if (speed < 0) {
        m3pi.backward(MAX_SPEED*-speed/100.0);
    }
}

/*
 * Turn the robot left or right.
 *
 * @speed The speed percentage with which to turn the robot.
 * Can range from -100 (full throttle left) to +100 (full throttle right).
 */
void turn(int turnspeed) {
    if (turnspeed > 100 || turnspeed < -100) {
        printf("[ERROR] Turn speed out of range");
        return;
    }

    if (turnspeed == 0) {
        m3pi.stop();
    } else if (turnspeed > 0) {
        m3pi.right(MAX_TURN_SPEED*turnspeed/100.0);
    } else if (turnspeed < 0) {
        m3pi.left(MAX_TURN_SPEED*-turnspeed/100.0);
    }
}

/*
 * Stop the robot.
 */
void stop(void) {
    m3pi.stop();
}

/*
 * Read the value from the line sensor. The returned value indicates the
 * position of the line. The value ranges from -100 to +100 where -100 is
 * fully left, +100 is fully right and 0 means the line is detected in the middle.
 *
 * @return The position of the line with a range of -100 to +100.
 */
int line_sensor(void) {
    // Get position of line [-1.0, +1.0]
    float pos = m3pi.line_position();
    return ((int)(pos * 100));
}



// Show drive speed and sensor data
void showStats(void) {
    m3pi.cls();          // Clear display

    // Display speed
    m3pi.locate(0, 0);
        // x   The horizontal position, from 0 to 7
        // y   The vertical position, from 0 to 1
    m3pi.printf("FOR 100%%");

    // Display line
    m3pi.locate(0, 1);
    int line = line_sensor();
    m3pi.printf("LINE %d", line);
}




/*
 * Wait for an approximate number of milliseconds.
 *
 * @milliseconds The number of milliseconds to wait.
 */
void doWait(int milliseconds) {
    wait_ms(milliseconds);
}