Control of motors

Dependencies:   MODSERIAL QEI mbed

Revision:
6:5b4bd05ca1d6
Parent:
5:f3cca442e347
--- a/main.cpp	Fri Sep 28 13:21:22 2018 +0000
+++ b/main.cpp	Mon Oct 01 11:25:43 2018 +0000
@@ -8,7 +8,8 @@
 Ticker TickerReadPots;
 Ticker TickerGetCounts;
 QEI Encoder1(D10,D11,NC,32);
-Timer PrintCounts;
+QEI Encoder2(D12,D13,NC,32);
+Timer TimerCounts;
 
 DigitalOut DirectionPin1(D4);
 DigitalOut DirectionPin2(D7);
@@ -22,7 +23,8 @@
 volatile float Duty2;
 volatile float MotorSignal1;
 volatile float MotorSignal2;
-volatile int counts;
+volatile int counts1;
+volatile int counts2;
 
 // function for reading potentiometers and set to dutycycle
 void ReadPots(void)
@@ -32,7 +34,8 @@
  
     MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1]
     MotorSignal2 = 1 - 2*Duty2; 
-    counts = Encoder1.getPulses();
+    counts1 = Encoder1.getPulses();
+    counts2 = Encoder2.getPulses();
 }
 
 
@@ -44,7 +47,7 @@
     pc.printf("Hello World!\r\n");
     PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
     TickerReadPots.attach(&ReadPots,0.1); // every 100 milli seconds.
-    PrintCounts.start();
+    TimerCounts.start();
     while (true) 
     {
     // motor 1
@@ -55,13 +58,14 @@
     DirectionPin2 = MotorSignal2 > 0.0f; 
     PwmPin2 = fabs(MotorSignal2); 
     
-    float Time = PrintCounts.read();
+    float Time = TimerCounts.read();
        
         if (Time > 1.0)
         {
-            pc.printf("number of counts per second is %i counts/second\n\r",counts);
-            PrintCounts.reset();
+            pc.printf("number of counts per second: motor 1 = %i and motor 2 = %i\n\r",counts1,counts2);
+            TimerCounts.reset();
             Encoder1.reset();
+            Encoder2.reset();
         }
     
     wait(0.01);  //Do while loop a hundred times per second.