to send emg signal to motor with test programm

Dependencies:   HIDScope biquadFilter mbed

Fork of TestProgramm by Roy Theussing

Committer:
Roytsg
Date:
Thu Oct 26 07:49:21 2017 +0000
Revision:
32:3832f732f17a
Parent:
30:4c6321941515
Child:
33:84d986230e15
EMG Signaal werkt ligt mooi tussen de 0 en 1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
Roytsg 21:77998ce2c0dd 3 #include "BiQuad.h"
Roytsg 28:4b22455930ff 4 #include "math.h"
Roytsg 24:fe3825337233 5
vsluiter 4:8b298dfada81 6 //Define objects
Roytsg 28:4b22455930ff 7 AnalogIn emg( A0 ); //EMG
Roytsg 28:4b22455930ff 8 AnalogIn emg1( A1 ); //EMG
Roytsg 29:a48b63e60a40 9 HIDScope scope( 6 ); // aantal scopes dat gemaakt kan worden
Roytsg 28:4b22455930ff 10 DigitalOut ledB(LED_BLUE);
Roytsg 28:4b22455930ff 11 DigitalOut ledG(LED_GREEN);
Roytsg 28:4b22455930ff 12 DigitalIn TestButton(PTA4); // button naast het ledje
Roytsg 28:4b22455930ff 13 DigitalIn onoff(PTC6); // button aan de andere kant
Roytsg 28:4b22455930ff 14 Ticker emgSampleTicker; // Ticker voor de sample frequency
Roytsg 29:a48b63e60a40 15 DigitalOut motor2MagnitudePin(D5); // magnitude motor 2
Roytsg 29:a48b63e60a40 16 DigitalOut motor2DirectionPin(D4); // direction of the motor 2
Roytsg 29:a48b63e60a40 17 InterruptIn MotorOn(D10);
vsluiter 2:e314bb3b2d99 18
Roytsg 28:4b22455930ff 19
Roytsg 28:4b22455930ff 20 int P= 200; // aantal test punten voor de moving average
Roytsg 28:4b22455930ff 21 double A[200]; // de vector waar punten in worden opgeslagen voor de moving average moet even groot zijn als P
Roytsg 28:4b22455930ff 22 int k = 0; // counter voor de configuratie
Roytsg 28:4b22455930ff 23 double Vvector[200]; // vector voor de Vwaarde configuratie
Roytsg 28:4b22455930ff 24 double Vwaarde[2]; // vector voor waardes van V
Roytsg 28:4b22455930ff 25 int x = 0; // x waarde voor de Vwaarde
Roytsg 29:a48b63e60a40 26 double movmean;
Roytsg 30:4c6321941515 27 int MotorLock = 0;
Roytsg 30:4c6321941515 28 double EMGInput;
Roytsg 32:3832f732f17a 29 float ErrorZero = 3;
Roytsg 24:fe3825337233 30
Roytsg 28:4b22455930ff 31 // Filters
Roytsg 28:4b22455930ff 32 BiQuadChain bqc;
Roytsg 28:4b22455930ff 33 BiQuad bq1( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265); //lp?
Roytsg 28:4b22455930ff 34 BiQuad bq2( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266); //hp?
Roytsg 28:4b22455930ff 35 BiQuad bq3( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266); // notch?
vsluiter 0:32bb76391d89 36
Roytsg 28:4b22455930ff 37
Roytsg 21:77998ce2c0dd 38
Roytsg 28:4b22455930ff 39 // sample function voor plotten van de emg signalen en moving average
Roytsg 21:77998ce2c0dd 40 void emgSample() {
Roytsg 21:77998ce2c0dd 41
Roytsg 28:4b22455930ff 42 double emgFiltered = bqc.step( emg.read() ); // gefilterde waarde van het emg signaal
Roytsg 28:4b22455930ff 43 double emgabs = abs(emgFiltered); // absolute waarde van het gefilterde signaal
Roytsg 28:4b22455930ff 44 scope.set(0, emgFiltered ); // stuurt de waarden naar de grafiek
Roytsg 28:4b22455930ff 45 scope.set(1, emgabs ); // stuurt de waarden naar de grafiek
Roytsg 32:3832f732f17a 46 Vwaarde[0] = 0.01;
Roytsg 24:fe3825337233 47
Roytsg 28:4b22455930ff 48 // deze for-loop maakt de vector voor de moving average
Roytsg 27:674193a62e06 49 for(int i = P-1; i >= 0; i--){
Roytsg 26:97a8adc9b895 50 if (i == 0) {
Roytsg 26:97a8adc9b895 51 A[i] = emgabs;
Roytsg 26:97a8adc9b895 52 }
Roytsg 26:97a8adc9b895 53 else {
Roytsg 26:97a8adc9b895 54 A[i] = A[i-1];
Roytsg 26:97a8adc9b895 55 }
Roytsg 28:4b22455930ff 56 }
Roytsg 26:97a8adc9b895 57 double sum = 0;
Roytsg 28:4b22455930ff 58 // deze for-loop sommeert de array
Roytsg 27:674193a62e06 59 for (int n = 0; n < P-1; n++) {
Roytsg 26:97a8adc9b895 60 sum = sum + A[n];
Roytsg 26:97a8adc9b895 61 }
Roytsg 24:fe3825337233 62
Roytsg 29:a48b63e60a40 63 movmean = sum/P; //dit is de moving average waarde
Roytsg 24:fe3825337233 64
Roytsg 28:4b22455930ff 65 // hier wordt het test programma opgestart, zodat zero waarde kan worden gekregen
Roytsg 28:4b22455930ff 66 if (TestButton==0 & k<200) {
Roytsg 28:4b22455930ff 67 Vvector[k] = movmean;
Roytsg 28:4b22455930ff 68 ledB = !ledB;
Roytsg 28:4b22455930ff 69 k++;
Roytsg 28:4b22455930ff 70 }
Roytsg 28:4b22455930ff 71 else if (k==200) { // hier moet de test klaar zijn
Roytsg 28:4b22455930ff 72 double sumZ = 0;
Roytsg 28:4b22455930ff 73 for (int n = 0; n < 199; n++) {
Roytsg 28:4b22455930ff 74 sumZ = sumZ + Vvector[n];
Roytsg 28:4b22455930ff 75 } // neemt de som van de zerovector array
Roytsg 28:4b22455930ff 76 Vwaarde[x] = sumZ/200; // dit is het gemiddelde voor een betrouwbare value
Roytsg 28:4b22455930ff 77 scope.set(3,Vwaarde[0]); //stuurt de zeroV waarde naar het plotje
Roytsg 28:4b22455930ff 78 if (x == 1) {
Roytsg 28:4b22455930ff 79 scope.set(4,Vwaarde[1]); //stuurt de maxV waarde naar het plotje
Roytsg 28:4b22455930ff 80 }
Roytsg 28:4b22455930ff 81 k++;
Roytsg 28:4b22455930ff 82 ledB = 1;
Roytsg 28:4b22455930ff 83 ledG = !ledG;
Roytsg 28:4b22455930ff 84 }
Roytsg 28:4b22455930ff 85 else if (k == 201 && onoff ==0) {// dit is om het ledje uit te doen en om het mogelijk te maken de test opnieuw te doen
Roytsg 28:4b22455930ff 86 ledG = !ledG;
Roytsg 28:4b22455930ff 87 k = 0;
Roytsg 28:4b22455930ff 88 if (x==0) {
Roytsg 28:4b22455930ff 89 x++;
Roytsg 28:4b22455930ff 90 }
Roytsg 28:4b22455930ff 91 else if (x==1) {
Roytsg 28:4b22455930ff 92 x=0;
Roytsg 28:4b22455930ff 93 }
Roytsg 28:4b22455930ff 94 }
Roytsg 32:3832f732f17a 95 scope.set(2, movmean); // stuurt de moving average naar de plot
Roytsg 32:3832f732f17a 96
Roytsg 32:3832f732f17a 97 if (movmean > Vwaarde[1]) {
Roytsg 32:3832f732f17a 98 movmean = Vwaarde[1]; // zorgt ervoor dat emg nooit > 1 kan zijn
Roytsg 32:3832f732f17a 99 }
Roytsg 32:3832f732f17a 100
Roytsg 30:4c6321941515 101 double EMGInputRaw = (movmean - Vwaarde[0]*ErrorZero)/(Vwaarde[1] - Vwaarde[0]*ErrorZero);
Roytsg 30:4c6321941515 102 if (EMGInputRaw < 0) {
Roytsg 30:4c6321941515 103 EMGInput = 0;
Roytsg 30:4c6321941515 104 }
Roytsg 30:4c6321941515 105 else {
Roytsg 30:4c6321941515 106 EMGInput = EMGInputRaw;
Roytsg 30:4c6321941515 107 }
Roytsg 29:a48b63e60a40 108 scope.set(5,EMGInput);
Roytsg 32:3832f732f17a 109
Roytsg 28:4b22455930ff 110
Roytsg 21:77998ce2c0dd 111 scope.send();
Roytsg 21:77998ce2c0dd 112 }
Roytsg 21:77998ce2c0dd 113
Roytsg 29:a48b63e60a40 114 float GetReferenceVelocity()
Roytsg 29:a48b63e60a40 115 {
Roytsg 29:a48b63e60a40 116 // Returns reference velocity in rad/s.
Roytsg 29:a48b63e60a40 117 // Positive value means clockwise rotation.
Roytsg 29:a48b63e60a40 118 const float maxVelocity=8.4; // in rad/s of course!
Roytsg 29:a48b63e60a40 119 float referenceVelocity; // in rad/s
Roytsg 30:4c6321941515 120 referenceVelocity = (EMGInput * maxVelocity) * MotorLock;
Roytsg 29:a48b63e60a40 121 return referenceVelocity;
Roytsg 29:a48b63e60a40 122 }
Roytsg 21:77998ce2c0dd 123
Roytsg 29:a48b63e60a40 124 void SetMotor2(float motorValue)
Roytsg 29:a48b63e60a40 125 {
Roytsg 29:a48b63e60a40 126 // Given -1<=motorValue<=1, this sets the PWM and direction
Roytsg 29:a48b63e60a40 127 // bits for motor 1. Positive value makes motor rotating
Roytsg 29:a48b63e60a40 128 // clockwise. motorValues outside range are truncated to
Roytsg 29:a48b63e60a40 129 // within range
Roytsg 29:a48b63e60a40 130 if (fabs(motorValue)>1) motor2MagnitudePin = 1;
Roytsg 29:a48b63e60a40 131 else motor2MagnitudePin = fabs(motorValue);
Roytsg 29:a48b63e60a40 132 }
Roytsg 29:a48b63e60a40 133 float FeedForwardControl(float referenceVelocity) {
Roytsg 29:a48b63e60a40 134 // very simple linear feed-forward control
Roytsg 29:a48b63e60a40 135 const float MotorGain=8.4; // unit: (rad/s) / PWM
Roytsg 29:a48b63e60a40 136 float motorValue = referenceVelocity / MotorGain;
Roytsg 29:a48b63e60a40 137 return motorValue;
Roytsg 29:a48b63e60a40 138 }
Roytsg 29:a48b63e60a40 139
Roytsg 29:a48b63e60a40 140 void MotorLocker() {
Roytsg 29:a48b63e60a40 141 if (MotorLock == 0) {
Roytsg 29:a48b63e60a40 142 MotorLock = 1;
Roytsg 29:a48b63e60a40 143 }
Roytsg 29:a48b63e60a40 144 else if (MotorLock == 1) {
Roytsg 29:a48b63e60a40 145 MotorLock = 0;
Roytsg 29:a48b63e60a40 146 }
Roytsg 29:a48b63e60a40 147 }
Roytsg 28:4b22455930ff 148
vsluiter 0:32bb76391d89 149 int main()
Roytsg 28:4b22455930ff 150 {
Roytsg 28:4b22455930ff 151 ledB = 1;
Roytsg 28:4b22455930ff 152 ledG = 1;
Roytsg 28:4b22455930ff 153 bqc.add( &bq1 ).add( &bq2 ).add( &bq3 ); // hier wordt het filter gemaakt
Roytsg 28:4b22455930ff 154 emgSampleTicker.attach( &emgSample, 0.002 ); //dit bepaald de sample frequency en is nu 500 Hz
Roytsg 29:a48b63e60a40 155
Roytsg 28:4b22455930ff 156
Roytsg 29:a48b63e60a40 157 while(1) {
Roytsg 29:a48b63e60a40 158 MotorOn.rise(&MotorLocker);
Roytsg 29:a48b63e60a40 159 motor2DirectionPin = 1;
Roytsg 29:a48b63e60a40 160 SetMotor2(FeedForwardControl(GetReferenceVelocity()));
Roytsg 29:a48b63e60a40 161 }
vsluiter 0:32bb76391d89 162 }