Skip to content

Commit

Permalink
Switched transport to ArduinoBLE library instead of previously used E…
Browse files Browse the repository at this point in the history
…SP-32 specific code
  • Loading branch information
the-3d6 committed Aug 19, 2023
1 parent 0e0c6ea commit a6562a0
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 170 deletions.
Empty file modified LICENSE.txt
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions README.adoc
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= uMyo_BLE Library for ESP32 =
= uMyo_BLE Library for nRF52x and ESP32 based Arduinos =

This library allows to receive data from uMyo devices using BLE This library allows to receive data from uMyo devices using BLE enabled Arduinos (both nRF52x and ESP32 based), providing muscle activity readings, roll, pitch, yaw angles and 4-bins spectrum

This library allows to receive data from uMyo devices using BLE mode of ESP32 radio,
providing muscle activity readings, roll, pitch, yaw angles and 4-bins spectrum

== License ==

Expand Down
2 changes: 1 addition & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# uMyo Library for Arduino

This library allows to receive data from uMyo devices using BLE mode of ESP32 radio,
This library allows to receive data from uMyo devices using BLE enabled Arduinos (both nRF52x and ESP32 based),
providing muscle activity readings, roll, pitch, yaw angles and 4-bins spectrum

# License
Expand Down
5 changes: 3 additions & 2 deletions examples/umyo_IMU/umyo_IMU.ino
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Obtaining orientation data from uMyo via BLE on ESP32
Usage: run the code on ESP32 device
Obtaining orientation data from uMyo via BLE on various Arduino boards
Usage: install ArduinoBLE library, run the code on BLE enabled Arduino (nRF52 or ESP32 core)
- open Serial Plotter (or Serial Monitor) at 115200 speed
- turn on uMyo device
- switch it into BLE mode if necessary (to switch, press button once or twice, depending on current mode)
Expand All @@ -16,6 +16,7 @@ void setup() {

void loop()
{
uMyo.run();
int dev_count = uMyo.getDeviceCount(); //if more than one device is present, show all of them
for(int d = 0; d < dev_count; d++)
{
Expand Down
5 changes: 3 additions & 2 deletions examples/umyo_LED_strip/umyo_LED_strip.ino
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Obtaining muscle data from uMyo via BLE on ESP32 and showing
activity level on a connected WS2812 LED strip
Obtaining muscle data from uMyo via BLE on various Arduino boards
and showing activity level on a connected WS2812 LED strip
*/

#include <uMyo_BLE.h>
Expand All @@ -22,6 +22,7 @@ void setup() {

void loop()
{
uMyo.run();
int dev_count = uMyo.getDeviceCount(); //if more than one device is present, show all of them
float muscle_level = 0;
if(dev_count > 0) muscle_level = uMyo.getMuscleLevel(0); //take data from the 1st connected device if more than 1 is connected
Expand Down
5 changes: 3 additions & 2 deletions examples/umyo_basic/umyo_basic.ino
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Obtaining muscle data from uMyo via BLE on ESP32
Usage: run the code on ESP32 device
Obtaining muscle data from uMyo via BLE on various Arduino boards
Usage: install ArduinoBLE library, run the code on BLE enabled Arduino (nRF52 or ESP32 core)
- open Serial Plotter (or Serial Monitor) at 115200 speed
- turn on uMyo device
- switch it into BLE mode if necessary (to switch, press button once or twice, depending on current mode)
Expand All @@ -16,6 +16,7 @@ void setup() {

void loop()
{
uMyo.run();
int dev_count = uMyo.getDeviceCount(); //if more than one device is present, show all of them
for(int d = 0; d < dev_count; d++)
{
Expand Down
Empty file modified keywords.txt
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions library.properties
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=uMyo_BLE
version=1.0.2
version=1.1.0
author=the_3d6 from Ultimate Robotics
category=Sensors
maintainer=hi@ultimaterobotics.com.ua
sentence=Allows to get data from uMyo devices using ESP32 device via BLE.
sentence=Allows to get data from uMyo devices using BLE-enabled Arduinos (nRF52x and ESP32).
paragraph=Receives data from uMyo devices using BLE radio and makes them available
url=https://github.com/ultimaterobotics/uMyo_BLE
architectures=esp32
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32,mbed_giga,renesas,renesas_portenta
Empty file modified src/quat_math.cpp
100644 → 100755
Empty file.
Empty file modified src/quat_math.h
100644 → 100755
Empty file.
211 changes: 65 additions & 146 deletions src/uMyo_BLE.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
uMyo_BLE.cpp
Copyright (c) 2022, Ultimate Robotics
Copyright (c) 2023, Ultimate Robotics
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -19,127 +19,71 @@
*/
#include "uMyo_BLE.h"

int uMyo_BLE_::scanTime = 5;
BLEScan* uMyo_BLE_::pBLEScan = NULL;

class UMyoAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
uint8_t *pack = advertisedDevice.getPayload();
int len = advertisedDevice.getPayloadLength();
int pp = 0;
while(pp < len-5)
{
pp++;
if(pack[pp] != 8) continue;
if(pack[pp+1] != 'u') continue;
if(pack[pp+2] != 'M') continue;
if(pack[pp+3] != 'y') continue;
if(pack[pp+4] != 'o') continue;
while(pack[pp] != 255 && pp < len) pp++;
if(pp >= len) return;
pp++;

int adc_id = pack[pp++];
int batt_level = pack[pp++];
int16_t sp0 = pack[pp++]<<8; //component 0 is sent at reduced precision
int muscle_avg = pack[pp++];
int16_t sp1 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp2 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp3 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qw = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qx = (pack[pp++]<<8);
int16_t qy = (pack[pp++]<<8);
int16_t qz = (pack[pp++]<<8);
sQ Qsg;
Qsg.w = qw;
Qsg.x = qx;
Qsg.y = qy;
Qsg.z = qz;
q_renorm(&Qsg);

uint8_t *mac = *advertisedDevice.getAddress().getNative();
uint32_t unit_id = (mac[1]<<24) | (mac[2]<<16) | (mac[3]<<8) | mac[4];
uMyo_data *device = uMyo.getDeviceByID(unit_id);
device->last_data_id = adc_id;
device->batt_mv = 2000 + batt_level*10;
device->device_avg_muscle_level = (muscle_avg*muscle_avg)>>3;
device->cur_spectrum[0] = sp0;
device->cur_spectrum[1] = sp1;
device->cur_spectrum[2] = sp2;
device->cur_spectrum[3] = sp3;
device->Qsg = Qsg;

}
return;
}
};

//'void (* (*)(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*))(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*)' to 'esp_gap_ble_cb_t' {aka 'void (*)(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*)'} [-fpermissive]

void scan_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void bleCentralDiscoverHandler(BLEDevice peripheral)
{
Serial.println("evt");
if(event == ESP_GAP_BLE_SCAN_RESULT_EVT)
uint8_t pack[40];
int len = peripheral.advertisementData(pack, 40);
if(len < 20) return;
int pp = 0;
while(pp < len-5)
{
Serial.println("scan res");
esp_ble_gap_cb_param_t::ble_scan_result_evt_param *par = (esp_ble_gap_cb_param_t::ble_scan_result_evt_param *)param;
uint8_t *pack = par->ble_adv;
int len = par->adv_data_len;
int pp = 0;
while(pp < len-5)
pp++;
if(pack[pp] != 8) continue;
if(pack[pp+1] != 'u') continue;
if(pack[pp+2] != 'M') continue;
if(pack[pp+3] != 'y') continue;
if(pack[pp+4] != 'o') continue;
while(pack[pp] != 255 && pp < len) pp++;
if(pp >= len) return;
pp++;

int adc_id = pack[pp++];
int batt_level = pack[pp++];
int16_t sp0 = pack[pp++]<<8; //component 0 is sent at reduced precision
int muscle_avg = pack[pp++];
int16_t sp1 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp2 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp3 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qw = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qx = (pack[pp++]<<8);
int16_t qy = (pack[pp++]<<8);
int16_t qz = (pack[pp++]<<8);
sQ Qsg;
Qsg.w = qw;
Qsg.x = qx;
Qsg.y = qy;
Qsg.z = qz;
q_renorm(&Qsg);


String address = peripheral.address(); //can only get string in AA:BB:CC:DD:EE:FF format
const char *addr = address.c_str();
uint8_t mac[6];
for(int n = 0; n < 6; n++)
{
pp++;
if(pack[pp] != 8) continue;
if(pack[pp+1] != 'u') continue;
if(pack[pp+2] != 'M') continue;
if(pack[pp+3] != 'y') continue;
if(pack[pp+4] != 'o') continue;
while(pack[pp] != 255 && pp < len) pp++;
if(pp >= len) return;
pp++;

int adc_id = pack[pp++];
int batt_level = pack[pp++];
int16_t sp0 = pack[pp++]<<8; //component 0 is sent at reduced precision
int muscle_avg = pack[pp++];
int16_t sp1 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp2 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t sp3 = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qw = (pack[pp]<<8) | pack[pp+1]; pp += 2;
int16_t qx = (pack[pp++]<<8);
int16_t qy = (pack[pp++]<<8);
int16_t qz = (pack[pp++]<<8);
sQ Qsg;
Qsg.w = qw;
Qsg.x = qx;
Qsg.y = qy;
Qsg.z = qz;
q_renorm(&Qsg);

uint8_t *mac = par->bda;
uint32_t unit_id = (mac[1]<<24) | (mac[2]<<16) | (mac[3]<<8) | mac[4];
uMyo_data *device = uMyo.getDeviceByID(unit_id);
device->last_data_id = adc_id;
device->batt_mv = 2000 + batt_level*10;
device->device_avg_muscle_level = (muscle_avg*muscle_avg)>>3;
device->cur_spectrum[0] = sp0;
device->cur_spectrum[1] = sp1;
device->cur_spectrum[2] = sp2;
device->cur_spectrum[3] = sp3;
device->Qsg = Qsg;

mac[n] = 0;
for(int p = 0; p < 2; p++)
{
mac[n]<<=4;
char c = addr[n*3+p];
if(c >= '0' && c <= '9') mac[n] += c - '0';
if(c >= 'a' && c <= 'f') mac[n] += c + 10 - 'a';
if(c >= 'A' && c <= 'F') mac[n] += c + 10 - 'A';
}
}
return;
uint32_t unit_id = (mac[1]<<24) | (mac[2]<<16) | (mac[3]<<8) | mac[4];
uMyo_data *device = uMyo.getDeviceByID(unit_id);
device->last_data_id = adc_id;
device->batt_mv = 2000 + batt_level*10;
device->device_avg_muscle_level = (muscle_avg*muscle_avg)>>3;
device->cur_spectrum[0] = sp0;
device->cur_spectrum[1] = sp1;
device->cur_spectrum[2] = sp2;
device->cur_spectrum[3] = sp3;
device->Qsg = Qsg;

}
}

void uMyo_BLE_::rescan(BLEScanResults res)
{
pBLEScan->clearResults();
if(scanTime > 0) pBLEScan->start(scanTime, uMyo_BLE_::rescan, true);
return;
}

uMyo_BLE_::uMyo_BLE_()
Expand All @@ -151,39 +95,14 @@ uMyo_BLE_::uMyo_BLE_()
}
void uMyo_BLE_::begin()
{
/* scanTime = 85; //seconds
memset(&m_scan_params, 0, sizeof(m_scan_params)); // Initialize all params
m_scan_params.scan_type = BLE_SCAN_TYPE_ACTIVE; // Default is a passive scan.
m_scan_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
m_scan_params.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL;
m_scan_params.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE;
m_scan_params.scan_interval = 16; //10 ms
m_scan_params.scan_window = 16;
esp_ble_gap_set_scan_params(&m_scan_params);
esp_ble_gap_register_callback(&scan_cb);
esp_ble_gap_start_scanning(scanTime);
return;*/

scanTime = 1; //seconds
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new UMyoAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(32);
pBLEScan->setWindow(31); // less or equal setInterval value

pBLEScan->start(scanTime, this->rescan, false);
BLE.begin();
BLE.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler);
BLE.scan(true);
}
void uMyo_BLE_::pause()
void uMyo_BLE_::run()
{
scanTime = 0; //stops rescan
BLE.poll();
}
void uMyo_BLE_::resume()
{
scanTime = 5;
pBLEScan->start(scanTime, this->rescan, true);
}

uint8_t uMyo_BLE_::idToIdx(uint32_t id)
{
uint32_t ms = millis();
Expand Down
13 changes: 2 additions & 11 deletions src/uMyo_BLE.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@

#include <Arduino.h>

#include <BLEAdvertisedDevice.h>
#include <BLEClient.h>
#include <esp_gap_ble_api.h>
#include <ArduinoBLE.h>

#include <BLEDevice.h>
#include "quat_math.h"
#define MAX_UMYO_DEVICES 12

Expand Down Expand Up @@ -60,16 +57,10 @@ class uMyo_BLE_
sV nx, ny, nz;
int device_count;
uint8_t idToIdx(uint32_t id);
static int scanTime;
static BLEScan *pBLEScan;
static void rescan(BLEScanResults res);

esp_ble_scan_params_t m_scan_params;
public:
uMyo_BLE_(void);
void begin();
void pause();
void resume();
void run();
uint8_t getDeviceCount();
int getBattery(uint8_t devidx);
uint32_t getID(uint8_t devidx);
Expand Down

0 comments on commit a6562a0

Please sign in to comment.