svoe

Dependencies:   mbed mbed-STM32F103C8T6 MPU6050_1

echo.h

Committer:
Stas285
Date:
2017-05-14
Revision:
4:904b737ef08a
Parent:
1:e2a6e523bf1f
Child:
17:bd6b6ac89e0e

File content as of revision 4:904b737ef08a:

int obstacle[100];//13 steps [-90;+90] by 15 deg
int echo_current_step;//[0;36], (step=18) => 0 deg
int echo_old_step;
int echo_old_old_step;
int echo_current_dir = -1;
int echo_cm;
float tmf;
int min_dist;
int min_dist_angle;
int max_dist;
int max_dist_angle;
int front_dist;

Timer echo_timer;
InterruptIn echo(PB_12);
DigitalOut sonar_triger(PB_13);

void echo_begin(){ //begin pulsewidth measurement
    echo_timer.reset();
    }
    
void echo_end(){ //end pulsewidth measurement
    echo_cm = echo_timer.read_us()/58;
    }

void echo_start(){
    sonar_triger = 1;
    tmf=log(1.0);//delay 5us
    tmf=log(2.0);//delay 5us
    sonar_triger = 0;//sonar start
    }
    
void echo_init(){
    echo.rise(&echo_begin);
    echo.fall(&echo_end);
    echo_timer.start();
    }
    
void   echo_transmit(int steps_number)  { //printf is not recommended for interrupts
    int tmstep;
    wifi.putc(0xff); //sync symbol
    for(tmstep=0;tmstep<steps_number;tmstep++){wifi.putc((obstacle[tmstep]<500) ? obstacle[tmstep]/2 : 250);}
    }

void echo_scan(){ // rewrite with argument: n or step //will not work in interrupt ??
   serva(-90);
   wait_ms(300);
   for(int point = 0;point <= 12;point ++){
            serva(point*15-90);
            wait_ms(50);
            echo_start();
            wait_ms(50);
            obstacle[point]=echo_cm;}      
   }
   
void echo_step(bool transmit = 0){
    obstacle[echo_old_old_step]=echo_cm;
    //echo_transmit(echo_current_step);
    echo_old_old_step = echo_old_step;
    echo_old_step = echo_current_step;
    echo_current_step += echo_current_dir;
    if (echo_current_step > 36) {echo_current_step = 35 ; echo_current_dir = -1; /*echo_transmit(37);*/} 
    if (echo_current_step < 0) {echo_current_step = 1 ; echo_current_dir = 1; /*echo_transmit(37);*/} 
    serva(echo_current_step*5-90);
    echo_start(); //the last to exit ticker interrupt ASAP
    }

bool analyze_obstacle(){
    min_dist = 400;max_dist = 0; front_dist = 400;
    for(int point = 0;point <= 12;point ++){
        if(obstacle[point]<min_dist) {min_dist=obstacle[point];min_dist_angle = point*15-90;}
        if(obstacle[point]>max_dist) {max_dist=obstacle[point];max_dist_angle = point*15-90;}
        if((obstacle[point]*abs(point-6)*3<15)&&(obstacle[point]<front_dist)) {front_dist=obstacle[point];}
        }
    if(front_dist < 20) return(1); else return(0);
    }
    
void echo_full_scan(int steps){ //scan by wheels (calculate speed)
    
    }