Overzetten

Dependencies:   HIDScope MODSERIAL mbed

Committer:
ThomBMT
Date:
Mon Oct 01 13:42:56 2018 +0000
Revision:
1:f6b25b03b8e2
Parent:
0:877f950fdfb5
Child:
2:4d593341fd7a
Child:
3:4f5a67f4170b
We finally were able to add the EMG-reading program into the main script. This now (sort off) works;

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 0:877f950fdfb5 14
ThomBMT 0:877f950fdfb5 15 //Define objects
ThomBMT 0:877f950fdfb5 16 AnalogIn emg0( A0 );
ThomBMT 0:877f950fdfb5 17 AnalogIn emg1( A1 );
ThomBMT 0:877f950fdfb5 18
ThomBMT 0:877f950fdfb5 19 Ticker sample_timer;
ThomBMT 0:877f950fdfb5 20 HIDScope scope( 2 );
ThomBMT 0:877f950fdfb5 21 DigitalOut led(LED1);
ThomBMT 0:877f950fdfb5 22
ThomBMT 0:877f950fdfb5 23 /** Sample function
ThomBMT 0:877f950fdfb5 24 * this function samples the emg and sends it to HIDScope
ThomBMT 0:877f950fdfb5 25 **/
ThomBMT 0:877f950fdfb5 26 void sample()
ThomBMT 0:877f950fdfb5 27 {
ThomBMT 0:877f950fdfb5 28 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
ThomBMT 0:877f950fdfb5 29 scope.set(0, emg0.read() );
ThomBMT 0:877f950fdfb5 30 scope.set(1, emg1.read() );
ThomBMT 0:877f950fdfb5 31 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
ThomBMT 0:877f950fdfb5 32 * Ensure that enough channels are available (HIDScope scope( 2 ))
ThomBMT 0:877f950fdfb5 33 * Finally, send all channels to the PC at once */
ThomBMT 0:877f950fdfb5 34 scope.send();
ThomBMT 0:877f950fdfb5 35 /* To indicate that the function is working, the LED is toggled */
ThomBMT 0:877f950fdfb5 36 led = !led;
ThomBMT 0:877f950fdfb5 37 }
ThomBMT 0:877f950fdfb5 38
ThomBMT 0:877f950fdfb5 39
ThomBMT 0:877f950fdfb5 40 int main()
ThomBMT 0:877f950fdfb5 41 {
ThomBMT 0:877f950fdfb5 42
ThomBMT 0:877f950fdfb5 43 pc.baud(115200);
ThomBMT 0:877f950fdfb5 44 sample_timer.attach(&sample, 0.002);
ThomBMT 0:877f950fdfb5 45 PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz
ThomBMT 0:877f950fdfb5 46
ThomBMT 0:877f950fdfb5 47 for(;;)
ThomBMT 0:877f950fdfb5 48 {
ThomBMT 0:877f950fdfb5 49 if(Knop1==false) // Motor 1 rotates CW
ThomBMT 0:877f950fdfb5 50 {
ThomBMT 0:877f950fdfb5 51 float u = 0.8f; //determine useful value, this is not final
ThomBMT 0:877f950fdfb5 52 DirectionPin1 = u > 0.0f; //either true or false
ThomBMT 0:877f950fdfb5 53 // True = CW, for False = CW
ThomBMT 0:877f950fdfb5 54 PwmPin1 = fabs(u);
ThomBMT 0:877f950fdfb5 55 PwmPin2 = fabs(0.0);
ThomBMT 0:877f950fdfb5 56 }
ThomBMT 0:877f950fdfb5 57
ThomBMT 0:877f950fdfb5 58 else if (Knop2==false)// We see that Motor2 keeps rotating if we leave out the "else" statement, somehow the signal leaks
ThomBMT 0:877f950fdfb5 59 {
ThomBMT 0:877f950fdfb5 60 float u = 0.8f;
ThomBMT 0:877f950fdfb5 61 DirectionPin2 = u < 0.0f;
ThomBMT 0:877f950fdfb5 62 PwmPin1 = fabs(0.0);
ThomBMT 0:877f950fdfb5 63 PwmPin2 = fabs(u);
ThomBMT 0:877f950fdfb5 64 }
ThomBMT 0:877f950fdfb5 65
ThomBMT 0:877f950fdfb5 66 else if (Knop3==false)
ThomBMT 0:877f950fdfb5 67 {
ThomBMT 0:877f950fdfb5 68 float u = 0.8f;
ThomBMT 0:877f950fdfb5 69 DirectionPin1 = u < 0.0f;
ThomBMT 0:877f950fdfb5 70 PwmPin1 = fabs(u);
ThomBMT 0:877f950fdfb5 71 PwmPin2 = fabs(0.0);
ThomBMT 0:877f950fdfb5 72 }
ThomBMT 0:877f950fdfb5 73
ThomBMT 0:877f950fdfb5 74 else if (Knop4==false)
ThomBMT 0:877f950fdfb5 75 {
ThomBMT 0:877f950fdfb5 76 float u = 0.8f;
ThomBMT 0:877f950fdfb5 77 DirectionPin2 = u > 0.0f;
ThomBMT 0:877f950fdfb5 78 PwmPin1 = fabs(0.0);
ThomBMT 0:877f950fdfb5 79 PwmPin2 = fabs(u);
ThomBMT 0:877f950fdfb5 80
ThomBMT 0:877f950fdfb5 81 }
ThomBMT 0:877f950fdfb5 82
ThomBMT 0:877f950fdfb5 83 else
ThomBMT 0:877f950fdfb5 84 {
ThomBMT 0:877f950fdfb5 85 float u = 0.0f;
ThomBMT 0:877f950fdfb5 86 PwmPin1 = PwmPin2 = fabs(u);
ThomBMT 0:877f950fdfb5 87 }
ThomBMT 0:877f950fdfb5 88 }
ThomBMT 0:877f950fdfb5 89 }