electro

Dependencies:   mbed QEI mbed-os KeypadLib TextLCD

Committer:
afsaavedram
Date:
Tue Mar 05 13:33:10 2019 +0000
Revision:
7:52b459308d55
Parent:
6:0d2b3f985e41
Child:
8:3649a359ee91
v1.1 con control PI para motor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
afsaavedram 0:6cde030caebb 1 #include "mbed.h"
afsaavedram 0:6cde030caebb 2 #include "keypad.h"
afsaavedram 0:6cde030caebb 3 #include "TextLCD.h"
evanegasa 6:0d2b3f985e41 4 #include "stdlib.h"
afsaavedram 7:52b459308d55 5 #include "QEI.h"
afsaavedram 4:2636fe1c6527 6
evanegasa 6:0d2b3f985e41 7 /******************* IO ********************/
evanegasa 6:0d2b3f985e41 8 DigitalOut E_1(D8,0); // Control de electrovalvula 1
evanegasa 6:0d2b3f985e41 9 DigitalOut E_2(D9,0); // Control de electrovalvula 2
evanegasa 6:0d2b3f985e41 10 DigitalOut E_3(D10,0); // Control de electrovalvula 3
evanegasa 6:0d2b3f985e41 11 DigitalOut E_4(D12,0); // Control de electrovalvula 4
afsaavedram 7:52b459308d55 12 QEI encoder(PD_6,PD_5,NC,4576,QEI::X2_ENCODING);
afsaavedram 7:52b459308d55 13 PwmOut motor_plus(PB_8);
afsaavedram 7:52b459308d55 14 PwmOut motor_cero(PB_9); // Variables para el motor
afsaavedram 7:52b459308d55 15
afsaavedram 0:6cde030caebb 16
evanegasa 6:0d2b3f985e41 17 /******************* INTERRUPCIONES ********************/
evanegasa 6:0d2b3f985e41 18 Timeout t;
evanegasa 6:0d2b3f985e41 19 InterruptIn button(USER_BUTTON); // Cancelar operación
afsaavedram 0:6cde030caebb 20
evanegasa 6:0d2b3f985e41 21 /*******************VARIABLES********************/
afsaavedram 7:52b459308d55 22 int tempUser1 = 5; // Tiempo tope de electrocoagulacion
afsaavedram 7:52b459308d55 23 int tempUser2 = 5; // Tiempo tope de reposo de la vinaza
afsaavedram 7:52b459308d55 24 int tempUser3 = 5; // Tiempo tope de vaciado del agua en el tanque 2
afsaavedram 7:52b459308d55 25 int tempUser4 = 5; // Tiempo tope de vaciado de residuos en el tanque 2
afsaavedram 7:52b459308d55 26 int pwm = 50.0f; // Duty cycle del motor
afsaavedram 7:52b459308d55 27 int tiempoLlenado = 5; // Tiempo de llenado del tanque 1
afsaavedram 0:6cde030caebb 28
evanegasa 6:0d2b3f985e41 29 int a = 0;
evanegasa 6:0d2b3f985e41 30 int b = 0;
evanegasa 6:0d2b3f985e41 31 int c = 0;
evanegasa 6:0d2b3f985e41 32 int running = 0;
evanegasa 6:0d2b3f985e41 33 int finished = 0;
evanegasa 6:0d2b3f985e41 34 int canceled = 0;
evanegasa 6:0d2b3f985e41 35 int campo = 1;
afsaavedram 0:6cde030caebb 36
afsaavedram 7:52b459308d55 37 float y; //vel
afsaavedram 7:52b459308d55 38 float pwm_pas=50;
afsaavedram 7:52b459308d55 39 float e=0;
afsaavedram 7:52b459308d55 40 float r=1.0f;
afsaavedram 7:52b459308d55 41 float e_pas=0;
afsaavedram 7:52b459308d55 42
evanegasa 6:0d2b3f985e41 43 /******************** TECLADO Y LCD ***********************/
evanegasa 6:0d2b3f985e41 44 Serial pc(USBTX, USBRX); //rs, e,d4,d5,d6,d7
evanegasa 6:0d2b3f985e41 45 TextLCD lcd(PC_8,PC_9,PC_10,PC_11,PC_12,PD_2);
evanegasa 6:0d2b3f985e41 46 Keypad keypad(D3, D2, D1, D0, D7, D6, D5, D4);
afsaavedram 0:6cde030caebb 47
evanegasa 6:0d2b3f985e41 48 /********************* FUNCIONES **************************/
afsaavedram 4:2636fe1c6527 49
afsaavedram 7:52b459308d55 50 void printLCD()
afsaavedram 7:52b459308d55 51 {
afsaavedram 7:52b459308d55 52 while(1) {
evanegasa 5:9869290cb13f 53 lcd.cls();
evanegasa 5:9869290cb13f 54 lcd.locate(0,0);
afsaavedram 7:52b459308d55 55 lcd.printf("t1=%d",tempUser1);
evanegasa 5:9869290cb13f 56 lcd.locate(6,0);
afsaavedram 7:52b459308d55 57 lcd.printf("t2=%d",tempUser2);
evanegasa 5:9869290cb13f 58 lcd.locate(0,1);
afsaavedram 7:52b459308d55 59 lcd.printf("t3=%d",tempUser3);
evanegasa 5:9869290cb13f 60 lcd.locate(6,1);
afsaavedram 7:52b459308d55 61 lcd.printf("t4=%d",tempUser4);
evanegasa 5:9869290cb13f 62 lcd.locate(12,0);
afsaavedram 7:52b459308d55 63 lcd.printf("PWM");
evanegasa 5:9869290cb13f 64 lcd.locate(12,1);
afsaavedram 7:52b459308d55 65 lcd.printf("%d",pwm);
afsaavedram 7:52b459308d55 66 wait_ms(150);
afsaavedram 7:52b459308d55 67 switch(campo) {
evanegasa 5:9869290cb13f 68 case 1:
evanegasa 5:9869290cb13f 69 lcd.locate(0,0);
afsaavedram 7:52b459308d55 70 lcd.printf("t1= ",tempUser1);
afsaavedram 7:52b459308d55 71 break;
evanegasa 5:9869290cb13f 72 case 2:
evanegasa 5:9869290cb13f 73 lcd.locate(6,0);
afsaavedram 7:52b459308d55 74 lcd.printf("t2= ",tempUser2);
afsaavedram 7:52b459308d55 75 break;
evanegasa 5:9869290cb13f 76 case 3:
evanegasa 5:9869290cb13f 77 lcd.locate(0,1);
afsaavedram 7:52b459308d55 78 lcd.printf("t3= ",tempUser3);
afsaavedram 7:52b459308d55 79 break;
evanegasa 5:9869290cb13f 80 case 4:
evanegasa 5:9869290cb13f 81 lcd.locate(6,1);
afsaavedram 7:52b459308d55 82 lcd.printf("t4= ",tempUser4);
afsaavedram 7:52b459308d55 83 break;
evanegasa 5:9869290cb13f 84 case 5:
evanegasa 5:9869290cb13f 85 lcd.locate(12,1);
evanegasa 5:9869290cb13f 86 lcd.printf(" ");
afsaavedram 7:52b459308d55 87 default:
afsaavedram 7:52b459308d55 88 ;
afsaavedram 7:52b459308d55 89 break;
afsaavedram 4:2636fe1c6527 90 }
afsaavedram 7:52b459308d55 91 wait_ms(150);
afsaavedram 7:52b459308d55 92 }
afsaavedram 4:2636fe1c6527 93 }
afsaavedram 4:2636fe1c6527 94
afsaavedram 7:52b459308d55 95 int getNum()
afsaavedram 7:52b459308d55 96 {
afsaavedram 7:52b459308d55 97 while(1) {
evanegasa 5:9869290cb13f 98 char key = keypad.getKey();
evanegasa 5:9869290cb13f 99 switch(key) {
afsaavedram 7:52b459308d55 100 case '0':
afsaavedram 7:52b459308d55 101 return 0;
afsaavedram 7:52b459308d55 102 case '1':
afsaavedram 7:52b459308d55 103 return 1;
afsaavedram 7:52b459308d55 104 case '2':
afsaavedram 7:52b459308d55 105 return 2;
afsaavedram 7:52b459308d55 106 case '3':
afsaavedram 7:52b459308d55 107 return 3;
afsaavedram 7:52b459308d55 108 case '4':
afsaavedram 7:52b459308d55 109 return 4;
afsaavedram 7:52b459308d55 110 case '5':
afsaavedram 7:52b459308d55 111 return 5;
afsaavedram 7:52b459308d55 112 case '6':
afsaavedram 7:52b459308d55 113 return 6;
afsaavedram 7:52b459308d55 114 case '7':
afsaavedram 7:52b459308d55 115 return 7;
afsaavedram 7:52b459308d55 116 case '8':
afsaavedram 7:52b459308d55 117 return 8;
afsaavedram 7:52b459308d55 118 case '9':
afsaavedram 7:52b459308d55 119 return 9;
afsaavedram 7:52b459308d55 120 case 'A':
evanegasa 6:0d2b3f985e41 121 campo++;
afsaavedram 7:52b459308d55 122 if(campo==6) campo=1;
afsaavedram 7:52b459308d55 123 return -1;
evanegasa 6:0d2b3f985e41 124 //case 'B': return -2;
evanegasa 6:0d2b3f985e41 125 //case 'C': return -3;
afsaavedram 7:52b459308d55 126 case 'D':
afsaavedram 7:52b459308d55 127 return -4;
afsaavedram 7:52b459308d55 128 default:
afsaavedram 7:52b459308d55 129 ;
afsaavedram 7:52b459308d55 130 break;
afsaavedram 4:2636fe1c6527 131 }
evanegasa 5:9869290cb13f 132 wait_ms(100);
afsaavedram 4:2636fe1c6527 133 }
afsaavedram 7:52b459308d55 134 }
afsaavedram 4:2636fe1c6527 135
evanegasa 6:0d2b3f985e41 136 /*********************** MAIN ***************************/
afsaavedram 0:6cde030caebb 137
afsaavedram 7:52b459308d55 138 void init()
afsaavedram 7:52b459308d55 139 {
evanegasa 6:0d2b3f985e41 140 E_1 = 0;
evanegasa 6:0d2b3f985e41 141 E_2 = 0;
evanegasa 6:0d2b3f985e41 142 E_3 = 0;
evanegasa 6:0d2b3f985e41 143 E_4 = 0;
afsaavedram 0:6cde030caebb 144 keypad.enablePullUp();
afsaavedram 0:6cde030caebb 145 wait(1);
afsaavedram 0:6cde030caebb 146 lcd.locate(0,0);
afsaavedram 0:6cde030caebb 147 lcd.printf(" Control para\n");
afsaavedram 0:6cde030caebb 148 lcd.locate(0,1);
afsaavedram 0:6cde030caebb 149 lcd.printf("electrocoagular\n");
afsaavedram 3:a4994da37d1f 150 wait(1.5);
afsaavedram 7:52b459308d55 151
evanegasa 6:0d2b3f985e41 152 return;
evanegasa 6:0d2b3f985e41 153 }
afsaavedram 2:4b27d3d7341a 154
afsaavedram 7:52b459308d55 155 void setUserTimes()
afsaavedram 7:52b459308d55 156 {
afsaavedram 7:52b459308d55 157 while(1) {
afsaavedram 7:52b459308d55 158 setvalue:
evanegasa 6:0d2b3f985e41 159 a = 100*getNum();
afsaavedram 7:52b459308d55 160 wait_ms(200);
afsaavedram 7:52b459308d55 161 if(a==-1)goto setvalue;
afsaavedram 7:52b459308d55 162 if(a==-4)break;
evanegasa 6:0d2b3f985e41 163 b = 10*getNum();
afsaavedram 7:52b459308d55 164 wait_ms(200);
afsaavedram 7:52b459308d55 165 if(b==-1)goto setvalue;
afsaavedram 7:52b459308d55 166 if(b==-4)break;
afsaavedram 7:52b459308d55 167 c = getNum();
afsaavedram 7:52b459308d55 168 wait_ms(200);
afsaavedram 7:52b459308d55 169 if(c==-1)goto setvalue;
afsaavedram 7:52b459308d55 170 if(c==-4)break;
afsaavedram 7:52b459308d55 171
afsaavedram 7:52b459308d55 172 switch(campo) {
afsaavedram 7:52b459308d55 173 case 1:
afsaavedram 7:52b459308d55 174 tempUser1=a+b+c;
afsaavedram 7:52b459308d55 175 break;
afsaavedram 7:52b459308d55 176 case 2:
afsaavedram 7:52b459308d55 177 tempUser2=a+b+c;
afsaavedram 7:52b459308d55 178 break;
afsaavedram 7:52b459308d55 179 case 3:
afsaavedram 7:52b459308d55 180 tempUser3=a+b+c;
afsaavedram 7:52b459308d55 181 break;
afsaavedram 7:52b459308d55 182 case 4:
afsaavedram 7:52b459308d55 183 tempUser4=a+b+c;
afsaavedram 7:52b459308d55 184 break;
afsaavedram 7:52b459308d55 185 case 5:
afsaavedram 7:52b459308d55 186 pwm=a+b+c;
afsaavedram 7:52b459308d55 187 break;
afsaavedram 7:52b459308d55 188 default:
afsaavedram 7:52b459308d55 189 ;
afsaavedram 7:52b459308d55 190 break;
afsaavedram 3:a4994da37d1f 191 }
afsaavedram 7:52b459308d55 192 wait_ms(200);
evanegasa 6:0d2b3f985e41 193 }
afsaavedram 7:52b459308d55 194 return;
evanegasa 6:0d2b3f985e41 195 }
afsaavedram 0:6cde030caebb 196
afsaavedram 7:52b459308d55 197 void vaciadoSolido()
afsaavedram 7:52b459308d55 198 {
evanegasa 6:0d2b3f985e41 199 E_4 = 0;
afsaavedram 7:52b459308d55 200 finished = 1;
evanegasa 6:0d2b3f985e41 201 }
evanegasa 6:0d2b3f985e41 202
afsaavedram 7:52b459308d55 203 void vaciadoLiquido()
afsaavedram 7:52b459308d55 204 {
evanegasa 6:0d2b3f985e41 205 E_3 = 0;
evanegasa 6:0d2b3f985e41 206 E_4 = 1;
evanegasa 6:0d2b3f985e41 207 t.attach(&vaciadoSolido, tempUser4);
evanegasa 6:0d2b3f985e41 208 }
evanegasa 6:0d2b3f985e41 209
afsaavedram 7:52b459308d55 210 void reposo()
afsaavedram 7:52b459308d55 211 {
evanegasa 6:0d2b3f985e41 212 E_3 = 1;
evanegasa 6:0d2b3f985e41 213 t.attach(&vaciadoLiquido, tempUser3);
evanegasa 6:0d2b3f985e41 214 }
evanegasa 6:0d2b3f985e41 215
afsaavedram 7:52b459308d55 216 void vaciado1()
afsaavedram 7:52b459308d55 217 {
afsaavedram 7:52b459308d55 218 E_2 = 0;
evanegasa 6:0d2b3f985e41 219 t.attach(&reposo, tempUser2);
afsaavedram 7:52b459308d55 220 motor_plus.write(0.0f);
evanegasa 6:0d2b3f985e41 221 }
evanegasa 6:0d2b3f985e41 222
afsaavedram 7:52b459308d55 223 void electro()
afsaavedram 7:52b459308d55 224 {
afsaavedram 7:52b459308d55 225 motor_plus.write((float)pwm/100);
afsaavedram 7:52b459308d55 226 motor_cero.write(0.0f);
evanegasa 6:0d2b3f985e41 227 E_2 = 1;
evanegasa 6:0d2b3f985e41 228 t.attach(&vaciado1, tiempoLlenado);
evanegasa 6:0d2b3f985e41 229 }
evanegasa 6:0d2b3f985e41 230
afsaavedram 7:52b459308d55 231 void tiempoValvula1()
afsaavedram 7:52b459308d55 232 {
evanegasa 6:0d2b3f985e41 233 E_1 = 0;
afsaavedram 7:52b459308d55 234 //motor_plus.write( pwm/100);
evanegasa 6:0d2b3f985e41 235 t.attach(&electro, tempUser1);
afsaavedram 7:52b459308d55 236
afsaavedram 7:52b459308d55 237
afsaavedram 7:52b459308d55 238 y = encoder.getSumangle();
afsaavedram 7:52b459308d55 239 e=r-y;
afsaavedram 7:52b459308d55 240 pwm=(1.639*e)-(1.341*e_pas)+(pwm_pas);
afsaavedram 7:52b459308d55 241 pwm_pas=pwm;
afsaavedram 7:52b459308d55 242 e_pas=e;
afsaavedram 7:52b459308d55 243 motor_plus.write((float)pwm*0.01f);
afsaavedram 7:52b459308d55 244
afsaavedram 7:52b459308d55 245
afsaavedram 7:52b459308d55 246
evanegasa 6:0d2b3f985e41 247 }
evanegasa 6:0d2b3f985e41 248
afsaavedram 7:52b459308d55 249 void cancel_op()
afsaavedram 7:52b459308d55 250 {
evanegasa 6:0d2b3f985e41 251 canceled = 1;
evanegasa 6:0d2b3f985e41 252 t.detach();
evanegasa 6:0d2b3f985e41 253 }
evanegasa 6:0d2b3f985e41 254
afsaavedram 7:52b459308d55 255 int main()
afsaavedram 7:52b459308d55 256 {
afsaavedram 7:52b459308d55 257
afsaavedram 7:52b459308d55 258 motor_plus.period(0.015f);
afsaavedram 7:52b459308d55 259 motor_cero.write(0.0f);
afsaavedram 7:52b459308d55 260
afsaavedram 7:52b459308d55 261 button.rise(&cancel_op);
afsaavedram 7:52b459308d55 262 while(1) {
afsaavedram 7:52b459308d55 263 if(running==0) {
evanegasa 6:0d2b3f985e41 264 running = 1;
evanegasa 6:0d2b3f985e41 265 init();
evanegasa 6:0d2b3f985e41 266 Thread lcdprint;
evanegasa 6:0d2b3f985e41 267 lcdprint.start(printLCD);
evanegasa 6:0d2b3f985e41 268
afsaavedram 7:52b459308d55 269
afsaavedram 7:52b459308d55 270
evanegasa 6:0d2b3f985e41 271 campo = 1;
evanegasa 6:0d2b3f985e41 272 setUserTimes();
afsaavedram 7:52b459308d55 273
evanegasa 6:0d2b3f985e41 274 campo = 0;
evanegasa 6:0d2b3f985e41 275 finished = 0;
evanegasa 6:0d2b3f985e41 276 canceled = 0;
evanegasa 6:0d2b3f985e41 277 E_1 = 1;
afsaavedram 7:52b459308d55 278
afsaavedram 7:52b459308d55 279 t.attach(&tiempoValvula1, tiempoLlenado);
afsaavedram 7:52b459308d55 280
evanegasa 6:0d2b3f985e41 281 }
afsaavedram 7:52b459308d55 282 if(finished == 1) {
evanegasa 6:0d2b3f985e41 283 lcd.cls();
evanegasa 6:0d2b3f985e41 284 lcd.locate(0,0);
evanegasa 6:0d2b3f985e41 285 lcd.printf("Finalizado\n");
evanegasa 6:0d2b3f985e41 286 running = 0;
evanegasa 6:0d2b3f985e41 287 wait(2);
evanegasa 6:0d2b3f985e41 288 }
afsaavedram 7:52b459308d55 289 if(canceled == 1) {
evanegasa 6:0d2b3f985e41 290 lcd.cls();
evanegasa 6:0d2b3f985e41 291 lcd.locate(0,0);
evanegasa 6:0d2b3f985e41 292 lcd.printf("Operacion\n");
evanegasa 6:0d2b3f985e41 293 lcd.locate(0,1);
evanegasa 6:0d2b3f985e41 294 lcd.printf("cancelada\n");
evanegasa 6:0d2b3f985e41 295 running = 0;
evanegasa 6:0d2b3f985e41 296 wait(2);
evanegasa 6:0d2b3f985e41 297 }
evanegasa 6:0d2b3f985e41 298 }
evanegasa 6:0d2b3f985e41 299 }