![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Using HIDScope for P(I)D controller
Dependencies: FastPWM HIDScope MODSERIAL QEI biquadFilter mbed
Fork of PES_tutorial_5 by
main.cpp@0:2e33035d4e86, 2018-10-15 (annotated)
- Committer:
- 1856413
- Date:
- Mon Oct 15 08:15:14 2018 +0000
- Revision:
- 0:2e33035d4e86
- Child:
- 1:c19fc63d555f
Overgenomen van Milestone 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
1856413 | 0:2e33035d4e86 | 1 | #include "mbed.h" |
1856413 | 0:2e33035d4e86 | 2 | #include "FastPWM.h" // FastPWM library |
1856413 | 0:2e33035d4e86 | 3 | //#include "MODSERIAL.h" |
1856413 | 0:2e33035d4e86 | 4 | //MODSERIAL pc(USBTX, USBRX); |
1856413 | 0:2e33035d4e86 | 5 | DigitalOut motor2_direction(D4); |
1856413 | 0:2e33035d4e86 | 6 | DigitalOut motor1_direction(D7); |
1856413 | 0:2e33035d4e86 | 7 | AnalogIn pot1(A4); |
1856413 | 0:2e33035d4e86 | 8 | AnalogIn pot2(A5); |
1856413 | 0:2e33035d4e86 | 9 | FastPWM motor2_pwm(D5); //FastPWM input, PES lecture 2 |
1856413 | 0:2e33035d4e86 | 10 | FastPWM motor1_pwm(D6); |
1856413 | 0:2e33035d4e86 | 11 | Ticker MotorInterrupt; |
1856413 | 0:2e33035d4e86 | 12 | |
1856413 | 0:2e33035d4e86 | 13 | void Motor() |
1856413 | 0:2e33035d4e86 | 14 | { |
1856413 | 0:2e33035d4e86 | 15 | // Aflezen Potentiometers |
1856413 | 0:2e33035d4e86 | 16 | volatile float pwm_pct1 = pot1.read(); // Aflezen PotMeter 1 (volatile omdat je altijd de potmeter wil blijven lezen) |
1856413 | 0:2e33035d4e86 | 17 | volatile float pwm_pct2 = pot2.read(); // Aflezen PotMeter 2 |
1856413 | 0:2e33035d4e86 | 18 | // Interval van (0,1) scalen naar (-1,1) |
1856413 | 0:2e33035d4e86 | 19 | volatile float a1 = (2.0*pwm_pct1)- 1; // POT1. pwm_pct1 wordt gescaled, en offset number -1 wordt erbij opgeteld. |
1856413 | 0:2e33035d4e86 | 20 | volatile float a2 = (2.0*pwm_pct2)- 1; // POT2. Idem. |
1856413 | 0:2e33035d4e86 | 21 | // Motor Direction bepalen |
1856413 | 0:2e33035d4e86 | 22 | motor1_direction = a1 > 0; // Bool variable. Al gespecificeerd als direction pin, daarom hoeft er geen bool voor. |
1856413 | 0:2e33035d4e86 | 23 | motor2_direction = a2 > 0; // Indien aX = 1, dan TRUE. Indien aX = -1, dan FALSE. |
1856413 | 0:2e33035d4e86 | 24 | // Motor PWM |
1856413 | 0:2e33035d4e86 | 25 | motor1_pwm = fabs(a1); // Range van 0.0f tot 1.0f |
1856413 | 0:2e33035d4e86 | 26 | motor2_pwm = fabs(a2); |
1856413 | 0:2e33035d4e86 | 27 | } |
1856413 | 0:2e33035d4e86 | 28 | |
1856413 | 0:2e33035d4e86 | 29 | |
1856413 | 0:2e33035d4e86 | 30 | int main() |
1856413 | 0:2e33035d4e86 | 31 | { |
1856413 | 0:2e33035d4e86 | 32 | motor1_pwm.period_us(60.0); // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to done once), FastPWM variabele |
1856413 | 0:2e33035d4e86 | 33 | MotorInterrupt.attach(Motor, 0.5); // Ticker die de functie Motor aanroept elke halve seconde, meer tijd is tragere respons op potmeter |
1856413 | 0:2e33035d4e86 | 34 | while(true){} // Endless loop |
1856413 | 0:2e33035d4e86 | 35 | } |