Sille Van Landschoot
/
RacingRobotsLib
Racing Robots Session
Fork of racing_robots by
Diff: robot_logic.cpp
- Revision:
- 0:c0ae66a0ec7a
- Child:
- 2:356bb8d99326
--- /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