Close

Step 3 - IMU and BLE functions

A project log for Personal Guardian

A wearable device to allow vulnerable people to maintain their independence, by providing a safety link with a remote carer.

ray-lynchRay Lynch 09/02/2017 at 00:100 Comments

IMU functions

The Arduino 101 Inertial Measurement Unit is fully documented and works well. For the fall detection, the software detects a short period of freefall followed by a shock. Both events are handled via callbacks:

void imuInit(void) {
  // Initialise the IMU
  lcd.setCursor(0,1);
  lcd.print("Starting IMU... ");
  CurieIMU.begin();
  CurieIMU.setAccelerometerRange(4);
  CurieIMU.attachInterrupt(imuCallback);
  // Enable Freefall Detection
  CurieIMU.setDetectionThreshold(CURIE_IMU_FREEFALL,500);
  CurieIMU.setDetectionDuration(CURIE_IMU_FREEFALL,50); //ms
  CurieIMU.interrupts(CURIE_IMU_FREEFALL);
  // Enable Shock Detection
  CurieIMU.setDetectionThreshold(CURIE_IMU_SHOCK,500); //mg
  CurieIMU.setDetectionDuration(CURIE_IMU_SHOCK,20); //ms
  CurieIMU.interrupts(CURIE_IMU_SHOCK);
}
// IMU Callback
static void imuCallback(void) {
  if(CurieIMU.getInterruptStatus(CURIE_IMU_SHOCK)) {
    if(falling) {
      lcd.setRGB(255,0,0);
      fallEvent=true;    
    }
  }
  if(CurieIMU.getInterruptStatus(CURIE_IMU_FREEFALL)) {
    if(!fallEvent) lcd.setRGB(255,128,0);
    falling=true;
    lcdTimeout=fallTimeout=millis();
  }  
}

When the freefall begins, the LCD colour is set to orange. If a shock subsequently happens within a certain timeout period, the LCD colour changes to red and an alarm is triggered. An emergency text message will be sent unless the user presses a button to cancel it. These functions are handled by the main code polling loop.

BLE functions

Implementing the Bluetooth functions involved a bit more effort! The first step was to acquire a BLE heart rate monitor. There is a huge variety available, so it was pot luck whether the one I chose would be easy to use or not. It turned out to be... not.

Bingo M2

The device I got from China via ebay is the Bingo M2. As far as I can tell, this seems to be an Indian copy of the Xiaomi Mi Band 2. The latter seems to be a fairly decent low cost device, whereas the former, even cheaper device has some rather dubious features.

Firstly, when I received the M2 it was completely dead. It comes with a USB adaptor for charging, but plugging it in did nothing. I decided to cut it open and see what was inside.


As well as the circuit board, vibration motor and pulse sensor, it contained a small Li-poly cell that measured about 2V. It seems that at such a low voltage, the charge circuitry doesn't work, which is a bit of a design flaw.

I connected up some AA cells as a rudimentary battery charger (note: use a resistor), and with the cell charged up to 3V it started to work (and the charger subsequently worked as well).


Now came the task of interfacing to it. Most of the BLE examples for the Arduino 101 configure it as a peripheral (providing a service), but in this case I wanted to act as a central (client) device. Luckily there are some examples of how to do this here. With the PeripheralExplorer example, the M2 showed up and announced its services. Success!

Er, not quite. Unfortunately the M2 does not support the standard BLE heart rate service with UUID 0x180D. In fact it has a proprietary, undocumented interface. Although I could figure out how to read the step count from the device, trigger a notification or vibration and even start a heart rate measurement (turning the sensor LEDs on), I couldn't work out how to read the heart rate result.

Looking at the code of the official Droihealth app, it seems that the heart rate result will be provided in a UUID that is not visible in the initial BLE service scan, but only provided after triggering a measurement. I could not find out how to read this new UUID with the Arduino 101 BLE implementation. Eventually I gave up, because in any case the heart rate measurements of this device (when triggered manually) don't seem to correlate in any way with the user's actual heart rate. Whether the heart rate is fast or slow, or the device is sitting on the table, the heart rate always seems to be 89. It might be just my device, or it might be a good idea to try a different device...

So, for now, the software just assumes a heart rate of 80, and the heart rate monitoring feature is not working. The wrist strap is still used to provide vibration notification when a text message is received, and the measurement is triggered by the software periodically (so the sensor LEDs illuminate), but sadly no measurement is forthcoming.

The next step will hopefully be more likely to succeed - the integration of the GPS sensor.

Discussions