read emg to turn motor shaft
Dependencies: MODSERIAL QEI mbed
Diff: main.cpp
- Revision:
- 0:e46276179d7f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Oct 01 13:40:37 2018 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" +#include "MODSERIAL.h" +#include "QEI.h" + +PwmOut pwmpin(D5); +DigitalOut directionpin(D4); +AnalogIn potmeter(A2); + +AnalogIn emg0( A0 ); +AnalogIn emg1( A1 ); + +MODSERIAL pc(USBTX, USBRX); +QEI wheel (D13, D12, NC, 32); + +float angle_resolution = (360.0/32.0)*(1.0/131.0); //degrees/counts + +float mean(float *samples, int n) { + float sum = 0.0; + for (int i=0; i<n; i++) { + sum += samples[i]; + } + return sum / (float)n; +} + +int main() +{ + pc.baud(115200); + + pwmpin.period_us(60); + int pulses = 0; + float angle; + int n = 1; + int i = 0; + float meanmuscle; + float u[500]; + while (true) { + u[i] = fabs(2*(emg0.read() - 0.5)); + + pulses = wheel.getPulses(); + angle = pulses*angle_resolution; + if(n%500 == 0){ + pc.printf("Angle is: %f degrees \r\n", meanmuscle); + meanmuscle = (mean(u,500)-0.1)*4; + directionpin = meanmuscle > 0.0f; + pwmpin = fabs(meanmuscle); + i=0; + } + n++; + i++; + wait(0.001); + } +} \ No newline at end of file