Control of motors
Dependencies: MODSERIAL QEI mbed
Diff: main.cpp
- Revision:
- 7:34f941f8587d
- Parent:
- 4:f324aa81d7bd
- Child:
- 8:10d6f6ad03c8
--- a/main.cpp Fri Sep 28 12:15:12 2018 +0000 +++ b/main.cpp Mon Oct 01 12:25:12 2018 +0000 @@ -7,22 +7,25 @@ MODSERIAL pc(USBTX,USBRX); Ticker TickerReadPots; Ticker TickerGetCounts; +QEI Encoder1(D10,D11,NC,32); QEI Encoder1(D12,D13,NC,32); +InterruptIn PrintCounts(SW2); DigitalOut DirectionPin1(D4); DigitalOut DirectionPin2(D7); PwmOut PwmPin1(D5); PwmOut PwmPin2(D6); -AnalogIn potmeter1(A0); -AnalogIn potmeter2(A1); +AnalogIn potmeter1(A1); +AnalogIn potmeter2(A0); volatile float Duty1; 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) @@ -37,11 +40,17 @@ // printing counts to pc void GetCounts(void) { - counts = Encoder1.getPulses(); - pc.printf("Number counts per second is: %i counts/second \r\n",counts); + counts1 = Encoder1.getPulses(); + counts2 = Encoder2.getPulses(); + Encoder1.reset(); + Encoder2.reset(); } +void Print() +{ + pc.printf("Number counts per second: motor1 = %i , motor2 = %i \r\n",counts1,counts2); +} int main() @@ -51,6 +60,7 @@ 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 + PrintCounts.fall(&Print); while (true) { // motor 1