Afgesplitste versie van motor control waarbij we ook iets met EMG gaan doen

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

Revision:
5:54ce02ad7a50
Parent:
4:28d71b0a29aa
Child:
6:c352578a95b3
--- a/main.cpp	Fri Oct 04 12:58:04 2019 +0000
+++ b/main.cpp	Fri Oct 04 13:24:06 2019 +0000
@@ -9,7 +9,8 @@
 DigitalOut ledg(LED_GREEN);
 DigitalOut ledb(LED_BLUE);
 PwmOut led1(D10);
-DigitalIn button1(D11);
+InterruptIn button1(D11);
+InterruptIn button2(D10);
 AnalogIn potmeter(A0);
 DigitalIn sw(SW2);
 MODSERIAL pc(USBTX, USBRX);
@@ -25,13 +26,16 @@
 Ticker motorTicker;
 Ticker encoderTicker;
 
-const float maxVelocity = 7.958701; // 76 RPM to rad/s
+const float maxVelocity = 7.958701 * 0.75; // 76 RPM to rad/s at 75% efficiency due to 9V instead of 12V
 const float PWM_period = 1e-6;
 
 volatile int counts; // Encoder counts
 volatile int countsPrev = 0;
 volatile int deltaCounts;
 
+// motor1Direction = 1;
+volatile int motor1Toggle = 1;
+
 float factorin = 6.23185/64; // Convert encoder counts to angle in rad
 float gearratio = 131.25; // Gear ratio of gearbox
 
@@ -50,7 +54,7 @@
 void motorControl()
 {
     float potValue = potmeter.read();
-    motor1Power.pulsewidth(potValue * PWM_period);
+    motor1Power.pulsewidth(potValue * PWM_period * motor1Toggle);
 }
 
 void readEncoder()
@@ -61,6 +65,16 @@
     countsPrev = counts;
 }
 
+void flipDirection()
+{
+    motor1Direction = !motor1Direction;
+}
+
+void toggleMotor()
+{
+    motor1Toggle = !motor1Toggle;
+}
+
 int main()
 {
     pc.baud(115200);
@@ -69,23 +83,22 @@
     motor1Power.period(PWM_period);
 
     motorTicker.attach(motorControl, 0.01);
-    
+
     float T_encoder = 0.1;
     encoderTicker.attach(readEncoder, T_encoder);
 
-    motor1Direction = 0;
+    button1.fall(&flipDirection);
+    button2.fall(&toggleMotor);
 
     while (true) {
-
         float potValue = potmeter.read(); // Read potmeter
-        
         float angle = counts * factorin / gearratio; // Angle of motor shaft in rad
         float omega = deltaCounts / T_encoder * factorin / gearratio; // Angular velocity of motor shaft in rad/s
-        
+
         pc.printf("Potmeter: %f \r\n", potValue);
         pc.printf("Counts: %i   DeltaCounts: %i\r\n", counts, deltaCounts);
         pc.printf("Angle:  %f   Omega:       %f\r\n", angle, omega);
-        
+
         wait(0.5);
     }
 }