Jurriën Bos
/
TutorialPES
Potreaders op motoren
Revision 1:4bf64d003f3a, committed 2018-10-15
- Comitter:
- JurrienBos
- Date:
- Mon Oct 15 13:55:45 2018 +0000
- Parent:
- 0:4591ba678a39
- Commit message:
- Uitgewerkte potreader die beide motoren aansturen
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 4591ba678a39 -r 4bf64d003f3a main.cpp --- a/main.cpp Mon Oct 15 13:32:30 2018 +0000 +++ b/main.cpp Mon Oct 15 13:55:45 2018 +0000 @@ -2,66 +2,86 @@ #include "MODSERIAL.h" MODSERIAL pc(USBTX, USBRX); DigitalOut DirectionPin1(D4); +DigitalOut DirectionPin2(D7); PwmOut PwmPin1(D5); +PwmOut PwmPin2(D6); DigitalIn Knop1(D2); AnalogIn pot1 (A5); +AnalogIn pot2 (A4); -Ticker mycontrollerTicker; -Ticker printTicker; -Ticker Velo; -volatile bool printer; -const float maxVelocity=8.4; // in rad/s -volatile float referenceVelocity = 0.5; //dit is de gecentreerde waarde en dus de nulstand +Ticker mycontrollerTicker1; +Ticker mycontrollerTicker2; +Ticker Velo1; +Ticker Velo2; -void velocityref_print() -{ - printer = true; -} - -void velocity() +//const float maxVelocity=8.4; // in rad/s +volatile float referenceVelocity1 = 0.5; //dit is de gecentreerde waarde en dus de nulstand +volatile float referenceVelocity2 = 0.5; + +void velocity1() { - if (pot1.read()>0.5f) + if ((pot1.read()>0.5f) || (Knop1 == true))//gezeik met die knop doet het niet ff uitzoeken nog { // Clockwise rotation - referenceVelocity = (pot1.read()-0.5f) * 2.0f; + referenceVelocity1 = (pot1.read()-0.5f) * 2.0f; } - else if (pot1.read() == 0.5f) + else if (pot1.read() == 0.5f || !Knop1 == (pot1.read()<0.5f)) { - referenceVelocity = pot1.read() * 0.0f; + referenceVelocity1 = pot1.read() * 0.0f; } else if (pot1.read() < 0.5f) { // Counterclockwise rotation - referenceVelocity = 2.0f * (pot1.read()-0.5f) ; + referenceVelocity1 = 2.0f * (pot1.read()-0.5f) ; } - } - void motor() +void velocity2() + { + if (pot2.read()>0.5f) + { + // Clockwise rotation + referenceVelocity2 = (pot2.read()-0.5f) * 2.0f; + } + + else if (pot2.read() == 0.5f) + { + referenceVelocity2 = pot2.read() * 0.0f; + } + + else if (pot2.read() < 0.5f) + { + // Counterclockwise rotation + referenceVelocity2 = 2.0f * (pot2.read()-0.5f) ; + } + } + +void motor1() { - float u = referenceVelocity; + float u = referenceVelocity1; DirectionPin1 = u < 0.0f; PwmPin1 = fabs(u); } +void motor2() + { + float u = referenceVelocity2; + DirectionPin2 = u > 0.0f; + PwmPin2 = fabs(u); + } int main() { pc.baud(115200); PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz - mycontrollerTicker.attach(motor, 0.002);//500Hz - printTicker.attach(velocityref_print, 0.5); - Velo.attach(velocity, 0.002); + mycontrollerTicker1.attach(motor1, 0.002);//500Hz + Velo1.attach(velocity1, 0.002); + mycontrollerTicker2.attach(motor2, 0.002); + Velo2.attach(velocity2, 0.002); + while(true) { - if (printer) - { - pc.printf("%f \n",referenceVelocity); - pc.printf("%f \n",pot1.read()); - printer = false; - } } -} - +} \ No newline at end of file