MBSD_assignment / Ultrasonic

Dependents:   test_ultrasonic AEB Car_Simulator

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ultrasonic.h Source File

Ultrasonic.h

00001 #ifndef ULTRASONIC_H
00002 #define ULTRASONIC_H
00003 
00004 #include "mbed.h"
00005 
00006 /** Ultrasonic Class
00007 * This class is intended to be used with a 
00008 * ultrasonic sensor HC-SR04.
00009 * Once initialized the Ultrasonic object just call the read function to 
00010 * get the distance from object.
00011 */
00012 class Ultrasonic
00013 {
00014 public :
00015     /** Constructor
00016     * 
00017     * @note This is used to initialize the ultrasonic object corresponding to one sensor, 
00018     * just pass as input the pin where the sensor is connected (they must be digital pins).
00019     *
00020     * @param trigger the trigger pin name
00021     * @param echo the echo pin name
00022     *
00023     */
00024     Ultrasonic(PinName trigger, PinName echo);
00025     
00026     /** Function to read the distance in cm
00027     *
00028     * @note the distance is read internally every 60 ms so invoke this function
00029     *       not less than every 60 ms
00030     * @return
00031     *       0 if timeout(50ms) elapsed (error or out of range measure), 
00032     *       1-400 distance from object in cm
00033     * 
00034     */
00035     int read_cm();
00036 
00037 private :
00038     DigitalOut _trigger;
00039     InterruptIn _echo;
00040     Timer timer;
00041     Ticker tick;
00042     Timeout timeout;
00043     int DistanceCM;
00044         
00045     void start();
00046     void stop();
00047     void trig();
00048     void Ultrasonic_init();
00049     void timeout_err();
00050 };
00051 
00052 #endif