RYU HoSeong

Dependencies:   HC_SR04_Ultrasonic_Library mbed-rtos mbed-src mbed HCSR04

Fork of Nucleo_UltrasonicHelloWorld by EJ Teb

main.cpp

Committer:
ryuhs74
Date:
2015-08-06
Revision:
2:3dcb6f86ed22
Parent:
1:4a5586eb1765
Child:
4:eb5a51640b43

File content as of revision 2:3dcb6f86ed22:

#include "mbed.h"
#include "ultrasonic.h"


//#define USE_THREAD
#ifdef USE_THREAD
#include "rtos.h"
#endif

int sum_dist[3] = {0,}; //ryuhs74@20150712 - 
int avg_dist = 0; //ryuhs74@20150712 - 
int index_dist = 0; //ryuhs74@20150712 - 

Serial pc(USBTX, USBRX); // tx, rx

#ifdef USE_THREAD
int isRun_thread = 0;
Thread *pThread = NULL;

void camera_thread( void const* args)
{
    isRun_thread = 1;
    
    while(isRun_thread){
        //ryuhs74@20150713 START - TEST code 
        for(int i = 0; i<100;i++){
            wait(1);
        }        
        break;
        //ryuhs74@20150713 - TEST code END       
    }
    
    if( pThread != NULL ){
        pThread->terminate();
        pThread = NULL;
    }
    isRun_thread = 0;
    return;
}
#endif

void dist(int distance)
{
    //put code here to happen when the distance is changed
    pc.printf("Distance changed to %d mm\r\n", distance);
}

#define TEST_ULTRASONIC
#ifdef TEST_ULTRASONIC
ultrasonic mu(D8, D9, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
//have updates every .1 seconds and a timeout after 1
//second, and call dist when the distance changes
#endif

int main()
{
    pc.printf("Start ----> \n");
    
#ifdef TEST_ULTRASONIC
    mu.startUpdates();//start mesuring the distance
#endif

    while(1) {
#ifdef TEST_ULTRASONIC
        pc.printf("Before mu.checkDistance();\n");
        //Do something else here
        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
        //the class checks if dist needs to be called.
        pc.printf("After mu.checkDistance();\n");
               
       //ryuhs74@20150712 START - 
        if( index_dist < 3){
            pc.printf("Before mu.getCurrentDistance();\n");
            sum_dist[index_dist] = mu.getCurrentDistance();
            pc.printf("sum_dist[index_dist(%d)] = %d\n", index_dist, sum_dist[index_dist]);
            index_dist ++;
        } else {
            pc.printf("Before index_dist( %d ) ", index_dist);
            index_dist = 0;
            pc.printf("After index_dist( %d )\n", index_dist);
            
            for(int i =0; i<3;i++){
                avg_dist += sum_dist[i];
            }
            
            //avg_dist /= avg_dist;
            
            if( avg_dist <= 1 ){
                #ifdef USE_THREAD
                if( isRun_thread == 0 ){
                    Thread thread(camera_thread);
                    
                    if( pThread == NULL)
                        pThread = (Thread*)&thread;
                    
                }
                #endif
                
            }
        }
#endif
        pc.printf("wait(1)\n");
        //ryuhs74@20150712 - END        
        wait(1); //ryuhs7474@20150713 - 초당 한번씩 딜레이를 준다          
    }
}