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
Committer:
sillevl
Date:
Mon Jun 01 14:53:39 2015 +0000
Revision:
9:0385d1bfc38b
Parent:
8:597ce8a7d34b
added Xbee support for start/stop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pcordemans 4:3743cbfe031b 1 #ifndef H_ROBOT_LOGIC
pcordemans 4:3743cbfe031b 2 #define H_ROBOT_LOGIC
pcordemans 4:3743cbfe031b 3
dwini 0:c0ae66a0ec7a 4 #include "mbed.h"
dwini 0:c0ae66a0ec7a 5 #include "m3pi.h"
dwini 0:c0ae66a0ec7a 6
dwini 2:356bb8d99326 7 typedef enum {
dwini 2:356bb8d99326 8 LED_1 = 0,
dwini 2:356bb8d99326 9 LED_2 = 1,
dwini 2:356bb8d99326 10 LED_3 = 2,
dwini 2:356bb8d99326 11 LED_4 = 3,
dwini 2:356bb8d99326 12 LED_5 = 4,
dwini 2:356bb8d99326 13 LED_6 = 5,
dwini 2:356bb8d99326 14 LED_7 = 6,
dwini 2:356bb8d99326 15 LED_8 = 7
dwini 2:356bb8d99326 16 } LedIndex;
dwini 0:c0ae66a0ec7a 17
dwini 0:c0ae66a0ec7a 18 typedef enum {
dwini 0:c0ae66a0ec7a 19 LED_ON = 0,
dwini 0:c0ae66a0ec7a 20 LED_OFF = 1,
dwini 0:c0ae66a0ec7a 21 LED_TOGGLE = 2
dwini 0:c0ae66a0ec7a 22 } LedState;
dwini 0:c0ae66a0ec7a 23
dwini 0:c0ae66a0ec7a 24
pcordemans 6:0dc4e4225881 25 /**
dwini 0:c0ae66a0ec7a 26 * Drive the robot forward or backward.
dwini 2:356bb8d99326 27 * If the robot was turning it will stop turning and drive in a straight line.
dwini 0:c0ae66a0ec7a 28 *
pcordemans 6:0dc4e4225881 29 * @param speed The speed percentage with which to drive forward or backward.
dwini 2:356bb8d99326 30 * Can range from -100 (full throttle backward) to +100 (full throttle forward).
dwini 0:c0ae66a0ec7a 31 */
dwini 0:c0ae66a0ec7a 32 void drive(int speed);
dwini 0:c0ae66a0ec7a 33
pcordemans 6:0dc4e4225881 34 /**
dwini 2:356bb8d99326 35 * Turn the robot left or right while driving.
dwini 2:356bb8d99326 36 *
pcordemans 6:0dc4e4225881 37 * @param turnspeed The percentage with which to turn the robot.
pcordemans 6:0dc4e4225881 38 * Can range from -100 (full throttle left) to +100 (full throttle right).
dwini 2:356bb8d99326 39 */
dwini 0:c0ae66a0ec7a 40 void turn(int turnspeed);
dwini 0:c0ae66a0ec7a 41
pcordemans 6:0dc4e4225881 42 /**
dwini 2:356bb8d99326 43 * Stop the robot.
dwini 2:356bb8d99326 44 */
dwini 2:356bb8d99326 45 void stop(void);
dwini 0:c0ae66a0ec7a 46
pcordemans 6:0dc4e4225881 47 /**
dwini 2:356bb8d99326 48 * Calibrate the line follow sensors.
pcordemans 7:a72215b1910b 49 * Take note that the robot should be placed over the line
dwini 2:356bb8d99326 50 * before this function is called and that it will rotate to
dwini 2:356bb8d99326 51 * both sides.
dwini 2:356bb8d99326 52 */
dwini 2:356bb8d99326 53 void sensor_calibrate(void);
dwini 0:c0ae66a0ec7a 54
pcordemans 6:0dc4e4225881 55 /**
dwini 2:356bb8d99326 56 * Read the value from the line sensor. The returned value indicates the
dwini 2:356bb8d99326 57 * position of the line. The value ranges from -100 to +100 where -100 is
dwini 2:356bb8d99326 58 * fully left, +100 is fully right and 0 means the line is detected in the middle.
dwini 2:356bb8d99326 59 *
dwini 2:356bb8d99326 60 * @return The position of the line with a range of -100 to +100.
dwini 2:356bb8d99326 61 */
dwini 2:356bb8d99326 62 int line_sensor(void);
dwini 0:c0ae66a0ec7a 63
pcordemans 6:0dc4e4225881 64 /**
dwini 2:356bb8d99326 65 * Initialize the PID drive control with
pcordemans 6:0dc4e4225881 66 * the P, I and D factors.
dwini 2:356bb8d99326 67 *
pcordemans 6:0dc4e4225881 68 * @param p The P factor
pcordemans 6:0dc4e4225881 69 * @param i The I factor
pcordemans 6:0dc4e4225881 70 * @param d The D factor
dwini 2:356bb8d99326 71 */
dwini 0:c0ae66a0ec7a 72 void pid_init(int p, int i, int d);
dwini 0:c0ae66a0ec7a 73
pcordemans 6:0dc4e4225881 74 /**
pcordemans 7:a72215b1910b 75 * Determine PID turnspeed with which the robot should
dwini 2:356bb8d99326 76 * turn to follow the line at the given position.
dwini 2:356bb8d99326 77 *
pcordemans 6:0dc4e4225881 78 * @param line_position The position of the line in a range of [-100, +100]
dwini 2:356bb8d99326 79 *
pcordemans 6:0dc4e4225881 80 * @return The turnspeed in a range of [-100, +100]
dwini 2:356bb8d99326 81 */
dwini 0:c0ae66a0ec7a 82 int pid_turn(int line_position);
dwini 0:c0ae66a0ec7a 83
pcordemans 6:0dc4e4225881 84 /**
pcordemans 6:0dc4e4225881 85 *Show speed, turn and sensor data on the LCD
pcordemans 6:0dc4e4225881 86 */
dwini 2:356bb8d99326 87 void show_stats();
dwini 0:c0ae66a0ec7a 88
pcordemans 6:0dc4e4225881 89 /**
pcordemans 6:0dc4e4225881 90 * Shows the name of the robot on the display.
pcordemans 6:0dc4e4225881 91 *
pcordemans 6:0dc4e4225881 92 * @param name C character string (null-terminated) with the name of the robot (max 8 chars)
pcordemans 6:0dc4e4225881 93 */
dwini 2:356bb8d99326 94 void show_name(char * name);
dwini 2:356bb8d99326 95
dwini 0:c0ae66a0ec7a 96
pcordemans 6:0dc4e4225881 97 /**
pcordemans 6:0dc4e4225881 98 * Turn on, off or toggle a specific LED
pcordemans 6:0dc4e4225881 99 * @param i the LED number LED_0 .. LED_7
pcordemans 6:0dc4e4225881 100 * @param state the LED state LED_ON, LED_OFF, LED_TOGGLE
pcordemans 7:a72215b1910b 101 * @example led(LED_0, LED_ON); turns LED 0 on.
pcordemans 6:0dc4e4225881 102 */
dwini 2:356bb8d99326 103 void led(LedIndex i, LedState state);
dwini 0:c0ae66a0ec7a 104
pcordemans 6:0dc4e4225881 105 /**
pcordemans 6:0dc4e4225881 106 * Wait for an approximate number of milliseconds.
pcordemans 6:0dc4e4225881 107 *
pcordemans 6:0dc4e4225881 108 * @param milliseconds The number of milliseconds to wait.
pcordemans 6:0dc4e4225881 109 */
dwini 0:c0ae66a0ec7a 110 void await(int milliseconds);
dwini 0:c0ae66a0ec7a 111
sillevl 8:597ce8a7d34b 112 /**
sillevl 9:0385d1bfc38b 113 * If using Xbee to start/stop the robot, you can set a unique code to prevent starting or stopping other robots.
sillevl 9:0385d1bfc38b 114 * To start or stop the robot, send the a json string only containing a "start" or "stop" key with the code as a value.
sillevl 9:0385d1bfc38b 115 * eg: {"start": 1234} will start the robot with code set to 1234, {"stop": 9876} will stop the robot with code 9876.
sillevl 8:597ce8a7d34b 116 *
sillevl 8:597ce8a7d34b 117 * @param code A number between 0 and 1000.
sillevl 8:597ce8a7d34b 118 */
sillevl 8:597ce8a7d34b 119 void setCode(int code);
sillevl 8:597ce8a7d34b 120
sillevl 8:597ce8a7d34b 121 /**
sillevl 8:597ce8a7d34b 122 * Use this method if you need to know if something (eg: Xbee) is preventing the robot from driving
sillevl 8:597ce8a7d34b 123 *
sillevl 8:597ce8a7d34b 124 * @return Boolean true if the robot can drive, false if the robot cannot drive
sillevl 8:597ce8a7d34b 125 */
sillevl 8:597ce8a7d34b 126 int canDrive();
sillevl 8:597ce8a7d34b 127
dwini 0:c0ae66a0ec7a 128 #endif