RYU HoSeong
Dependencies: HC_SR04_Ultrasonic_Library mbed-rtos mbed-src mbed HCSR04
Fork of Nucleo_UltrasonicHelloWorld by
main.cpp@6:7ceab53d9a2f, 2015-08-10 (annotated)
- Committer:
- ryuhs74
- Date:
- Mon Aug 10 01:02:41 2015 +0000
- Revision:
- 6:7ceab53d9a2f
- Parent:
- 5:e1849ee9651d
create;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ejteb | 0:1704ea055c4f | 1 | #include "mbed.h" |
ryuhs74 | 4:eb5a51640b43 | 2 | |
ryuhs74 | 4:eb5a51640b43 | 3 | //#define TEST_ULTRASONIC |
ryuhs74 | 4:eb5a51640b43 | 4 | #ifdef TEST_ULTRASONIC |
ejteb | 0:1704ea055c4f | 5 | #include "ultrasonic.h" |
ryuhs74 | 4:eb5a51640b43 | 6 | #else |
ryuhs74 | 4:eb5a51640b43 | 7 | #include "hcsr04.h" |
ryuhs74 | 4:eb5a51640b43 | 8 | #endif |
ejteb | 0:1704ea055c4f | 9 | |
ryuhs74 | 2:3dcb6f86ed22 | 10 | |
ryuhs74 | 2:3dcb6f86ed22 | 11 | //#define USE_THREAD |
ryuhs74 | 2:3dcb6f86ed22 | 12 | #ifdef USE_THREAD |
ryuhs74 | 2:3dcb6f86ed22 | 13 | #include "rtos.h" |
ryuhs74 | 2:3dcb6f86ed22 | 14 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 15 | |
ryuhs74 | 6:7ceab53d9a2f | 16 | #define CHECK_DISTANCE 1000 |
ryuhs74 | 6:7ceab53d9a2f | 17 | |
ryuhs74 | 2:3dcb6f86ed22 | 18 | int sum_dist[3] = {0,}; //ryuhs74@20150712 - |
ryuhs74 | 2:3dcb6f86ed22 | 19 | int avg_dist = 0; //ryuhs74@20150712 - |
ryuhs74 | 2:3dcb6f86ed22 | 20 | int index_dist = 0; //ryuhs74@20150712 - |
ryuhs74 | 2:3dcb6f86ed22 | 21 | |
ryuhs74 | 2:3dcb6f86ed22 | 22 | Serial pc(USBTX, USBRX); // tx, rx |
ryuhs74 | 2:3dcb6f86ed22 | 23 | |
ryuhs74 | 2:3dcb6f86ed22 | 24 | #ifdef USE_THREAD |
ryuhs74 | 2:3dcb6f86ed22 | 25 | int isRun_thread = 0; |
ryuhs74 | 2:3dcb6f86ed22 | 26 | Thread *pThread = NULL; |
ryuhs74 | 2:3dcb6f86ed22 | 27 | |
ryuhs74 | 2:3dcb6f86ed22 | 28 | void camera_thread( void const* args) |
ryuhs74 | 2:3dcb6f86ed22 | 29 | { |
ryuhs74 | 2:3dcb6f86ed22 | 30 | isRun_thread = 1; |
ryuhs74 | 2:3dcb6f86ed22 | 31 | |
ryuhs74 | 2:3dcb6f86ed22 | 32 | while(isRun_thread){ |
ryuhs74 | 2:3dcb6f86ed22 | 33 | //ryuhs74@20150713 START - TEST code |
ryuhs74 | 2:3dcb6f86ed22 | 34 | for(int i = 0; i<100;i++){ |
ryuhs74 | 2:3dcb6f86ed22 | 35 | wait(1); |
ryuhs74 | 2:3dcb6f86ed22 | 36 | } |
ryuhs74 | 2:3dcb6f86ed22 | 37 | break; |
ryuhs74 | 2:3dcb6f86ed22 | 38 | //ryuhs74@20150713 - TEST code END |
ryuhs74 | 2:3dcb6f86ed22 | 39 | } |
ryuhs74 | 2:3dcb6f86ed22 | 40 | |
ryuhs74 | 2:3dcb6f86ed22 | 41 | if( pThread != NULL ){ |
ryuhs74 | 2:3dcb6f86ed22 | 42 | pThread->terminate(); |
ryuhs74 | 2:3dcb6f86ed22 | 43 | pThread = NULL; |
ryuhs74 | 2:3dcb6f86ed22 | 44 | } |
ryuhs74 | 2:3dcb6f86ed22 | 45 | isRun_thread = 0; |
ryuhs74 | 2:3dcb6f86ed22 | 46 | return; |
ryuhs74 | 2:3dcb6f86ed22 | 47 | } |
ryuhs74 | 2:3dcb6f86ed22 | 48 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 49 | |
ryuhs74 | 2:3dcb6f86ed22 | 50 | void dist(int distance) |
ejteb | 0:1704ea055c4f | 51 | { |
ejteb | 1:4a5586eb1765 | 52 | //put code here to happen when the distance is changed |
ryuhs74 | 2:3dcb6f86ed22 | 53 | pc.printf("Distance changed to %d mm\r\n", distance); |
ejteb | 0:1704ea055c4f | 54 | } |
ejteb | 0:1704ea055c4f | 55 | |
ryuhs74 | 4:eb5a51640b43 | 56 | |
ryuhs74 | 2:3dcb6f86ed22 | 57 | #ifdef TEST_ULTRASONIC |
ejteb | 1:4a5586eb1765 | 58 | ultrasonic mu(D8, D9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 |
ryuhs74 | 2:3dcb6f86ed22 | 59 | //have updates every .1 seconds and a timeout after 1 |
ryuhs74 | 2:3dcb6f86ed22 | 60 | //second, and call dist when the distance changes |
ryuhs74 | 4:eb5a51640b43 | 61 | #else |
ryuhs74 | 4:eb5a51640b43 | 62 | HCSR04 sensor(D12, D11); |
ryuhs74 | 2:3dcb6f86ed22 | 63 | #endif |
ejteb | 0:1704ea055c4f | 64 | |
ejteb | 0:1704ea055c4f | 65 | int main() |
ejteb | 0:1704ea055c4f | 66 | { |
ryuhs74 | 5:e1849ee9651d | 67 | #ifdef TEST_ULTRASONIC |
ryuhs74 | 5:e1849ee9651d | 68 | pc.printf("Start ----> TEST_ULTRASONIC\n"); |
ryuhs74 | 5:e1849ee9651d | 69 | #else |
ryuhs74 | 5:e1849ee9651d | 70 | pc.printf("Start ----> HCSR04\n"); |
ryuhs74 | 5:e1849ee9651d | 71 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 72 | |
ryuhs74 | 2:3dcb6f86ed22 | 73 | #ifdef TEST_ULTRASONIC |
ejteb | 1:4a5586eb1765 | 74 | mu.startUpdates();//start mesuring the distance |
ryuhs74 | 2:3dcb6f86ed22 | 75 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 76 | |
ryuhs74 | 2:3dcb6f86ed22 | 77 | while(1) { |
ryuhs74 | 4:eb5a51640b43 | 78 | |
ryuhs74 | 2:3dcb6f86ed22 | 79 | pc.printf("Before mu.checkDistance();\n"); |
ejteb | 1:4a5586eb1765 | 80 | //Do something else here |
ryuhs74 | 4:eb5a51640b43 | 81 | #ifdef TEST_ULTRASONIC |
ejteb | 1:4a5586eb1765 | 82 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
ryuhs74 | 4:eb5a51640b43 | 83 | #else |
ryuhs74 | 4:eb5a51640b43 | 84 | long distance = sensor.distance(); |
ryuhs74 | 4:eb5a51640b43 | 85 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 86 | //the class checks if dist needs to be called. |
ryuhs74 | 2:3dcb6f86ed22 | 87 | pc.printf("After mu.checkDistance();\n"); |
ryuhs74 | 2:3dcb6f86ed22 | 88 | |
ryuhs74 | 2:3dcb6f86ed22 | 89 | //ryuhs74@20150712 START - |
ryuhs74 | 2:3dcb6f86ed22 | 90 | if( index_dist < 3){ |
ryuhs74 | 2:3dcb6f86ed22 | 91 | pc.printf("Before mu.getCurrentDistance();\n"); |
ryuhs74 | 4:eb5a51640b43 | 92 | #ifdef TEST_ULTRASONIC |
ryuhs74 | 2:3dcb6f86ed22 | 93 | sum_dist[index_dist] = mu.getCurrentDistance(); |
ryuhs74 | 4:eb5a51640b43 | 94 | #else |
ryuhs74 | 4:eb5a51640b43 | 95 | sum_dist[index_dist] = distance; |
ryuhs74 | 4:eb5a51640b43 | 96 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 97 | pc.printf("sum_dist[index_dist(%d)] = %d\n", index_dist, sum_dist[index_dist]); |
ryuhs74 | 2:3dcb6f86ed22 | 98 | index_dist ++; |
ryuhs74 | 2:3dcb6f86ed22 | 99 | } else { |
ryuhs74 | 2:3dcb6f86ed22 | 100 | pc.printf("Before index_dist( %d ) ", index_dist); |
ryuhs74 | 2:3dcb6f86ed22 | 101 | index_dist = 0; |
ryuhs74 | 2:3dcb6f86ed22 | 102 | pc.printf("After index_dist( %d )\n", index_dist); |
ryuhs74 | 2:3dcb6f86ed22 | 103 | |
ryuhs74 | 2:3dcb6f86ed22 | 104 | for(int i =0; i<3;i++){ |
ryuhs74 | 2:3dcb6f86ed22 | 105 | avg_dist += sum_dist[i]; |
ryuhs74 | 2:3dcb6f86ed22 | 106 | } |
ryuhs74 | 2:3dcb6f86ed22 | 107 | |
ryuhs74 | 2:3dcb6f86ed22 | 108 | //avg_dist /= avg_dist; |
ryuhs74 | 2:3dcb6f86ed22 | 109 | |
ryuhs74 | 6:7ceab53d9a2f | 110 | if( avg_dist <= CHECK_DISTANCE ){ |
ryuhs74 | 2:3dcb6f86ed22 | 111 | #ifdef USE_THREAD |
ryuhs74 | 2:3dcb6f86ed22 | 112 | if( isRun_thread == 0 ){ |
ryuhs74 | 2:3dcb6f86ed22 | 113 | Thread thread(camera_thread); |
ryuhs74 | 2:3dcb6f86ed22 | 114 | |
ryuhs74 | 2:3dcb6f86ed22 | 115 | if( pThread == NULL) |
ryuhs74 | 2:3dcb6f86ed22 | 116 | pThread = (Thread*)&thread; |
ryuhs74 | 2:3dcb6f86ed22 | 117 | |
ryuhs74 | 2:3dcb6f86ed22 | 118 | } |
ryuhs74 | 6:7ceab53d9a2f | 119 | #else |
ryuhs74 | 2:3dcb6f86ed22 | 120 | #endif |
ryuhs74 | 2:3dcb6f86ed22 | 121 | |
ryuhs74 | 2:3dcb6f86ed22 | 122 | } |
ryuhs74 | 2:3dcb6f86ed22 | 123 | } |
ryuhs74 | 2:3dcb6f86ed22 | 124 | pc.printf("wait(1)\n"); |
ryuhs74 | 2:3dcb6f86ed22 | 125 | //ryuhs74@20150712 - END |
ryuhs74 | 4:eb5a51640b43 | 126 | wait(1.0); //ryuhs7474@20150713 - 초당 한번씩 딜레이를 준다 |
ejteb | 0:1704ea055c4f | 127 | } |
ejteb | 0:1704ea055c4f | 128 | } |