AFO Source Code

Arduino motor code
/*
  ME379M_Project.ino
  
  The University of Texas at Austin
  ME379M: Robot Mechanism Design
  Fall 2013
  Group: Blake Anderson, Andrew Sharp, Courtney Shell
  Written By: Blake Anderson
  
  This program for the Arduino Uno is used to control the active response of an ankle orthosis device.
  The program reads potentiometer AND EMG inputs corresponding to the foot's rotational position about the coronal axis.
  A PWM signal is then delivered to a Sabretooth 2x5 motor driver.
  
  INPUTS
    Pin A0: 10kOhm Linear Potentiometer. 0-5V Range. 2.5V at zero ankle rotation.
    Pin A1: 0-9V EMG Sensor. External circuit downscales to 0-5V. 0V at zero ankle rotation.
    
  OUTPUTS
    Pin 10: 500Hz Pulse Width Modulated square wave. 50% duty cycle corresponds to midpoint.
  
*/
/* DEFINE PINS */
const int potInputPin = A0;   // Input pin for the potentiometer
const int emgInputPin = A1;   // Input pin for the EMG
const int motorOutPin = 10;   // Output motor signal
/* DEFINE VARIABLES */
double potValue = 0;   // The ADC value read from the potentiometer (0-255)
double emgValue = 0;   // The ADC value read from the EMG (0-255)
const double setpoint = 127;  // Motor setpoint
const double deadband = 1;    // Setpoint deadband
bool emgEnable = true;  // Enable reading of EMG, enable(1) or disable(0)
bool potEnable = true;  // Enable reading of potentiometer, enable(1) or disable(0)
const double emgThreshold = 25;   // Theshold value in (0-255) for the EMG signal
/* SETUP - RUNS ONCE */
void setup()
{
  // Declare the sensor input
  pinMode(potInputPin, INPUT);
  pinMode(emgInputPin, INPUT);
  
  // Declare the motor control output
  pinMode(motorOutPin, OUTPUT);
}
/* OPERATING LOOP */
void loop()
{
  /* READ INPUTS */
  potValue = analogRead(potInputPin);        // Read potentiometer input (10-bit)
  potValue = map(potValue, 0, 1023, 0, 255); // Downscale to 8-bit
  
  emgValue = analogRead(emgInputPin);        // Read EMG input (10-bit)
  emgValue = map(emgValue, 0, 1023, 0, 255); // Downscale to 8-bit
  
  /* DETERMINE CONTROL SCHEME (EMG or POT) */
  /* EMG CONTROL */
  if ( emgEnable == true && emgValue > emgThreshold )
  {
    emgValue = map(emgValue, emgThreshold, 255, 192, 255);          // Response is linear above the threshold
    analogWrite(motorOutPin, emgValue);
  }
  /* POT CONTROL */
  else if ( potEnable == true )
  {
    if (potValue < setpoint-deadband)
      potValue = map(potValue, 0, setpoint-deadband, 0, 63);        // -12V to -6V
    else if (potValue > setpoint+deadband)
      potValue = map(potValue, setpoint+deadband, 255, 192, 255);   // +6V to +12V
    else
      potValue = setpoint;                                               // Motor stop
    
    analogWrite(motorOutPin, potValue);
  }
  else                                                              // Motor stop
    analogWrite(motorOutPin, setpoint);
}