Delta for AGV
Dependencies: mbed HC-SR04 ros_lib_kinetic
main.cpp@0:ebee649c5b1b, 2015-12-03 (annotated)
- Committer:
- tbjazic
- Date:
- Thu Dec 03 15:43:54 2015 +0000
- Revision:
- 0:ebee649c5b1b
- Child:
- 1:22043b67c31c
Revision 1: Solution in the main program, without class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:ebee649c5b1b | 1 | /** Initial test of the ultrasonic sensor HC-SR04 */ |
tbjazic | 0:ebee649c5b1b | 2 | |
tbjazic | 0:ebee649c5b1b | 3 | #include "mbed.h" |
tbjazic | 0:ebee649c5b1b | 4 | |
tbjazic | 0:ebee649c5b1b | 5 | Serial pc(USBTX, USBRX); // communication with terminal |
tbjazic | 0:ebee649c5b1b | 6 | InterruptIn echo(p5); // echo pin |
tbjazic | 0:ebee649c5b1b | 7 | DigitalOut trigger(p7); // trigger pin |
tbjazic | 0:ebee649c5b1b | 8 | Timer timer; // echo pulsewidth measurement |
tbjazic | 0:ebee649c5b1b | 9 | |
tbjazic | 0:ebee649c5b1b | 10 | /** Start the echo pulsewidth measurement */ |
tbjazic | 0:ebee649c5b1b | 11 | void startTimer() { |
tbjazic | 0:ebee649c5b1b | 12 | timer.start(); // start the timer |
tbjazic | 0:ebee649c5b1b | 13 | } |
tbjazic | 0:ebee649c5b1b | 14 | |
tbjazic | 0:ebee649c5b1b | 15 | /** Stop the echo pulsewidth measurement */ |
tbjazic | 0:ebee649c5b1b | 16 | void stopTimer() { |
tbjazic | 0:ebee649c5b1b | 17 | timer.stop(); // stop the timer |
tbjazic | 0:ebee649c5b1b | 18 | } |
tbjazic | 0:ebee649c5b1b | 19 | |
tbjazic | 0:ebee649c5b1b | 20 | int main() { |
tbjazic | 0:ebee649c5b1b | 21 | /** configure the rising edge to start the timer */ |
tbjazic | 0:ebee649c5b1b | 22 | echo.rise(&startTimer); |
tbjazic | 0:ebee649c5b1b | 23 | |
tbjazic | 0:ebee649c5b1b | 24 | /** configure the falling edge to stop the timer */ |
tbjazic | 0:ebee649c5b1b | 25 | echo.fall(&stopTimer); |
tbjazic | 0:ebee649c5b1b | 26 | |
tbjazic | 0:ebee649c5b1b | 27 | while(1) { |
tbjazic | 0:ebee649c5b1b | 28 | /** Start the measurement by sending the 10us trigger pulse */ |
tbjazic | 0:ebee649c5b1b | 29 | trigger = 1; |
tbjazic | 0:ebee649c5b1b | 30 | wait_us(10); |
tbjazic | 0:ebee649c5b1b | 31 | trigger = 0; |
tbjazic | 0:ebee649c5b1b | 32 | |
tbjazic | 0:ebee649c5b1b | 33 | /** Wait for the sensor to finish measurement (generate rise and fall interrupts). |
tbjazic | 0:ebee649c5b1b | 34 | * Minimum wait time is determined by maximum measurement distance of 400 cm. |
tbjazic | 0:ebee649c5b1b | 35 | * t_min = 400 * 58 = 23200 us = 23.2 ms */ |
tbjazic | 0:ebee649c5b1b | 36 | wait(1); |
tbjazic | 0:ebee649c5b1b | 37 | |
tbjazic | 0:ebee649c5b1b | 38 | /** calculate the distance in cm */ |
tbjazic | 0:ebee649c5b1b | 39 | float distance = timer.read() * 1e6 / 58; |
tbjazic | 0:ebee649c5b1b | 40 | timer.reset(); // reset the timer to 0 |
tbjazic | 0:ebee649c5b1b | 41 | |
tbjazic | 0:ebee649c5b1b | 42 | /** Print the result in cm to the terminal with 1 decimal place |
tbjazic | 0:ebee649c5b1b | 43 | * (number 5 after % means that total of 5 digits will be reserved |
tbjazic | 0:ebee649c5b1b | 44 | * for printing the number, including the dot and one decimal place). */ |
tbjazic | 0:ebee649c5b1b | 45 | pc.printf("Distance: %5.1f cm\r", distance); |
tbjazic | 0:ebee649c5b1b | 46 | } |
tbjazic | 0:ebee649c5b1b | 47 | } |