# Measure shock size with The CurieIMU library

Hello guys,
I’m currently working on a project where I would like to detect a shock everytime I take a step with a crutch, and more specific output the “size” of the shock, so I can compare the shock/vibrations when walking on different surrfaces with the crutch.

I’m using the CurielIMU library, together wih an Arduino 101 which is attached to the crutch, to detect the shock everytime I take a step, and it works really well. However, I’m unsure on how to measure and output the “size” of the each vibration/shock. Right now I meassure the acceleration and rotation of the Arduino in x,y,z space by running the CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz); in the loop. Every time a shock is detected, the measured ax, ay and az is printed to the serial.monitor.

It’s possible to set the Shock threshhold in “mg” and I guess this measured “mg” is what I’m looking for. But the values I get from ax, ay and az when a shock is detected, dosen’t really make sense to me:
The following code is 4 readings from tapping the crutch soft into a pillow
acc: 1.58 -2170 10123
acc: 1.74 -1365 11205
acc: 1.59 -1671 11419
acc: 1.99 -1017 10117

And the next 4 readings is from harder taps into a wooden floor.
acc: 1.67 -6678 10631
acc: 1.83 -1487 167
acc: 1.80 -4050 8429
acc: 1.93 5119 -4129

I can't really se a relation to my movement and the outputs, so if I would like to output the "shock size” which might be the mg value compared to the treshhold, how does I measure that and output it correctly?

here is my code.

``````#include "CurieIMU.h"

boolean blinkState = false;          // state of the LED

int ax, ay, az;
int gx, gy, gz;  // not used
float aix, aiy, aiz;
int calibrateOffsets = 1;

void setup() {
Serial.begin(9600); // initialize Serial communication
while(!Serial) ;    // wait for serial port to connect..

/* Initialise the IMU */
CurieIMU.begin();
CurieIMU.attachInterrupt(eventCallback);

// use the code below to calibrate acc offset values
if (calibrateOffsets == 1) {
Serial.println("Internal sensor offsets BEFORE calibration...");
Serial.print( CurieIMU.getAccelerometerOffset(X_AXIS));
Serial.print("\t"); // -76
Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS));
Serial.print("\t"); // -235
Serial.println(CurieIMU.getAccelerometerOffset(Z_AXIS));

CurieIMU.setAccelerometerOffset(X_AXIS,495.3);
CurieIMU.setAccelerometerOffset(Y_AXIS,-15.6);
CurieIMU.setAccelerometerOffset(Z_AXIS,491.4);
}

/* Enable Shock Detection */
CurieIMU.setDetectionThreshold(CURIE_IMU_SHOCK, 1500); // 1.5g = 1500 mg
CurieIMU.setDetectionDuration(CURIE_IMU_SHOCK, 500);   // 50ms
CurieIMU.interrupts(CURIE_IMU_SHOCK);

Serial.println("IMU initialisation complete, waiting for events...");

}

void loop() {

// measure raw acceleration.
CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz); //the gyroscope measurements aren't used.
// concert raw data to mg
aix = convertRawAcceleration(ax);
aiy = convertRawAcceleration(ay);
aiz = convertRawAcceleration(az);

delay(1);
}

static void eventCallback(void)
{
if (CurieIMU.getInterruptStatus(CURIE_IMU_SHOCK)) {

Serial.print("a/g:\t");
Serial.print(aix);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.println(az);

if (CurieIMU.shockDetected(X_AXIS, POSITIVE))
Serial.println("Negative shock detected on X-axis");

if (CurieIMU.shockDetected(X_AXIS, NEGATIVE))
Serial.println("Positive shock detected on X-axis");

if (CurieIMU.shockDetected(Y_AXIS, POSITIVE))
Serial.println("Negative shock detected on Y-axis");

if (CurieIMU.shockDetected(Y_AXIS, NEGATIVE))
Serial.println("Positive shock detected on Y-axis");

if (CurieIMU.shockDetected(Z_AXIS, POSITIVE))
Serial.println("Negative shock detected on Z-axis");

if (CurieIMU.shockDetected(Z_AXIS, NEGATIVE))
Serial.println("Positive shock detected on Z-axis");
}
}

float convertRawAcceleration(int aRaw) {
float a = (aRaw * 2.0) / -32768.0;
return a;
}
``````

I suggest to not use the library for shock detection until you have a better understanding of what the measurements mean.

Try measuring accelerations as rapidly as you can, printing (or better, plotting) them out and learn to understand what they report during a "shock".

If printing, keep in mind that printing is slow, so use the highest baud rate possible on the serial monitor.

Incidentally there is a problem in the way that your program prints the x versus y and z accelerations.