Objective
Use of myoelectrical signals in prosthesis has been showing increasingly promising results. A myoelectric signal, also called a motor action potential, is an electrical impulse that produces contraction of muscle fibers in the body. These signals are sent to muscles that control voluntary movements. Electromyography (EMG) measures muscle response or electrical activity in response to a nerve's stimulation of the muscle. This project describes the process of using EMG data from a muscle flex to control a basic prosthetic arm remotely.
The project uses two Curiosity High Pin Count (HPC) Development Boards, each with a PIC16F18875 microcontroller. One of the boards, connected to the EMG sensor, acts as the Master. It reads the raw EMG data from the sensor and processes the signals to achieve a smoother signal, more suitable for embedded control. The master then sends commands based on muscle flex data to a Slave Curiosity HPC Board that receives the commands and controls the motors attached to a prosthetic arm. The result is a prosthetic arm with one control signal that flexes whenever you flex your arm.
This project is built on top of the EMG Signal Processing Module: "EMG Signal Processing Module for Embedded Applications". The module provides the functionality required for processing the raw EMG signals output by the analog pin of the EMG Sensor Module. In addition to development boards and two PIC16F18875 microcontrollers, the project uses an EMG click™ board, 3-connector sensor cable, electrode pads, micro servo motors, and any prosthetic arm prototype or another terminal end device as necessary.
Materials
Hardware Tools
- 2x Curiosity HPC Development Boards
- 2x PIC16F18875
- EMG Click: MIKROE-2621
- EMG Cable: MIKROE-2457
- Sensor Adhesive Electrodes: MIKROE-2456
- Micro Servo(s)
- Prosthetic Prototype
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC8
C Compiler |
| | | | |
MPLAB® Code Configurator
Dynamic Code Generation |
| |
Connection Diagram
HARDWARE SETUP
Procedure
Master Device
The following procedure is for programming the master microcontroller (Curiosity HPC 1):
1
Installation and Setup
You need to have MPLAB® X IDE installed on your computer along with MPLAB XC8 Compiler and MPLAB Code Configurator (MCC). If you haven't used these tools before, the setup is described in more detail here: "Installation and Setup".
2
Load EMG Signal Processing Module
Since this project is built on top of the EMG Processing Module, it is required for it to be loaded first. You can either download the Exercise Project Files or choose to build your project from scratch with the help of the instructions given under the "Procedure" section of the "EMG Signal Processing for Embedded Applications" page.
After you have added the project containing the module files from the link above, you need to add additional code to the main.c file.
There is no need to add peripherals in MCC other than what’s given in the module. Just ensure to include the UART peripheral as indicated in the "Procedure" section.
3
Edit main.c
The module provides all the code for processing EMG signals from the muscle flex into smooth signals. The next step is to include functionality which is twofold:
- Determine an appropriate threshold or cutoff voltage level at which you register a voluntary muscle flex.
- Send appropriate commands to the slave microcontroller (Curiosity HPC 2), which will then control the motor.
In the range of values from ADC, on experimentation with a lot of people and varying flex strengths, we found a great cutoff at the value ‘25’. However, this might change for other situations and persons, and you are encouraged to determine the cutoff that best suits your requirements.
- Double-click on the main.c file to open it up in the editor window.
- Flags are to be added for sending control commands to the slave microcontroller (Curiosity HPC 2). In the main function, you will find the ‘Local Variable Declaration’ section. Add the following code to this section:
- Scroll down within the main() function and locate the variable called result. After the result variable is calculated, functionality must be added to send commands to the slave microcontroller. Add the following code between the lines result = get_moving_average(abs(datapoint - neutral_datapoint)); and sbuf_remove();:
4
Programming the Device
Make sure your project has the programming tool selected and that your development board is connected to power. Click on the Make and Program Device icon. This will build the project and launch the programmer. In the output window, you should see a series of messages and if successful it will end with a Programming/Verify Complete message.
Now the master microcontroller board (Curiosity HPC 1) has been programmed. Make sure that you connect the EMG click to mikroBUS™ Slot 2 as shown in the "Hardware Setup Diagram".
5
Closing your Project
The project can be closed in MPLAB X IDE. The project is saved automatically when it is built, but if any changes to the files or configuration were made, MPLAB X IDE will ask you to save them before the project is closed. The project can be closed under the File > Close Project.
Slave Device
The following is the procedure for programming the slave microcontroller (Curiosity HPC 2).
1
Create New Project
Download the Exercise Project Files or create a new standalone project in MPLAB X for a PIC16F18875. If this is your first time using MPLAB X, follow the instructions in the following page: "Create a Standalone Project".
3
Setup Project Resources
In this project, these peripherals need to be selected:
- System Module
- Interrupt Module
- Pin Module
- PWM6
- TMR2
- EUSART
The System Module, Interrupt Module, and Pin Module are automatically included but the PWM6, TMR2, and EUSART must be selected for the project. The list of peripherals shows up under the "Device Resources" list. Double-click on each to have them added to the project. The result should look like the picture below:
4
System Module Setup
Click on the System Module and the MCC screen shows the 'Easy Setup' menu.
- Select 'HFINTOSC' from the Oscillator selection drop-down.
- Select '2_MHz' from HF Internal Clock selection drop-down.
- Select '1' from the Clock Divider drop-down.
- For the Watchdog Timer Enable Option, select 'WDT Disabled, SWDTEN is ignored'.
- At the bottom "Programming" section, check the box for 'Low-Voltage Programming Enable'.
- On the 'Registers' tab, all registers can be left to their default values.
5
Timer Module Setup
Click on the TMR2 Module and the MCC screen shows the 'Easy Setup' menu.
- Check the 'Enable Timer' option box.
- Select 'FOSC/4' as the Clock Source.
- Choose the Postscaler value as '1:1'.
- Choose the Prescaler value as '1:128'.
- Choose 'Rising Edge' for Polarity.
- Type 20 ms in the Timer Period box and press Enter. The nearest available period will show under 'Actual Period'.
- Do not check the box for 'Enable Timer Interrupt'.
- All other fields can be left to their default values.
6
PWM6 Module Setup
Click on the PWM6 Module and the MCC screen shows the 'Easy Setup' menu.
- Check the box for 'Enable PWM'.
- Choose 'Timer2' from the dropdown for Select a Timer.
- Keep the Duty Cycle at '50%'.
- Choose 'active_hi' for PWM Polarity.
- The PWM now has the following parameters:
- Period: 19.968ms (~20ms)
- Frequency: 50.08Hz (~50Hz)
- Resolution: 8 bits
This project controls a single motor with a single PWM signal. However, you may choose to run multiple motors using multiple PWM signals for your application.
7
UART Module Setup
Click on the EUSART Module and the MCC screen shows the 'Easy Setup' menu.
- Select 'asynchronous mode' from the dropdown.
- Check the box for 'Enable EUSART'.
- Check the box for 'Enable Receive'.
- Set the Baud Rate to '19200'.
- Choose '8-bit' for Transmission and Reception.
- Check the box for 'Enable EUSART Interrupts'.
- Check the box for 'Redirect STDIO to USART'.
- Leave buffer Size as '8' for Transmit and Receive buffers.
- All other fields in 'Registers' tab can be left to their default values.
8
Configure Pin Manager
To assign pins on the microcontroller, open the Pin Manager window at the bottom of the screen. To select a pin, click on the corresponding blue unlocked button, turning it into a green locked symbol. Ensure the pins look as shown in the picture below:
- Choose the 'PDIP40' package from the drop-down menu.
- Module EUSART: Similarly, click on (Port C – Pin 7) for RX and (Port C – Pin 6) for TX.
- Module PWM6: Click on (Port A – Pin 4) for PWM6OUT.
- Pin Module: Click on (Port A – Pin 6) for GPIO Output.
- RESET: Click on (Port E – Pin 3) for MCLR (or leave it if it already shows green and locked).
- Module TMR2: Ensure (Port C – Pin 3) is green for T6IN input.
9
Customize Names for Pins
To easily understand and program, we can have custom names for the pins on the microcontroller that we intend to observe or control. Click on 'Pin Module' in the 'Project Resources' tab on the left while MCC is open.
To rename a pin, click on the 'Custom Name' for the pin and type in the new name. Uncheck the 'Analog' box for GPIO.
Rename the pins according to the following diagram:
Now you can see the names on the Pin Manager: Package View window change as well:
11
Add functionality
The main file needs to be edited to include reading commands from the master and sending control signals to the motor. Double-click on the main.c file to open it up in the editor window.
- The main.c file requires a few lines to be uncommented for global and peripheral interrupts to work. MCC already has the control commands in the default main.c file, but they are commented out with two forward slashes (//). The forward slashes need to be removed to enable these lines of code.
- The following two variables need to be declared right after the System_Initialize() function call:
After adding the two lines of code above, the file should look like this:
- In the while loop, the following code needs to be added to read the commands from the master via UART and then send PWM signals to control the servo motors. For more information on how to program a servo motor, visit the "Controlling an Analog Servo" page.
12
Programming the Device
Make sure your project has the programming tool selected and that your development board is connected to power. Click on the Make and Program Device icon. This builds the project and launches the programmer. In the output window, you will see a series of messages and if successful it will end with a Programming/Verify Complete message.
13
Closing your Project
The project can be closed in MPLAB X IDE. The project is saved automatically when it is built, but if any changes to files or configuration were made, MPLAB X IDE will ask you to save them before the project is closed. The project can be closed under File > Close Project.
The slave microcontroller board (Curiosity HPC 2) has been programmed. Make sure that you make the following connections as shown in the Hardware Setup Diagram:
- Ground wire of the motor to the GND pin on the Curiosity board (ground wire on a micro servo is usually black or brown)
- VDD wire of the motor to the TVDD pin on the Curiosity board (VDD wire on a micro servo is usually red)
- Control wire of the motor to the RA4 pin on the Curiosity board (RA4 is the PWM Out) (control wire is usually orange)
HARDWARE CONNECTIONS
Now both the master and slave microcontroller development boards are programmed and ready for execution.
- Place both the boards in close proximity, preferably next to each other (refer to the "Hardware Setup Diagram").
- Connect USB power to both the boards to turn them on.
- Connect the 3.5 mm phone jack to the EMG sensor module and connect the electrodes to the cable.
- Insert the EMG click module on mikroBUS Slot 2 of Curiosity HPC 1 (Master).
- Make the motor connections from the prosthetic arm to Curiosity HPC 2 (Slave) as given above.
- Use a jumper wire to connect pin RC7 on Board 1 to pin RC6 on Board 2.
- Use another jumper wire to connect pin RC6 on Board 1 to pin RC7 on Board 2. These connections will establish the UART communication between both the boards.
Results
- Prep the skin where you would like to attach the electrodes by wiping it clean and letting the area dry.
- Your forearm is recommended for best results, although any muscle group would give you EMG signals.
- Place the red electrode at the center of the muscle group and blue electrode adjacent to it at the end of the muscle group.
- Place the black electrode (reference electrode) where a bone is prominent (such as the side of your wrist).
- For operation in Mode 0, press the Start button (Switch2 – RC5) on the Curiosity Board 1. The LED should be on.
- Now, flex your muscle and watch the motor turn clockwise, making the prosthetic arm clutch.
- Release your muscle to watch the motor turn counter-clockwise, making the prosthetic arm release.
- For operation in Mode 1, first press the RESET button on the Curiosity Board 1. Remember to reset the microcontroller every time you change the mode of operation.
- Now, press the Mode button (Switch1 – RB4) on the Curiosity Board 1 and then the Start button (Switch2 – RC5). LED D5 should be on indicating Mode 1.
- Now, flex your muscle and watch the motor turn clockwise making the prosthetic arm clutch.
- Flex your muscle again to watch the motor turn counter-clockwise making the prosthetic arm release.
This project shows controlling a single motor with a single PWM signal. However, you may choose to run multiple motors using multiple PWM signals for your application.