Library for setting up the Parallax X-Band Motion Detector.

Dependents:   Parallax_XBAND_Demo Motion_Detection_Parallax

Committer:
ottaviano3
Date:
Mon Mar 09 20:16:05 2015 +0000
Revision:
4:efdfdab29a20
Parent:
3:b9e54783e0c5
Child:
5:9cfc7b541791
Added float overload.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ottaviano3 0:ff6dc90dff0b 1 #include "mbed.h"
ottaviano3 0:ff6dc90dff0b 2
ottaviano3 0:ff6dc90dff0b 3 /**
ottaviano3 0:ff6dc90dff0b 4 * Wrapper for the Parallax X-Band Motion Detector
ottaviano3 0:ff6dc90dff0b 5 */
ottaviano3 0:ff6dc90dff0b 6 class xband
ottaviano3 0:ff6dc90dff0b 7 {
ottaviano3 0:ff6dc90dff0b 8 public:
ottaviano3 0:ff6dc90dff0b 9
ottaviano3 0:ff6dc90dff0b 10 /**
ottaviano3 0:ff6dc90dff0b 11 * Initialize the xband motion detector and setup interrupts on specified pins.
ottaviano3 0:ff6dc90dff0b 12 * Default is to be disabled.
ottaviano3 0:ff6dc90dff0b 13 *
ottaviano3 0:ff6dc90dff0b 14 * @param pin_enable Enable pin. Connected to EN on xband.
ottaviano3 0:ff6dc90dff0b 15 * @param pin_input Data pin. Connected to OUT on xband.
ottaviano3 0:ff6dc90dff0b 16 */
ottaviano3 1:860badad8be9 17 xband(PinName pin_enable, PinName pin_input);
ottaviano3 0:ff6dc90dff0b 18
ottaviano3 0:ff6dc90dff0b 19 /**
ottaviano3 0:ff6dc90dff0b 20 * Destructor
ottaviano3 0:ff6dc90dff0b 21 */
ottaviano3 0:ff6dc90dff0b 22 ~xband();
ottaviano3 0:ff6dc90dff0b 23
ottaviano3 0:ff6dc90dff0b 24 /**
ottaviano3 0:ff6dc90dff0b 25 * Subroutine to enable and disable the detector.
ottaviano3 0:ff6dc90dff0b 26 *
ottaviano3 0:ff6dc90dff0b 27 * @param enable True to enable, False to disable.
ottaviano3 0:ff6dc90dff0b 28 */
ottaviano3 0:ff6dc90dff0b 29 void enable(bool enable);
ottaviano3 0:ff6dc90dff0b 30
ottaviano3 0:ff6dc90dff0b 31 /**
ottaviano3 0:ff6dc90dff0b 32 * Get count value and return it as an integer.
ottaviano3 0:ff6dc90dff0b 33 *
ottaviano3 0:ff6dc90dff0b 34 * @return Integer value of current count.
ottaviano3 0:ff6dc90dff0b 35 */
ottaviano3 0:ff6dc90dff0b 36 int read_count();
ottaviano3 0:ff6dc90dff0b 37
ottaviano3 0:ff6dc90dff0b 38 /**
ottaviano3 0:ff6dc90dff0b 39 * Resets the count variable to 0.
ottaviano3 0:ff6dc90dff0b 40 */
ottaviano3 0:ff6dc90dff0b 41 void reset_count();
ottaviano3 0:ff6dc90dff0b 42
ottaviano3 0:ff6dc90dff0b 43 /**
ottaviano3 0:ff6dc90dff0b 44 * Checks if a new velocity value has been calculated.
ottaviano3 0:ff6dc90dff0b 45 *
ottaviano3 0:ff6dc90dff0b 46 * @return True if a new velocity value is avaliable. False if the value has
ottaviano3 0:ff6dc90dff0b 47 * already been read.
ottaviano3 0:ff6dc90dff0b 48 */
ottaviano3 0:ff6dc90dff0b 49 bool velocityack();
ottaviano3 0:ff6dc90dff0b 50
ottaviano3 0:ff6dc90dff0b 51 /**
ottaviano3 3:b9e54783e0c5 52 * Get current velocity reading in ticks/sec. Sets a flag to false to indicate a stale value after reading.
ottaviano3 0:ff6dc90dff0b 53 *
ottaviano3 0:ff6dc90dff0b 54 * @return Float value of currently measured velocity.
ottaviano3 0:ff6dc90dff0b 55 */
ottaviano3 0:ff6dc90dff0b 56 float read_velocity();
ottaviano3 0:ff6dc90dff0b 57
ottaviano3 4:efdfdab29a20 58 /**
ottaviano3 4:efdfdab29a20 59 * Default overload to read velocity.
ottaviano3 4:efdfdab29a20 60 *
ottaviano3 4:efdfdab29a20 61 * @return Float value of currently measured velocity.
ottaviano3 4:efdfdab29a20 62 */
ottaviano3 4:efdfdab29a20 63 operator float();
ottaviano3 4:efdfdab29a20 64
ottaviano3 0:ff6dc90dff0b 65 private:
ottaviano3 0:ff6dc90dff0b 66 /**
ottaviano3 0:ff6dc90dff0b 67 * Interrupt service routine to collect data on the input pin.
ottaviano3 0:ff6dc90dff0b 68 */
ottaviano3 0:ff6dc90dff0b 69 void xband_RiseISR();
ottaviano3 0:ff6dc90dff0b 70
ottaviano3 0:ff6dc90dff0b 71 /**
ottaviano3 0:ff6dc90dff0b 72 * Interrupt service routine to calculate the velocity of the moving object.
ottaviano3 0:ff6dc90dff0b 73 */
ottaviano3 0:ff6dc90dff0b 74 void xband_velocityISR();
ottaviano3 0:ff6dc90dff0b 75
ottaviano3 0:ff6dc90dff0b 76 // Setup pin input types.
ottaviano3 0:ff6dc90dff0b 77 DigitalOut _pin_enable;
ottaviano3 0:ff6dc90dff0b 78 InterruptIn _pin_input;
ottaviano3 0:ff6dc90dff0b 79
ottaviano3 0:ff6dc90dff0b 80 // Stores count value.
ottaviano3 0:ff6dc90dff0b 81 volatile int _count;
ottaviano3 0:ff6dc90dff0b 82 // Old count value.
ottaviano3 0:ff6dc90dff0b 83 volatile int _lastcount;
ottaviano3 0:ff6dc90dff0b 84 // Store calculated velocity.
ottaviano3 0:ff6dc90dff0b 85 float _velocity;
ottaviano3 0:ff6dc90dff0b 86 // Velocity read status flag.
ottaviano3 0:ff6dc90dff0b 87 bool _velocityflag;
ottaviano3 0:ff6dc90dff0b 88
ottaviano3 0:ff6dc90dff0b 89 // Timer for differentiation
ottaviano3 0:ff6dc90dff0b 90 Timer _sampletime;
ottaviano3 0:ff6dc90dff0b 91
ottaviano3 0:ff6dc90dff0b 92 // Interrupt to calculate velocity.
ottaviano3 0:ff6dc90dff0b 93 Ticker _samplevelocity;
ottaviano3 0:ff6dc90dff0b 94 };