Control of motors

Dependencies:   MODSERIAL QEI mbed

Revision:
0:2d34e3627dae
Child:
1:941f4059c0de
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Sep 28 09:08:18 2018 +0000
@@ -0,0 +1,44 @@
+#include "mbed.h"
+#include "QEI.h"
+#include "math.h"
+
+DigitalOut DirectionPin1(D4);
+DigitalOut DirectionPin2(D6);
+PwmOut PwmPin1(D5);
+PwmOut PwmPin2(D7);
+AnalogIn potmeter1(A0);
+AnalogIn potmeter2(A1);
+Ticker TickerReadPots;
+
+volatile float Duty1;
+volatile float Duty2;
+volatile float MotorSignal1;
+volatile float MotorSignal2;
+
+void ReadPots(void)
+{
+    Duty1 = potmeter1.read(); // read value potentiometer 1 
+    Duty2 = potmeter2.read(); // read value potentiometer 2
+ 
+    MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1]
+    MotorSignal2 = 2*Duty2 - 1;
+}
+
+int main()
+{
+    PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
+    TickerReadPots.attach(&ReadPots,0.05); // every 50 milli seconds.
+    
+    while (true) 
+    {
+    // motor 1
+    DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW
+    PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value
+    
+    // motor 2
+    DirectionPin2 = MotorSignal2 > 0.0f; 
+    PwmPin2 = fabs(MotorSignal2); 
+    
+    wait(0.01);  //Do while loop a hundred times per second.
+    }
+}
\ No newline at end of file