Control of motors

Dependencies:   MODSERIAL QEI mbed

Committer:
rubenlucas
Date:
Fri Sep 28 09:20:44 2018 +0000
Revision:
1:941f4059c0de
Parent:
0:2d34e3627dae
Child:
2:e4f92118a829
Digitalpin of for motor 2 were reversed. solved!

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 0:2d34e3627dae 3 #include "math.h"
rubenlucas 0:2d34e3627dae 4
rubenlucas 0:2d34e3627dae 5 DigitalOut DirectionPin1(D4);
rubenlucas 1:941f4059c0de 6 DigitalOut DirectionPin2(D7);
rubenlucas 0:2d34e3627dae 7 PwmOut PwmPin1(D5);
rubenlucas 1:941f4059c0de 8 PwmOut PwmPin2(D6);
rubenlucas 0:2d34e3627dae 9 AnalogIn potmeter1(A0);
rubenlucas 0:2d34e3627dae 10 AnalogIn potmeter2(A1);
rubenlucas 0:2d34e3627dae 11 Ticker TickerReadPots;
rubenlucas 0:2d34e3627dae 12
rubenlucas 0:2d34e3627dae 13 volatile float Duty1;
rubenlucas 0:2d34e3627dae 14 volatile float Duty2;
rubenlucas 0:2d34e3627dae 15 volatile float MotorSignal1;
rubenlucas 0:2d34e3627dae 16 volatile float MotorSignal2;
rubenlucas 0:2d34e3627dae 17
rubenlucas 0:2d34e3627dae 18 void ReadPots(void)
rubenlucas 0:2d34e3627dae 19 {
rubenlucas 0:2d34e3627dae 20 Duty1 = potmeter1.read(); // read value potentiometer 1
rubenlucas 0:2d34e3627dae 21 Duty2 = potmeter2.read(); // read value potentiometer 2
rubenlucas 0:2d34e3627dae 22
rubenlucas 0:2d34e3627dae 23 MotorSignal1 = 2*Duty1 - 1; //scaling potmeter to motor control signal [0 - 1] --> [(-1) - 1]
rubenlucas 0:2d34e3627dae 24 MotorSignal2 = 2*Duty2 - 1;
rubenlucas 0:2d34e3627dae 25 }
rubenlucas 0:2d34e3627dae 26
rubenlucas 0:2d34e3627dae 27 int main()
rubenlucas 0:2d34e3627dae 28 {
rubenlucas 0:2d34e3627dae 29 PwmPin1.period_us(60); // 16.66667 kHz (default period is too slow!)
rubenlucas 1:941f4059c0de 30
rubenlucas 0:2d34e3627dae 31 TickerReadPots.attach(&ReadPots,0.05); // every 50 milli seconds.
rubenlucas 0:2d34e3627dae 32
rubenlucas 0:2d34e3627dae 33 while (true)
rubenlucas 0:2d34e3627dae 34 {
rubenlucas 0:2d34e3627dae 35 // motor 1
rubenlucas 0:2d34e3627dae 36 DirectionPin1 = MotorSignal1 > 0.0f; //either true or false, CW or CCW
rubenlucas 0:2d34e3627dae 37 PwmPin1 = fabs(MotorSignal1); //pwm duty cycle can only be positive, floating point absolute value
rubenlucas 0:2d34e3627dae 38
rubenlucas 0:2d34e3627dae 39 // motor 2
rubenlucas 0:2d34e3627dae 40 DirectionPin2 = MotorSignal2 > 0.0f;
rubenlucas 0:2d34e3627dae 41 PwmPin2 = fabs(MotorSignal2);
rubenlucas 0:2d34e3627dae 42
rubenlucas 0:2d34e3627dae 43 wait(0.01); //Do while loop a hundred times per second.
rubenlucas 0:2d34e3627dae 44 }
rubenlucas 0:2d34e3627dae 45 }