Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FastPWM mbed QEI biquadFilter HIDScope MODSERIAL
Diff: main.cpp
- Revision:
- 11:3efd6a324f16
- Parent:
- 10:ac36f9a204dd
- Child:
- 12:ef4ba26d0e2f
- Child:
- 13:6556cd086d07
diff -r ac36f9a204dd -r 3efd6a324f16 main.cpp --- a/main.cpp Mon Oct 08 13:28:40 2018 +0000 +++ b/main.cpp Mon Oct 08 15:10:47 2018 +0000 @@ -1,12 +1,24 @@ #include "mbed.h" -#include "FastPWM.h" +#include "FastPWM.h" +#include "QEI.h" // Includes library for encoder Ticker motor; AnalogIn pot1(A1); AnalogIn pot2(A2); -InterruptIn button2(SW2); -InterruptIn button3(SW3); +InterruptIn button1(D0); +InterruptIn button2(D1); +InterruptIn emergencybutton(SW2); /* This is not yet implemented! +The button SW2 on the K64F is the emergency button: if you press this, +everything will abort as soon as possible +*/ + +DigitalIn pin8(D8); // Encoder 1 u3 +DigitalIn pin9(D9); // Encoder 1 A +DigitalIn pin10(D10); // Encoder 2 u3 +DigitalIn pin11(D11); // Encoder 2 A +DigitalIn pin12(D12); // Encoder 3 u3 +DigitalIn pin13(D13); // Encoder 3 A DigitalOut pin2(D2); // Motor 3 direction FastPWM pin3(D3); // Motor 3 pwm @@ -16,48 +28,41 @@ DigitalOut pin7(D7); // Motor 1 direction //float u1 = pot1; -DigitalIn pin8(D8); // Encoder 1 B -DigitalIn pin9(D9); // Encoder 1 A -DigitalIn pin10(D10); // Encoder 2 B -DigitalIn pin11(D11); // Encoder 2 A -DigitalIn pin12(D12); // Encoder 3 B -DigitalIn pin13(D13); // Encoder 3 A - -float b; +float u3 = 0.0; // Normalised variable for the movement of motor 3 -void draaisnel() - { if(button2 == 1) - { //Interrupt voor rotatierichting clockwise - b = 0.4; //In stapjes van 0.1 - pin3 = fabs(b); - } - else if (button2 == 0) - { //Interrupt voor rotatierichting +void draaibuttons() +{ /* Pressing button 2 concludes in a change of speed. While button 1 is pressed, + the direction of change of speed is reversed. So pressing button 1 and 2 + simultaneously results for the turning speed of motor 3 in a slower movement, + and eventually the motor will turn the other way around. + */ + if (button1 == 1) + { if (button2 == 1) + { u3 = u3 + 0.1; //In stapjes van 0.1 + pin3 = fabs(u3); + if (u3>1.0) + { u3 = 1.0; + } } } -void draailangzaam() - { if(button3 == 1) - { //Interrupt voor minder snelheid - b = -0.4; - pin3 = fabs(b); + else if (button1 == 0) + { if (button2 == 1) + { u3 = u3 - 0.1; + pin3 = fabs(u3); + if (u3>1.0) + { u3 = 1.0; + } } } +} -void draai() -{ if (b>0) - { pin2 = true; - b = b+0.1; - } - else if(b<0) - { pin2 = false; - b = b-0.1; - } - else - { pin3 = 0; - } - pin3 = fabs(b); - - float u1 = 2.0*(pot1 - 0.5); +void draai() +/* Function for the movement of all motors, using the potmeters for the moving + direction and speed of motor 1 and 2, and using button 1 and 2 on the biorobotics + shield for the moving direction and speed of motor 3. +*/ +{ + float u1 = 2.0*(pot1 - 0.5); // Normalised variable for the movement of motor 1 if (u1>0) { pin4 = true; } @@ -66,20 +71,31 @@ } pin5 = fabs(u1); - float u2 = 2.0*(pot2 - 0.5); - if (u2>0) + float u2 = 2.0*(pot2 - 0.5); // Normalised variable for the movement of motor 2 + if (u2<0) { pin7 = true; } - else if(u2<0) + else if(u2>0) { pin7 = false; } - pin6 = fabs(u2); + pin6 = fabs(u2); + + if (u3>0) + { pin2 = true; + } + else if(u3<0) + { pin2 = false; + } + else + { pin3 = 0; + } } -int main(){ - pin3 = 0.0; + +int main() +{ pin5.period(1.0/10000); - button2.rise(&draaisnel); // interrupt koppelen - button3.rise(&draailangzaam); + button1.rise(&draaibuttons); + button2.rise(&draaibuttons); pin3.period_us(50); motor.attach(draai, 0.001); @@ -89,7 +105,7 @@ pin6.period_us(50); motor.attach(draai, 0.001); - while(true){ - + while(true) + { } } \ No newline at end of file