![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Racing Robots Session
Fork of racing_robots by
Revision 8:c5dccd557aab, committed 2015-06-01
- Comitter:
- sillevl
- Date:
- Mon Jun 01 10:13:18 2015 +0000
- Parent:
- 7:a72215b1910b
- Commit message:
- renamed files to mbed standard
Changed in this revision
diff -r a72215b1910b -r c5dccd557aab Logic.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Logic.cpp Mon Jun 01 10:13:18 2015 +0000 @@ -0,0 +1,163 @@ +#include "Logic.h" + +// Some defines +#define MAX_SPEED 100 +#define MAX_SENSOR 100 +#define MAX_REAL_SPEED 0.75 + +#define MIN 0 + +// Static scope variables +static m3pi m3pi; + +// Static scope variables for keeping internal state +static int internal_speed = 0; // [-100, +100] +static int internal_turnspeed = 0; // [-100, +100] +static int internal_p = 0; +static int internal_i = 0; +static int internal_d = 0; +static int internal_previous_pos_of_line = 0; +static int internal_led_state = 0; + +void drive(int speed) +{ + if (speed > MAX_SPEED || speed < -MAX_SPEED) { + error("Speed out of range"); + return; + } + + internal_speed = speed; + + if (speed == 0) { + m3pi.stop(); + } else if (speed > 0) { + m3pi.forward(MAX_REAL_SPEED*speed/MAX_SPEED); + } else if (speed < 0) { + m3pi.backward(MAX_REAL_SPEED*(-speed)/MAX_SPEED); + } +} + +void turn(int turnspeed) +{ + if (turnspeed > MAX_SPEED || turnspeed < -MAX_SPEED) { + error("Turn out of range"); + return; + } + internal_turnspeed = turnspeed; + + float left = internal_speed; + float right = internal_speed; + + left -= turnspeed; + right += turnspeed; + + if (right < MIN) + right = MIN; + else if (right > MAX_SPEED) + right = MAX_SPEED; + + if (left < MIN) + left = MIN; + else if (left > MAX_SPEED) + left = MAX_SPEED; + m3pi.left_motor(MAX_REAL_SPEED*left/MAX_SPEED); + m3pi.right_motor(MAX_REAL_SPEED*right/MAX_SPEED); +} + +void stop(void) +{ + m3pi.stop(); +} +void sensor_calibrate(void) +{ + m3pi.sensor_auto_calibrate(); +} + +int line_sensor(void) +{ + // Get position of line [-1.0, +1.0] + float pos = m3pi.line_position(); + return ((int)(pos * MAX_SENSOR)); +} + +void pid_init(int p, int i, int d) +{ + internal_p = p; + internal_i = i; + internal_d = d; +} + +int pid_turn(int line_position) +{ + float derivative, proportional, integral = 0; + float power; + + proportional = line_position / 100.0; + + // Compute the derivative + derivative = line_position - internal_previous_pos_of_line; + + // Compute the integral + integral += proportional; + + // Remember the last position. + internal_previous_pos_of_line = line_position; + + // Compute the power + power = (proportional * (internal_p) ) + (integral*(internal_i)) + (derivative*(internal_d)) ; + + power = power * MAX_SPEED; + if (power < -MAX_SPEED) + power = -MAX_SPEED; + else if (power > MAX_SPEED) + power = MAX_SPEED; + return power ; +} + +void show_stats(void) +{ + m3pi.cls(); // Clear display + + // Display speed + m3pi.locate(0, 0); + m3pi.printf("S%d", internal_speed); + + // Display turn + m3pi.locate(4,0); + m3pi.printf("T%d", internal_turnspeed); + + // Display line + m3pi.locate(0, 1); + int line = line_sensor(); + m3pi.printf("POS %d", line); +} + +void show_name(char * name) +{ + 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("%s", name); +} + +void await(int milliseconds) +{ + wait_ms(milliseconds); +} + +void led(LedIndex i, LedState state) +{ + if(state == LED_ON) { + internal_led_state |= (1 << i); + } else if(state == LED_OFF) { + internal_led_state &= ~(1 << i); + } else if(state == LED_TOGGLE) { + internal_led_state ^= (1 << i); + } else { + error("Illegal LED state"); + } + m3pi.leds(internal_led_state); +} \ No newline at end of file
diff -r a72215b1910b -r c5dccd557aab Logic.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Logic.h Mon Jun 01 10:13:18 2015 +0000 @@ -0,0 +1,112 @@ +#ifndef H_LOGIC +#define H_LOGIC + +#include "mbed.h" +#include "m3pi.h" + +typedef enum { + LED_1 = 0, + LED_2 = 1, + LED_3 = 2, + LED_4 = 3, + LED_5 = 4, + LED_6 = 5, + LED_7 = 6, + LED_8 = 7 +} LedIndex; + +typedef enum { + LED_ON = 0, + LED_OFF = 1, + LED_TOGGLE = 2 +} LedState; + + +/** + * Drive the robot forward or backward. + * If the robot was turning it will stop turning and drive in a straight line. + * + * @param 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); + +/** + * Turn the robot left or right while driving. + * + * @param turnspeed The percentage with which to turn the robot. + * Can range from -100 (full throttle left) to +100 (full throttle right). + */ +void turn(int turnspeed); + +/** + * Stop the robot. + */ +void stop(void); + +/** + * Calibrate the line follow sensors. + * Take note that the robot should be placed over the line + * before this function is called and that it will rotate to + * both sides. + */ +void sensor_calibrate(void); + +/** + * 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); + +/** + * Initialize the PID drive control with + * the P, I and D factors. + * + * @param p The P factor + * @param i The I factor + * @param d The D factor + */ +void pid_init(int p, int i, int d); + +/** + * 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] + * + * @return The turnspeed in a range of [-100, +100] + */ +int pid_turn(int line_position); + +/** + *Show speed, turn and sensor data on the LCD + */ +void show_stats(); + +/** + * Shows the name of the robot on the display. + * + * @param name C character string (null-terminated) with the name of the robot (max 8 chars) + */ +void show_name(char * name); + + +/** + * 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); + +/** + * Wait for an approximate number of milliseconds. + * + * @param milliseconds The number of milliseconds to wait. + */ +void await(int milliseconds); + +#endif \ No newline at end of file
diff -r a72215b1910b -r c5dccd557aab RacingRobots.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RacingRobots.cpp Mon Jun 01 10:13:18 2015 +0000 @@ -0,0 +1,28 @@ +#include "Logic.h" + +// External functions called from our library +extern void init(void); +extern void loop(void); + +/* + * System initialization. + * Also calls external init() function. + */ +void _init(void) { + // DO our init here + + init(); // Students init +} + +/* + * Entry point. + * Also calls external loop function. + */ +int main (void) { + // Initialize system + _init(); + + while (true) { + loop(); // Students loop + } +} \ No newline at end of file
diff -r a72215b1910b -r c5dccd557aab racing_robots.cpp --- a/racing_robots.cpp Wed Feb 25 15:56:14 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#include "robot_logic.h" - -// External functions called from our library -extern void init(void); -extern void loop(void); - -/* - * System initialization. - * Also calls external init() function. - */ -void _init(void) { - // DO our init here - - init(); // Students init -} - -/* - * Entry point. - * Also calls external loop function. - */ -int main (void) { - // Initialize system - _init(); - - while (true) { - loop(); // Students loop - } -} \ No newline at end of file
diff -r a72215b1910b -r c5dccd557aab robot_logic.cpp --- a/robot_logic.cpp Wed Feb 25 15:56:14 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,163 +0,0 @@ -#include "robot_logic.h" - -// Some defines -#define MAX_SPEED 100 -#define MAX_SENSOR 100 -#define MAX_REAL_SPEED 0.75 - -#define MIN 0 - -// Static scope variables -static m3pi m3pi; - -// Static scope variables for keeping internal state -static int internal_speed = 0; // [-100, +100] -static int internal_turnspeed = 0; // [-100, +100] -static int internal_p = 0; -static int internal_i = 0; -static int internal_d = 0; -static int internal_previous_pos_of_line = 0; -static int internal_led_state = 0; - -void drive(int speed) -{ - if (speed > MAX_SPEED || speed < -MAX_SPEED) { - error("Speed out of range"); - return; - } - - internal_speed = speed; - - if (speed == 0) { - m3pi.stop(); - } else if (speed > 0) { - m3pi.forward(MAX_REAL_SPEED*speed/MAX_SPEED); - } else if (speed < 0) { - m3pi.backward(MAX_REAL_SPEED*(-speed)/MAX_SPEED); - } -} - -void turn(int turnspeed) -{ - if (turnspeed > MAX_SPEED || turnspeed < -MAX_SPEED) { - error("Turn out of range"); - return; - } - internal_turnspeed = turnspeed; - - float left = internal_speed; - float right = internal_speed; - - left -= turnspeed; - right += turnspeed; - - if (right < MIN) - right = MIN; - else if (right > MAX_SPEED) - right = MAX_SPEED; - - if (left < MIN) - left = MIN; - else if (left > MAX_SPEED) - left = MAX_SPEED; - m3pi.left_motor(MAX_REAL_SPEED*left/MAX_SPEED); - m3pi.right_motor(MAX_REAL_SPEED*right/MAX_SPEED); -} - -void stop(void) -{ - m3pi.stop(); -} -void sensor_calibrate(void) -{ - m3pi.sensor_auto_calibrate(); -} - -int line_sensor(void) -{ - // Get position of line [-1.0, +1.0] - float pos = m3pi.line_position(); - return ((int)(pos * MAX_SENSOR)); -} - -void pid_init(int p, int i, int d) -{ - internal_p = p; - internal_i = i; - internal_d = d; -} - -int pid_turn(int line_position) -{ - float derivative, proportional, integral = 0; - float power; - - proportional = line_position / 100.0; - - // Compute the derivative - derivative = line_position - internal_previous_pos_of_line; - - // Compute the integral - integral += proportional; - - // Remember the last position. - internal_previous_pos_of_line = line_position; - - // Compute the power - power = (proportional * (internal_p) ) + (integral*(internal_i)) + (derivative*(internal_d)) ; - - power = power * MAX_SPEED; - if (power < -MAX_SPEED) - power = -MAX_SPEED; - else if (power > MAX_SPEED) - power = MAX_SPEED; - return power ; -} - -void show_stats(void) -{ - m3pi.cls(); // Clear display - - // Display speed - m3pi.locate(0, 0); - m3pi.printf("S%d", internal_speed); - - // Display turn - m3pi.locate(4,0); - m3pi.printf("T%d", internal_turnspeed); - - // Display line - m3pi.locate(0, 1); - int line = line_sensor(); - m3pi.printf("POS %d", line); -} - -void show_name(char * name) -{ - 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("%s", name); -} - -void await(int milliseconds) -{ - wait_ms(milliseconds); -} - -void led(LedIndex i, LedState state) -{ - if(state == LED_ON) { - internal_led_state |= (1 << i); - } else if(state == LED_OFF) { - internal_led_state &= ~(1 << i); - } else if(state == LED_TOGGLE) { - internal_led_state ^= (1 << i); - } else { - error("Illegal LED state"); - } - m3pi.leds(internal_led_state); -} \ No newline at end of file
diff -r a72215b1910b -r c5dccd557aab robot_logic.h --- a/robot_logic.h Wed Feb 25 15:56:14 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,112 +0,0 @@ -#ifndef H_ROBOT_LOGIC -#define H_ROBOT_LOGIC - -#include "mbed.h" -#include "m3pi.h" - -typedef enum { - LED_1 = 0, - LED_2 = 1, - LED_3 = 2, - LED_4 = 3, - LED_5 = 4, - LED_6 = 5, - LED_7 = 6, - LED_8 = 7 -} LedIndex; - -typedef enum { - LED_ON = 0, - LED_OFF = 1, - LED_TOGGLE = 2 -} LedState; - - -/** - * Drive the robot forward or backward. - * If the robot was turning it will stop turning and drive in a straight line. - * - * @param 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); - -/** - * Turn the robot left or right while driving. - * - * @param turnspeed The percentage with which to turn the robot. - * Can range from -100 (full throttle left) to +100 (full throttle right). - */ -void turn(int turnspeed); - -/** - * Stop the robot. - */ -void stop(void); - -/** - * Calibrate the line follow sensors. - * Take note that the robot should be placed over the line - * before this function is called and that it will rotate to - * both sides. - */ -void sensor_calibrate(void); - -/** - * 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); - -/** - * Initialize the PID drive control with - * the P, I and D factors. - * - * @param p The P factor - * @param i The I factor - * @param d The D factor - */ -void pid_init(int p, int i, int d); - -/** - * 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] - * - * @return The turnspeed in a range of [-100, +100] - */ -int pid_turn(int line_position); - -/** - *Show speed, turn and sensor data on the LCD - */ -void show_stats(); - -/** - * Shows the name of the robot on the display. - * - * @param name C character string (null-terminated) with the name of the robot (max 8 chars) - */ -void show_name(char * name); - - -/** - * 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); - -/** - * Wait for an approximate number of milliseconds. - * - * @param milliseconds The number of milliseconds to wait. - */ -void await(int milliseconds); - -#endif \ No newline at end of file