ticker aan de praat proberen te krijgen
Dependencies: MODSERIAL mbed QEI
main.cpp@2:f70238e93bf0, 2016-10-18 (annotated)
- Committer:
- sivuu
- Date:
- Tue Oct 18 14:18:29 2016 +0000
- Revision:
- 2:f70238e93bf0
- Parent:
- 1:9a06f249ccf0
- Child:
- 3:2a721ba5f4f5
met werkende encoder! probleem zat in verkeerde encoder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sivuu | 0:e97d89c1bf32 | 1 | #include <iostream> |
sivuu | 0:e97d89c1bf32 | 2 | #include <cstdio> |
sivuu | 0:e97d89c1bf32 | 3 | #include <ctime> |
sivuu | 0:e97d89c1bf32 | 4 | #include "MODSERIAL.h" |
sivuu | 0:e97d89c1bf32 | 5 | #include "stdio.h" |
sivuu | 1:9a06f249ccf0 | 6 | #include "mbed.h" //standaard bieb mbed |
sivuu | 1:9a06f249ccf0 | 7 | #include "QEI.h" //bieb voor encoderfuncties in c++ |
sivuu | 2:f70238e93bf0 | 8 | InterruptIn sw3(D9); |
sivuu | 1:9a06f249ccf0 | 9 | DigitalIn encoder1A(D13); |
sivuu | 1:9a06f249ccf0 | 10 | DigitalIn encoder1B(D12); |
sivuu | 1:9a06f249ccf0 | 11 | DigitalIn encoder2A(D11); |
sivuu | 1:9a06f249ccf0 | 12 | DigitalIn encoder2B(D10); |
sivuu | 2:f70238e93bf0 | 13 | DigitalIn button_cw(SW2); |
sivuu | 2:f70238e93bf0 | 14 | DigitalIn button_ccw(SW3); |
sivuu | 1:9a06f249ccf0 | 15 | MODSERIAL pc(USBTX, USBRX); |
sivuu | 1:9a06f249ccf0 | 16 | DigitalOut richting_motor1(D4); |
sivuu | 1:9a06f249ccf0 | 17 | PwmOut pwm_motor1(D5); |
sivuu | 1:9a06f249ccf0 | 18 | DigitalOut richting_motor2(D7); |
sivuu | 1:9a06f249ccf0 | 19 | PwmOut pwm_motor2(D6); |
sivuu | 0:e97d89c1bf32 | 20 | |
sivuu | 2:f70238e93bf0 | 21 | volatile double current_position = 0; // current position is 0 op het begin |
sivuu | 2:f70238e93bf0 | 22 | volatile double previous_position = 0; // previous position is 0 op het begin |
sivuu | 2:f70238e93bf0 | 23 | volatile bool tickerflag; //bool zorgt er voor dat de tickerflag alleen 1 of 0 kan zijn (true or false) |
sivuu | 2:f70238e93bf0 | 24 | volatile double snelheid; |
sivuu | 2:f70238e93bf0 | 25 | double ticker_TS=0.01; // zorgt voor een tijdstap van de ticker van 0.01 seconde |
sivuu | 2:f70238e93bf0 | 26 | volatile double timepassed=0; //de waarde van hoeveel tijd er verstreken is |
sivuu | 2:f70238e93bf0 | 27 | Ticker t; // maakt ticker t aan |
sivuu | 0:e97d89c1bf32 | 28 | |
sivuu | 2:f70238e93bf0 | 29 | const int CW = 2.5; //definitie rechtsom 'lage waarde' |
sivuu | 2:f70238e93bf0 | 30 | const int CCW =0; //definitie linksom 'hoge waarde' |
sivuu | 2:f70238e93bf0 | 31 | const float gearboxratio=131.25; // gearboxratio van encoder naar motor |
sivuu | 2:f70238e93bf0 | 32 | const int rev_rond=32; // aantal revoluties per omgang van de encoder |
sivuu | 1:9a06f249ccf0 | 33 | |
sivuu | 2:f70238e93bf0 | 34 | const float a=10.0; //voltingang motor 1 |
sivuu | 2:f70238e93bf0 | 35 | const float b=10.0;//voltingang motor 2 |
sivuu | 2:f70238e93bf0 | 36 | |
sivuu | 2:f70238e93bf0 | 37 | |
sivuu | 2:f70238e93bf0 | 38 | |
sivuu | 2:f70238e93bf0 | 39 | void tickerfunctie() // maakt een ticker functie aan |
sivuu | 0:e97d89c1bf32 | 40 | { |
sivuu | 2:f70238e93bf0 | 41 | tickerflag = 1; // het enige wat deze functie doet is zorgen dat tickerflag 1 is |
sivuu | 0:e97d89c1bf32 | 42 | } |
sivuu | 0:e97d89c1bf32 | 43 | |
sivuu | 0:e97d89c1bf32 | 44 | int main() |
sivuu | 0:e97d89c1bf32 | 45 | { |
sivuu | 0:e97d89c1bf32 | 46 | pc.baud(115200); |
sivuu | 2:f70238e93bf0 | 47 | QEI Encoder2(D12,D13, NC, rev_rond); // maakt een encoder aan! D12/D13 ingangen, rev_rond zijn aantal pulsen per revolutie! Bovenaan in te stellen. |
sivuu | 2:f70238e93bf0 | 48 | QEI Encoder1(D10,D11, NC, rev_rond); |
sivuu | 2:f70238e93bf0 | 49 | float counts_encoder1; //variabele counts aanmaken |
sivuu | 2:f70238e93bf0 | 50 | float rev_counts_motor1; //variabele motor rondjes aanmaken in radialen!! |
sivuu | 2:f70238e93bf0 | 51 | float counts_encoder2; |
sivuu | 2:f70238e93bf0 | 52 | float rev_counts_motor2; |
sivuu | 2:f70238e93bf0 | 53 | t.attach(&tickerfunctie,ticker_TS); // attacht de ticker met een tick van 0.01 seconde (gelijk aan de tijdstap) |
sivuu | 2:f70238e93bf0 | 54 | while (true){ |
sivuu | 2:f70238e93bf0 | 55 | if (button_cw==0) { // als knopje wordt ingedrukt |
sivuu | 2:f70238e93bf0 | 56 | if ( tickerflag == 1) // als de ticker flag 1 is gaat dit loopje lopen. |
sivuu | 1:9a06f249ccf0 | 57 | { |
sivuu | 2:f70238e93bf0 | 58 | richting_motor1 = CW; |
sivuu | 2:f70238e93bf0 | 59 | pwm_motor1 = a; |
sivuu | 2:f70238e93bf0 | 60 | |
sivuu | 2:f70238e93bf0 | 61 | counts_encoder1 = Encoder1.getPulses(); //tellen van de pulsen in |
sivuu | 2:f70238e93bf0 | 62 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); //berekenen van het aantal rondjes van motor. Gedeeld door gearboxratio en rev rond, om naar motorrondjes te gaan in plaats van pulsen van encoder! |
sivuu | 2:f70238e93bf0 | 63 | pc.printf("motor rondjes motor1: %f \r\n", rev_counts_motor1); |
sivuu | 2:f70238e93bf0 | 64 | |
sivuu | 1:9a06f249ccf0 | 65 | previous_position = current_position; |
sivuu | 2:f70238e93bf0 | 66 | current_position = rev_counts_motor1; |
sivuu | 0:e97d89c1bf32 | 67 | |
sivuu | 2:f70238e93bf0 | 68 | snelheid = (current_position - previous_position) / ticker_TS; |
sivuu | 2:f70238e93bf0 | 69 | pc.printf("snelheid is: %f \r\n", snelheid); |
sivuu | 1:9a06f249ccf0 | 70 | |
sivuu | 1:9a06f249ccf0 | 71 | tickerflag = 0; |
sivuu | 1:9a06f249ccf0 | 72 | } |
sivuu | 2:f70238e93bf0 | 73 | |
sivuu | 2:f70238e93bf0 | 74 | } |
sivuu | 2:f70238e93bf0 | 75 | else{ |
sivuu | 2:f70238e93bf0 | 76 | pwm_motor2=0; |
sivuu | 2:f70238e93bf0 | 77 | pwm_motor1=0; |
sivuu | 2:f70238e93bf0 | 78 | } |
sivuu | 0:e97d89c1bf32 | 79 | } |
sivuu | 0:e97d89c1bf32 | 80 | } |