
Control of motors
Dependencies: MODSERIAL QEI mbed
main.cpp@5:f3cca442e347, 2018-09-28 (annotated)
- Committer:
- rubenlucas
- Date:
- Fri Sep 28 13:21:22 2018 +0000
- Revision:
- 5:f3cca442e347
- Parent:
- 4:f324aa81d7bd
- Child:
- 6:5b4bd05ca1d6
Encoder works for Potmeter1, but Potmeter2 is still weird
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 | 5:f3cca442e347 | 11 | Timer PrintCounts; |
rubenlucas | 4:f324aa81d7bd | 12 | |
rubenlucas | 0:2d34e3627dae | 13 | DigitalOut DirectionPin1(D4); |
rubenlucas | 1:941f4059c0de | 14 | DigitalOut DirectionPin2(D7); |
rubenlucas | 0:2d34e3627dae | 15 | PwmOut PwmPin1(D5); |
rubenlucas | 1:941f4059c0de | 16 | PwmOut PwmPin2(D6); |
rubenlucas | 0:2d34e3627dae | 17 | AnalogIn potmeter1(A0); |
rubenlucas | 0:2d34e3627dae | 18 | AnalogIn potmeter2(A1); |
rubenlucas | 4:f324aa81d7bd | 19 | |
rubenlucas | 0:2d34e3627dae | 20 | |
rubenlucas | 0:2d34e3627dae | 21 | volatile float Duty1; |
rubenlucas | 0:2d34e3627dae | 22 | volatile float Duty2; |
rubenlucas | 0:2d34e3627dae | 23 | volatile float MotorSignal1; |
rubenlucas | 0:2d34e3627dae | 24 | volatile float MotorSignal2; |
rubenlucas | 4:f324aa81d7bd | 25 | volatile int counts; |
rubenlucas | 0:2d34e3627dae | 26 | |
rubenlucas | 4:f324aa81d7bd | 27 | // function for reading potentiometers and set to dutycycle |
rubenlucas | 0:2d34e3627dae | 28 | void ReadPots(void) |
rubenlucas | 0:2d34e3627dae | 29 | { |
rubenlucas | 0:2d34e3627dae | 30 | Duty1 = potmeter1.read(); // read value potentiometer 1 |
rubenlucas | 0:2d34e3627dae | 31 | Duty2 = potmeter2.read(); // read value potentiometer 2 |
rubenlucas | 0:2d34e3627dae | 32 | |
rubenlucas | 0:2d34e3627dae | 33 | MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1] |
rubenlucas | 3:fea443c3be53 | 34 | MotorSignal2 = 1 - 2*Duty2; |
rubenlucas | 5:f3cca442e347 | 35 | counts = Encoder1.getPulses(); |
rubenlucas | 0:2d34e3627dae | 36 | } |
rubenlucas | 0:2d34e3627dae | 37 | |
rubenlucas | 4:f324aa81d7bd | 38 | |
rubenlucas | 4:f324aa81d7bd | 39 | |
rubenlucas | 4:f324aa81d7bd | 40 | |
rubenlucas | 0:2d34e3627dae | 41 | int main() |
rubenlucas | 0:2d34e3627dae | 42 | { |
rubenlucas | 3:fea443c3be53 | 43 | pc.baud(115200); |
rubenlucas | 4:f324aa81d7bd | 44 | pc.printf("Hello World!\r\n"); |
rubenlucas | 0:2d34e3627dae | 45 | PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!) |
rubenlucas | 5:f3cca442e347 | 46 | TickerReadPots.attach(&ReadPots,0.1); // every 100 milli seconds. |
rubenlucas | 5:f3cca442e347 | 47 | PrintCounts.start(); |
rubenlucas | 0:2d34e3627dae | 48 | while (true) |
rubenlucas | 0:2d34e3627dae | 49 | { |
rubenlucas | 0:2d34e3627dae | 50 | // motor 1 |
rubenlucas | 0:2d34e3627dae | 51 | DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW |
rubenlucas | 0:2d34e3627dae | 52 | PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value |
rubenlucas | 0:2d34e3627dae | 53 | |
rubenlucas | 0:2d34e3627dae | 54 | // motor 2 |
rubenlucas | 0:2d34e3627dae | 55 | DirectionPin2 = MotorSignal2 > 0.0f; |
rubenlucas | 0:2d34e3627dae | 56 | PwmPin2 = fabs(MotorSignal2); |
rubenlucas | 0:2d34e3627dae | 57 | |
rubenlucas | 5:f3cca442e347 | 58 | float Time = PrintCounts.read(); |
rubenlucas | 5:f3cca442e347 | 59 | |
rubenlucas | 5:f3cca442e347 | 60 | if (Time > 1.0) |
rubenlucas | 5:f3cca442e347 | 61 | { |
rubenlucas | 5:f3cca442e347 | 62 | pc.printf("number of counts per second is %i counts/second\n\r",counts); |
rubenlucas | 5:f3cca442e347 | 63 | PrintCounts.reset(); |
rubenlucas | 5:f3cca442e347 | 64 | Encoder1.reset(); |
rubenlucas | 5:f3cca442e347 | 65 | } |
rubenlucas | 5:f3cca442e347 | 66 | |
rubenlucas | 0:2d34e3627dae | 67 | wait(0.01); //Do while loop a hundred times per second. |
rubenlucas | 0:2d34e3627dae | 68 | } |
rubenlucas | 0:2d34e3627dae | 69 | } |