element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Design For A Cause 2021
  • Challenges & Projects
  • Design Challenges
  • Design For A Cause 2021
  • More
  • Cancel
Design For A Cause 2021
Blog Trakcore #4 AI Assisted Posture Modification - Enabling IMU and BLE with Edge Impulse ML on Arduino Nano 33 IoT
  • Blog
  • Forum
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
Author: vlasov01
Date Created: 20 Apr 2021 10:50 AM
Views: 190
Likes: 5
Comments: 2
  • android
  • ble
  • nordic
  • imu
  • designforcause
  • designforcause arduino
  • edge impulse
  • arduino nano 33 iot
  • design_for_a_cause_2021
Related
Recommended

Trakcore #4 AI Assisted Posture Modification - Enabling IMU and BLE with Edge Impulse ML on Arduino Nano 33 IoT

vlasov01
vlasov01
20 Apr 2021

Design for A Cause 2021Trakcore #1 AI Assisted Posture Modification - Project Introduction

#1 - Project Introduction

#2 - Acquiring Data and Designing a Model

#3 - Deploying and Testing Edge Impulse ML Model on Arduino Nano 33 IoT

#4 - Enabling IMU and BLE with Edge Impulse ML on Arduino Nano 33 IoT

#5 - Using Cordova Bluetooth (BLE) Plugin to Connect Arduino Nano 33 IoT with Android Phone

#6 - Arduino Nano 33 IoT Wearable Design

#7 - Adding Pedometer to Reduce False Positives

#8 - Refining User Experience AKA Bugs Fix

 

Adding IMU and BLE Libraries

 

MCU needs to provide posture position inference. I've decided to use BLE due to low power consumption. Arduino provides a ready to use BLE library, which supports  Arduino Nano 33 IoT radio.

My project requires to calculate position of the device from built-in accelerometer. Arduino provides a ready to use library for LSM6D53.

Both libraries come with easy to use examples.

Adding IMU and BLE Libraries

Edge Impulse support Arduino Nano 33 BLE and provides examples of using it with BLE and IMU. So I've decided to use this example to accelerate development instead of improving my previous Arduino sketch. I've imported Edge Impulse Arduino Nano 33 BLE accelerometer example.

I've modified code to include LSM6D53 header instead one that comes with the example. As well I've applied the same modifications to the code as I've dome in my previous sketch.

 

Transitioning from phone to Arduino IMU

I've build and deployed my new code. Than I've started testing and my test results where wrong. I've realized that the phone and its accelerometer  orientation during the data collection is not necessary the same as during my testing with MCU.

After some exploration I've adjusted the code to account for this difference. In the line 1 I've changed the order of first two parameters and in line 4 I've added minus sign.

    IMU.readAcceleration(buffer[ix + 1], buffer[ix], buffer[ix + 2]);
    buffer[ix + 0] *= CONVERT_G_TO_MS2;
    buffer[ix + 1] *= CONVERT_G_TO_MS2;
    buffer[ix + 2] *= -CONVERT_G_TO_MS2;

 

After this modification my tests start generating correct results.

Publishing data over BLE

I've created my custom BLE posture service and characteristic. So the posture state can be read by a phone.

// Custom BLE Posture Service
BLEService postureService("b7469627-3ad1-4b96-8684-14d6cb73014a");
// Custom BLE Posture Position Characteristic
BLEUnsignedCharCharacteristic  postureChar("b7469627-3ad1-4b96-8684-14d6cb73014a", // custom 128-bit characteristic UUID
    BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes

 

And I've initialized BLE in the setup() function:

  // Set advertised local name and services UUID
  BLE.setDeviceName("Arduino Nano 33 IoT");
  BLE.setLocalName("Trakcore");
  BLE.setAdvertisedService(postureService); // add the service UUID
  postureService.addCharacteristic(postureChar); // add the posture position characteristic
  BLE.addService(postureService); // Add the posture service
  postureChar.writeValue(oldPosture); // set initial value for this characteristic


  /* Start advertising BLE.  It will start continuously transmitting BLE
     advertising packets and will be visible to remote BLE central devices
     until it receives a new connection */


  // start advertising
  BLE.advertise();

 

And than I've modified the loop() function to publish posture readings:

void loop()
{
  // wait for a BLE central
  BLEDevice central = BLE.central();


  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());




    // check the position every 2000ms
    // while the central is connected:
    while (central.connected()) {
      updatePosture();


      Serial.print("Disconnected from central: ");
      Serial.println(central.address());
      ei_printf("\nStarting inferencing in 2 seconds...\n");
      delay(2000);
    }
  }
}

 

I'm only publishing posture data (line 2) when it changes. As well I've added LED indication as it simpler to find out test results.

  if (posture != oldPosture) {      // if the posture has changed
    postureChar.writeValue(posture);  // and update the battery level characteristic
    if (posture == EI_STRAIGHT)
      digitalWrite(LED_BUILTIN, LOW);
    else
      digitalWrite(LED_BUILTIN, HIGH);
    oldPosture = posture;           // save the level for next comparison
  }

 

Testing BLE functionality on the phone

I've installed nRF Connect for mobile from Nordic on my phone. I've started it and was able to see my device as Trakcore on the scanner screen.

I've select my device and on the client screen I've expanded my custom service.

Now I was able to select my custom characteristic of the posture service.

And I was able to read the posture state as a value (0-curved, 1-straight) of my characteristic:

 

Summary and next steps

I'm quite happy with the development progress. The next step for me is to build an app which will read data from MCU and will raise alerts if posture is curved.

 

Thank you for reading my project blog post!

 

References

https://www.element14.com/community/community/design-challenges/design-for-a-cause-2021/blog/2021/03/28/venttracker-04-playing-with-the-imu

https://www.element14.com/community/docs/DOC-95660/l/the-learning-circuit-79-how-accelerometers-work

https://www.arduino.cc/en/Reference/ArduinoBLE

https://www.bluetooth.com/specifications/assigned-numbers/

https://www.novelbits.io/uuid-for-custom-services-and-characteristics/

https://github.com/arduino-libraries/ArduinoBLE/blob/master/examples/Peripheral/BatteryMonitor/BatteryMonitor.ino

Anonymous
Parents
  • DAB
    DAB over 1 year ago

    Looks like you need to have a lot of detail about setting up the interfaces.

     

    DAB

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Comment
  • DAB
    DAB over 1 year ago

    Looks like you need to have a lot of detail about setting up the interfaces.

     

    DAB

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Children
  • vlasov01
    vlasov01 over 1 year ago in reply to DAB

    You are right. Arduino Nano 33 BLE supports by Edge Impuls, but Arduino Nano 33 IoT is not. But it is not so bad. Especially if you compare to any other option to perform ML on MCU. It was very painful to perform MLeven on RPi not so long ago.

     

    Sergey

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube