EMG aansluiten op motor

Dependencies:   FastPWM HIDScope MODSERIAL mbed

Revision:
0:73cf7fc57ab7
Child:
1:83531c955134
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 11 13:58:41 2018 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "FastPWM.h"
+#include "MODSERIAL.h"
+#include "HIDScope.h"
+
+// Define Pins
+FastPWM pwmpin1(D5);            // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
+FastPWM pwmpin2(D6);            // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
+DigitalOut directionpin1(D4);   // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
+DigitalOut directionpin2(D7);   // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
+AnalogIn    emg0( A0 );         // EMG on Bicep 1
+AnalogIn    emg1( A1 );         // EMG on Bicep 2
+DigitalOut  led(LED1);          // Check for Sample function
+InterruptIn button1(PTC6);      // Interrupt1 for EMG 1
+InterruptIn button2(
+
+// Define variables
+Ticker sample_timer;            // Ticker for EMG sample function
+HIDScope    scope( 2 );         // HIDScope for EMG
+Ticker motor;                   // Ticker function for motor function
+volatile float u1;              // Motor 1 control signal
+volatile float u2;              // Motor 2 control signal
+
+void sample()                   // Function for getting EMG signals
+{
+    /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
+    scope.set(0, emg0.read() );
+    scope.set(1, emg1.read() );
+    /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) 
+    *  Ensure that enough channels are available (HIDScope scope( 2 ))
+    *  Finally, send all channels to the PC at once */
+    scope.send();
+    /* To indicate that the function is working, the LED is toggled */
+    led = !led;
+}
+
+void motorfunction()            // Function for motor control
+{          
+        u1 = EMG1_scale;                  // motor control signal
+        u2 = EMG2_scale;                  // motor control signal
+        directionpin1 = u1 > 0.0f;        // either true or false, determines direction (0 or 1)
+        directionpin2 = u2 > 0.0f;        // either true or false, determines direction (0 or 1)
+        pwmpin1 = fabs(u1);               // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value).
+        pwmpin2 = fabs(u2);               // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value).
+}
+
+int main()
+{   
+    sample_timer.attach(&sample, 0.002);  // 500 Hz
+    pwmpin1.period_us(60.0); //60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once)
+    motor.attach(motorfunction,0.5);
+    while(1) {}
+}
\ No newline at end of file