electro

Dependencies:   mbed QEI mbed-os KeypadLib TextLCD

Committer:
evanegasa
Date:
Sat Mar 09 17:20:18 2019 +0000
Revision:
8:3649a359ee91
Parent:
7:52b459308d55
Child:
9:1683e8d3454f
12:20

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