Control of motors

Dependencies:   MODSERIAL QEI mbed

Revision:
4:f324aa81d7bd
Parent:
3:fea443c3be53
Child:
5:f3cca442e347
Child:
7:34f941f8587d
diff -r fea443c3be53 -r f324aa81d7bd main.cpp
--- a/main.cpp	Fri Sep 28 11:41:02 2018 +0000
+++ b/main.cpp	Fri Sep 28 12:15:12 2018 +0000
@@ -3,20 +3,28 @@
 #include "MODSERIAL.h"
 #include "math.h"
 
-MODSERIAL pc(USBTX,USBRX)
+
+MODSERIAL pc(USBTX,USBRX);
+Ticker TickerReadPots;
+Ticker TickerGetCounts;
+QEI Encoder1(D12,D13,NC,32);
+
+
 DigitalOut DirectionPin1(D4);
 DigitalOut DirectionPin2(D7);
 PwmOut PwmPin1(D5);
 PwmOut PwmPin2(D6);
 AnalogIn potmeter1(A0);
 AnalogIn potmeter2(A1);
-Ticker TickerReadPots;
+
 
 volatile float Duty1;
 volatile float Duty2;
 volatile float MotorSignal1;
 volatile float MotorSignal2;
+volatile int counts;
 
+// function for reading potentiometers and set to dutycycle
 void ReadPots(void)
 {
     Duty1 = potmeter1.read(); // read value potentiometer 1 
@@ -26,13 +34,23 @@
     MotorSignal2 = 1 - 2*Duty2; 
 }
 
+// printing counts to pc
+void GetCounts(void)
+{
+ counts = Encoder1.getPulses();
+ pc.printf("Number counts per second is: %i counts/second \r\n",counts);
+ Encoder1.reset();
+}
+
+
+
 int main()
 {
     pc.baud(115200);
-    pc.printf("Hello World!\r\
+    pc.printf("Hello World!\r\n");
     PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
     TickerReadPots.attach(&ReadPots,0.05); // every 50 milli seconds.
-    
+    TickerGetCounts.attach(&GetCounts,1); //Print amount of counts every second
     while (true) 
     {
     // motor 1