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
main.cpp@20:695140b8db2f, 2018-10-22 (annotated)
- Committer:
- efvanmarrewijk
- Date:
- Mon Oct 22 14:29:37 2018 +0000
- Revision:
- 20:695140b8db2f
- Parent:
- 19:1d0b25d4d775
- Parent:
- 18:ca084c362855
- Child:
- 21:363271dcfe1f
Partially merged version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
efvanmarrewijk | 16:720365110953 | 1 | // Inclusion of libraries |
Ramonwaninge | 0:3ea1bbfbeaae | 2 | #include "mbed.h" |
efvanmarrewijk | 11:3efd6a324f16 | 3 | #include "FastPWM.h" |
efvanmarrewijk | 11:3efd6a324f16 | 4 | #include "QEI.h" // Includes library for encoder |
efvanmarrewijk | 13:6556cd086d07 | 5 | #include "MODSERIAL.h" |
efvanmarrewijk | 13:6556cd086d07 | 6 | #include "HIDScope.h" |
efvanmarrewijk | 13:6556cd086d07 | 7 | #include "BiQuad.h" |
Ramonwaninge | 0:3ea1bbfbeaae | 8 | |
efvanmarrewijk | 14:e21cb701ccb8 | 9 | // Input |
efvanmarrewijk | 9:65c52c1f4a57 | 10 | |
efvanmarrewijk | 14:e21cb701ccb8 | 11 | // Output |
Ramonwaninge | 2:d8a552d1d33a | 12 | |
Ramonwaninge | 2:d8a552d1d33a | 13 | |
efvanmarrewijk | 16:720365110953 | 14 | // Utilisation of libraries |
efvanmarrewijk | 16:720365110953 | 15 | MODSERIAL pc(USBTX, USBRX); |
efvanmarrewijk | 18:ca084c362855 | 16 | QEI Encoder1(D11,D10,NC,4200); // Counterclockwise motor rotation is the positive direction |
efvanmarrewijk | 18:ca084c362855 | 17 | QEI Encoder2(D9,D8,NC,4200); // Counterclockwise motor rotation is the positive direction |
efvanmarrewijk | 18:ca084c362855 | 18 | QEI Encoder3(D13,D12,NC,4200); // Counterclockwise motor rotation is the positive direction |
efvanmarrewijk | 20:695140b8db2f | 19 | Ticker motor; |
efvanmarrewijk | 9:65c52c1f4a57 | 20 | |
efvanmarrewijk | 16:720365110953 | 21 | // Global variables |
efvanmarrewijk | 18:ca084c362855 | 22 | const float pi = 3.14159265358979; |
efvanmarrewijk | 11:3efd6a324f16 | 23 | float u3 = 0.0; // Normalised variable for the movement of motor 3 |
efvanmarrewijk | 16:720365110953 | 24 | |
efvanmarrewijk | 16:720365110953 | 25 | // Functions |
efvanmarrewijk | 18:ca084c362855 | 26 | void Encoderinput() |
efvanmarrewijk | 18:ca084c362855 | 27 | { int counts1; |
efvanmarrewijk | 18:ca084c362855 | 28 | int counts2; |
efvanmarrewijk | 18:ca084c362855 | 29 | int counts3; |
efvanmarrewijk | 18:ca084c362855 | 30 | float angle1; |
efvanmarrewijk | 18:ca084c362855 | 31 | float angle2; |
efvanmarrewijk | 18:ca084c362855 | 32 | float angle3; |
efvanmarrewijk | 18:ca084c362855 | 33 | counts1 = Encoder1.getPulses(); |
efvanmarrewijk | 18:ca084c362855 | 34 | counts2 = Encoder2.getPulses(); |
efvanmarrewijk | 18:ca084c362855 | 35 | counts3 = Encoder3.getPulses(); |
efvanmarrewijk | 18:ca084c362855 | 36 | angle1 = ((float)counts1*2.0*pi)/4200.0; |
efvanmarrewijk | 18:ca084c362855 | 37 | angle2 = ((float)counts2*2.0*pi)/4200.0; |
efvanmarrewijk | 18:ca084c362855 | 38 | angle3 = ((float)counts3*2.0*pi)/4200.0; |
efvanmarrewijk | 18:ca084c362855 | 39 | |
efvanmarrewijk | 18:ca084c362855 | 40 | pc.printf("Counts1: %i Angle1: %f Counts2: %i Angle2: %f\r\n",counts1,angle1,counts2,angle2); |
efvanmarrewijk | 18:ca084c362855 | 41 | } |
efvanmarrewijk | 16:720365110953 | 42 | |
efvanmarrewijk | 11:3efd6a324f16 | 43 | void draaibuttons() |
efvanmarrewijk | 11:3efd6a324f16 | 44 | { /* Pressing button 2 concludes in a change of speed. While button 1 is pressed, |
efvanmarrewijk | 11:3efd6a324f16 | 45 | the direction of change of speed is reversed. So pressing button 1 and 2 |
efvanmarrewijk | 11:3efd6a324f16 | 46 | simultaneously results for the turning speed of motor 3 in a slower movement, |
efvanmarrewijk | 11:3efd6a324f16 | 47 | and eventually the motor will turn the other way around. |
efvanmarrewijk | 11:3efd6a324f16 | 48 | */ |
efvanmarrewijk | 14:e21cb701ccb8 | 49 | if (button1 == 1 && button2 == 1) |
efvanmarrewijk | 14:e21cb701ccb8 | 50 | { u3 = u3 + 0.1f; //In stapjes van 0.1 |
efvanmarrewijk | 13:6556cd086d07 | 51 | if (u3>1.0f) |
efvanmarrewijk | 13:6556cd086d07 | 52 | { u3 = 1.0f; |
efvanmarrewijk | 11:3efd6a324f16 | 53 | } |
efvanmarrewijk | 10:ac36f9a204dd | 54 | } |
efvanmarrewijk | 14:e21cb701ccb8 | 55 | |
efvanmarrewijk | 14:e21cb701ccb8 | 56 | else if (button1 == 0 && button2 == 1) |
efvanmarrewijk | 13:6556cd086d07 | 57 | { u3 = u3 - 0.1f; |
efvanmarrewijk | 13:6556cd086d07 | 58 | if (u3>1.0f) |
efvanmarrewijk | 13:6556cd086d07 | 59 | { u3 = 1.0f; |
efvanmarrewijk | 11:3efd6a324f16 | 60 | } |
efvanmarrewijk | 9:65c52c1f4a57 | 61 | } |
efvanmarrewijk | 14:e21cb701ccb8 | 62 | |
efvanmarrewijk | 11:3efd6a324f16 | 63 | void draai() |
efvanmarrewijk | 11:3efd6a324f16 | 64 | /* Function for the movement of all motors, using the potmeters for the moving |
efvanmarrewijk | 11:3efd6a324f16 | 65 | direction and speed of motor 1 and 2, and using button 1 and 2 on the biorobotics |
efvanmarrewijk | 11:3efd6a324f16 | 66 | shield for the moving direction and speed of motor 3. |
efvanmarrewijk | 11:3efd6a324f16 | 67 | */ |
efvanmarrewijk | 11:3efd6a324f16 | 68 | { |
efvanmarrewijk | 11:3efd6a324f16 | 69 | float u1 = 2.0*(pot1 - 0.5); // Normalised variable for the movement of motor 1 |
efvanmarrewijk | 6:3c9569087274 | 70 | if (u1>0) |
efvanmarrewijk | 6:3c9569087274 | 71 | { pin4 = true; |
efvanmarrewijk | 6:3c9569087274 | 72 | } |
efvanmarrewijk | 6:3c9569087274 | 73 | else if(u1<0) |
efvanmarrewijk | 6:3c9569087274 | 74 | { pin4 = false; |
efvanmarrewijk | 6:3c9569087274 | 75 | } |
efvanmarrewijk | 6:3c9569087274 | 76 | pin5 = fabs(u1); |
efvanmarrewijk | 6:3c9569087274 | 77 | |
efvanmarrewijk | 11:3efd6a324f16 | 78 | float u2 = 2.0*(pot2 - 0.5); // Normalised variable for the movement of motor 2 |
efvanmarrewijk | 11:3efd6a324f16 | 79 | if (u2<0) |
efvanmarrewijk | 6:3c9569087274 | 80 | { pin7 = true; |
efvanmarrewijk | 6:3c9569087274 | 81 | } |
efvanmarrewijk | 11:3efd6a324f16 | 82 | else if(u2>0) |
efvanmarrewijk | 6:3c9569087274 | 83 | { pin7 = false; |
efvanmarrewijk | 6:3c9569087274 | 84 | } |
efvanmarrewijk | 11:3efd6a324f16 | 85 | pin6 = fabs(u2); |
efvanmarrewijk | 11:3efd6a324f16 | 86 | |
efvanmarrewijk | 11:3efd6a324f16 | 87 | if (u3>0) |
efvanmarrewijk | 11:3efd6a324f16 | 88 | { pin2 = true; |
efvanmarrewijk | 11:3efd6a324f16 | 89 | } |
efvanmarrewijk | 11:3efd6a324f16 | 90 | else if(u3<0) |
efvanmarrewijk | 11:3efd6a324f16 | 91 | { pin2 = false; |
efvanmarrewijk | 11:3efd6a324f16 | 92 | } |
efvanmarrewijk | 11:3efd6a324f16 | 93 | else |
efvanmarrewijk | 11:3efd6a324f16 | 94 | { pin3 = 0; |
efvanmarrewijk | 14:e21cb701ccb8 | 95 | } |
efvanmarrewijk | 14:e21cb701ccb8 | 96 | pin3 = fabs(u3); |
Ramonwaninge | 3:d39285fdd103 | 97 | } |
efvanmarrewijk | 11:3efd6a324f16 | 98 | |
efvanmarrewijk | 16:720365110953 | 99 | // Main program |
efvanmarrewijk | 11:3efd6a324f16 | 100 | int main() |
efvanmarrewijk | 18:ca084c362855 | 101 | { |
efvanmarrewijk | 14:e21cb701ccb8 | 102 | pc.baud(115200); |
efvanmarrewijk | 18:ca084c362855 | 103 | |
efvanmarrewijk | 17:0ae9e8c958f8 | 104 | |
efvanmarrewijk | 16:720365110953 | 105 | while (true) |
efvanmarrewijk | 18:ca084c362855 | 106 | { Encoderinput(); |
Ramonwaninge | 3:d39285fdd103 | 107 | } |
Ramonwaninge | 0:3ea1bbfbeaae | 108 | } |