first publish not working

Dependencies:   MODSERIAL mbed ttmath FastPWM Motor_with_encoder biquadFilter

Committer:
Arnoud113
Date:
Thu Nov 02 15:43:46 2017 +0000
Revision:
14:54cbd8f0efe4
Parent:
13:b5868fd8ffe9
Child:
15:65f295a49a4b
Working quite alright, starting vaues won't work too well

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 13:b5868fd8ffe9 7 #include "BiQuad.h"
Arnoud113 0:77ad62c61c78 8
Arnoud113 0:77ad62c61c78 9
Arnoud113 0:77ad62c61c78 10
Arnoud113 0:77ad62c61c78 11 DigitalOut gpo(D0);
Arnoud113 0:77ad62c61c78 12 DigitalOut ledb(LED_BLUE);
Arnoud113 0:77ad62c61c78 13 DigitalOut ledr(LED_RED);
Arnoud113 0:77ad62c61c78 14 DigitalOut ledg(LED_GREEN);
Arnoud113 12:02eba9a294d2 15
Arnoud113 0:77ad62c61c78 16 DigitalOut motor1DC(D7);
Arnoud113 3:b353ee86230a 17 DigitalOut motor2DC(D4);
Arnoud113 8:b932f8b71d3a 18 FastPWM motor1PWM(D6);
Arnoud113 8:b932f8b71d3a 19 FastPWM motor2PWM(D5);
Arnoud113 0:77ad62c61c78 20
Arnoud113 12:02eba9a294d2 21 AnalogIn potMeter1(A0);
Arnoud113 12:02eba9a294d2 22 AnalogIn potMeter2(A1);
Arnoud113 12:02eba9a294d2 23 AnalogIn emg1(A2);
Arnoud113 12:02eba9a294d2 24 AnalogIn emg2(A3);
Arnoud113 12:02eba9a294d2 25 AnalogIn emg3(A4);
Arnoud113 12:02eba9a294d2 26 AnalogIn emg4(A5);
Arnoud113 12:02eba9a294d2 27
Arnoud113 12:02eba9a294d2 28 DigitalIn button1(D11);
Arnoud113 12:02eba9a294d2 29 DigitalIn button2(D12);
Arnoud113 12:02eba9a294d2 30 DigitalIn button3(SW2);
Arnoud113 12:02eba9a294d2 31 DigitalIn button4(SW3);
Arnoud113 12:02eba9a294d2 32 Encoder Encoder1(D12,D13);
Arnoud113 12:02eba9a294d2 33 Encoder Encoder2(D8,D9);
Arnoud113 0:77ad62c61c78 34
Arnoud113 0:77ad62c61c78 35 MODSERIAL pc(USBTX,USBRX);
Arnoud113 0:77ad62c61c78 36
Arnoud113 0:77ad62c61c78 37 Ticker controller;
Arnoud113 0:77ad62c61c78 38
Arnoud113 12:02eba9a294d2 39 // Constants EMG ----------------------- Start
Arnoud113 12:02eba9a294d2 40 double X;
Arnoud113 13:b5868fd8ffe9 41 double X_maxTreshold = 480;
Arnoud113 12:02eba9a294d2 42 double X_minTreshold = 20;
Arnoud113 12:02eba9a294d2 43 const double X_Callibrate = 300;
Arnoud113 12:02eba9a294d2 44
Arnoud113 12:02eba9a294d2 45 double Y;
Arnoud113 13:b5868fd8ffe9 46 double Y_maxTreshold = 480;
Arnoud113 14:54cbd8f0efe4 47 double Y_minTreshold = 20;
Arnoud113 12:02eba9a294d2 48 const double Y_Callibrate = 300;
Arnoud113 12:02eba9a294d2 49
Arnoud113 12:02eba9a294d2 50
Arnoud113 3:b353ee86230a 51 // ---- Motor Constants-------
Arnoud113 12:02eba9a294d2 52 const double pi = 3.1415926535897;
Arnoud113 12:02eba9a294d2 53 float Pwmperiod = 0.001f;
Arnoud113 12:02eba9a294d2 54 int potmultiplier = 600; // Multiplier for the pot meter reference which is normally between 0 and 1
Arnoud113 12:02eba9a294d2 55 const float gearM1 = 6.2;
Arnoud113 13:b5868fd8ffe9 56 const double gainM1 = 1/38.17; // encoder pulses per degree theta
Arnoud113 14:54cbd8f0efe4 57 const double gainM2 = 1/100.8; // pulses per mm
Arnoud113 0:77ad62c61c78 58
Arnoud113 12:02eba9a294d2 59 double motor1;
Arnoud113 12:02eba9a294d2 60 double motor2;
Arnoud113 14:54cbd8f0efe4 61
Arnoud113 14:54cbd8f0efe4 62 double reference_motor1 = 0; // reference for Theta
Arnoud113 14:54cbd8f0efe4 63 double reference_motor2 = 0;
Arnoud113 14:54cbd8f0efe4 64 double pos_M1 = 0; // start angle theta
Arnoud113 14:54cbd8f0efe4 65 double pos_M2 = 0; // start radius
Arnoud113 14:54cbd8f0efe4 66
Arnoud113 3:b353ee86230a 67
Arnoud113 3:b353ee86230a 68 //Start constants PID -------------------------------
Arnoud113 12:02eba9a294d2 69 const double M1_TS = 0.01; // (was 0.0001) 0.001 and 0.01 work without biquad filter. // // Sample time (motor - timestep)
Arnoud113 0:77ad62c61c78 70
Arnoud113 0:77ad62c61c78 71 //verplaatst
Arnoud113 3:b353ee86230a 72 const float RAD_PER_PULSE = (2*pi)/4200;
Arnoud113 8:b932f8b71d3a 73 const float CONTROLLER_TS = 0.01; //TIME INTERVAL/ hZ
Arnoud113 8:b932f8b71d3a 74
Arnoud113 11:66d0be7efd3f 75 const float M1_KP = 10;
Arnoud113 7:88d1ccba9200 76 const float M1_KI = 0.5;
Arnoud113 8:b932f8b71d3a 77 const float M1_KD = 0.5; //was KP=10 KI=0.5 KD=0.5
Arnoud113 7:88d1ccba9200 78 double m1_err_int = 0;
Arnoud113 7:88d1ccba9200 79 double m1_prev_err = 0;
Arnoud113 7:88d1ccba9200 80
Arnoud113 13:b5868fd8ffe9 81 const float M2_KP = 30;
Arnoud113 11:66d0be7efd3f 82 const float M2_KI = 0.5;
Arnoud113 7:88d1ccba9200 83 const float M2_KD = 0.5; //was KP=10 KI=0.5 KD=0.5
Arnoud113 7:88d1ccba9200 84 double m2_err_int = 0;
Arnoud113 7:88d1ccba9200 85 double m2_prev_err = 0;
Arnoud113 7:88d1ccba9200 86
Arnoud113 8:b932f8b71d3a 87 // Constants Biquad
Arnoud113 13:b5868fd8ffe9 88 BiQuad bq1(6.38946e-01,-1.27789e+00,6.38946e-01,-1.14298e+00,4.12802e-01);
Arnoud113 13:b5868fd8ffe9 89 BiQuad bq2(6.38946e-01,-1.27789e+00,6.38946e-01,-1.14298e+00,4.12802e-01);
Arnoud113 13:b5868fd8ffe9 90 BiQuadChain bqc;
Arnoud113 13:b5868fd8ffe9 91
Arnoud113 7:88d1ccba9200 92 const double M1_F_A1 = 1.0;
Arnoud113 8:b932f8b71d3a 93 const double M1_F_A2 = 2.0;
Arnoud113 8:b932f8b71d3a 94 const double M1_F_B0 = 1.0;
Arnoud113 7:88d1ccba9200 95 const double M1_F_B1 = 3.0;
Arnoud113 7:88d1ccba9200 96 const double M1_F_B2 = 4.0;
Arnoud113 7:88d1ccba9200 97 double m1_f_v1 = 0;
Arnoud113 7:88d1ccba9200 98 double m1_f_v2 = 0;
Arnoud113 12:02eba9a294d2 99
Arnoud113 12:02eba9a294d2 100
Arnoud113 12:02eba9a294d2 101
Arnoud113 5:a1a5b5bebd5c 102 //---------------------------------End of constants PID
Arnoud113 0:77ad62c61c78 103
Arnoud113 3:b353ee86230a 104 //-----------------Start PID part----------------------------START
Arnoud113 7:88d1ccba9200 105 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 106
Arnoud113 8:b932f8b71d3a 107 double e_der1 = (e1 - e_prev1)/Ts; // Ts = motor1-timestep // Derivative
Arnoud113 13:b5868fd8ffe9 108
Arnoud113 13:b5868fd8ffe9 109 e_der1 = bqc.step(e_der1);
Arnoud113 7:88d1ccba9200 110 // biquad part, see slide
Arnoud113 7:88d1ccba9200 111 //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2);
Arnoud113 8:b932f8b71d3a 112
Arnoud113 7:88d1ccba9200 113 e_prev1 = e1;
Arnoud113 12:02eba9a294d2 114 e_int1 += Ts*e1;
Arnoud113 12:02eba9a294d2 115
Arnoud113 12:02eba9a294d2 116 if(button3 == 0 || button4 ==0){
Arnoud113 12:02eba9a294d2 117 e1 = 0;
Arnoud113 12:02eba9a294d2 118 e_prev1 = 0;
Arnoud113 12:02eba9a294d2 119 e_int1 = 0;
Arnoud113 12:02eba9a294d2 120 e_der1 = 0;
Arnoud113 12:02eba9a294d2 121 }
Arnoud113 12:02eba9a294d2 122 // Integral
Arnoud113 8:b932f8b71d3a 123 return Kp1*e1 + Ki1*e_int1 + Kd1 * e_der1; //PID
Arnoud113 7:88d1ccba9200 124 }
Arnoud113 3:b353ee86230a 125
Arnoud113 7:88d1ccba9200 126 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 127
Arnoud113 8:b932f8b71d3a 128 double e_der2 = (e2 - e_prev2)/Ts; // Ts = motor1-timestep // Derivative
Arnoud113 7:88d1ccba9200 129 // biquad part, see slide
Arnoud113 7:88d1ccba9200 130 //e_der = biquad(e_der, f_v1, f_v2, f_a1, f_a2, f_b0, f_b1, f_b2);
Arnoud113 8:b932f8b71d3a 131
Arnoud113 13:b5868fd8ffe9 132 e_der2 = bqc.step(e_der2);
Arnoud113 13:b5868fd8ffe9 133
Arnoud113 7:88d1ccba9200 134 e_prev2 = e2;
Arnoud113 12:02eba9a294d2 135 e_int2 += Ts*e2;
Arnoud113 12:02eba9a294d2 136
Arnoud113 12:02eba9a294d2 137 if(button3 == 0 || button4 ==0){
Arnoud113 12:02eba9a294d2 138 e2 = 0;
Arnoud113 12:02eba9a294d2 139 e_prev2 = 0;
Arnoud113 12:02eba9a294d2 140 e_int2 = 0;
Arnoud113 12:02eba9a294d2 141 e_der2 = 0;
Arnoud113 12:02eba9a294d2 142 }
Arnoud113 12:02eba9a294d2 143 // Integral
Arnoud113 8:b932f8b71d3a 144 return Kp2*e2 + Ki2*e_int2 + Kd2 * e_der2; //PID
Arnoud113 3:b353ee86230a 145 }
Arnoud113 3:b353ee86230a 146
Arnoud113 3:b353ee86230a 147 //------------Get reference position-----------------START
Arnoud113 0:77ad62c61c78 148 float Get_X_Position(){
Arnoud113 12:02eba9a294d2 149 // X = potMeter1 * potmultiplier; //--------- for Potmerter use
Arnoud113 12:02eba9a294d2 150
Arnoud113 12:02eba9a294d2 151 // -- Potmeter use -----------------------------------
Arnoud113 12:02eba9a294d2 152 if (potMeter1 < 0.3)
Arnoud113 12:02eba9a294d2 153 {
Arnoud113 12:02eba9a294d2 154 X = X-0.5;
Arnoud113 12:02eba9a294d2 155 }
Arnoud113 12:02eba9a294d2 156 else if (potMeter1> 0.7)
Arnoud113 12:02eba9a294d2 157 {
Arnoud113 12:02eba9a294d2 158 X = X+0.5;
Arnoud113 12:02eba9a294d2 159 }
Arnoud113 12:02eba9a294d2 160 else
Arnoud113 12:02eba9a294d2 161 {
Arnoud113 12:02eba9a294d2 162 X = X;
Arnoud113 12:02eba9a294d2 163 }
Arnoud113 12:02eba9a294d2 164
Arnoud113 12:02eba9a294d2 165
Arnoud113 12:02eba9a294d2 166 /*
Arnoud113 12:02eba9a294d2 167 double in1 = emg1.read();
Arnoud113 12:02eba9a294d2 168 double in2 = emg2.read();
Arnoud113 12:02eba9a294d2 169
Arnoud113 12:02eba9a294d2 170 double RA = in1+in2;
Arnoud113 12:02eba9a294d2 171
Arnoud113 12:02eba9a294d2 172
Arnoud113 12:02eba9a294d2 173 if (RA < 0.5)
Arnoud113 12:02eba9a294d2 174 {
Arnoud113 12:02eba9a294d2 175 X = X;
Arnoud113 12:02eba9a294d2 176 }
Arnoud113 12:02eba9a294d2 177 else if (RA > 1.5)
Arnoud113 12:02eba9a294d2 178 {
Arnoud113 12:02eba9a294d2 179 X = X-0.1;
Arnoud113 12:02eba9a294d2 180 }
Arnoud113 12:02eba9a294d2 181 else
Arnoud113 12:02eba9a294d2 182 {
Arnoud113 12:02eba9a294d2 183 X = X+0.1;
Arnoud113 12:02eba9a294d2 184 }
Arnoud113 12:02eba9a294d2 185 */
Arnoud113 12:02eba9a294d2 186
Arnoud113 13:b5868fd8ffe9 187 if (X >= X_maxTreshold){
Arnoud113 12:02eba9a294d2 188 X = X_maxTreshold;
Arnoud113 12:02eba9a294d2 189 }
Arnoud113 13:b5868fd8ffe9 190 else if (X <= X_minTreshold){
Arnoud113 12:02eba9a294d2 191 X = X_minTreshold;
Arnoud113 12:02eba9a294d2 192 }
Arnoud113 12:02eba9a294d2 193 else{
Arnoud113 12:02eba9a294d2 194 X = X;
Arnoud113 12:02eba9a294d2 195 }
Arnoud113 12:02eba9a294d2 196
Arnoud113 12:02eba9a294d2 197 if(button3 == 0){
Arnoud113 12:02eba9a294d2 198 X = X_minTreshold;
Arnoud113 12:02eba9a294d2 199 }
Arnoud113 12:02eba9a294d2 200 else if (button4 == 0){
Arnoud113 12:02eba9a294d2 201 X = X_Callibrate;
Arnoud113 12:02eba9a294d2 202 }
Arnoud113 12:02eba9a294d2 203 else{
Arnoud113 12:02eba9a294d2 204 X = X;
Arnoud113 12:02eba9a294d2 205 }
Arnoud113 12:02eba9a294d2 206 //pc.baud(115200);
Arnoud113 12:02eba9a294d2 207 //pc.printf("\r (in1,in2):(%f,%f), RA = %f, X = %f \n",in1, in2, RA, X);
Arnoud113 12:02eba9a294d2 208
Arnoud113 1:13d8940f0fd4 209 return X;
Arnoud113 0:77ad62c61c78 210 }
Arnoud113 0:77ad62c61c78 211
Arnoud113 0:77ad62c61c78 212 float Get_Y_Position(){
Arnoud113 12:02eba9a294d2 213 //Y = potMeter2 * potmultiplier; //--------- for Potmerter use
Arnoud113 12:02eba9a294d2 214
Arnoud113 12:02eba9a294d2 215 // ---- Potmeter Use--------------------------
Arnoud113 12:02eba9a294d2 216 if (potMeter2 < 0.3)
Arnoud113 12:02eba9a294d2 217 {
Arnoud113 12:02eba9a294d2 218 Y = Y-0.5;
Arnoud113 12:02eba9a294d2 219 }
Arnoud113 12:02eba9a294d2 220 else if (potMeter2 > 0.7)
Arnoud113 12:02eba9a294d2 221 {
Arnoud113 12:02eba9a294d2 222 Y = Y+0.5;
Arnoud113 12:02eba9a294d2 223 }
Arnoud113 12:02eba9a294d2 224 else
Arnoud113 12:02eba9a294d2 225 {
Arnoud113 12:02eba9a294d2 226 Y = Y;
Arnoud113 12:02eba9a294d2 227 }
Arnoud113 12:02eba9a294d2 228
Arnoud113 12:02eba9a294d2 229
Arnoud113 12:02eba9a294d2 230 /*
Arnoud113 12:02eba9a294d2 231 double in3 = emg3.read();
Arnoud113 12:02eba9a294d2 232 double in4 = emg4.read();
Arnoud113 12:02eba9a294d2 233
Arnoud113 12:02eba9a294d2 234
Arnoud113 12:02eba9a294d2 235 double LA = in3+in4;
Arnoud113 12:02eba9a294d2 236
Arnoud113 12:02eba9a294d2 237 if (LA < 0.5)
Arnoud113 12:02eba9a294d2 238 {
Arnoud113 12:02eba9a294d2 239 Y = Y;
Arnoud113 12:02eba9a294d2 240 }
Arnoud113 12:02eba9a294d2 241 else if (LA > 1.5)
Arnoud113 12:02eba9a294d2 242 {
Arnoud113 12:02eba9a294d2 243 Y = Y-0.1;
Arnoud113 12:02eba9a294d2 244 }
Arnoud113 12:02eba9a294d2 245 else
Arnoud113 12:02eba9a294d2 246 {
Arnoud113 12:02eba9a294d2 247 Y = Y+0.1;
Arnoud113 12:02eba9a294d2 248 }
Arnoud113 12:02eba9a294d2 249 */
Arnoud113 13:b5868fd8ffe9 250 if (Y >= Y_maxTreshold){
Arnoud113 12:02eba9a294d2 251 Y = Y_maxTreshold;
Arnoud113 12:02eba9a294d2 252 }
Arnoud113 13:b5868fd8ffe9 253 else if (Y <= Y_minTreshold){
Arnoud113 12:02eba9a294d2 254 Y = Y_minTreshold;
Arnoud113 12:02eba9a294d2 255 }
Arnoud113 12:02eba9a294d2 256 else{
Arnoud113 12:02eba9a294d2 257 Y = Y;
Arnoud113 12:02eba9a294d2 258 }
Arnoud113 12:02eba9a294d2 259
Arnoud113 12:02eba9a294d2 260 if(button3 == 0){
Arnoud113 12:02eba9a294d2 261 Y = Y_minTreshold;
Arnoud113 12:02eba9a294d2 262 }
Arnoud113 12:02eba9a294d2 263 else if (button4 == 0){
Arnoud113 12:02eba9a294d2 264 Y = Y_Callibrate;
Arnoud113 12:02eba9a294d2 265 }
Arnoud113 12:02eba9a294d2 266 else{
Arnoud113 12:02eba9a294d2 267 Y = Y;
Arnoud113 12:02eba9a294d2 268 }
Arnoud113 12:02eba9a294d2 269
Arnoud113 12:02eba9a294d2 270 //pc.printf("\r (in3,in4):(%f,%f), LA = %f, Y = %f \n",in3, in4, LA, Y);
Arnoud113 12:02eba9a294d2 271
Arnoud113 1:13d8940f0fd4 272 return Y;
Arnoud113 0:77ad62c61c78 273 }
Arnoud113 3:b353ee86230a 274 //----------------------------------------------------END
Arnoud113 0:77ad62c61c78 275
Arnoud113 3:b353ee86230a 276 //-------------Get current Position-------------------START
Arnoud113 3:b353ee86230a 277 double motor1_Position(){ // has as output Theta
Arnoud113 12:02eba9a294d2 278
Arnoud113 12:02eba9a294d2 279 if (button3 == 0){
Arnoud113 12:02eba9a294d2 280 int T1 = ((atan(Y_minTreshold/X_minTreshold)*180)/pi)/gainM1;
Arnoud113 12:02eba9a294d2 281 Encoder1.setPosition(T1);
Arnoud113 12:02eba9a294d2 282 }
Arnoud113 12:02eba9a294d2 283 else if (button4 ==0){
Arnoud113 12:02eba9a294d2 284 int T1 = ((atan(Y_Callibrate/X_Callibrate)*180)/pi)/gainM1;
Arnoud113 12:02eba9a294d2 285 Encoder1.setPosition(T1);
Arnoud113 12:02eba9a294d2 286 }
Arnoud113 12:02eba9a294d2 287 else{
Arnoud113 12:02eba9a294d2 288 }
Arnoud113 12:02eba9a294d2 289 double pos_M1 = gainM1*Encoder1.getPosition(); // current position for theta
Arnoud113 14:54cbd8f0efe4 290 double countM1 = Encoder1.getPosition();
Arnoud113 14:54cbd8f0efe4 291
Arnoud113 14:54cbd8f0efe4 292 pc.baud(115200);
Arnoud113 14:54cbd8f0efe4 293 pc.printf("\r counts encoder: %f\n",countM1);
Arnoud113 14:54cbd8f0efe4 294
Arnoud113 12:02eba9a294d2 295 return pos_M1;
Arnoud113 0:77ad62c61c78 296 }
Arnoud113 3:b353ee86230a 297 double motor2_Position(){ //output R
Arnoud113 12:02eba9a294d2 298 int R1;
Arnoud113 12:02eba9a294d2 299
Arnoud113 12:02eba9a294d2 300 if (button3 == 0){
Arnoud113 12:02eba9a294d2 301 R1 = (sqrt(X_minTreshold*X_minTreshold+Y_minTreshold*Y_minTreshold))/gainM2;
Arnoud113 12:02eba9a294d2 302 Encoder2.setPosition(R1);
Arnoud113 12:02eba9a294d2 303 }
Arnoud113 12:02eba9a294d2 304 else if (button4 ==0){
Arnoud113 12:02eba9a294d2 305 R1 = (sqrt(X_Callibrate*X_Callibrate+Y_Callibrate*Y_Callibrate))/gainM2;
Arnoud113 12:02eba9a294d2 306 Encoder2.setPosition(R1);
Arnoud113 12:02eba9a294d2 307 }
Arnoud113 12:02eba9a294d2 308 else{
Arnoud113 12:02eba9a294d2 309 }
Arnoud113 12:02eba9a294d2 310
Arnoud113 12:02eba9a294d2 311 double pos_M2 = gainM2 *Encoder2.getPosition(); // current position for the radius;
Arnoud113 12:02eba9a294d2 312 pc.baud(115200);
Arnoud113 12:02eba9a294d2 313 // pc.printf("\r R1 = %f, pos_m2 = %f\n", R1,pos_m2);
Arnoud113 12:02eba9a294d2 314 return pos_M2;
Arnoud113 3:b353ee86230a 315 }
Arnoud113 3:b353ee86230a 316 //-----------------------------------------------------END
Arnoud113 0:77ad62c61c78 317
Arnoud113 0:77ad62c61c78 318
Arnoud113 3:b353ee86230a 319 //------------Controller-------------------------------START
Arnoud113 0:77ad62c61c78 320 void Controller(){
Arnoud113 3:b353ee86230a 321
Arnoud113 1:13d8940f0fd4 322 double x = Get_X_Position();
Arnoud113 1:13d8940f0fd4 323 double y = Get_Y_Position();
Arnoud113 3:b353ee86230a 324
Arnoud113 14:54cbd8f0efe4 325 reference_motor1 = (atan(y/x)*180)/pi; // reference for Theta
Arnoud113 14:54cbd8f0efe4 326 reference_motor2 = sqrt((x*x+y*y)); // reference for radius
Arnoud113 0:77ad62c61c78 327
Arnoud113 14:54cbd8f0efe4 328 pos_M1 = motor1_Position(); // current position for theta
Arnoud113 14:54cbd8f0efe4 329 pos_M2 = motor2_Position(); // current position for the radius
Arnoud113 12:02eba9a294d2 330
Arnoud113 7:88d1ccba9200 331 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 332 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 333
Arnoud113 3:b353ee86230a 334 double dTheta = reference_motor1 - pos_M1;
Arnoud113 3:b353ee86230a 335 double dRadius = reference_motor2 - pos_M2;
Arnoud113 0:77ad62c61c78 336
Arnoud113 0:77ad62c61c78 337 pc.baud(115200);
Arnoud113 13:b5868fd8ffe9 338 //pc.printf("\r DesPosition(X,Y):(%f,%f), posError(dTheta, dError):(%f,%f), (delta1,delta2):(%f,%f)\n",x,y, dTheta ,dRadius,delta1, delta2);
Arnoud113 14:54cbd8f0efe4 339 //pc.printf("\r pos(M1,M2):(%f,%f)\n", pos_M1, pos_M2);
Arnoud113 0:77ad62c61c78 340
Arnoud113 2:2563d1d8461f 341 //motor1PWM = motor1;
Arnoud113 2:2563d1d8461f 342 //motor2PWM = motor2;
Arnoud113 0:77ad62c61c78 343
Arnoud113 11:66d0be7efd3f 344 if(delta1 > 0.5){
Arnoud113 3:b353ee86230a 345 motor1DC = 0;
Arnoud113 0:77ad62c61c78 346
Arnoud113 0:77ad62c61c78 347 ledr = 1;
Arnoud113 0:77ad62c61c78 348 ledg = 1; //Blau
Arnoud113 0:77ad62c61c78 349 ledb = 0;
Arnoud113 0:77ad62c61c78 350 }
Arnoud113 11:66d0be7efd3f 351 else if (delta1< -0.5) {
Arnoud113 3:b353ee86230a 352 motor1DC = 1;
Arnoud113 0:77ad62c61c78 353
Arnoud113 0:77ad62c61c78 354 ledb = 1;
Arnoud113 0:77ad62c61c78 355 ledr = 1;
Arnoud113 0:77ad62c61c78 356 ledg = 0; //Groen
Arnoud113 0:77ad62c61c78 357
Arnoud113 0:77ad62c61c78 358 }
Arnoud113 0:77ad62c61c78 359 else{
Arnoud113 0:77ad62c61c78 360 motor1PWM = 0;
Arnoud113 0:77ad62c61c78 361
Arnoud113 0:77ad62c61c78 362 ledb = 1; //Rood
Arnoud113 0:77ad62c61c78 363 ledr = 0;
Arnoud113 0:77ad62c61c78 364 ledg = 1;
Arnoud113 0:77ad62c61c78 365 }
Arnoud113 3:b353ee86230a 366
Arnoud113 9:edf01d06935e 367 motor1 = abs(delta1)/1000.0;
Arnoud113 13:b5868fd8ffe9 368 if(motor1 >= 0.20) {
Arnoud113 13:b5868fd8ffe9 369 motor1 = 0.20;
Arnoud113 9:edf01d06935e 370 //pc.baud(115200);
Arnoud113 9:edf01d06935e 371 //pc.printf("\r val motor1: %f\n", motor1);
Arnoud113 11:66d0be7efd3f 372 }
Arnoud113 0:77ad62c61c78 373
Arnoud113 11:66d0be7efd3f 374 if(delta2 > 2.0){
Arnoud113 3:b353ee86230a 375 motor2DC = 0;
Arnoud113 0:77ad62c61c78 376
Arnoud113 8:b932f8b71d3a 377 ledr = 1;
Arnoud113 8:b932f8b71d3a 378 ledg = 1; //Blau
Arnoud113 8:b932f8b71d3a 379 ledb = 0;
Arnoud113 0:77ad62c61c78 380 }
Arnoud113 11:66d0be7efd3f 381 else if (delta2<-2.0) {
Arnoud113 3:b353ee86230a 382 motor2DC = 1;
Arnoud113 0:77ad62c61c78 383
Arnoud113 8:b932f8b71d3a 384 ledb = 1;
Arnoud113 8:b932f8b71d3a 385 ledr = 1;
Arnoud113 8:b932f8b71d3a 386 ledg = 0; //Groen
Arnoud113 0:77ad62c61c78 387
Arnoud113 0:77ad62c61c78 388 }
Arnoud113 0:77ad62c61c78 389 else{
Arnoud113 0:77ad62c61c78 390 motor2PWM = 0;
Arnoud113 0:77ad62c61c78 391
Arnoud113 8:b932f8b71d3a 392 ledb = 1; //Rood
Arnoud113 8:b932f8b71d3a 393 ledr = 0;
Arnoud113 8:b932f8b71d3a 394 ledg = 1;
Arnoud113 0:77ad62c61c78 395 }
Arnoud113 3:b353ee86230a 396
Arnoud113 9:edf01d06935e 397 motor2 = abs(delta2)/1000.0;
Arnoud113 11:66d0be7efd3f 398 if(motor2 >= 0.50) {
Arnoud113 11:66d0be7efd3f 399 motor2 = 0.50;
Arnoud113 11:66d0be7efd3f 400 }
Arnoud113 3:b353ee86230a 401
Arnoud113 13:b5868fd8ffe9 402 motor1PWM = motor1 + 0.80;
Arnoud113 12:02eba9a294d2 403 motor2PWM = motor2 + 0.50;
Arnoud113 3:b353ee86230a 404
Arnoud113 11:66d0be7efd3f 405 //pc.printf("\r delta(1,2):(%f,%f)\n", delta1,delta2);
Arnoud113 12:02eba9a294d2 406 //pc.printf("\r motorvalues (M1,M2):(%f,%f),\n", motor1 + 0.65, motor2 + 0.20);
Arnoud113 3:b353ee86230a 407 //pc.printf("\r
Arnoud113 0:77ad62c61c78 408 }
Arnoud113 0:77ad62c61c78 409
Arnoud113 0:77ad62c61c78 410 int main()
Arnoud113 0:77ad62c61c78 411 {
Arnoud113 0:77ad62c61c78 412 controller.attach(&Controller, M1_TS);
Arnoud113 10:4b0b4f2abacf 413 motor1PWM.period(Pwmperiod);
Arnoud113 10:4b0b4f2abacf 414 motor2PWM.period(Pwmperiod);
Arnoud113 0:77ad62c61c78 415
Arnoud113 13:b5868fd8ffe9 416 bqc.add(&bq1).add(&bq2);
Arnoud113 13:b5868fd8ffe9 417
Arnoud113 3:b353ee86230a 418 while(1){
Arnoud113 3:b353ee86230a 419 /*
Arnoud113 3:b353ee86230a 420 double x = Get_X_Position();
Arnoud113 3:b353ee86230a 421 double y = Get_Y_Position();
Arnoud113 3:b353ee86230a 422 double reference_motor1 = atan(y/x);
Arnoud113 3:b353ee86230a 423 int position_Motor1 = motor1_Position();
Arnoud113 3:b353ee86230a 424 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 425
Arnoud113 3:b353ee86230a 426 pc.baud(115200);
Arnoud113 3:b353ee86230a 427 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 428 */
Arnoud113 3:b353ee86230a 429 }
Arnoud113 0:77ad62c61c78 430
Arnoud113 0:77ad62c61c78 431 }