Control of motors
Dependencies: MODSERIAL QEI mbed
main.cpp@6:5b4bd05ca1d6, 2018-10-01 (annotated)
- Committer:
- rubenlucas
- Date:
- Mon Oct 01 11:25:43 2018 +0000
- Revision:
- 6:5b4bd05ca1d6
- Parent:
- 5:f3cca442e347
Second encoder added and timername changed to TimerCounts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rubenlucas | 0:2d34e3627dae | 1 | #include "mbed.h" |
rubenlucas | 0:2d34e3627dae | 2 | #include "QEI.h" |
rubenlucas | 3:fea443c3be53 | 3 | #include "MODSERIAL.h" |
rubenlucas | 0:2d34e3627dae | 4 | #include "math.h" |
rubenlucas | 0:2d34e3627dae | 5 | |
rubenlucas | 4:f324aa81d7bd | 6 | |
rubenlucas | 4:f324aa81d7bd | 7 | MODSERIAL pc(USBTX,USBRX); |
rubenlucas | 4:f324aa81d7bd | 8 | Ticker TickerReadPots; |
rubenlucas | 4:f324aa81d7bd | 9 | Ticker TickerGetCounts; |
rubenlucas | 5:f3cca442e347 | 10 | QEI Encoder1(D10,D11,NC,32); |
rubenlucas | 6:5b4bd05ca1d6 | 11 | QEI Encoder2(D12,D13,NC,32); |
rubenlucas | 6:5b4bd05ca1d6 | 12 | Timer TimerCounts; |
rubenlucas | 4:f324aa81d7bd | 13 | |
rubenlucas | 0:2d34e3627dae | 14 | DigitalOut DirectionPin1(D4); |
rubenlucas | 1:941f4059c0de | 15 | DigitalOut DirectionPin2(D7); |
rubenlucas | 0:2d34e3627dae | 16 | PwmOut PwmPin1(D5); |
rubenlucas | 1:941f4059c0de | 17 | PwmOut PwmPin2(D6); |
rubenlucas | 0:2d34e3627dae | 18 | AnalogIn potmeter1(A0); |
rubenlucas | 0:2d34e3627dae | 19 | AnalogIn potmeter2(A1); |
rubenlucas | 4:f324aa81d7bd | 20 | |
rubenlucas | 0:2d34e3627dae | 21 | |
rubenlucas | 0:2d34e3627dae | 22 | volatile float Duty1; |
rubenlucas | 0:2d34e3627dae | 23 | volatile float Duty2; |
rubenlucas | 0:2d34e3627dae | 24 | volatile float MotorSignal1; |
rubenlucas | 0:2d34e3627dae | 25 | volatile float MotorSignal2; |
rubenlucas | 6:5b4bd05ca1d6 | 26 | volatile int counts1; |
rubenlucas | 6:5b4bd05ca1d6 | 27 | volatile int counts2; |
rubenlucas | 0:2d34e3627dae | 28 | |
rubenlucas | 4:f324aa81d7bd | 29 | // function for reading potentiometers and set to dutycycle |
rubenlucas | 0:2d34e3627dae | 30 | void ReadPots(void) |
rubenlucas | 0:2d34e3627dae | 31 | { |
rubenlucas | 0:2d34e3627dae | 32 | Duty1 = potmeter1.read(); // read value potentiometer 1 |
rubenlucas | 0:2d34e3627dae | 33 | Duty2 = potmeter2.read(); // read value potentiometer 2 |
rubenlucas | 0:2d34e3627dae | 34 | |
rubenlucas | 0:2d34e3627dae | 35 | MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1] |
rubenlucas | 3:fea443c3be53 | 36 | MotorSignal2 = 1 - 2*Duty2; |
rubenlucas | 6:5b4bd05ca1d6 | 37 | counts1 = Encoder1.getPulses(); |
rubenlucas | 6:5b4bd05ca1d6 | 38 | counts2 = Encoder2.getPulses(); |
rubenlucas | 0:2d34e3627dae | 39 | } |
rubenlucas | 0:2d34e3627dae | 40 | |
rubenlucas | 4:f324aa81d7bd | 41 | |
rubenlucas | 4:f324aa81d7bd | 42 | |
rubenlucas | 4:f324aa81d7bd | 43 | |
rubenlucas | 0:2d34e3627dae | 44 | int main() |
rubenlucas | 0:2d34e3627dae | 45 | { |
rubenlucas | 3:fea443c3be53 | 46 | pc.baud(115200); |
rubenlucas | 4:f324aa81d7bd | 47 | pc.printf("Hello World!\r\n"); |
rubenlucas | 0:2d34e3627dae | 48 | PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!) |
rubenlucas | 5:f3cca442e347 | 49 | TickerReadPots.attach(&ReadPots,0.1); // every 100 milli seconds. |
rubenlucas | 6:5b4bd05ca1d6 | 50 | TimerCounts.start(); |
rubenlucas | 0:2d34e3627dae | 51 | while (true) |
rubenlucas | 0:2d34e3627dae | 52 | { |
rubenlucas | 0:2d34e3627dae | 53 | // motor 1 |
rubenlucas | 0:2d34e3627dae | 54 | DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW |
rubenlucas | 0:2d34e3627dae | 55 | PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value |
rubenlucas | 0:2d34e3627dae | 56 | |
rubenlucas | 0:2d34e3627dae | 57 | // motor 2 |
rubenlucas | 0:2d34e3627dae | 58 | DirectionPin2 = MotorSignal2 > 0.0f; |
rubenlucas | 0:2d34e3627dae | 59 | PwmPin2 = fabs(MotorSignal2); |
rubenlucas | 0:2d34e3627dae | 60 | |
rubenlucas | 6:5b4bd05ca1d6 | 61 | float Time = TimerCounts.read(); |
rubenlucas | 5:f3cca442e347 | 62 | |
rubenlucas | 5:f3cca442e347 | 63 | if (Time > 1.0) |
rubenlucas | 5:f3cca442e347 | 64 | { |
rubenlucas | 6:5b4bd05ca1d6 | 65 | pc.printf("number of counts per second: motor 1 = %i and motor 2 = %i\n\r",counts1,counts2); |
rubenlucas | 6:5b4bd05ca1d6 | 66 | TimerCounts.reset(); |
rubenlucas | 5:f3cca442e347 | 67 | Encoder1.reset(); |
rubenlucas | 6:5b4bd05ca1d6 | 68 | Encoder2.reset(); |
rubenlucas | 5:f3cca442e347 | 69 | } |
rubenlucas | 5:f3cca442e347 | 70 | |
rubenlucas | 0:2d34e3627dae | 71 | wait(0.01); //Do while loop a hundred times per second. |
rubenlucas | 0:2d34e3627dae | 72 | } |
rubenlucas | 0:2d34e3627dae | 73 | } |