Myoware Muscle Sensor EMG Project (IoT Bluetooth Data Logging)

Myoware Muscle Sensor IoT Data Logging

The Myoware Muscle Sensor is used to measure the electrical activity of muscles. The sensor uses differential input at two different locations along the muscle in order to calculate a difference in voltage when the muscle fires.

/media/uploads/kyle207/myoware_image.jpg

The three pads on the Myoware should be attached to electrodes with the two on the body of the sensor representing the differential inputs and the third external pad being ground.

/media/uploads/kyle207/img_6465.jpg

The Myoware outputs an analog signal, and it is possible to read the raw data and a rectified version of the data from the sensor. The sensor needs 3.3 V to operate but can operate at 5 V. Due to the differential aspect of EMG sensors, the actual voltage level obtained from the sensor on it's own is not very useful. In order to become useful, the data must be viewed over time and compared to baseline values, so it is possible to view where spikes are which represent muscle activity. When the muscle is flexing, the voltage measured increases and when the muscle is relaxed, the EMG data is at a baseline. Therefore, in order to create a useful visual interface for the EMG, a graphical representation is necessary. In this project, this is accomplished in two methods. The first method is by plotting it on the uLCD screen through the MBED. The other method implemented takes advantage of the Python Bluefruit LE library (https://learn.adafruit.com/bluefruit-le-python-library/overview) using the Adafruit Bluefruit module connected to the MBED. The MBED gathers the data from the Myoware sensor using the analog input pins and plots it on the uLCD, then sends the data through bluetooth to a computer connected to the bluetooth module and updates a graph in real time.

Components

Setup

Myoware Pin Connections

/media/uploads/kyle207/screen_shot_2017-03-12_at_2.55.47_pm.png

uLCD Pin Connections

/media/uploads/kyle207/screen_shot_2017-03-12_at_3.17.58_pm.png

Pushbutton Pin Connection

/media/uploads/kyle207/screen_shot_2017-03-12_at_3.21.36_pm.png

Bluetooth Bluefruit Module

/media/uploads/kyle207/screen_shot_2017-03-12_at_3.25.56_pm.png

MBED Connections

/media/uploads/kyle207/img_6467.jpg

The project implements a graphing module on the uLCD screen as well as a graphing module through the bluetooth module and an interfacing python script. The Python library used can be found on GitHub (https://github.com/adafruit/Adafruit_Python_BluefruitLE) and requires an interfacing device (such as a Mac or Linux machine) with a bluetooth 4.0 module in order to properly function. Unfortunately, Windows devices are not currently able to run the script as their Bluetooth software has only recently been released. Along with the Adafruit_Python_BluefruitLE library, the Python script implemented also requires the numpy and matplotlib libraries in order to log and plot the data in real time.

Project Steps

When the MBED is powered on, the MBED waits until it receives a signal from python bluetooth program. The python program asks for user input in order to determine whether the user wants to read in the raw data or the rectified data from the sensor. The python program then sends the choice across bluetooth and the MBED program determines which data set to send across bluetooth to the computer. The MBED then plots both sets of data on the uLCD screen, but transfers only one set of data across bluetooth where the python program reads the data and plots the data on a real-time updating graph. This continues until the cutoff button is pressed in which the python program receives a kill-code and performs a soft-shutoff.

Python Bluetooth Functions

Using the Python library, it is possible to search for UART Bluetooth devices, connect to the devices, disconnect from devices, as well as writing and reading to the devices. Using these functions, it is easy to send and receive data to the MBED through the bluetooth module, making it possible to wirelessly communicate and log data. After receiving the data, the data is added to the python graph.

Python Program Running On MAC

Python Shell While Data Logging

/media/uploads/kyle207/screen_shot_2017-03-11_at_4.06.27_pm.png

Python Shell After Emergency Cutoff Button Pressed

/media/uploads/kyle207/screen_shot_2017-03-11_at_4.07.31_pm_1.png

Python Code

Bluetooth Python Myoware Interfacing Code

import time
import Adafruit_BluefruitLE
import numpy
import matplotlib.pyplot as plt
from Adafruit_BluefruitLE.services import UART

def main():
    break1 = 1
    choice = '0'
    while(break1):
        dataType = input('1. Raw Data\n2.Rectified Data\nEnter your choice: ')
        break1 = 0
        if int(dataType) == 1:
            choice = '1'
        elif int(dataType) == 2:
            choice = '2'
        else:
            break1 = 0
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = UART.find_device()
        print device
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect()  # Will time out after 60 seconds, specify timeout_sec parameter
                      # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    try:
        # Wait for service discovery to complete for the UART service.  Will
        # time out after 60 seconds (specify timeout_sec parameter to override).
        print('Discovering services...')
        UART.discover(device)

        # Once service discovery is complete create an instance of the service
        # and start interacting with it.
        uart = UART(device)

        # Write a string to the TX characteristic.
        uart.write(choice)
        print("Start Signal Sent to MBED!")

        # Now wait up to one minute to receive data from the device.
        print('Listening for data...')
        i = 0;
        plt.axis([0, 100, 0, 1])
        hl, = plt.plot([], [])
        plt.ylabel('Magnitude of Force')
        plt.xlabel('time(.1 s)');
        
        while(1):
            received = uart.read(timeout_sec=20)
            if received is not None:
                data = float(received)
                if(data < 0):
                    print('')
                    print('')
                    print('Kill Switch Engaged! Program Stopping!')
                    print('')
                    print('')
                    break
                if((data < 1 and data> 0)or(data == 1 and len(received) == 4)):
                    print('Received: {0}'.format(received))
                    i = i+1
                    if(i>100):
                        plt.axis([i-100, i, 0, 1])
                    
                    hl.set_xdata(numpy.append(hl.get_xdata(), i))
                    hl.set_ydata(numpy.append(hl.get_ydata(), data))
                    plt.pause(.01);
                    uart.write('c')
                    
            else:
                
                # Timeout waiting for data, None is returned.
                print('TIMED OUT! NO DATA RECEIVED FOR EXTENDED PERIOD OF TIME!')
                break;
            
    finally:
        # Make sure device is disconnected on exit.
        device.disconnect()


# Initialize the BLE system.  MUST be called before other BLE calls!
ble.initialize()

# Start the mainloop to process BLE events, and run the provided function in
# a background thread.  When the provided main function stops running, returns
# an integer status code, or throws an error the program will exit.
ble.run_mainloop_with(main)

Project Running


Please log in to post comments.