STM32 Nucleo F207Z ultrasonic radar. TODO: send the data over the net
Dependencies: HC_SR04_Ultrasonic_Library PinDetect
radar.cpp@1:b4179d7a5f2c, 2018-06-29 (annotated)
- Committer:
- vhx
- Date:
- Fri Jun 29 08:52:41 2018 +0000
- Revision:
- 1:b4179d7a5f2c
- Parent:
- 0:23a36e52a5fa
Envoi d'une donn?e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vhx | 0:23a36e52a5fa | 1 | #include "radar.h" |
vhx | 1:b4179d7a5f2c | 2 | #include "client.h" |
vhx | 0:23a36e52a5fa | 3 | |
vhx | 0:23a36e52a5fa | 4 | /* local function declarations */ |
vhx | 0:23a36e52a5fa | 5 | void dist(int distance); |
vhx | 0:23a36e52a5fa | 6 | |
vhx | 0:23a36e52a5fa | 7 | /* variable(s) definition(s) */ |
vhx | 0:23a36e52a5fa | 8 | int volatile position=RANGE_NONE; |
vhx | 0:23a36e52a5fa | 9 | ultrasonic mu(PB_8, PB_9, .1, 1, &dist); |
vhx | 0:23a36e52a5fa | 10 | DigitalOut led1(LED1); |
vhx | 0:23a36e52a5fa | 11 | DigitalOut led2(LED2); |
vhx | 0:23a36e52a5fa | 12 | DigitalOut led3(LED3); |
vhx | 0:23a36e52a5fa | 13 | // Global flip count variable |
vhx | 0:23a36e52a5fa | 14 | int volatile flipcount=0; |
vhx | 0:23a36e52a5fa | 15 | |
vhx | 0:23a36e52a5fa | 16 | DigitalOut led4(PC_6); // external LED or buzzer |
vhx | 0:23a36e52a5fa | 17 | //PinDetect pb(PC_13); |
vhx | 0:23a36e52a5fa | 18 | Ticker flipper; |
vhx | 0:23a36e52a5fa | 19 | |
vhx | 0:23a36e52a5fa | 20 | /* function(s) definition(s) */ |
vhx | 0:23a36e52a5fa | 21 | void dist(int distance) |
vhx | 0:23a36e52a5fa | 22 | { |
vhx | 0:23a36e52a5fa | 23 | //put code here to execute when the distance has changed |
vhx | 0:23a36e52a5fa | 24 | printf("Distance %d mm\r\n", distance); |
vhx | 1:b4179d7a5f2c | 25 | send_data2(distance); |
vhx | 0:23a36e52a5fa | 26 | if (distance < 300) position = RANGE_CLOSE; |
vhx | 0:23a36e52a5fa | 27 | else if (distance < 600) position = RANGE_MID; |
vhx | 0:23a36e52a5fa | 28 | else if (distance < 1000) position = RANGE_FAR; |
vhx | 0:23a36e52a5fa | 29 | else position = RANGE_NONE; |
vhx | 0:23a36e52a5fa | 30 | |
vhx | 0:23a36e52a5fa | 31 | led3 = (position >= RANGE_CLOSE); |
vhx | 0:23a36e52a5fa | 32 | led2 = (position >= RANGE_MID); |
vhx | 0:23a36e52a5fa | 33 | led1 = (position >= RANGE_FAR); |
vhx | 0:23a36e52a5fa | 34 | } |
vhx | 0:23a36e52a5fa | 35 | |
vhx | 0:23a36e52a5fa | 36 | |
vhx | 0:23a36e52a5fa | 37 | void flip() { |
vhx | 0:23a36e52a5fa | 38 | if(RANGE_NONE == position) { |
vhx | 0:23a36e52a5fa | 39 | led4 = 0; |
vhx | 0:23a36e52a5fa | 40 | return; |
vhx | 0:23a36e52a5fa | 41 | } |
vhx | 0:23a36e52a5fa | 42 | flipcount++; |
vhx | 0:23a36e52a5fa | 43 | flipcount = flipcount % (RANGE_MAX - position); |
vhx | 0:23a36e52a5fa | 44 | if(flipcount == 0) { |
vhx | 0:23a36e52a5fa | 45 | led4 = !led4; |
vhx | 0:23a36e52a5fa | 46 | } |
vhx | 0:23a36e52a5fa | 47 | } |
vhx | 0:23a36e52a5fa | 48 | |
vhx | 0:23a36e52a5fa | 49 | // run a radar forever |
vhx | 0:23a36e52a5fa | 50 | void radar() { |
vhx | 0:23a36e52a5fa | 51 | //start measuring the distance |
vhx | 0:23a36e52a5fa | 52 | mu.startUpdates(); |
vhx | 0:23a36e52a5fa | 53 | // the address of the function to be attached (flip) and the interval (2 seconds) |
vhx | 0:23a36e52a5fa | 54 | flipper.attach(&flip, 0.2); |
vhx | 0:23a36e52a5fa | 55 | |
vhx | 0:23a36e52a5fa | 56 | while(1) |
vhx | 0:23a36e52a5fa | 57 | { |
vhx | 0:23a36e52a5fa | 58 | //Do something else here |
vhx | 0:23a36e52a5fa | 59 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
vhx | 0:23a36e52a5fa | 60 | //the class checks if dist needs to be called. |
vhx | 0:23a36e52a5fa | 61 | } |
vhx | 0:23a36e52a5fa | 62 | } |