Overzetten

Dependencies:   HIDScope MODSERIAL mbed

Committer:
ThomBMT
Date:
Fri Oct 12 09:27:32 2018 +0000
Revision:
9:836d6ac23481
Parent:
8:81f2c8ae4427
Werkend met EMG;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomBMT 0:877f950fdfb5 1 #include "mbed.h"
ThomBMT 0:877f950fdfb5 2 #include "MODSERIAL.h"
ThomBMT 0:877f950fdfb5 3 #include "HIDScope.h"
ThomBMT 0:877f950fdfb5 4 MODSERIAL pc(USBTX, USBRX);
ThomBMT 0:877f950fdfb5 5
ThomBMT 0:877f950fdfb5 6 DigitalOut DirectionPin1(D4);
ThomBMT 0:877f950fdfb5 7 PwmOut PwmPin1(D5);
ThomBMT 0:877f950fdfb5 8 DigitalOut DirectionPin2(D7);
ThomBMT 0:877f950fdfb5 9 PwmOut PwmPin2(D6);
ThomBMT 0:877f950fdfb5 10 DigitalIn Knop1(D3);
ThomBMT 0:877f950fdfb5 11 DigitalIn Knop2(D2);
ThomBMT 0:877f950fdfb5 12 DigitalIn Knop3(PTA4);
ThomBMT 0:877f950fdfb5 13 DigitalIn Knop4(PTC6);
ThomBMT 6:bbef54156775 14 AnalogIn emg0( A0 );
ThomBMT 6:bbef54156775 15 AnalogIn emg1( A1 );
ThomBMT 6:bbef54156775 16
ThomBMT 6:bbef54156775 17 Ticker sample_timer;
ThomBMT 6:bbef54156775 18 HIDScope scope( 2 );
ThomBMT 6:bbef54156775 19 DigitalOut led(LED1);
ThomBMT 6:bbef54156775 20
ThomBMT 8:81f2c8ae4427 21 Ticker EMG_Read_Ticker;
ThomBMT 8:81f2c8ae4427 22
ThomBMT 7:d57eba2aed38 23 volatile float Bicep_Right = 0.0;
ThomBMT 7:d57eba2aed38 24
ThomBMT 7:d57eba2aed38 25 void EMG_Read()
ThomBMT 7:d57eba2aed38 26 {
ThomBMT 7:d57eba2aed38 27 Bicep_Right = emg0.read();
ThomBMT 7:d57eba2aed38 28 pc.printf("%f ", Bicep_Right);
ThomBMT 7:d57eba2aed38 29 }
ThomBMT 6:bbef54156775 30
ThomBMT 6:bbef54156775 31 void sample()
ThomBMT 6:bbef54156775 32 {
ThomBMT 6:bbef54156775 33
ThomBMT 6:bbef54156775 34 scope.set(0, emg0.read() );
ThomBMT 6:bbef54156775 35 scope.set(1, emg1.read() );
ThomBMT 6:bbef54156775 36
ThomBMT 6:bbef54156775 37 scope.send();
ThomBMT 6:bbef54156775 38 led = !led;
ThomBMT 6:bbef54156775 39 }
ThomBMT 0:877f950fdfb5 40
ThomBMT 4:a1691164d879 41 int Turn_Motor1()
ThomBMT 7:d57eba2aed38 42 {
ThomBMT 7:d57eba2aed38 43
ThomBMT 7:d57eba2aed38 44
ThomBMT 5:0ae4951e9b81 45 if(!Knop1 && !Knop3 == true) // Motor 1 rotates CW
ThomBMT 0:877f950fdfb5 46 {
ThomBMT 5:0ae4951e9b81 47 PwmPin1 = fabs(0.0);
ThomBMT 0:877f950fdfb5 48 }
ThomBMT 0:877f950fdfb5 49
ThomBMT 5:0ae4951e9b81 50 else if (Knop1==false) // Motor 1 rotates CW
ThomBMT 5:0ae4951e9b81 51 {
ThomBMT 5:0ae4951e9b81 52 float u = 0.8f; //determine useful value, this is not final
ThomBMT 5:0ae4951e9b81 53 DirectionPin1 = u > 0.0f; //either true or false
ThomBMT 5:0ae4951e9b81 54 // True = CW, for False = CW
ThomBMT 5:0ae4951e9b81 55 PwmPin1 = fabs(u);
ThomBMT 5:0ae4951e9b81 56 PwmPin2 = fabs(0.0);
ThomBMT 5:0ae4951e9b81 57 }
ThomBMT 5:0ae4951e9b81 58
ThomBMT 6:bbef54156775 59 else if (Knop3==false)// We see that Motor2 keeps rotating if we leave out the "else" statement, somehow the signal leaks
ThomBMT 6:bbef54156775 60 {
ThomBMT 6:bbef54156775 61 float u = 0.8f;
ThomBMT 6:bbef54156775 62 DirectionPin1 = u < 0.0f;
ThomBMT 6:bbef54156775 63 PwmPin1 = fabs(u);
ThomBMT 6:bbef54156775 64 PwmPin2 = fabs(0.0);
ThomBMT 6:bbef54156775 65 }
ThomBMT 7:d57eba2aed38 66
ThomBMT 9:836d6ac23481 67 else if (Bicep_Right > 0.55f)
ThomBMT 6:bbef54156775 68 {
ThomBMT 6:bbef54156775 69 float u = 0.8f; //determine useful value, this is not final
ThomBMT 6:bbef54156775 70 DirectionPin1 = u > 0.0f; //either true or false
ThomBMT 6:bbef54156775 71 // True = CW, for False = CW
ThomBMT 6:bbef54156775 72 PwmPin1 = fabs(u);
ThomBMT 6:bbef54156775 73 PwmPin2 = fabs(0.0);
ThomBMT 6:bbef54156775 74 }
ThomBMT 7:d57eba2aed38 75
ThomBMT 4:a1691164d879 76 else
ThomBMT 4:a1691164d879 77 {
ThomBMT 4:a1691164d879 78 float u = 0.0f;
ThomBMT 4:a1691164d879 79 PwmPin1 = fabs(u);
ThomBMT 4:a1691164d879 80 }
ThomBMT 4:a1691164d879 81 return 0;
ThomBMT 4:a1691164d879 82 }
ThomBMT 4:a1691164d879 83
ThomBMT 4:a1691164d879 84 int Turn_Motor2()
ThomBMT 4:a1691164d879 85 {
ThomBMT 5:0ae4951e9b81 86 if (!Knop2 && !Knop4 == true)
ThomBMT 4:a1691164d879 87 {
ThomBMT 5:0ae4951e9b81 88 PwmPin2 = fabs(0.0);
ThomBMT 4:a1691164d879 89 }
ThomBMT 4:a1691164d879 90
ThomBMT 6:bbef54156775 91 else if (Knop2==false)
ThomBMT 6:bbef54156775 92 {
ThomBMT 6:bbef54156775 93 float u = 0.8f;
ThomBMT 6:bbef54156775 94 DirectionPin2 = u < 0.0f;
ThomBMT 6:bbef54156775 95 PwmPin1 = fabs(0.0);
ThomBMT 6:bbef54156775 96 PwmPin2 = fabs(u);
ThomBMT 6:bbef54156775 97 }
ThomBMT 6:bbef54156775 98
ThomBMT 0:877f950fdfb5 99 else if (Knop4==false)
ThomBMT 0:877f950fdfb5 100 {
ThomBMT 0:877f950fdfb5 101 float u = 0.8f;
ThomBMT 0:877f950fdfb5 102 DirectionPin2 = u > 0.0f;
ThomBMT 0:877f950fdfb5 103 PwmPin1 = fabs(0.0);
ThomBMT 0:877f950fdfb5 104 PwmPin2 = fabs(u);
ThomBMT 0:877f950fdfb5 105
ThomBMT 0:877f950fdfb5 106 }
ThomBMT 0:877f950fdfb5 107
ThomBMT 0:877f950fdfb5 108 else
ThomBMT 0:877f950fdfb5 109 {
ThomBMT 0:877f950fdfb5 110 float u = 0.0f;
ThomBMT 4:a1691164d879 111 PwmPin2 = fabs(u);
ThomBMT 0:877f950fdfb5 112 }
ThomBMT 4:a1691164d879 113 return 0;
ThomBMT 4:a1691164d879 114 }
ThomBMT 5:0ae4951e9b81 115
ThomBMT 7:d57eba2aed38 116 int main()//Ticker toevoegen
ThomBMT 4:a1691164d879 117 {
ThomBMT 4:a1691164d879 118 pc.baud(115200);
ThomBMT 5:0ae4951e9b81 119 sample_timer.attach(&sample, 0.002);
ThomBMT 4:a1691164d879 120 PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz
ThomBMT 7:d57eba2aed38 121
ThomBMT 8:81f2c8ae4427 122 EMG_Read_Ticker.attach(&EMG_Read, 0.002);
ThomBMT 8:81f2c8ae4427 123
ThomBMT 7:d57eba2aed38 124 while(true)
ThomBMT 6:bbef54156775 125 {
ThomBMT 4:a1691164d879 126 Turn_Motor1();
ThomBMT 7:d57eba2aed38 127 Turn_Motor2();
ThomBMT 7:d57eba2aed38 128 pc.printf("%f ", Bicep_Right);
ThomBMT 0:877f950fdfb5 129 }
ThomBMT 0:877f950fdfb5 130 }