test with PID control to screen on your computer
Dependencies: Motor_with_encoder MODSERIAL QEI mbed
Fork of Project_motor by
main.cpp@7:f54ff293f31a, 2017-10-13 (annotated)
- Committer:
- vera1
- Date:
- Fri Oct 13 10:20:35 2017 +0000
- Revision:
- 7:f54ff293f31a
- Parent:
- 6:b89ca8add6ee
smth with pid
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 | 7:f54ff293f31a | 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 | 7:f54ff293f31a | 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 | 7:f54ff293f31a | 35 | |
vera1 | 3:4520d0ca4e56 | 36 | // PID controller function |
vera1 | 7:f54ff293f31a | 37 | double PID(volatile double e, const double Kp, const double Ki, const double Kd, double Ts){ |
vera1 | 7:f54ff293f31a | 38 | e = potvalue - position; |
vera1 | 3:4520d0ca4e56 | 39 | double e_der = (e - e_prev)/Ts; // derivative |
vera1 | 6:b89ca8add6ee | 40 | //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2); |
vera1 | 3:4520d0ca4e56 | 41 | e_prev = e; |
vera1 | 3:4520d0ca4e56 | 42 | e_int = e_int + Ts*e; // integral |
vera1 | 7:f54ff293f31a | 43 | return Kp*e+Ki*e_int; //PID controller |
vera1 | 3:4520d0ca4e56 | 44 | } |
vera1 | 3:4520d0ca4e56 | 45 | |
vera1 | 7:f54ff293f31a | 46 | */ |
vera1 | 3:4520d0ca4e56 | 47 | |
vera1 | 6:b89ca8add6ee | 48 | volatile double potvalue = 0.0; |
vera1 | 1:e0f6cdefcd6e | 49 | volatile float position = 0.0; |
vera1 | 0:bc949f735b8d | 50 | void readpot(){ |
vera1 | 7:f54ff293f31a | 51 | potvalue = pot.read(); |
MarkHeimgartner | 4:abdc2d24d0bc | 52 | position = motor1.getPosition()/4200.00*6.28; |
MarkHeimgartner | 4:abdc2d24d0bc | 53 | pc.printf("pos: %f, speed %f reference velocity = %.2f\r\n",position, motor1.getSpeed(), potvalue); |
vera1 | 7:f54ff293f31a | 54 | motorspeed = potvalue; |
vera1 | 7:f54ff293f31a | 55 | //float motorPID = PID(potvalue - position, M1_KP, M1_KI, M1_KD, M1_TS); |
vera1 | 7:f54ff293f31a | 56 | //motorspeed = motorPID; |
vera1 | 0:bc949f735b8d | 57 | } |
vera1 | 3:4520d0ca4e56 | 58 | // output PID controller is not yet tested. |
vera1 | 3:4520d0ca4e56 | 59 | |
vera1 | 0:bc949f735b8d | 60 | int main() |
vera1 | 1:e0f6cdefcd6e | 61 | { |
vera1 | 0:bc949f735b8d | 62 | |
vera1 | 0:bc949f735b8d | 63 | pc.baud(9600); |
vera1 | 7:f54ff293f31a | 64 | potmeterreadout.attach(readpot,0.01f); |
vera1 | 3:4520d0ca4e56 | 65 | motorspeed.period(PwmPeriod); |
vera1 | 0:bc949f735b8d | 66 | //float motorspeed = 0.0f; |
vera1 | 7:f54ff293f31a | 67 | while (true) { |
vera1 | 1:e0f6cdefcd6e | 68 | //pc.printf("reference velocity = %.2f\r\n", potvalue); |
vera1 | 0:bc949f735b8d | 69 | if ((button2 == 1)&&(button1 == 0)) { |
vera1 | 0:bc949f735b8d | 70 | motorposition = 0; // motor turns anticlockwise |
vera1 | 7:f54ff293f31a | 71 | led2 = 0; |
vera1 | 0:bc949f735b8d | 72 | } |
vera1 | 7:f54ff293f31a | 73 | if ((button2 ==0)&&(button1 ==1)){ |
vera1 | 0:bc949f735b8d | 74 | motorposition = 1; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 75 | led2 = 1; |
vera1 | 7:f54ff293f31a | 76 | } |
vera1 | 7:f54ff293f31a | 77 | |
vera1 | 0:bc949f735b8d | 78 | |
vera1 | 0:bc949f735b8d | 79 | } |
vera1 | 0:bc949f735b8d | 80 | } |
vera1 | 0:bc949f735b8d | 81 |