From 99dea359907f37c1030e5abb6d3df3ef6af4ab79 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Sat, 24 Nov 2018 14:47:42 -0800 Subject: Support threaded & pin based motion detection --- motionMonitor/motionMonitor.c | 51 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/motionMonitor/motionMonitor.c b/motionMonitor/motionMonitor.c index c56a55c..4406bf3 100644 --- a/motionMonitor/motionMonitor.c +++ b/motionMonitor/motionMonitor.c @@ -1,13 +1,11 @@ #include "interfaces.h" #include "legato.h" #include "util.h" -#include #define N_CHANGE_BLOCKS 200 -#define SAMPLE_PERIOD_MS 100 #define DEFAULT_THRESHOLD_MS2 17 - -void* impactMonitor(void*); +// only used if -DMOTION_MONITOR_USE_THREAD is set +#define SAMPLE_PERIOD_MS 100 static const char FormatStr[] = "/sys/bus/iio/devices/iio:device0/in_%s_%s"; static const char AccType[] = "accel"; @@ -24,7 +22,9 @@ struct suddenImpacts_t { double y[N_CHANGE_BLOCKS]; double z[N_CHANGE_BLOCKS]; uint64_t timestamps[N_CHANGE_BLOCKS]; +#ifdef MOTION_MONITOR_USE_THREAD pthread_mutex_t lock; +#endif } impacts = {0, DEFAULT_THRESHOLD_MS2}; /* @@ -69,6 +69,7 @@ le_result_t recordImpact(struct suddenImpacts_t* it, double xAcc, double yAcc, double zAcc) { + LE_INFO("Recording impact..."); if (it->nValues > N_CHANGE_BLOCKS || it->nValues > N_CHANGE_BLOCKS || it->nValues > N_CHANGE_BLOCKS) return LE_OUT_OF_RANGE; @@ -97,8 +98,9 @@ le_result_t brnkl_motion_getSuddenImpact(double* xAcc, if (!impacts.nValues) LE_INFO("No Sudden Impacts to Report"); else { +#ifdef MOTION_MONITOR_USE_THREAD pthread_mutex_lock(&impacts.lock); - +#endif if (impacts.nValues > *xSize || impacts.nValues > *ySize || impacts.nValues > *zSize) return LE_OUT_OF_RANGE; @@ -113,13 +115,16 @@ le_result_t brnkl_motion_getSuddenImpact(double* xAcc, *xSize = *ySize = *zSize = impacts.nValues; impacts.nValues = 0; - +#ifdef MOTION_MONITOR_USE_THREAD pthread_mutex_unlock(&impacts.lock); +#endif } return LE_OK; } +#ifdef MOTION_MONITOR_USE_THREAD + /* * Monitors accelerometer from iio on 100ms intervals * @@ -171,3 +176,37 @@ void initThread() { COMPONENT_INIT { initThread(); } + +#else + +void interruptHandler(bool val, void* ctx) { + double x, y, z; + le_result_t r = LE_OK; + struct suddenImpacts_t* it = ctx; + if (ctx == NULL) { + LE_ERROR("No context passed"); + } else { + brnkl_motion_getCurrentAcceleration(&x, &y, &z); + + double impactMagnitude = sqrt(x * x + y * y + z * z); + + if (impactMagnitude > it->threshold) { + // 3. add x, y, z to impact array + r = recordImpact(it, x, y, z); + } + if (r != LE_OK) + LE_ERROR("Impact Not Recorded"); + } +} + +void initGpio() { + interrupt_SetInput(INTERRUPT_ACTIVE_HIGH); + interrupt_AddChangeEventHandler(INTERRUPT_EDGE_RISING, interruptHandler, + &impacts, 0); +} + +COMPONENT_INIT { + initGpio(); +} + +#endif -- cgit v1.2.3 From db785f4f3e56336e8d6a12e211433dd7ba410505 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Sat, 24 Nov 2018 14:58:19 -0800 Subject: Example thread flag in readme --- readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fdd3703..a1913aa 100644 --- a/readme.md +++ b/readme.md @@ -6,12 +6,15 @@ This application is designed to operate on a [MongOH Red](https://mangoh.io/mang When cloning use ``git clone https://github.com/brnkl/motion-service.git``. - ## Building Compile the project using wp85: ``make wp85`` +### Flags + +- `-DMOTION_MONITOR_USE_THREAD` enables thread based mode (polling). By default, an interrupt pin is used to detect motion. This results in lower traffic on the bus (i2c or SPI), but also requires special drivers for the BMI160. + ## Setting Variables To change the sensitivity of impact detection you must edit the `motionMonitor/motionMonitor.c` file. The Accelerometer measures acceleration in 3 dimensions, X, Y, and Z. These dimensions of acceleration are recorded and the magnitude of their resulting vector is calculated using ``double impactMagnitude = sqrt(x * x + y * y + z * z);``. As visualized in the image below. -- cgit v1.2.3