j
Dependencies: Encoder HIDScope mbed
Fork of Motor_component_testing by
main.cpp@3:c6b8499a9021, 2014-10-27 (annotated)
- Committer:
- phgbartels
- Date:
- Mon Oct 27 12:13:24 2014 +0000
- Revision:
- 3:c6b8499a9021
- Parent:
- 2:b7d757570748
- Child:
- 4:71ada7709c46
Opschonen script + omschrijven tot 2 motoren
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phgbartels | 0:2240263f7068 | 1 | #include "mbed.h" |
phgbartels | 0:2240263f7068 | 2 | #include "encoder.h" |
phgbartels | 3:c6b8499a9021 | 3 | #include "HIDScope.h" |
phgbartels | 0:2240263f7068 | 4 | #include "PwmOut.h" |
phgbartels | 3:c6b8499a9021 | 5 | /*definieren pinnen Motor 1*/ |
phgbartels | 0:2240263f7068 | 6 | #define M1_PWM PTA5 |
phgbartels | 0:2240263f7068 | 7 | #define M1_DIR PTA4 |
phgbartels | 3:c6b8499a9021 | 8 | #define M2_PWM PTC8 |
phgbartels | 3:c6b8499a9021 | 9 | #define M2_DIR PTC9 |
phgbartels | 3:c6b8499a9021 | 10 | /*Definieren minimale waarden PWM per motor*/ |
phgbartels | 3:c6b8499a9021 | 11 | #define PWM1_min_30 0.529 /*met koppelstuk!*/ |
phgbartels | 3:c6b8499a9021 | 12 | #define PWM2_min_50 0.519 /*aanpassen! Klopt nog niet met koppelstuk*/ |
phgbartels | 3:c6b8499a9021 | 13 | /*Definieren om de hoeveel seconden er gegevens naar de HID-scope gestuurd worden.*/ |
phgbartels | 3:c6b8499a9021 | 14 | #define TSAMP 0.005 |
phgbartels | 0:2240263f7068 | 15 | |
BArendshorst | 1:0471de635f87 | 16 | Encoder motor1(PTD3,PTD1); |
phgbartels | 3:c6b8499a9021 | 17 | Encoder motor2(PTD5, PTD0); |
phgbartels | 0:2240263f7068 | 18 | PwmOut pwm_motor1(M1_PWM); |
phgbartels | 3:c6b8499a9021 | 19 | PwmOut pwm_motor2(M2_PWM); |
phgbartels | 0:2240263f7068 | 20 | DigitalOut motordir1(M1_DIR); |
phgbartels | 3:c6b8499a9021 | 21 | DigitalOut motordir2(M2_DIR); |
phgbartels | 0:2240263f7068 | 22 | DigitalOut LED(LED_GREEN); |
phgbartels | 0:2240263f7068 | 23 | |
phgbartels | 3:c6b8499a9021 | 24 | volatile bool looptimerflag; |
phgbartels | 3:c6b8499a9021 | 25 | int PWM1_percentage = 1; |
phgbartels | 3:c6b8499a9021 | 26 | int PWM2_percentage = 1; |
phgbartels | 3:c6b8499a9021 | 27 | float PWM1 = ((1-PWM1_min_30)/100)*PWM1_percentage + PWM1_min_30; |
phgbartels | 3:c6b8499a9021 | 28 | float PWM2 = ((1-PWM2_min_50)/100)*PWM2_percentage + PWM1_min_30; |
phgbartels | 3:c6b8499a9021 | 29 | |
phgbartels | 3:c6b8499a9021 | 30 | HIDScope scope(3); |
phgbartels | 3:c6b8499a9021 | 31 | |
phgbartels | 3:c6b8499a9021 | 32 | //Ticker tick; |
phgbartels | 3:c6b8499a9021 | 33 | /*void PWMc() |
phgbartels | 3:c6b8499a9021 | 34 | { |
phgbartels | 3:c6b8499a9021 | 35 | PWM = abs(PWM-0.1); |
phgbartels | 3:c6b8499a9021 | 36 | pwm_motor1.write(PWM); |
phgbartels | 3:c6b8499a9021 | 37 | scope.set(0, motor1.getPosition()); |
phgbartels | 3:c6b8499a9021 | 38 | scope.set(1, DigitalIn(PTD3)); |
phgbartels | 3:c6b8499a9021 | 39 | scope.set(2, DigitalIn(PTD1)); |
phgbartels | 3:c6b8499a9021 | 40 | scope.send(); |
phgbartels | 3:c6b8499a9021 | 41 | }*/ |
phgbartels | 3:c6b8499a9021 | 42 | |
phgbartels | 3:c6b8499a9021 | 43 | void setlooptimerflag(void) //Ticker maakt een constructie die zoveel keer per seconde een functie uitvoerd, met sampingling freq TSAMP |
phgbartels | 3:c6b8499a9021 | 44 | { |
phgbartels | 3:c6b8499a9021 | 45 | looptimerflag = true; |
phgbartels | 3:c6b8499a9021 | 46 | } |
phgbartels | 0:2240263f7068 | 47 | |
phgbartels | 0:2240263f7068 | 48 | int main() { |
phgbartels | 3:c6b8499a9021 | 49 | //tick.attach(&PWMc, 5); |
phgbartels | 3:c6b8499a9021 | 50 | |
BArendshorst | 1:0471de635f87 | 51 | pwm_motor1.period_us(100); |
phgbartels | 3:c6b8499a9021 | 52 | pwm_motor2.period_us(100); |
phgbartels | 3:c6b8499a9021 | 53 | pwm_motor1.write(PWM1); |
phgbartels | 3:c6b8499a9021 | 54 | pwm_motor2.write(PWM2); |
phgbartels | 0:2240263f7068 | 55 | LED = 0; |
phgbartels | 3:c6b8499a9021 | 56 | motordir1 = 0; |
phgbartels | 3:c6b8499a9021 | 57 | motordir2 = 0; |
phgbartels | 3:c6b8499a9021 | 58 | |
phgbartels | 3:c6b8499a9021 | 59 | Ticker looptimer; // looptimer = variabele van het type ticker(niet int of float), Daarmee is looptimer een classe. Omdat een classe is heeft het methods oftewel functies, zoals attach. Setlooptimerflag is een functie |
phgbartels | 3:c6b8499a9021 | 60 | looptimer.attach(setlooptimerflag,TSAMP); // attach betekend het koppelen van een functie aan een classe. |
phgbartels | 3:c6b8499a9021 | 61 | while(true) { |
phgbartels | 3:c6b8499a9021 | 62 | |
phgbartels | 0:2240263f7068 | 63 | //wait(3); |
BArendshorst | 1:0471de635f87 | 64 | //pwm_motor1.write(0.4); |
phgbartels | 0:2240263f7068 | 65 | LED = 1; |
phgbartels | 0:2240263f7068 | 66 | //wait(1); |
phgbartels | 0:2240263f7068 | 67 | //PWM = PWM + 0.05; |
phgbartels | 0:2240263f7068 | 68 | |
phgbartels | 0:2240263f7068 | 69 | //pwm_motor1.pulsewidth_us(50); |
phgbartels | 3:c6b8499a9021 | 70 | while(!looptimerflag) //Hierdoor zeg je eigenlijk tegen je systeem; voer dit script uit na elke TSAMP s. |
phgbartels | 3:c6b8499a9021 | 71 | { |
phgbartels | 3:c6b8499a9021 | 72 | /*do nothing*/ //Zolang dit waar is, doe je niets. |
phgbartels | 0:2240263f7068 | 73 | } |
phgbartels | 3:c6b8499a9021 | 74 | looptimerflag = false; //clear flag |
phgbartels | 3:c6b8499a9021 | 75 | |
phgbartels | 0:2240263f7068 | 76 | scope.set(0, motor1.getPosition()); |
phgbartels | 3:c6b8499a9021 | 77 | scope.set(1, motor1.getSpeed()); |
phgbartels | 3:c6b8499a9021 | 78 | scope.set(2, motor2.getPosition()); |
phgbartels | 3:c6b8499a9021 | 79 | scope.set(3, motor2.getSpeed()); |
phgbartels | 0:2240263f7068 | 80 | scope.send(); |
phgbartels | 0:2240263f7068 | 81 | } |
phgbartels | 0:2240263f7068 | 82 | } |