Control of motors

Dependencies:   MODSERIAL QEI mbed

Committer:
rubenlucas
Date:
Fri Sep 28 12:15:12 2018 +0000
Revision:
4:f324aa81d7bd
Parent:
3:fea443c3be53
Child:
5:f3cca442e347
Child:
7:34f941f8587d
Encoder added, but gives error in screen that interrupt not possible.

Who changed what in which revision?

UserRevisionLine numberNew 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 4:f324aa81d7bd 10 QEI Encoder1(D12,D13,NC,32);
rubenlucas 4:f324aa81d7bd 11
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 0:2d34e3627dae 35 }
rubenlucas 0:2d34e3627dae 36
rubenlucas 4:f324aa81d7bd 37 // printing counts to pc
rubenlucas 4:f324aa81d7bd 38 void GetCounts(void)
rubenlucas 4:f324aa81d7bd 39 {
rubenlucas 4:f324aa81d7bd 40 counts = Encoder1.getPulses();
rubenlucas 4:f324aa81d7bd 41 pc.printf("Number counts per second is: %i counts/second \r\n",counts);
rubenlucas 4:f324aa81d7bd 42 Encoder1.reset();
rubenlucas 4:f324aa81d7bd 43 }
rubenlucas 4:f324aa81d7bd 44
rubenlucas 4:f324aa81d7bd 45
rubenlucas 4:f324aa81d7bd 46
rubenlucas 0:2d34e3627dae 47 int main()
rubenlucas 0:2d34e3627dae 48 {
rubenlucas 3:fea443c3be53 49 pc.baud(115200);
rubenlucas 4:f324aa81d7bd 50 pc.printf("Hello World!\r\n");
rubenlucas 0:2d34e3627dae 51 PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
rubenlucas 0:2d34e3627dae 52 TickerReadPots.attach(&ReadPots,0.05); // every 50 milli seconds.
rubenlucas 4:f324aa81d7bd 53 TickerGetCounts.attach(&GetCounts,1); //Print amount of counts every second
rubenlucas 0:2d34e3627dae 54 while (true)
rubenlucas 0:2d34e3627dae 55 {
rubenlucas 0:2d34e3627dae 56 // motor 1
rubenlucas 0:2d34e3627dae 57 DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW
rubenlucas 0:2d34e3627dae 58 PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value
rubenlucas 0:2d34e3627dae 59
rubenlucas 0:2d34e3627dae 60 // motor 2
rubenlucas 0:2d34e3627dae 61 DirectionPin2 = MotorSignal2 > 0.0f;
rubenlucas 0:2d34e3627dae 62 PwmPin2 = fabs(MotorSignal2);
rubenlucas 0:2d34e3627dae 63
rubenlucas 0:2d34e3627dae 64 wait(0.01); //Do while loop a hundred times per second.
rubenlucas 0:2d34e3627dae 65 }
rubenlucas 0:2d34e3627dae 66 }