Control of motors
Dependencies: MODSERIAL QEI mbed
Diff: main.cpp
- Revision:
- 5:f3cca442e347
- Parent:
- 4:f324aa81d7bd
- Child:
- 6:5b4bd05ca1d6
--- a/main.cpp Fri Sep 28 12:15:12 2018 +0000 +++ b/main.cpp Fri Sep 28 13:21:22 2018 +0000 @@ -7,8 +7,8 @@ MODSERIAL pc(USBTX,USBRX); Ticker TickerReadPots; Ticker TickerGetCounts; -QEI Encoder1(D12,D13,NC,32); - +QEI Encoder1(D10,D11,NC,32); +Timer PrintCounts; DigitalOut DirectionPin1(D4); DigitalOut DirectionPin2(D7); @@ -32,15 +32,9 @@ MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1] MotorSignal2 = 1 - 2*Duty2; + counts = Encoder1.getPulses(); } -// 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(); -} @@ -49,8 +43,8 @@ pc.baud(115200); 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 + TickerReadPots.attach(&ReadPots,0.1); // every 100 milli seconds. + PrintCounts.start(); while (true) { // motor 1 @@ -61,6 +55,15 @@ DirectionPin2 = MotorSignal2 > 0.0f; PwmPin2 = fabs(MotorSignal2); + float Time = PrintCounts.read(); + + if (Time > 1.0) + { + pc.printf("number of counts per second is %i counts/second\n\r",counts); + PrintCounts.reset(); + Encoder1.reset(); + } + wait(0.01); //Do while loop a hundred times per second. } } \ No newline at end of file