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

Files at this revision

API Documentation at this revision

Comitter:
dwini
Date:
Mon Feb 23 12:24:31 2015 +0000
Child:
1:43c91152e9ce
Commit message:
Define interface of pololu for racing robots.

Changed in this revision

racing_robots.cpp Show annotated file Show diff for this revision Revisions of this file
racing_robots.h Show annotated file Show diff for this revision Revisions of this file
robot_logic.cpp Show annotated file Show diff for this revision Revisions of this file
robot_logic.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/racing_robots.cpp	Mon Feb 23 12:24:31 2015 +0000
@@ -0,0 +1,27 @@
+#include "racing_robots.h"
+
+DigitalOut myled(LED1);
+
+extern void loop(void);
+extern void init(void);
+
+
+void _init(void) {
+    // DO our init here
+
+    init(); // Students init
+}
+
+void racing_robots(void) {
+    // Initialize system
+    _init();
+
+    while (true) {
+        loop();     // Students loop
+    }
+}
+
+
+int main (void) {
+    racing_robots();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/racing_robots.h	Mon Feb 23 12:24:31 2015 +0000
@@ -0,0 +1,10 @@
+#include "mbed.h"
+#include "m3pi.h"
+#include "robot_logic.h"
+
+#ifndef H_RACING_ROBOTS
+#define H_RACING_ROBOTS
+
+void racing_robots(void);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/robot_logic.cpp	Mon Feb 23 12:24:31 2015 +0000
@@ -0,0 +1,98 @@
+#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);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/robot_logic.h	Mon Feb 23 12:24:31 2015 +0000
@@ -0,0 +1,59 @@
+#include "mbed.h"
+#include "m3pi.h"
+
+#ifndef H_ROBOT_LOGIC
+#define H_ROBOT_LOGIC
+
+//typedef enum {
+//    LED_1 = 0,
+//    LED_2 = 1,
+//    LED_3 = 2,
+//    LED_4 = 3
+//    //.....
+//} LedIndex;
+
+typedef enum {
+    LED_ON = 0,
+    LED_OFF = 1,
+    LED_TOGGLE = 2
+} LedState;
+
+
+/*
+ * Drive the robot forward or backward.
+ *
+ * @speed The speed percentage with which to drive forward or backward [-100, +100].
+ */
+void drive(int speed);
+
+// speed between -100% en +100%
+void turn(int turnspeed);
+
+// Stop the robot
+void stop();
+
+void sensor_calibrate();
+
+// Read the value from the line sensor
+int line_sensor();
+
+// PID drive control
+void pid_init(int p, int i, int d);
+
+
+// returns turnspeed
+int pid_turn(int line_position);
+
+// Show drive speed and sensor data
+void showStats();
+
+// Show text on display
+void display(char* fmt, ...);
+
+// Turn a led on or off
+//void led(LedIndex i, LedState state);
+
+// Wait for a number of milliseconds
+void await(int milliseconds);
+
+#endif
\ No newline at end of file