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:
pcordemans
Date:
Wed Feb 25 15:56:14 2015 +0000
Parent:
6:0dc4e4225881
Child:
8:597ce8a7d34b
Commit message:
PID works, updated the comments

Changed in this revision

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
--- 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
--- a/robot_logic.h	Wed Feb 25 09:10:13 2015 +0000
+++ b/robot_logic.h	Wed Feb 25 15:56:14 2015 +0000
@@ -46,7 +46,7 @@
 
 /**
  * Calibrate the line follow sensors.
- * Take note that the pololu should be placed over the line
+ * Take note that the robot should be placed over the line
  * before this function is called and that it will rotate to
  * both sides.
  */
@@ -72,7 +72,7 @@
 void pid_init(int p, int i, int d);
 
 /**
- * Determine PID turnspeed with which the pololu should
+ * Determine PID turnspeed with which the robot should
  * turn to follow the line at the given position.
  *
  * @param line_position The position of the line in a range of [-100, +100]
@@ -98,6 +98,7 @@
   * Turn on, off or toggle a specific LED
   * @param i the LED number LED_0 .. LED_7
   * @param state the LED state LED_ON, LED_OFF, LED_TOGGLE 
+  * @example led(LED_0, LED_ON); turns LED 0 on.
   */
 void led(LedIndex i, LedState state);