![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Control up to two motors using filtered EMG signals and a PID controller
Dependencies: FastPWM HIDScope MODSERIAL QEI Matrix biquadFilter controller errorFetch mbed motorConfig refGen MatrixMath inverseKinematics
Fork of Minor_test_serial by
main.cpp@14:664870b5d153, 2017-09-21 (annotated)
- Committer:
- tvlogman
- Date:
- Thu Sep 21 10:02:29 2017 +0000
- Revision:
- 14:664870b5d153
- Parent:
- 13:83e3672b24ee
- Child:
- 15:b76b8cff4d8f
Added killswitch on sw2 and switch-direction function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:c8f15874531b | 1 | #include "mbed.h" |
vsluiter | 0:c8f15874531b | 2 | #include "MODSERIAL.h" |
tvlogman | 8:0067469c3389 | 3 | #include "HIDScope.h" |
tvlogman | 9:5f0e796c9489 | 4 | #include "QEI.h" |
tvlogman | 8:0067469c3389 | 5 | |
tvlogman | 12:0462757e1ed2 | 6 | QEI Encoder(D12,D13,NC,64, QEI::X4_ENCODING); |
tvlogman | 10:e23cbcdde7e3 | 7 | MODSERIAL pc(USBTX, USBRX); |
tvlogman | 8:0067469c3389 | 8 | |
tvlogman | 14:664870b5d153 | 9 | // Defining outputs |
tvlogman | 13:83e3672b24ee | 10 | DigitalOut motor1_direction(D4); |
tvlogman | 13:83e3672b24ee | 11 | PwmOut motor1_pwm(D5); |
tvlogman | 14:664870b5d153 | 12 | |
tvlogman | 14:664870b5d153 | 13 | // Defining inputs |
tvlogman | 14:664870b5d153 | 14 | InterruptIn sw2(SW2); |
tvlogman | 14:664870b5d153 | 15 | |
tvlogman | 14:664870b5d153 | 16 | |
tvlogman | 13:83e3672b24ee | 17 | float pwmPeriod = 1.0/5000.0; |
tvlogman | 13:83e3672b24ee | 18 | |
tvlogman | 10:e23cbcdde7e3 | 19 | Ticker encoderTicker; |
tvlogman | 10:e23cbcdde7e3 | 20 | volatile int counts = 0; |
tvlogman | 12:0462757e1ed2 | 21 | volatile float revs = 0.00; |
tvlogman | 7:1bffab95fc5f | 22 | |
tvlogman | 10:e23cbcdde7e3 | 23 | void readEncoder(){ |
tvlogman | 10:e23cbcdde7e3 | 24 | counts = Encoder.getPulses(); |
tvlogman | 12:0462757e1ed2 | 25 | revs = counts/64.0f; |
tvlogman | 12:0462757e1ed2 | 26 | pc.printf("%i pulses \r\n", counts); |
tvlogman | 12:0462757e1ed2 | 27 | pc.printf("%f revolutions \r\n", revs); |
tvlogman | 10:e23cbcdde7e3 | 28 | } |
tvlogman | 14:664870b5d153 | 29 | |
tvlogman | 14:664870b5d153 | 30 | void killSwitch(){ |
tvlogman | 14:664870b5d153 | 31 | motor1_pwm.write(0.0); |
tvlogman | 14:664870b5d153 | 32 | } |
tvlogman | 14:664870b5d153 | 33 | |
tvlogman | 14:664870b5d153 | 34 | void M1switchDirection(){ |
tvlogman | 14:664870b5d153 | 35 | motor1_direction = !motor1_direction; |
tvlogman | 14:664870b5d153 | 36 | } |
vsluiter | 0:c8f15874531b | 37 | |
tvlogman | 13:83e3672b24ee | 38 | int frequency_pwm = 10000; //10kHz PWM |
tvlogman | 13:83e3672b24ee | 39 | |
tvlogman | 7:1bffab95fc5f | 40 | |
vsluiter | 0:c8f15874531b | 41 | int main() |
tvlogman | 10:e23cbcdde7e3 | 42 | { |
tvlogman | 14:664870b5d153 | 43 | motor1_direction = false; |
tvlogman | 13:83e3672b24ee | 44 | motor1_pwm.period(pwmPeriod);//T=1/f |
tvlogman | 14:664870b5d153 | 45 | sw2.fall(&killSwitch); |
tvlogman | 13:83e3672b24ee | 46 | |
vsluiter | 0:c8f15874531b | 47 | pc.baud(115200); |
tvlogman | 12:0462757e1ed2 | 48 | encoderTicker.attach(readEncoder, 1.0); |
tvlogman | 10:e23cbcdde7e3 | 49 | pc.printf("Encoder ticker attached and baudrate set"); |
tvlogman | 13:83e3672b24ee | 50 | |
tvlogman | 14:664870b5d153 | 51 | motor1_pwm.write(100.0/100.0);//write Duty Cycle |
tvlogman | 14:664870b5d153 | 52 | |
tvlogman | 14:664870b5d153 | 53 | sw2.fall(&killSwitch); |
vsluiter | 0:c8f15874531b | 54 | } |
tvlogman | 7:1bffab95fc5f | 55 |