Library for setting up the Parallax X-Band Motion Detector.
Dependents: Parallax_XBAND_Demo Motion_Detection_Parallax
Diff: Parallax_X-Band.cpp
- Revision:
- 2:6e294a3a74f8
- Child:
- 3:b9e54783e0c5
diff -r 860badad8be9 -r 6e294a3a74f8 Parallax_X-Band.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Parallax_X-Band.cpp Sun Mar 08 20:01:37 2015 +0000 @@ -0,0 +1,109 @@ +#include "Parallax_X-Band.h" + +/** +* Initialize the xband motion detector and setup interrupts. Default is to be disabled. +* +* @param pin_enable Enable pin. Connected to EN on xband. +* @param pin_input Data pin. Connected to OUT on xband. +*/ +xband::xband(PinName pin_enable, PinName pin_input) + : _pin_enable(pin_enable), _pin_input(pin_input) +{ + // Default disabled. + _pin_enable=0; + + // Initialize count to be 0. + _count = 0; + + // No velocity reading has been calculated yet. + _velocityflag = false; + + // Setup interrupt on input pin. + _pin_input.rise(this, &xband::xband_RiseISR); + + // Timer for count differntiation. + _sampletime.start(); + + // Interrupt to calculate the velocity. Default every half second. + _samplevelocity.attach(this, &xband::xband_velocityISR, 0.5); +} + +/** +* Destructor +*/ +xband::~xband() {} + +/** +* Subroutine to enable and disable the detector. +* +* @param enable True to enable, False to disable. +*/ +void xband::enable(bool enable) +{ + _pin_enable=enable; +} + +/** +* Interrupt service routine to collect data on the input pin. +*/ +void xband::xband_RiseISR() +{ + // Increment count for every positive rising edge pulse on input pin. + _count++; +} + +/** +* Interrupt service routine to calculate the velocity of the moving object. +*/ +void xband::xband_velocityISR() +{ + // Differentiate current count value change during time slice. + _velocity = ((_count - _lastcount)/(_sampletime.read_ms()/1000.0)); + + // Set a flag to indicate a new velocity calculation. + _velocityflag = true; + + // Reset timer for next differentiation. + _sampletime.reset(); + _lastcount = _count; +} + +/** +* Get count value and return it as an integer. +* +* @return Integer value of current count. +*/ +int xband::read_count() +{ + return _count; +} + +/** +* Get current velocity reading. Sets a flag to false to indicate a stale value after reading. +* +* @return Float value of currently measured velocity. +*/ +float xband::read_velocity() +{ + _velocityflag = false; + return _velocity; +} + +/** +* Resets the count variable to 0. +*/ +void xband::reset_count() +{ + _count = 0; +} + +/** +* Checks if a new velocity value has been calculated. +* +* @return True if a new velocity value is avaliable. False if the value has +* already been read. +*/ +bool xband::velocityack() +{ + return _velocityflag; +} \ No newline at end of file