Overzetten
Dependencies: HIDScope MODSERIAL mbed
main.cpp@0:877f950fdfb5, 2018-10-01 (annotated)
- Committer:
- ThomBMT
- Date:
- Mon Oct 01 13:20:19 2018 +0000
- Revision:
- 0:877f950fdfb5
- Child:
- 1:f6b25b03b8e2
Overzetten;
Who changed what in which revision?
User | Revision | Line number | New 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 | AnalogIn pot1(A0); |
ThomBMT | 0:877f950fdfb5 | 7 | AnalogIn pot2(A1); |
ThomBMT | 0:877f950fdfb5 | 8 | DigitalOut DirectionPin1(D4); |
ThomBMT | 0:877f950fdfb5 | 9 | PwmOut PwmPin1(D5); |
ThomBMT | 0:877f950fdfb5 | 10 | DigitalOut DirectionPin2(D7); |
ThomBMT | 0:877f950fdfb5 | 11 | PwmOut PwmPin2(D6); |
ThomBMT | 0:877f950fdfb5 | 12 | DigitalIn Knop1(D3); |
ThomBMT | 0:877f950fdfb5 | 13 | DigitalIn Knop2(D2); |
ThomBMT | 0:877f950fdfb5 | 14 | DigitalIn Knop3(PTA4); |
ThomBMT | 0:877f950fdfb5 | 15 | DigitalIn Knop4(PTC6); |
ThomBMT | 0:877f950fdfb5 | 16 | |
ThomBMT | 0:877f950fdfb5 | 17 | //Define objects |
ThomBMT | 0:877f950fdfb5 | 18 | AnalogIn emg0( A0 ); |
ThomBMT | 0:877f950fdfb5 | 19 | AnalogIn emg1( A1 ); |
ThomBMT | 0:877f950fdfb5 | 20 | |
ThomBMT | 0:877f950fdfb5 | 21 | Ticker sample_timer; |
ThomBMT | 0:877f950fdfb5 | 22 | HIDScope scope( 2 ); |
ThomBMT | 0:877f950fdfb5 | 23 | DigitalOut led(LED1); |
ThomBMT | 0:877f950fdfb5 | 24 | |
ThomBMT | 0:877f950fdfb5 | 25 | /** Sample function |
ThomBMT | 0:877f950fdfb5 | 26 | * this function samples the emg and sends it to HIDScope |
ThomBMT | 0:877f950fdfb5 | 27 | **/ |
ThomBMT | 0:877f950fdfb5 | 28 | void sample() |
ThomBMT | 0:877f950fdfb5 | 29 | { |
ThomBMT | 0:877f950fdfb5 | 30 | /* 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 | 31 | scope.set(0, emg0.read() ); |
ThomBMT | 0:877f950fdfb5 | 32 | scope.set(1, emg1.read() ); |
ThomBMT | 0:877f950fdfb5 | 33 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
ThomBMT | 0:877f950fdfb5 | 34 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
ThomBMT | 0:877f950fdfb5 | 35 | * Finally, send all channels to the PC at once */ |
ThomBMT | 0:877f950fdfb5 | 36 | scope.send(); |
ThomBMT | 0:877f950fdfb5 | 37 | /* To indicate that the function is working, the LED is toggled */ |
ThomBMT | 0:877f950fdfb5 | 38 | led = !led; |
ThomBMT | 0:877f950fdfb5 | 39 | } |
ThomBMT | 0:877f950fdfb5 | 40 | |
ThomBMT | 0:877f950fdfb5 | 41 | |
ThomBMT | 0:877f950fdfb5 | 42 | int main() |
ThomBMT | 0:877f950fdfb5 | 43 | { |
ThomBMT | 0:877f950fdfb5 | 44 | |
ThomBMT | 0:877f950fdfb5 | 45 | pc.baud(115200); |
ThomBMT | 0:877f950fdfb5 | 46 | sample_timer.attach(&sample, 0.002); |
ThomBMT | 0:877f950fdfb5 | 47 | PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz |
ThomBMT | 0:877f950fdfb5 | 48 | |
ThomBMT | 0:877f950fdfb5 | 49 | for(;;) |
ThomBMT | 0:877f950fdfb5 | 50 | { |
ThomBMT | 0:877f950fdfb5 | 51 | if(Knop1==false) // Motor 1 rotates CW |
ThomBMT | 0:877f950fdfb5 | 52 | { |
ThomBMT | 0:877f950fdfb5 | 53 | float u = 0.8f; //determine useful value, this is not final |
ThomBMT | 0:877f950fdfb5 | 54 | DirectionPin1 = u > 0.0f; //either true or false |
ThomBMT | 0:877f950fdfb5 | 55 | // True = CW, for False = CW |
ThomBMT | 0:877f950fdfb5 | 56 | PwmPin1 = fabs(u); |
ThomBMT | 0:877f950fdfb5 | 57 | PwmPin2 = fabs(0.0); |
ThomBMT | 0:877f950fdfb5 | 58 | } |
ThomBMT | 0:877f950fdfb5 | 59 | |
ThomBMT | 0:877f950fdfb5 | 60 | else if (Knop2==false)// We see that Motor2 keeps rotating if we leave out the "else" statement, somehow the signal leaks |
ThomBMT | 0:877f950fdfb5 | 61 | { |
ThomBMT | 0:877f950fdfb5 | 62 | float u = 0.8f; |
ThomBMT | 0:877f950fdfb5 | 63 | DirectionPin2 = u < 0.0f; |
ThomBMT | 0:877f950fdfb5 | 64 | PwmPin1 = fabs(0.0); |
ThomBMT | 0:877f950fdfb5 | 65 | PwmPin2 = fabs(u); |
ThomBMT | 0:877f950fdfb5 | 66 | } |
ThomBMT | 0:877f950fdfb5 | 67 | |
ThomBMT | 0:877f950fdfb5 | 68 | else if (Knop3==false) |
ThomBMT | 0:877f950fdfb5 | 69 | { |
ThomBMT | 0:877f950fdfb5 | 70 | float u = 0.8f; |
ThomBMT | 0:877f950fdfb5 | 71 | DirectionPin1 = u < 0.0f; |
ThomBMT | 0:877f950fdfb5 | 72 | PwmPin1 = fabs(u); |
ThomBMT | 0:877f950fdfb5 | 73 | PwmPin2 = fabs(0.0); |
ThomBMT | 0:877f950fdfb5 | 74 | } |
ThomBMT | 0:877f950fdfb5 | 75 | |
ThomBMT | 0:877f950fdfb5 | 76 | else if (Knop4==false) |
ThomBMT | 0:877f950fdfb5 | 77 | { |
ThomBMT | 0:877f950fdfb5 | 78 | float u = 0.8f; |
ThomBMT | 0:877f950fdfb5 | 79 | DirectionPin2 = u > 0.0f; |
ThomBMT | 0:877f950fdfb5 | 80 | PwmPin1 = fabs(0.0); |
ThomBMT | 0:877f950fdfb5 | 81 | PwmPin2 = fabs(u); |
ThomBMT | 0:877f950fdfb5 | 82 | |
ThomBMT | 0:877f950fdfb5 | 83 | } |
ThomBMT | 0:877f950fdfb5 | 84 | |
ThomBMT | 0:877f950fdfb5 | 85 | else |
ThomBMT | 0:877f950fdfb5 | 86 | { |
ThomBMT | 0:877f950fdfb5 | 87 | float u = 0.0f; |
ThomBMT | 0:877f950fdfb5 | 88 | PwmPin1 = PwmPin2 = fabs(u); |
ThomBMT | 0:877f950fdfb5 | 89 | } |
ThomBMT | 0:877f950fdfb5 | 90 | } |
ThomBMT | 0:877f950fdfb5 | 91 | } |