test with PID control to screen on your computer

Dependencies:   Motor_with_encoder MODSERIAL QEI mbed

Fork of Project_motor by Biorobotics Project

Committer:
MarkHeimgartner
Date:
Mon Oct 09 14:52:21 2017 +0000
Revision:
5:05d09e921b5d
Parent:
4:abdc2d24d0bc
Child:
6:b89ca8add6ee
Child:
8:aff2a7d5861a
works now with position in rad/s;

Who changed what in which revision?

UserRevisionLine numberNew 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 3:4520d0ca4e56 21 /*const double M1_TS = 0.01f; // timestep
vera1 3:4520d0ca4e56 22 const double M1_KP = 2.5, M1_KI = 1.0, M1_KD = 0.5; // 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 3:4520d0ca4e56 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 3:4520d0ca4e56 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 3:4520d0ca4e56 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 3:4520d0ca4e56 42 return Kp*e + Ki*e_int + Kd*e_der; //PID controller
vera1 3:4520d0ca4e56 43 }
vera1 3:4520d0ca4e56 44
vera1 0:bc949f735b8d 45 */
vera1 3:4520d0ca4e56 46
vera1 0:bc949f735b8d 47 volatile float potvalue = 0.0;
vera1 1:e0f6cdefcd6e 48 volatile float position = 0.0;
vera1 0:bc949f735b8d 49 void readpot(){
vera1 0:bc949f735b8d 50 potvalue = pot.read();
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 2:80177e90c1e6 53 motorspeed = potvalue;
vera1 3:4520d0ca4e56 54 //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 55 }
vera1 3:4520d0ca4e56 56 // output PID controller is not yet tested.
vera1 3:4520d0ca4e56 57
vera1 0:bc949f735b8d 58 int main()
vera1 1:e0f6cdefcd6e 59 {
vera1 0:bc949f735b8d 60
vera1 0:bc949f735b8d 61 pc.baud(9600);
vera1 0:bc949f735b8d 62 potmeterreadout.attach(readpot,0.2f);
vera1 3:4520d0ca4e56 63 motorspeed.period(PwmPeriod);
vera1 0:bc949f735b8d 64 //float motorspeed = 0.0f;
vera1 0:bc949f735b8d 65 while (true) {
vera1 0:bc949f735b8d 66
vera1 2:80177e90c1e6 67
vera1 1:e0f6cdefcd6e 68 //pc.printf("reference velocity = %.2f\r\n", potvalue);
vera1 0:bc949f735b8d 69
vera1 0:bc949f735b8d 70
vera1 0:bc949f735b8d 71 if ((button2 == 1)&&(button1 == 0)) {
vera1 0:bc949f735b8d 72
vera1 0:bc949f735b8d 73 motorposition = 0; // motor turns anticlockwise
vera1 0:bc949f735b8d 74 led2 = 0;
vera1 0:bc949f735b8d 75
vera1 0:bc949f735b8d 76 }
vera1 0:bc949f735b8d 77 if ((button2 ==0)&&(button1 ==1)){
vera1 0:bc949f735b8d 78
vera1 0:bc949f735b8d 79 motorposition = 1; // motor turns anticlockwise
vera1 0:bc949f735b8d 80 led2 = 1;
vera1 0:bc949f735b8d 81 }
vera1 0:bc949f735b8d 82 //}
vera1 0:bc949f735b8d 83
vera1 0:bc949f735b8d 84 }
vera1 0:bc949f735b8d 85 }
vera1 0:bc949f735b8d 86