Hi,
Im using a m3pi robot with sharp IR sensors to implement an obstacle avoiding algorithm. Ideally i want to use a timer to see how long the robot will follow a particular wall for. As a result i am trying to use the timer function to help me do this. However, i cant seem to embed the timer section of the code properly within the obstacle avoiding program.
Any help would be appreciated.
The basic bit of code i am using so far is as follows :
#include "mbed.h"
#include "m3pi.h" // libraries
m3pi m3pi;
AnalogIn ir1(p20); // Front sensor
DigitalOut led(LED1);
AnalogIn ir2(p19); // Left sensor
DigitalOut led2(LED2);
AnalogIn ir3(p15); // right sensor
DigitalOut led3(LED3);
float x;
float bat;
Timer t;
LocalFileSystem local("local");
char buffer[4];
void battery() {
bat=m3pi.battery();
bat=bat*1000;
sprintf (buffer, "%f", bat);
m3pi.cls();
m3pi.printf(buffer);
wait(2);
}
void forward_movement(){
m3pi.cls();
m3pi.locate(0,1);
m3pi.printf("Moving");
m3pi.forward(0.2);
void turn_left(){
m3pi.cls();
m3pi.printf("obstacle");
m3pi.left(0.25);
wait(0.3);
}
int main(){
battery();
while(1){
forward:
x=ir1.read(); // front
y=ir2.read(); // left
z=ir3.read(); // right
// t.reset();
// t.start();
forward_movement();
if (x > 0.7000) {
// t.stop();
// FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
// fprintf(fp, "RHS%f",t.read());
// fclose(fp);
turn_left();
goto forward;
}
else{
led=0;
}
}
m3pi.cls();
m3pi.locate(0,0);
// m3pi.stop();
m3pi.printf("Finished");
}
The program works fine without the use of timer, but when i include the timer sections the robot does not execute the if statement. does that mean the timer cant run simultaneously?
Regards
Dan