test with PID control to screen on your computer
Dependencies: Motor_with_encoder MODSERIAL QEI mbed
Fork of Project_motor by
main.cpp@6:b89ca8add6ee, 2017-10-09 (annotated)
- Committer:
- vera1
- Date:
- Mon Oct 09 15:05:54 2017 +0000
- Revision:
- 6:b89ca8add6ee
- Parent:
- 5:05d09e921b5d
- Child:
- 7:f54ff293f31a
- Child:
- 9:986a0c5fba0e
PID control partly uncommented, but still not working
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; |
MarkHeimgartner | 5:05d09e921b5d | 20 | |
vera1 | 6:b89ca8add6ee | 21 | const double M1_TS = 0.01f; // timestep |
vera1 | 6:b89ca8add6ee | 22 | const double M1_KP = 0.216, M1_KI = 1.8, M1_KD = 0.0; // controller gains for motor 1 |
vera1 | 3:4520d0ca4e56 | 23 | double m1_err_int = 0, m1_prev_err = 0; // initiallize errors |
vera1 | 3:4520d0ca4e56 | 24 | 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 | 25 | double m1_f_v1 = 0.0, m1_f_v2 = 0.0; // filter variables |
vera1 | 6:b89ca8add6ee | 26 | /* |
vera1 | 3:4520d0ca4e56 | 27 | // biquad filter for emg signals |
vera1 | 3:4520d0ca4e56 | 28 | 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 | 29 | double v = u - a1*v1 - a2*v2; |
vera1 | 3:4520d0ca4e56 | 30 | double y = b0*v + b1*v1 + b2*v2; |
vera1 | 3:4520d0ca4e56 | 31 | v2 = v1; |
vera1 | 3:4520d0ca4e56 | 32 | v1 = v; |
vera1 | 3:4520d0ca4e56 | 33 | return y; |
vera1 | 3:4520d0ca4e56 | 34 | } |
vera1 | 6:b89ca8add6ee | 35 | */ |
vera1 | 3:4520d0ca4e56 | 36 | // PID controller function |
vera1 | 3:4520d0ca4e56 | 37 | 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 | 38 | double e_der = (e - e_prev)/Ts; // derivative |
vera1 | 6:b89ca8add6ee | 39 | //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2); |
vera1 | 3:4520d0ca4e56 | 40 | e_prev = e; |
vera1 | 3:4520d0ca4e56 | 41 | e_int = e_int + Ts*e; // integral |
vera1 | 6:b89ca8add6ee | 42 | return Kp*e + Ki*e_int; //PID controller |
vera1 | 3:4520d0ca4e56 | 43 | } |
vera1 | 3:4520d0ca4e56 | 44 | |
vera1 | 6:b89ca8add6ee | 45 | |
vera1 | 3:4520d0ca4e56 | 46 | |
vera1 | 6:b89ca8add6ee | 47 | volatile double potvalue = 0.0; |
vera1 | 1:e0f6cdefcd6e | 48 | volatile float position = 0.0; |
vera1 | 0:bc949f735b8d | 49 | void readpot(){ |
vera1 | 6:b89ca8add6ee | 50 | potvalue = pot.read()*6.28; |
MarkHeimgartner | 4:abdc2d24d0bc | 51 | position = motor1.getPosition()/4200.00*6.28; |
MarkHeimgartner | 4:abdc2d24d0bc | 52 | pc.printf("pos: %f, speed %f reference velocity = %.2f\r\n",position, motor1.getSpeed(), potvalue); |
vera1 | 6:b89ca8add6ee | 53 | //motorspeed = potvalue; |
vera1 | 6:b89ca8add6ee | 54 | float 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 | 6:b89ca8add6ee | 55 | motorspeed = motorPID; |
vera1 | 0:bc949f735b8d | 56 | } |
vera1 | 3:4520d0ca4e56 | 57 | // output PID controller is not yet tested. |
vera1 | 3:4520d0ca4e56 | 58 | |
vera1 | 0:bc949f735b8d | 59 | int main() |
vera1 | 1:e0f6cdefcd6e | 60 | { |
vera1 | 0:bc949f735b8d | 61 | |
vera1 | 0:bc949f735b8d | 62 | pc.baud(9600); |
vera1 | 0:bc949f735b8d | 63 | potmeterreadout.attach(readpot,0.2f); |
vera1 | 3:4520d0ca4e56 | 64 | motorspeed.period(PwmPeriod); |
vera1 | 0:bc949f735b8d | 65 | //float motorspeed = 0.0f; |
vera1 | 0:bc949f735b8d | 66 | while (true) { |
vera1 | 0:bc949f735b8d | 67 | |
vera1 | 2:80177e90c1e6 | 68 | |
vera1 | 1:e0f6cdefcd6e | 69 | //pc.printf("reference velocity = %.2f\r\n", potvalue); |
vera1 | 0:bc949f735b8d | 70 | |
vera1 | 0:bc949f735b8d | 71 | |
vera1 | 0:bc949f735b8d | 72 | if ((button2 == 1)&&(button1 == 0)) { |
vera1 | 0:bc949f735b8d | 73 | |
vera1 | 0:bc949f735b8d | 74 | motorposition = 0; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 75 | led2 = 0; |
vera1 | 0:bc949f735b8d | 76 | |
vera1 | 0:bc949f735b8d | 77 | } |
vera1 | 0:bc949f735b8d | 78 | if ((button2 ==0)&&(button1 ==1)){ |
vera1 | 0:bc949f735b8d | 79 | |
vera1 | 0:bc949f735b8d | 80 | motorposition = 1; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 81 | led2 = 1; |
vera1 | 0:bc949f735b8d | 82 | } |
vera1 | 0:bc949f735b8d | 83 | //} |
vera1 | 0:bc949f735b8d | 84 | |
vera1 | 0:bc949f735b8d | 85 | } |
vera1 | 0:bc949f735b8d | 86 | } |
vera1 | 0:bc949f735b8d | 87 |