summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Van Doorn <vandoorn.nick@gmail.com>2019-01-14 15:25:46 -0800
committerGitHub <noreply@github.com>2019-01-14 15:25:46 -0800
commit0f9b54f74e2ac3042e00c84fa08aea392f923e0f (patch)
treeb5db908dfb96e61cd13bef45ad6929083a69a8dd
parent3cf8270f0547789c2aa298531492d6d85fb9c3b1 (diff)
parentdb785f4f3e56336e8d6a12e211433dd7ba410505 (diff)
Merge pull request #10 from brnkl/use-interrupt-pin
Support threaded & pin based motion detection
-rw-r--r--motionMonitor/motionMonitor.c51
-rw-r--r--readme.md5
2 files changed, 49 insertions, 7 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 <pthread.h>
#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
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.