PID en biquad filter implemented and outcommented
Dependencies: Motor_with_encoder MODSERIAL mbed QEI
Fork of Tutorial_lecture5_pin_pot_motor by
main.cpp@3:4520d0ca4e56, 2017-10-06 (annotated)
- Committer:
- vera1
- Date:
- Fri Oct 06 12:14:32 2017 +0000
- Revision:
- 3:4520d0ca4e56
- Parent:
- 2:80177e90c1e6
- Child:
- 4:abdc2d24d0bc
PID added, commented out
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vera1 | 0:bc949f735b8d | 1 | #include "mbed.h" |
vera1 | 0:bc949f735b8d | 2 | #include "MODSERIAL.h" |
vera1 | 0:bc949f735b8d | 3 | #include "encoder.h" |
vera1 | 0:bc949f735b8d | 4 | // this program can turn the motor clockwise or counterclockwise depending on the value p or n typed into the terminal. with 's' you can stop the motor |
vera1 | 0:bc949f735b8d | 5 | |
vera1 | 0:bc949f735b8d | 6 | MODSERIAL pc(USBTX,USBRX); |
vera1 | 0:bc949f735b8d | 7 | PwmOut motorspeed(D5); |
vera1 | 0:bc949f735b8d | 8 | DigitalOut motorposition(D4); |
vera1 | 0:bc949f735b8d | 9 | DigitalOut led1(D8); |
vera1 | 1:e0f6cdefcd6e | 10 | DigitalOut led2(D11); |
vera1 | 0:bc949f735b8d | 11 | AnalogIn pot(A1); |
vera1 | 2:80177e90c1e6 | 12 | DigitalIn button1(D13); |
vera1 | 2:80177e90c1e6 | 13 | DigitalIn button2(D12); |
vera1 | 0:bc949f735b8d | 14 | Ticker potmeterreadout; |
vera1 | 1:e0f6cdefcd6e | 15 | Ticker encoderreadout; |
vera1 | 1:e0f6cdefcd6e | 16 | Encoder motor1(PTD0,PTC4); // motor 1 is from encoder, otherwise it would be a pain in the ass to change all that code to motorencoder |
vera1 | 0:bc949f735b8d | 17 | |
vera1 | 0:bc949f735b8d | 18 | |
vera1 | 3:4520d0ca4e56 | 19 | float PwmPeriod = 0.0001f; |
vera1 | 3:4520d0ca4e56 | 20 | /*const double M1_TS = 0.01f; // timestep |
vera1 | 3:4520d0ca4e56 | 21 | const double M1_KP = 2.5, M1_KI = 1.0, M1_KD = 0.5; // controller gains for motor 1 |
vera1 | 3:4520d0ca4e56 | 22 | double m1_err_int = 0, m1_prev_err = 0; // initiallize errors |
vera1 | 3:4520d0ca4e56 | 23 | const double M1_F_A1 = 1.0, M1_F_A2 = 2.0, M1_F_B0 = 1.0, M1_F_B1 = 3.0, M1_F_B2 = 4.0; // derivative filter coefficients |
vera1 | 3:4520d0ca4e56 | 24 | double m1_f_v1 = 0.0, m1_f_v2 = 0.0; // filter variables |
vera1 | 3:4520d0ca4e56 | 25 | |
vera1 | 3:4520d0ca4e56 | 26 | // biquad filter for emg signals |
vera1 | 3:4520d0ca4e56 | 27 | double biquad(double u, double&v1, double&v2, const double a1, const double a2, const double b0, const double b1, const double b2){ |
vera1 | 3:4520d0ca4e56 | 28 | double v = u - a1*v1 - a2*v2; |
vera1 | 3:4520d0ca4e56 | 29 | double y = b0*v + b1*v1 + b2*v2; |
vera1 | 3:4520d0ca4e56 | 30 | v2 = v1; |
vera1 | 3:4520d0ca4e56 | 31 | v1 = v; |
vera1 | 3:4520d0ca4e56 | 32 | return y; |
vera1 | 3:4520d0ca4e56 | 33 | } |
vera1 | 3:4520d0ca4e56 | 34 | |
vera1 | 3:4520d0ca4e56 | 35 | // PID controller function |
vera1 | 3:4520d0ca4e56 | 36 | double PID(double e, const double Kp, const double Ki, const double Kd, double Ts, double&e_int, double &e_prev, double &f_v1, double &f_v2, const double f_a1, const double f_a2, const double f_b0, const double f_b1, const double f_b2){ |
vera1 | 3:4520d0ca4e56 | 37 | double e_der = (e - e_prev)/Ts; // derivative |
vera1 | 3:4520d0ca4e56 | 38 | e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2); |
vera1 | 3:4520d0ca4e56 | 39 | e_prev = e; |
vera1 | 3:4520d0ca4e56 | 40 | e_int = e_int + Ts*e; // integral |
vera1 | 3:4520d0ca4e56 | 41 | return Kp*e + Ki*e_int + Kd*e_der; //PID controller |
vera1 | 3:4520d0ca4e56 | 42 | } |
vera1 | 3:4520d0ca4e56 | 43 | |
vera1 | 0:bc949f735b8d | 44 | */ |
vera1 | 3:4520d0ca4e56 | 45 | |
vera1 | 0:bc949f735b8d | 46 | volatile float potvalue = 0.0; |
vera1 | 1:e0f6cdefcd6e | 47 | volatile float position = 0.0; |
vera1 | 0:bc949f735b8d | 48 | void readpot(){ |
vera1 | 0:bc949f735b8d | 49 | potvalue = pot.read(); |
vera1 | 1:e0f6cdefcd6e | 50 | //position = motor1.getPosition(); |
vera1 | 2:80177e90c1e6 | 51 | pc.printf("pos: %d, speed %f reference velocity = %.2f\r\n",motor1.getPosition(), motor1.getSpeed(), potvalue); |
vera1 | 2:80177e90c1e6 | 52 | motorspeed = potvalue; |
vera1 | 3:4520d0ca4e56 | 53 | //motorpid = PID(potvalue - position, M1_KP, M1_KI, M1_KD, M1_TS, m1_err_int, m1_prev_err, m1_f_v1, m1_f_v2, M1_F_A1, M1_F_A2, M1_F_B0, M1_F_B1, M1_F_B2); |
vera1 | 0:bc949f735b8d | 54 | } |
vera1 | 3:4520d0ca4e56 | 55 | // output PID controller is not yet tested. |
vera1 | 3:4520d0ca4e56 | 56 | |
vera1 | 0:bc949f735b8d | 57 | int main() |
vera1 | 1:e0f6cdefcd6e | 58 | { |
vera1 | 0:bc949f735b8d | 59 | |
vera1 | 0:bc949f735b8d | 60 | pc.baud(9600); |
vera1 | 0:bc949f735b8d | 61 | potmeterreadout.attach(readpot,0.2f); |
vera1 | 3:4520d0ca4e56 | 62 | motorspeed.period(PwmPeriod); |
vera1 | 0:bc949f735b8d | 63 | //float motorspeed = 0.0f; |
vera1 | 0:bc949f735b8d | 64 | while (true) { |
vera1 | 0:bc949f735b8d | 65 | |
vera1 | 2:80177e90c1e6 | 66 | |
vera1 | 1:e0f6cdefcd6e | 67 | //pc.printf("reference velocity = %.2f\r\n", potvalue); |
vera1 | 0:bc949f735b8d | 68 | |
vera1 | 0:bc949f735b8d | 69 | |
vera1 | 0:bc949f735b8d | 70 | if ((button2 == 1)&&(button1 == 0)) { |
vera1 | 0:bc949f735b8d | 71 | |
vera1 | 0:bc949f735b8d | 72 | motorposition = 0; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 73 | led2 = 0; |
vera1 | 0:bc949f735b8d | 74 | |
vera1 | 0:bc949f735b8d | 75 | } |
vera1 | 0:bc949f735b8d | 76 | if ((button2 ==0)&&(button1 ==1)){ |
vera1 | 0:bc949f735b8d | 77 | |
vera1 | 0:bc949f735b8d | 78 | motorposition = 1; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 79 | led2 = 1; |
vera1 | 0:bc949f735b8d | 80 | } |
vera1 | 0:bc949f735b8d | 81 | //} |
vera1 | 0:bc949f735b8d | 82 | |
vera1 | 0:bc949f735b8d | 83 | } |
vera1 | 0:bc949f735b8d | 84 | } |
vera1 | 0:bc949f735b8d | 85 |