Code:
#include "open_interface.h"
#include "util.h"
#include <stdlib.h> /* required for randomize() and random() */
#include "lcd.h"
#include <stdio.h>
int goForward();
void correctDirection(char previousTurn);
void turn(char direction);
void printCrap(int which);
int forwardDistance = 0;
oi_t *sensor_status; //Declare a pointer to the data type oi_t defined in open_interface.h
int
main (void)
{
sensor_status = oi_alloc(); //Allocate memory appropriate for a oi_t type data structure and assign the returned address of the structure to the variable sensor_status
oi_init(sensor_status); // Initialize the Open Interface
oi_clear_distance(sensor_status);
while (forwardDistance <= (2 * 100) ) {
int bumperStatus = goForward();
switch (bumperStatus)
{
case 9:
printCrap(2);
break;
case 1:
printCrap(2);
turn('L');
correctDirection('L');
break;
case 2:
printCrap(2);
turn('R');
correctDirection('R');
break;
case 3:
printCrap(2);
turn('R');
correctDirection('R');
break;
}
}
return 0;
}
//Go straight until collision detected, then back up and return bumper states
int goForward() {
//keep going straight until conllision (or stop if the entire 2m is clear)
int currentBumperStatus = oi_bump_status(sensor_status);
while ((currentBumperStatus == 0) && ((oi_current_distance(sensor_status) + forwardDistance) <= (2 * 100))) {
oi_set_wheels(250,250);
currentBumperStatus = oi_bump_status(sensor_status);
}
oi_set_wheels(0,0); //STOP
forwardDistance += oi_current_distance(sensor_status); // copy over distance as it's about to be cleared
oi_clear_distance(sensor_status);
//Are 2m up? If so exit function & we're done!
if ((oi_current_distance(sensor_status) + forwardDistance) >= (2 * 100)) {
return 9;
}
//Now back that ass up for 15 cm
while (oi_current_distance(sensor_status) >= -15){
oi_set_wheels(-250,-250);
}
oi_set_wheels(0,0);
oi_clear_distance(sensor_status);
//Return which bumpber was hit to take appropriate action
return currentBumperStatus;
}
//Move 25cm forward & resume opposite direcion relative to original direction
void correctDirection(char previousTurn) {
oi_clear_distance(sensor_status);
while (oi_current_distance(sensor_status) <= 25) {
oi_set_wheels(250,250);
}
oi_set_wheels(0,0);
oi_clear_distance(sensor_status);
if (previousTurn == 'L'){
turn('R');
}
if (previousTurn == 'R') {
turn('L');
}
}
void turn(char direction) {
oi_clear_angle(sensor_status);
int angle = oi_current_angle(sensor_status);
//Turn left
if (direction == 'L') {
while ((angle >=0 && angle <= 90) || (angle >=270 && angle <= 360)) {
oi_set_wheels(250,0);
wait_ms(20);
angle = oi_current_angle(sensor_status);
}
}
//Turn right
if (direction == 'R') {
while ((angle >=0 && angle <= 90) || (angle >=270 && angle <= 360)) {
oi_set_wheels(0,250);
wait_ms(20);
angle = oi_current_angle(sensor_status);
}
}
oi_set_wheels(0,0);
oi_clear_distance(sensor_status);
}
//Print the distance, angle, or bumper status on the LCD for debugging purposes
void printCrap(int which) {
lcd_init();
if (which == 0 ) {
lcd_home_line1();
char str[10];
int distanceTraveled = oi_current_distance(sensor_status); //distance returned is in cm
sprintf(str, "Distance: %dcm", distanceTraveled);
lcd_puts(str);
}
if (which == 1 ) {
lcd_home_line1();
char str[10];
int distanceTraveled = oi_current_angle(sensor_status); //distance returned is in cm
sprintf(str, "Angle: %d deg", distanceTraveled);
lcd_puts(str);
}
if (which == 2 ) {
lcd_home_line1();
int bumperStatus = oi_bump_status(sensor_status); //distance returned is in cm;
char str[22];
sprintf(str,"%s %d", "Bumper status:", bumperStatus);
sprintf(str,"%s %s", str, ".");
lcd_puts(str);
}
}