Control of motors

Dependencies:   MODSERIAL QEI mbed

Committer:
rubenlucas
Date:
Mon Oct 01 12:26:28 2018 +0000
Revision:
8:10d6f6ad03c8
Parent:
7:34f941f8587d
via Ticker ask for print via interrupt button. input potmeters switched, was reversed

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 7:34f941f8587d 10 QEI Encoder1(D10,D11,NC,32);
rubenlucas 8:10d6f6ad03c8 11 QEI Encoder2(D12,D13,NC,32);
rubenlucas 7:34f941f8587d 12 InterruptIn PrintCounts(SW2);
rubenlucas 4:f324aa81d7bd 13
rubenlucas 4:f324aa81d7bd 14
rubenlucas 0:2d34e3627dae 15 DigitalOut DirectionPin1(D4);
rubenlucas 1:941f4059c0de 16 DigitalOut DirectionPin2(D7);
rubenlucas 0:2d34e3627dae 17 PwmOut PwmPin1(D5);
rubenlucas 1:941f4059c0de 18 PwmOut PwmPin2(D6);
rubenlucas 7:34f941f8587d 19 AnalogIn potmeter1(A1);
rubenlucas 7:34f941f8587d 20 AnalogIn potmeter2(A0);
rubenlucas 4:f324aa81d7bd 21
rubenlucas 0:2d34e3627dae 22
rubenlucas 0:2d34e3627dae 23 volatile float Duty1;
rubenlucas 0:2d34e3627dae 24 volatile float Duty2;
rubenlucas 0:2d34e3627dae 25 volatile float MotorSignal1;
rubenlucas 0:2d34e3627dae 26 volatile float MotorSignal2;
rubenlucas 7:34f941f8587d 27 volatile int counts1;
rubenlucas 7:34f941f8587d 28 volatile int counts2;
rubenlucas 0:2d34e3627dae 29
rubenlucas 4:f324aa81d7bd 30 // function for reading potentiometers and set to dutycycle
rubenlucas 0:2d34e3627dae 31 void ReadPots(void)
rubenlucas 0:2d34e3627dae 32 {
rubenlucas 0:2d34e3627dae 33 Duty1 = potmeter1.read(); // read value potentiometer 1
rubenlucas 0:2d34e3627dae 34 Duty2 = potmeter2.read(); // read value potentiometer 2
rubenlucas 0:2d34e3627dae 35
rubenlucas 0:2d34e3627dae 36 MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1]
rubenlucas 3:fea443c3be53 37 MotorSignal2 = 1 - 2*Duty2;
rubenlucas 0:2d34e3627dae 38 }
rubenlucas 0:2d34e3627dae 39
rubenlucas 4:f324aa81d7bd 40 // printing counts to pc
rubenlucas 4:f324aa81d7bd 41 void GetCounts(void)
rubenlucas 4:f324aa81d7bd 42 {
rubenlucas 7:34f941f8587d 43 counts1 = Encoder1.getPulses();
rubenlucas 7:34f941f8587d 44 counts2 = Encoder2.getPulses();
rubenlucas 7:34f941f8587d 45
rubenlucas 4:f324aa81d7bd 46 Encoder1.reset();
rubenlucas 7:34f941f8587d 47 Encoder2.reset();
rubenlucas 4:f324aa81d7bd 48 }
rubenlucas 4:f324aa81d7bd 49
rubenlucas 7:34f941f8587d 50 void Print()
rubenlucas 7:34f941f8587d 51 {
rubenlucas 7:34f941f8587d 52 pc.printf("Number counts per second: motor1 = %i , motor2 = %i \r\n",counts1,counts2);
rubenlucas 7:34f941f8587d 53 }
rubenlucas 4:f324aa81d7bd 54
rubenlucas 4:f324aa81d7bd 55
rubenlucas 0:2d34e3627dae 56 int main()
rubenlucas 0:2d34e3627dae 57 {
rubenlucas 3:fea443c3be53 58 pc.baud(115200);
rubenlucas 4:f324aa81d7bd 59 pc.printf("Hello World!\r\n");
rubenlucas 0:2d34e3627dae 60 PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
rubenlucas 0:2d34e3627dae 61 TickerReadPots.attach(&ReadPots,0.05); // every 50 milli seconds.
rubenlucas 4:f324aa81d7bd 62 TickerGetCounts.attach(&GetCounts,1); //Print amount of counts every second
rubenlucas 7:34f941f8587d 63 PrintCounts.fall(&Print);
rubenlucas 0:2d34e3627dae 64 while (true)
rubenlucas 0:2d34e3627dae 65 {
rubenlucas 0:2d34e3627dae 66 // motor 1
rubenlucas 0:2d34e3627dae 67 DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW
rubenlucas 0:2d34e3627dae 68 PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value
rubenlucas 0:2d34e3627dae 69
rubenlucas 0:2d34e3627dae 70 // motor 2
rubenlucas 0:2d34e3627dae 71 DirectionPin2 = MotorSignal2 > 0.0f;
rubenlucas 0:2d34e3627dae 72 PwmPin2 = fabs(MotorSignal2);
rubenlucas 0:2d34e3627dae 73
rubenlucas 0:2d34e3627dae 74 wait(0.01); //Do while loop a hundred times per second.
rubenlucas 0:2d34e3627dae 75 }
rubenlucas 0:2d34e3627dae 76 }