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
Added float overload.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ottaviano3 2:6e294a3a74f8 1 #include "Parallax_X-Band.h"
ottaviano3 2:6e294a3a74f8 2
ottaviano3 2:6e294a3a74f8 3 /**
ottaviano3 2:6e294a3a74f8 4 * Initialize the xband motion detector and setup interrupts. Default is to be disabled.
ottaviano3 2:6e294a3a74f8 5 *
ottaviano3 2:6e294a3a74f8 6 * @param pin_enable Enable pin. Connected to EN on xband.
ottaviano3 2:6e294a3a74f8 7 * @param pin_input Data pin. Connected to OUT on xband.
ottaviano3 2:6e294a3a74f8 8 */
ottaviano3 2:6e294a3a74f8 9 xband::xband(PinName pin_enable, PinName pin_input)
ottaviano3 2:6e294a3a74f8 10 : _pin_enable(pin_enable), _pin_input(pin_input)
ottaviano3 2:6e294a3a74f8 11 {
ottaviano3 2:6e294a3a74f8 12 // Default disabled.
ottaviano3 2:6e294a3a74f8 13 _pin_enable=0;
ottaviano3 2:6e294a3a74f8 14
ottaviano3 2:6e294a3a74f8 15 // Initialize count to be 0.
ottaviano3 2:6e294a3a74f8 16 _count = 0;
ottaviano3 2:6e294a3a74f8 17
ottaviano3 2:6e294a3a74f8 18 // No velocity reading has been calculated yet.
ottaviano3 2:6e294a3a74f8 19 _velocityflag = false;
ottaviano3 2:6e294a3a74f8 20
ottaviano3 2:6e294a3a74f8 21 // Setup interrupt on input pin.
ottaviano3 2:6e294a3a74f8 22 _pin_input.rise(this, &xband::xband_RiseISR);
ottaviano3 2:6e294a3a74f8 23
ottaviano3 2:6e294a3a74f8 24 // Timer for count differntiation.
ottaviano3 2:6e294a3a74f8 25 _sampletime.start();
ottaviano3 2:6e294a3a74f8 26
ottaviano3 2:6e294a3a74f8 27 // Interrupt to calculate the velocity. Default every half second.
ottaviano3 2:6e294a3a74f8 28 _samplevelocity.attach(this, &xband::xband_velocityISR, 0.5);
ottaviano3 2:6e294a3a74f8 29 }
ottaviano3 2:6e294a3a74f8 30
ottaviano3 2:6e294a3a74f8 31 /**
ottaviano3 2:6e294a3a74f8 32 * Destructor
ottaviano3 2:6e294a3a74f8 33 */
ottaviano3 2:6e294a3a74f8 34 xband::~xband() {}
ottaviano3 2:6e294a3a74f8 35
ottaviano3 4:efdfdab29a20 36
ottaviano3 4:efdfdab29a20 37
ottaviano3 2:6e294a3a74f8 38 /**
ottaviano3 2:6e294a3a74f8 39 * Subroutine to enable and disable the detector.
ottaviano3 2:6e294a3a74f8 40 *
ottaviano3 2:6e294a3a74f8 41 * @param enable True to enable, False to disable.
ottaviano3 2:6e294a3a74f8 42 */
ottaviano3 2:6e294a3a74f8 43 void xband::enable(bool enable)
ottaviano3 2:6e294a3a74f8 44 {
ottaviano3 2:6e294a3a74f8 45 _pin_enable=enable;
ottaviano3 2:6e294a3a74f8 46 }
ottaviano3 2:6e294a3a74f8 47
ottaviano3 2:6e294a3a74f8 48 /**
ottaviano3 2:6e294a3a74f8 49 * Interrupt service routine to collect data on the input pin.
ottaviano3 2:6e294a3a74f8 50 */
ottaviano3 2:6e294a3a74f8 51 void xband::xband_RiseISR()
ottaviano3 2:6e294a3a74f8 52 {
ottaviano3 2:6e294a3a74f8 53 // Increment count for every positive rising edge pulse on input pin.
ottaviano3 2:6e294a3a74f8 54 _count++;
ottaviano3 2:6e294a3a74f8 55 }
ottaviano3 2:6e294a3a74f8 56
ottaviano3 2:6e294a3a74f8 57 /**
ottaviano3 2:6e294a3a74f8 58 * Interrupt service routine to calculate the velocity of the moving object.
ottaviano3 2:6e294a3a74f8 59 */
ottaviano3 2:6e294a3a74f8 60 void xband::xband_velocityISR()
ottaviano3 2:6e294a3a74f8 61 {
ottaviano3 2:6e294a3a74f8 62 // Differentiate current count value change during time slice.
ottaviano3 2:6e294a3a74f8 63 _velocity = ((_count - _lastcount)/(_sampletime.read_ms()/1000.0));
ottaviano3 2:6e294a3a74f8 64
ottaviano3 2:6e294a3a74f8 65 // Set a flag to indicate a new velocity calculation.
ottaviano3 2:6e294a3a74f8 66 _velocityflag = true;
ottaviano3 2:6e294a3a74f8 67
ottaviano3 2:6e294a3a74f8 68 // Reset timer for next differentiation.
ottaviano3 2:6e294a3a74f8 69 _sampletime.reset();
ottaviano3 2:6e294a3a74f8 70 _lastcount = _count;
ottaviano3 2:6e294a3a74f8 71 }
ottaviano3 2:6e294a3a74f8 72
ottaviano3 2:6e294a3a74f8 73 /**
ottaviano3 2:6e294a3a74f8 74 * Get count value and return it as an integer.
ottaviano3 2:6e294a3a74f8 75 *
ottaviano3 2:6e294a3a74f8 76 * @return Integer value of current count.
ottaviano3 2:6e294a3a74f8 77 */
ottaviano3 2:6e294a3a74f8 78 int xband::read_count()
ottaviano3 2:6e294a3a74f8 79 {
ottaviano3 2:6e294a3a74f8 80 return _count;
ottaviano3 2:6e294a3a74f8 81 }
ottaviano3 2:6e294a3a74f8 82
ottaviano3 2:6e294a3a74f8 83 /**
ottaviano3 3:b9e54783e0c5 84 * Get current velocity reading in ticks/sec. Sets a flag to false to indicate a stale value after reading.
ottaviano3 2:6e294a3a74f8 85 *
ottaviano3 2:6e294a3a74f8 86 * @return Float value of currently measured velocity.
ottaviano3 3:b9e54783e0c5 87 */
ottaviano3 2:6e294a3a74f8 88 float xband::read_velocity()
ottaviano3 2:6e294a3a74f8 89 {
ottaviano3 2:6e294a3a74f8 90 _velocityflag = false;
ottaviano3 2:6e294a3a74f8 91 return _velocity;
ottaviano3 2:6e294a3a74f8 92 }
ottaviano3 2:6e294a3a74f8 93
ottaviano3 2:6e294a3a74f8 94 /**
ottaviano3 2:6e294a3a74f8 95 * Resets the count variable to 0.
ottaviano3 2:6e294a3a74f8 96 */
ottaviano3 2:6e294a3a74f8 97 void xband::reset_count()
ottaviano3 2:6e294a3a74f8 98 {
ottaviano3 2:6e294a3a74f8 99 _count = 0;
ottaviano3 2:6e294a3a74f8 100 }
ottaviano3 2:6e294a3a74f8 101
ottaviano3 2:6e294a3a74f8 102 /**
ottaviano3 2:6e294a3a74f8 103 * Checks if a new velocity value has been calculated.
ottaviano3 2:6e294a3a74f8 104 *
ottaviano3 2:6e294a3a74f8 105 * @return True if a new velocity value is avaliable. False if the value has
ottaviano3 2:6e294a3a74f8 106 * already been read.
ottaviano3 2:6e294a3a74f8 107 */
ottaviano3 2:6e294a3a74f8 108 bool xband::velocityack()
ottaviano3 2:6e294a3a74f8 109 {
ottaviano3 2:6e294a3a74f8 110 return _velocityflag;
ottaviano3 4:efdfdab29a20 111 }
ottaviano3 4:efdfdab29a20 112
ottaviano3 4:efdfdab29a20 113 /**
ottaviano3 4:efdfdab29a20 114 * Default overload to read velocity.
ottaviano3 4:efdfdab29a20 115 *
ottaviano3 4:efdfdab29a20 116 * @return Float value of currently measured velocity.
ottaviano3 4:efdfdab29a20 117 */
ottaviano3 4:efdfdab29a20 118 xband::operator float() {
ottaviano3 4:efdfdab29a20 119 return read_velocity();
ottaviano3 2:6e294a3a74f8 120 }