Arduino 101 CurieIMU Shock Detect

With this tutorial you learn to set up one of the features of the IMU and manage the interrupt generated when the feature sensing conditions are met.

With this tutorial you learn to set up one of the features of the IMU and manage the interrupt generated when the feature's sensing conditions are met.. Each feature can be fine tuned setting up trigger and duration values. Once set up, the IMU monitors the relevand accelerometer and gyroscope values looking for the data pattern that corresponds to the expected event. In this case we expect a shock.

Hardware Required

The Circuit

genuino101fzz

image developed using Fritzing. No additional hardware is needed to use this tutorial.

Software Essentials

Libraries

CurieIMU.h is the library that gives access to all the parameters, features and readings of the IMU chip of the 101 board. This unit contains a three axes accelerometer and a three axes gyroscope. This library is part of the 101 board core and it is loaded together with the core files for Arduino 101. In this tutorial we set up the Shock detection feature and we enable its interrupt.

Functions

none

Code

A shock is when the sensor reads a significative acceleration for a very short time. The thdeshold defines how big the acceleration should be to be considered relevant, while the duration that is either 50 or 75 ms. These two fixed values have been defined to spot the shock pattern in high g events. Once set up threshold and duration, the interrupt is armed and the callback function is set to

eventCallback
, When the interrupt is asserted, the execution goes to the callback function where the getInterruptStatus lets you check for the various axis and orientation combinations, finding exactly on which axis and which direction the shock happened.

1/*
2
3 * Copyright (c) 2016 Intel Corporation. All rights reserved.
4
5 * See the bottom of this file for the license terms.
6
7 */
8
9/*
10
11 This sketch example demonstrates how the BMI160 accelerometer on the
12
13 Intel(R) Curie(TM) module can be used to detect shocks or sudden movements
14
15*/
16
17#include "CurieIMU.h"
18
19bool blinkState = false; // state of the LED
20
21void setup() {
22
23 Serial.begin(9600); // initialize Serial communication
24
25 while(!Serial) ; // wait for serial port to connect..
26
27 /* Initialise the IMU */
28
29 CurieIMU.begin();
30
31 CurieIMU.attachInterrupt(eventCallback);
32
33 /* Enable Shock Detection */
34
35 CurieIMU.setDetectionThreshold(CURIE_IMU_SHOCK, 1500); // 1.5g = 1500 mg
36
37 CurieIMU.setDetectionDuration(CURIE_IMU_SHOCK, 50); // 50ms
38
39 CurieIMU.interrupts(CURIE_IMU_SHOCK);
40
41 Serial.println("IMU initialisation complete, waiting for events...");
42}
43
44void loop() {
45
46 // blink the LED in the main loop:
47
48 digitalWrite(13, blinkState);
49
50 blinkState = !blinkState;
51
52 delay(1000);
53}
54
55static void eventCallback(void)
56{
57
58 if (CurieIMU.getInterruptStatus(CURIE_IMU_SHOCK)) {
59
60 if (CurieIMU.shockDetected(X_AXIS, POSITIVE))
61
62 Serial.println("Negative shock detected on X-axis");
63
64 if (CurieIMU.shockDetected(X_AXIS, NEGATIVE))
65
66 Serial.println("Positive shock detected on X-axis");
67
68 if (CurieIMU.shockDetected(Y_AXIS, POSITIVE))
69
70 Serial.println("Negative shock detected on Y-axis");
71
72 if (CurieIMU.shockDetected(Y_AXIS, NEGATIVE))
73
74 Serial.println("Positive shock detected on Y-axis");
75
76 if (CurieIMU.shockDetected(Z_AXIS, POSITIVE))
77
78 Serial.println("Negative shock detected on Z-axis");
79
80 if (CurieIMU.shockDetected(Z_AXIS, NEGATIVE))
81
82 Serial.println("Positive shock detected on Z-axis");
83
84 }
85}
86
87/*
88
89 Copyright (c) 2016 Intel Corporation. All rights reserved.
90
91 This library is free software; you can redistribute it and/or
92
93 modify it under the terms of the GNU Lesser General Public
94
95 License as published by the Free Software Foundation; either
96
97 version 2.1 of the License, or (at your option) any later version.
98
99 This library is distributed in the hope that it will be useful,
100
101 but WITHOUT ANY WARRANTY; without even the implied warranty of
102
103 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
104
105 Lesser General Public License for more details.
106
107 You should have received a copy of the GNU Lesser General Public
108
109 License along with this library; if not, write to the Free Software
110
111 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
112
113*/

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.