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

Dependents:   Parallax_XBAND_Demo Motion_Detection_Parallax

Committer:
ottaviano3
Date:
Sun Mar 08 20:01:37 2015 +0000
Revision:
2:6e294a3a74f8
Child:
3:b9e54783e0c5
Naming Changes

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 2:6e294a3a74f8 36 /**
ottaviano3 2:6e294a3a74f8 37 * Subroutine to enable and disable the detector.
ottaviano3 2:6e294a3a74f8 38 *
ottaviano3 2:6e294a3a74f8 39 * @param enable True to enable, False to disable.
ottaviano3 2:6e294a3a74f8 40 */
ottaviano3 2:6e294a3a74f8 41 void xband::enable(bool enable)
ottaviano3 2:6e294a3a74f8 42 {
ottaviano3 2:6e294a3a74f8 43 _pin_enable=enable;
ottaviano3 2:6e294a3a74f8 44 }
ottaviano3 2:6e294a3a74f8 45
ottaviano3 2:6e294a3a74f8 46 /**
ottaviano3 2:6e294a3a74f8 47 * Interrupt service routine to collect data on the input pin.
ottaviano3 2:6e294a3a74f8 48 */
ottaviano3 2:6e294a3a74f8 49 void xband::xband_RiseISR()
ottaviano3 2:6e294a3a74f8 50 {
ottaviano3 2:6e294a3a74f8 51 // Increment count for every positive rising edge pulse on input pin.
ottaviano3 2:6e294a3a74f8 52 _count++;
ottaviano3 2:6e294a3a74f8 53 }
ottaviano3 2:6e294a3a74f8 54
ottaviano3 2:6e294a3a74f8 55 /**
ottaviano3 2:6e294a3a74f8 56 * Interrupt service routine to calculate the velocity of the moving object.
ottaviano3 2:6e294a3a74f8 57 */
ottaviano3 2:6e294a3a74f8 58 void xband::xband_velocityISR()
ottaviano3 2:6e294a3a74f8 59 {
ottaviano3 2:6e294a3a74f8 60 // Differentiate current count value change during time slice.
ottaviano3 2:6e294a3a74f8 61 _velocity = ((_count - _lastcount)/(_sampletime.read_ms()/1000.0));
ottaviano3 2:6e294a3a74f8 62
ottaviano3 2:6e294a3a74f8 63 // Set a flag to indicate a new velocity calculation.
ottaviano3 2:6e294a3a74f8 64 _velocityflag = true;
ottaviano3 2:6e294a3a74f8 65
ottaviano3 2:6e294a3a74f8 66 // Reset timer for next differentiation.
ottaviano3 2:6e294a3a74f8 67 _sampletime.reset();
ottaviano3 2:6e294a3a74f8 68 _lastcount = _count;
ottaviano3 2:6e294a3a74f8 69 }
ottaviano3 2:6e294a3a74f8 70
ottaviano3 2:6e294a3a74f8 71 /**
ottaviano3 2:6e294a3a74f8 72 * Get count value and return it as an integer.
ottaviano3 2:6e294a3a74f8 73 *
ottaviano3 2:6e294a3a74f8 74 * @return Integer value of current count.
ottaviano3 2:6e294a3a74f8 75 */
ottaviano3 2:6e294a3a74f8 76 int xband::read_count()
ottaviano3 2:6e294a3a74f8 77 {
ottaviano3 2:6e294a3a74f8 78 return _count;
ottaviano3 2:6e294a3a74f8 79 }
ottaviano3 2:6e294a3a74f8 80
ottaviano3 2:6e294a3a74f8 81 /**
ottaviano3 2:6e294a3a74f8 82 * Get current velocity reading. Sets a flag to false to indicate a stale value after reading.
ottaviano3 2:6e294a3a74f8 83 *
ottaviano3 2:6e294a3a74f8 84 * @return Float value of currently measured velocity.
ottaviano3 2:6e294a3a74f8 85 */
ottaviano3 2:6e294a3a74f8 86 float xband::read_velocity()
ottaviano3 2:6e294a3a74f8 87 {
ottaviano3 2:6e294a3a74f8 88 _velocityflag = false;
ottaviano3 2:6e294a3a74f8 89 return _velocity;
ottaviano3 2:6e294a3a74f8 90 }
ottaviano3 2:6e294a3a74f8 91
ottaviano3 2:6e294a3a74f8 92 /**
ottaviano3 2:6e294a3a74f8 93 * Resets the count variable to 0.
ottaviano3 2:6e294a3a74f8 94 */
ottaviano3 2:6e294a3a74f8 95 void xband::reset_count()
ottaviano3 2:6e294a3a74f8 96 {
ottaviano3 2:6e294a3a74f8 97 _count = 0;
ottaviano3 2:6e294a3a74f8 98 }
ottaviano3 2:6e294a3a74f8 99
ottaviano3 2:6e294a3a74f8 100 /**
ottaviano3 2:6e294a3a74f8 101 * Checks if a new velocity value has been calculated.
ottaviano3 2:6e294a3a74f8 102 *
ottaviano3 2:6e294a3a74f8 103 * @return True if a new velocity value is avaliable. False if the value has
ottaviano3 2:6e294a3a74f8 104 * already been read.
ottaviano3 2:6e294a3a74f8 105 */
ottaviano3 2:6e294a3a74f8 106 bool xband::velocityack()
ottaviano3 2:6e294a3a74f8 107 {
ottaviano3 2:6e294a3a74f8 108 return _velocityflag;
ottaviano3 2:6e294a3a74f8 109 }