Gait detection system for exoskeleton control

Dependencies:   Servo mbed

Revision:
0:a40546713f83
Child:
1:3ce5d2797365
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Feb 07 19:19:43 2017 +0000
@@ -0,0 +1,139 @@
+#include "mbed.h"
+#include <stdio.h>
+
+//Initialize pins
+AnalogIn   ain(p16);
+Serial device(p28,p27);
+
+//Variables
+Timer t;
+float gaitPeriod[6];
+int startTime = 0;
+int gaitCounter = 0;
+float gaitPeriodMean = 0.00f;
+float inputReading = 0.00f;
+float percentage = 0.00f;
+
+//function to calculate the mean 
+float mean (float tab[], int size){
+    float sum = 0.00f;
+    for (int i=1;i<size;i++){
+        sum += tab[i];
+    }
+    return sum/(size-1);
+}
+
+int main(void)
+{
+    device.baud(57600);
+
+    while (1) {
+        //Check for Serial Event
+        if (device.readable()){
+            char command = device.getc();
+            /*
+            Menu:
+            Case a: get the mean of gait period over 5 cycles
+            Case b: use the calculated mean to estimate gait 
+                    cycle percentage while walking
+            */
+            switch (command){            
+            case 'a':                              
+                //Get sensor reading
+                inputReading = ain.read();
+                wait(0.5);
+                
+                //Busy waiting for 1st heel strike
+                while (inputReading < 0.20f){
+                    device.printf("n|a");
+                    inputReading = ain.read();
+                    wait(0.5);
+                }
+                t.start();
+                t.reset();
+                gaitCounter = 0;
+                
+                //Get the mean of five gait cycles periods
+                while (gaitCounter<6){  
+                    inputReading = ain.read();
+                    //***This "wait" must be minimal***// 
+                    wait(0.5);         
+                    
+                    //Sensor threshold and time more than 0.8 sec
+                    if((inputReading>0.20f)&&(t.read()>0.8f)){
+                        gaitCounter++;
+                        gaitPeriod[gaitCounter] = t.read_ms();//-startTime;                              
+                        t.reset();//reset timer to zero
+                        device.printf("g|%d\r\n",gaitCounter);
+                        
+                        if (gaitCounter == 5){
+                            //mean =(gaitPeriod[2]+gaitPeriod[3]+gaitPeriod[4]+gaitPeriod[5]+gaitPeriod[6])/5.0f;
+                            gaitPeriodMean = mean(gaitPeriod,6);
+                            //device.printf("Gait period: %1.2f\r\n", mean);
+                            device.printf("m|%1.2f", gaitPeriodMean);
+                            gaitCounter = 100;//get out of While
+                        }                                                          
+                    }
+                }
+            break;
+                        
+            case 'b':
+                t.stop();
+                device.printf("n|b");
+                inputReading = ain.read();
+                wait(0.5);
+                
+                //Busy waiting for the first heel strike to occur
+                while(inputReading<0.20f){
+                    inputReading = ain.read();
+                    wait(0.5);
+                    //device.printf("I am in empty While B\r\n");
+                    }
+      
+                //device.printf("I just get out of Empty While\r\n");
+                
+                //Re-initialize gaitCounter to Zero to start a new cycle
+                percentage = 0.00;
+                t.reset();
+                t.start();
+                
+                while(percentage<1.00){
+                    //device.printf("I am in the Percentage While\r\n");
+                    percentage = t.read_ms()/gaitPeriodMean;  
+                    device.printf("p|%1.2f\r\n",percentage); 
+                    
+                    //***To Do***
+                    //Check if reading the sensor is necessary
+                    //to ensure starting a new cycle
+                    //Real test to determine
+                    
+                    //Check if the cycle is finished                        
+                    if(percentage >= 1.0){                         
+                        //Reset percentage
+                        percentage = 0.00;
+                        t.stop();  
+                        t.reset();
+                        //device.printf("Percentage set to zero,%1.2f\r\n",t.read_ms());  
+                        //Start timer for another gait cycle
+                        t.start();
+                    }
+                    
+                    //Check if the user wants to stop the system  
+                    if (device.readable()){
+                        command = device.getc();
+                        //Go back to the menu
+                        if (command == 'o'){
+                            percentage = 100;
+                            device.printf("o|%1.2f",percentage);
+                            t.stop();t.reset();t.start();
+                        }
+                    }
+                }
+                break;
+                
+            default:
+                device.printf("Waiting for your commad\r\n");
+            }   
+        }
+    }
+}
\ No newline at end of file