first publish not working

Dependencies:   MODSERIAL mbed ttmath FastPWM Motor_with_encoder biquadFilter

Committer:
Arnoud113
Date:
Wed Nov 01 00:01:58 2017 +0000
Revision:
7:88d1ccba9200
Parent:
6:d4f6d9400f53
Child:
8:b932f8b71d3a
was an working version, not after some tingeling;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arnoud113 0:77ad62c61c78 1 #include "mbed.h"
Arnoud113 0:77ad62c61c78 2 #include "QEI.h"
Arnoud113 0:77ad62c61c78 3 #include "MODSERIAL.h"
Arnoud113 0:77ad62c61c78 4 #include "math.h"
Arnoud113 2:2563d1d8461f 5 #include "FastPWM.h"
Arnoud113 3:b353ee86230a 6 #include "encoder.h"
Arnoud113 0:77ad62c61c78 7
Arnoud113 0:77ad62c61c78 8
Arnoud113 0:77ad62c61c78 9
Arnoud113 0:77ad62c61c78 10 DigitalOut gpo(D0);
Arnoud113 0:77ad62c61c78 11 DigitalOut ledb(LED_BLUE);
Arnoud113 0:77ad62c61c78 12 DigitalOut ledr(LED_RED);
Arnoud113 0:77ad62c61c78 13 DigitalOut ledg(LED_GREEN);
Arnoud113 0:77ad62c61c78 14 DigitalOut motor1DC(D7);
Arnoud113 3:b353ee86230a 15 DigitalOut motor2DC(D4);
Arnoud113 7:88d1ccba9200 16 PwmOut motor1PWM(D6);
Arnoud113 7:88d1ccba9200 17 PwmOut motor2PWM(D5);
Arnoud113 0:77ad62c61c78 18
Arnoud113 0:77ad62c61c78 19 AnalogIn potMeter1(A0);
Arnoud113 0:77ad62c61c78 20 AnalogIn potMeter2(A1);
Arnoud113 0:77ad62c61c78 21 DigitalIn button1(D11);
Arnoud113 0:77ad62c61c78 22 DigitalIn button2(D12);
Arnoud113 3:b353ee86230a 23 Encoder Encoder1(D12,D13);
Arnoud113 3:b353ee86230a 24 Encoder Encoder2(D8,D9);
Arnoud113 0:77ad62c61c78 25
Arnoud113 0:77ad62c61c78 26 MODSERIAL pc(USBTX,USBRX);
Arnoud113 0:77ad62c61c78 27
Arnoud113 0:77ad62c61c78 28 Ticker controller;
Arnoud113 0:77ad62c61c78 29
Arnoud113 3:b353ee86230a 30 // ---- Motor Constants-------
Arnoud113 3:b353ee86230a 31 float Pwmperiod = 0.0001f;
Arnoud113 3:b353ee86230a 32 int potmultiplier = 800; // Multiplier for the pot meter reference which is normally between 0 and 1
Arnoud113 3:b353ee86230a 33 float gainM1 = 1/35.17; // encoder pulses per degree theta
Arnoud113 3:b353ee86230a 34 float gainM2 = 0.01; // gain for radius r
Arnoud113 0:77ad62c61c78 35
Arnoud113 3:b353ee86230a 36 volatile float motor1;
Arnoud113 3:b353ee86230a 37 volatile float motor2;
Arnoud113 3:b353ee86230a 38
Arnoud113 3:b353ee86230a 39 //Start constants PID -------------------------------
Arnoud113 0:77ad62c61c78 40 const double pi = 3.1415926535897;
Arnoud113 7:88d1ccba9200 41 const double M1_TS = 0.001; // (was 0.0001) 0.001 and 0.01 work without biquad filter. // // Sample time (motor - timestep)
Arnoud113 0:77ad62c61c78 42
Arnoud113 0:77ad62c61c78 43 //verplaatst
Arnoud113 3:b353ee86230a 44 const float RAD_PER_PULSE = (2*pi)/4200;
Arnoud113 7:88d1ccba9200 45 const float CONTROLLER_TS = 0.001; //TIME INTERVAL/ hZ
Arnoud113 7:88d1ccba9200 46 const float M1_KP = 10;
Arnoud113 7:88d1ccba9200 47 const float M1_KI = 0.5;
Arnoud113 7:88d1ccba9200 48 const float M1_KD = 0.2; //was KP=10 KI=0.5 KD=0.5
Arnoud113 7:88d1ccba9200 49 double m1_err_int = 0;
Arnoud113 7:88d1ccba9200 50 double m1_prev_err = 0;
Arnoud113 7:88d1ccba9200 51
Arnoud113 7:88d1ccba9200 52 const float M2_KP = 10;
Arnoud113 7:88d1ccba9200 53 const float M2_KI = 0.5;
Arnoud113 7:88d1ccba9200 54 const float M2_KD = 0.5; //was KP=10 KI=0.5 KD=0.5
Arnoud113 7:88d1ccba9200 55 double m2_err_int = 0;
Arnoud113 7:88d1ccba9200 56 double m2_prev_err = 0;
Arnoud113 7:88d1ccba9200 57
Arnoud113 7:88d1ccba9200 58
Arnoud113 7:88d1ccba9200 59 // --- const biquad filter---------------------------
Arnoud113 7:88d1ccba9200 60 const double M1_F_A1 = 1.0;
Arnoud113 7:88d1ccba9200 61 const double M1_F_A2 = 2.0;
Arnoud113 7:88d1ccba9200 62 const double M1_F_B0 = 1.0;
Arnoud113 7:88d1ccba9200 63 const double M1_F_B1 = 3.0;
Arnoud113 7:88d1ccba9200 64 const double M1_F_B2 = 4.0;
Arnoud113 7:88d1ccba9200 65 double m1_f_v1 = 0;
Arnoud113 7:88d1ccba9200 66 double m1_f_v2 = 0;
Arnoud113 5:a1a5b5bebd5c 67 //---------------------------------End of constants PID
Arnoud113 0:77ad62c61c78 68
Arnoud113 3:b353ee86230a 69 //-----------------Start PID part----------------------------START
Arnoud113 7:88d1ccba9200 70 double PID1(double e1, const double Kp1, const double Ki1, const double Kd1, double Ts, double &e_int1, double &e_prev1, 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){
Arnoud113 3:b353ee86230a 71
Arnoud113 7:88d1ccba9200 72 double e_der1 = (e1 - e_prev1)/Ts; // Ts = motor1-timestep // Derivative
Arnoud113 7:88d1ccba9200 73 // biquad part, see slide
Arnoud113 7:88d1ccba9200 74 //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2);
Arnoud113 7:88d1ccba9200 75 e_prev1 = e1;
Arnoud113 7:88d1ccba9200 76 e_int1 += Ts*e1; // Integral
Arnoud113 7:88d1ccba9200 77 return Kp1*e1 + Ki1*e_int1 + Kd1 * e_der1; //PID
Arnoud113 7:88d1ccba9200 78 }
Arnoud113 3:b353ee86230a 79
Arnoud113 7:88d1ccba9200 80 double PID2(double e2, const double Kp2, const double Ki2, const double Kd2, double Ts, double &e_int2, double &e_prev2, 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){
Arnoud113 7:88d1ccba9200 81
Arnoud113 7:88d1ccba9200 82 double e_der2 = (e2 - e_prev2)/Ts; // Ts = motor1-timestep // Derivative
Arnoud113 7:88d1ccba9200 83 // biquad part, see slide
Arnoud113 7:88d1ccba9200 84 //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2);
Arnoud113 7:88d1ccba9200 85 e_prev2 = e2;
Arnoud113 7:88d1ccba9200 86 e_int2 += Ts*e2; // Integral
Arnoud113 7:88d1ccba9200 87 return Kp2*e2 + Ki2*e_int2 + Kd2 * e_der2; //PID
Arnoud113 3:b353ee86230a 88 }
Arnoud113 3:b353ee86230a 89
Arnoud113 3:b353ee86230a 90 //------------Get reference position-----------------START
Arnoud113 0:77ad62c61c78 91 float Get_X_Position(){
Arnoud113 1:13d8940f0fd4 92 double X = potMeter1 * potmultiplier;
Arnoud113 1:13d8940f0fd4 93 return X;
Arnoud113 0:77ad62c61c78 94 }
Arnoud113 0:77ad62c61c78 95
Arnoud113 0:77ad62c61c78 96 float Get_Y_Position(){
Arnoud113 1:13d8940f0fd4 97 double Y = potMeter2 * potmultiplier;
Arnoud113 1:13d8940f0fd4 98 return Y;
Arnoud113 0:77ad62c61c78 99 }
Arnoud113 3:b353ee86230a 100 //----------------------------------------------------END
Arnoud113 0:77ad62c61c78 101
Arnoud113 3:b353ee86230a 102 //-------------Get current Position-------------------START
Arnoud113 3:b353ee86230a 103 double motor1_Position(){ // has as output Theta
Arnoud113 5:a1a5b5bebd5c 104 double pos_m1 = gainM1*Encoder1.getPosition(); // current position for theta
Arnoud113 3:b353ee86230a 105 return pos_m1;
Arnoud113 0:77ad62c61c78 106 }
Arnoud113 3:b353ee86230a 107 double motor2_Position(){ //output R
Arnoud113 3:b353ee86230a 108 double pos_m2 = gainM2 *Encoder2.getPosition(); // current position for the radius;
Arnoud113 3:b353ee86230a 109 return pos_m2;
Arnoud113 3:b353ee86230a 110 }
Arnoud113 3:b353ee86230a 111 //-----------------------------------------------------END
Arnoud113 0:77ad62c61c78 112
Arnoud113 0:77ad62c61c78 113
Arnoud113 3:b353ee86230a 114 //------------Controller-------------------------------START
Arnoud113 0:77ad62c61c78 115 void Controller(){
Arnoud113 3:b353ee86230a 116
Arnoud113 1:13d8940f0fd4 117 double x = Get_X_Position();
Arnoud113 1:13d8940f0fd4 118 double y = Get_Y_Position();
Arnoud113 3:b353ee86230a 119
Arnoud113 3:b353ee86230a 120 double reference_motor1 = (atan(y/x)*180)/pi; // reference for Theta
Arnoud113 3:b353ee86230a 121 double reference_motor2 = sqrt((x*x+y*y)); // reference for radius
Arnoud113 0:77ad62c61c78 122
Arnoud113 3:b353ee86230a 123 float pos_M1 = motor1_Position(); // current position for theta
Arnoud113 3:b353ee86230a 124 float pos_M2 = motor2_Position(); // current position for the radius
Arnoud113 0:77ad62c61c78 125
Arnoud113 7:88d1ccba9200 126 double delta1 = PID1(reference_motor1 - pos_M1, 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);
Arnoud113 7:88d1ccba9200 127 double delta2 = PID2(reference_motor2 - pos_M2, M2_KP, M2_KI, M2_KD, M1_TS, m2_err_int, m2_prev_err, m1_f_v1, m1_f_v2, M1_F_A1, M1_F_A2, M1_F_B0, M1_F_B1, M1_F_B2);
Arnoud113 3:b353ee86230a 128
Arnoud113 3:b353ee86230a 129 double dTheta = reference_motor1 - pos_M1;
Arnoud113 3:b353ee86230a 130 double dRadius = reference_motor2 - pos_M2;
Arnoud113 0:77ad62c61c78 131
Arnoud113 0:77ad62c61c78 132 pc.baud(115200);
Arnoud113 5:a1a5b5bebd5c 133 pc.printf("\r DesPosition(X,Y):(%f,%f), pos Error(dTheta, dError):(%f,%f)\n",x,y, delta1 ,delta2);
Arnoud113 0:77ad62c61c78 134
Arnoud113 2:2563d1d8461f 135 //motor1PWM = motor1;
Arnoud113 2:2563d1d8461f 136 //motor2PWM = motor2;
Arnoud113 0:77ad62c61c78 137
Arnoud113 7:88d1ccba9200 138 if(delta1 > 5.0){
Arnoud113 3:b353ee86230a 139 motor1DC = 0;
Arnoud113 0:77ad62c61c78 140
Arnoud113 0:77ad62c61c78 141 ledr = 1;
Arnoud113 0:77ad62c61c78 142 ledg = 1; //Blau
Arnoud113 0:77ad62c61c78 143 ledb = 0;
Arnoud113 0:77ad62c61c78 144 }
Arnoud113 7:88d1ccba9200 145 else if (delta1< -5.0) {
Arnoud113 3:b353ee86230a 146 motor1DC = 1;
Arnoud113 0:77ad62c61c78 147
Arnoud113 0:77ad62c61c78 148 ledb = 1;
Arnoud113 0:77ad62c61c78 149 ledr = 1;
Arnoud113 0:77ad62c61c78 150 ledg = 0; //Groen
Arnoud113 0:77ad62c61c78 151
Arnoud113 0:77ad62c61c78 152 }
Arnoud113 0:77ad62c61c78 153 else{
Arnoud113 0:77ad62c61c78 154 motor1PWM = 0;
Arnoud113 0:77ad62c61c78 155
Arnoud113 0:77ad62c61c78 156 ledb = 1; //Rood
Arnoud113 0:77ad62c61c78 157 ledr = 0;
Arnoud113 0:77ad62c61c78 158 ledg = 1;
Arnoud113 0:77ad62c61c78 159 }
Arnoud113 3:b353ee86230a 160
Arnoud113 3:b353ee86230a 161 motor1 = abs(delta1)/1000.0f;
Arnoud113 3:b353ee86230a 162 if(motor1 >= 0.50f) {
Arnoud113 3:b353ee86230a 163 motor1 = 0.50f;
Arnoud113 3:b353ee86230a 164 }
Arnoud113 0:77ad62c61c78 165
Arnoud113 3:b353ee86230a 166 if(delta2 > 10.0){
Arnoud113 3:b353ee86230a 167 motor2DC = 0;
Arnoud113 0:77ad62c61c78 168
Arnoud113 7:88d1ccba9200 169 //ledr = 1;
Arnoud113 7:88d1ccba9200 170 //ledg = 1; //Blau
Arnoud113 7:88d1ccba9200 171 //ledb = 0;
Arnoud113 0:77ad62c61c78 172 }
Arnoud113 3:b353ee86230a 173 else if (delta2<-10.0) {
Arnoud113 3:b353ee86230a 174 motor2DC = 1;
Arnoud113 0:77ad62c61c78 175
Arnoud113 7:88d1ccba9200 176 //ledb = 1;
Arnoud113 7:88d1ccba9200 177 //ledr = 1;
Arnoud113 7:88d1ccba9200 178 //ledg = 0; //Groen
Arnoud113 0:77ad62c61c78 179
Arnoud113 0:77ad62c61c78 180 }
Arnoud113 0:77ad62c61c78 181 else{
Arnoud113 0:77ad62c61c78 182 motor2PWM = 0;
Arnoud113 0:77ad62c61c78 183
Arnoud113 7:88d1ccba9200 184 //ledb = 1; //Rood
Arnoud113 7:88d1ccba9200 185 //ledr = 0;
Arnoud113 7:88d1ccba9200 186 //ledg = 1;
Arnoud113 0:77ad62c61c78 187 }
Arnoud113 3:b353ee86230a 188
Arnoud113 3:b353ee86230a 189 motor2 = abs(delta2)/1000.0f;
Arnoud113 3:b353ee86230a 190 if(motor1 >= 0.50f) {
Arnoud113 3:b353ee86230a 191 motor1 = 0.50f;
Arnoud113 3:b353ee86230a 192 }
Arnoud113 3:b353ee86230a 193
Arnoud113 3:b353ee86230a 194 motor1PWM = motor1 + 0.50f;
Arnoud113 3:b353ee86230a 195 motor2PWM = motor1 + 0.50f;
Arnoud113 3:b353ee86230a 196
Arnoud113 7:88d1ccba9200 197 //pc.printf("\r motorvalues (M1,M2):(%f,%f) \n", motor1PWM, motor2PWM);
Arnoud113 3:b353ee86230a 198 //pc.printf("\r
Arnoud113 0:77ad62c61c78 199 }
Arnoud113 0:77ad62c61c78 200
Arnoud113 0:77ad62c61c78 201 int main()
Arnoud113 0:77ad62c61c78 202 {
Arnoud113 0:77ad62c61c78 203 controller.attach(&Controller, M1_TS);
Arnoud113 3:b353ee86230a 204 //motor1PWM.period(Pwmperiod);
Arnoud113 3:b353ee86230a 205 //motor2PWM.period(Pwmperiod);
Arnoud113 0:77ad62c61c78 206
Arnoud113 3:b353ee86230a 207 while(1){
Arnoud113 3:b353ee86230a 208 /*
Arnoud113 3:b353ee86230a 209 double x = Get_X_Position();
Arnoud113 3:b353ee86230a 210 double y = Get_Y_Position();
Arnoud113 3:b353ee86230a 211 double reference_motor1 = atan(y/x);
Arnoud113 3:b353ee86230a 212 int position_Motor1 = motor1_Position();
Arnoud113 3:b353ee86230a 213 double motor1 = PID(reference_motor1 - position_Motor1, 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);
Arnoud113 3:b353ee86230a 214
Arnoud113 3:b353ee86230a 215 pc.baud(115200);
Arnoud113 3:b353ee86230a 216 pc.printf("\r Position(X)=(%f), Ref(Theta,R): (%f,), Pos(Theta,R):(%i,), Motor Value(M1,M2):(%f,).\n",x, reference_motor1, position_Motor1, motor1);
Arnoud113 3:b353ee86230a 217 */
Arnoud113 3:b353ee86230a 218 }
Arnoud113 0:77ad62c61c78 219
Arnoud113 0:77ad62c61c78 220 }