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
Revision:
7:a72215b1910b
Parent:
6:0dc4e4225881
Child:
8:597ce8a7d34b
--- a/robot_logic.cpp	Wed Feb 25 09:10:13 2015 +0000
+++ b/robot_logic.cpp	Wed Feb 25 15:56:14 2015 +0000
@@ -3,8 +3,8 @@
 // Some defines
 #define MAX_SPEED 100
 #define MAX_SENSOR 100
-#define MAX_REAL_SPEED 0.3
-#define MAX 0.3
+#define MAX_REAL_SPEED 0.75
+
 #define MIN 0
 
 // Static scope variables
@@ -22,7 +22,7 @@
 void drive(int speed)
 {
     if (speed > MAX_SPEED || speed < -MAX_SPEED) {
-        printf("[ERROR] Drive speed out of range");
+        error("Speed out of range");
         return;
     }
 
@@ -40,7 +40,7 @@
 void turn(int turnspeed)
 {
     if (turnspeed > MAX_SPEED || turnspeed < -MAX_SPEED) {
-        error("Speed out of range");
+        error("Turn out of range");
         return;
     }
     internal_turnspeed = turnspeed;
@@ -89,14 +89,10 @@
 
 int pid_turn(int line_position)
 {
-    float right;
-    float left;
-
     float derivative, proportional, integral = 0;
     float power;
-    float speed = internal_speed;
 
-    proportional = line_position;
+    proportional = line_position / 100.0;
 
     // Compute the derivative
     derivative = line_position - internal_previous_pos_of_line;
@@ -110,12 +106,14 @@
     // Compute the power
     power = (proportional * (internal_p) ) + (integral*(internal_i)) + (derivative*(internal_d)) ;
 
-    return power;
+    power = power * MAX_SPEED;
+    if (power < -MAX_SPEED)
+        power = -MAX_SPEED;
+    else if (power > MAX_SPEED)
+        power = MAX_SPEED;
+    return power ;
 }
 
-/**
-  *Show speed, turn and sensor data
-  */
 void show_stats(void)
 {
     m3pi.cls();          // Clear display