Alzaga Uriarte / Mbed 2 deprecated TODOJUNTO

Dependencies:   mbed Adafruit_GFX SeeedShieldBot BluetoothSerial

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "hcsr04.h"
00003 #include "Adafruit_SSD1306.h"
00004 /*#include "estadomovder.h"
00005 #include "estadoinicio.h"
00006 #include "estadocalentar.h"
00007 #include "estadomovizq1.h"
00008 #include "estadocentro.h"
00009 #include "estadomovizq2.h"
00010 #include "estadosoplado.h"*/
00011 
00012 
00013 class I2CPreInit : public I2C
00014 {
00015 public:
00016     I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
00017     {
00018         frequency(100000);
00019         start();
00020     };
00021 };
00022 
00023 I2CPreInit gI2C(PB_9,PB_8);
00024 
00025 Serial pc(USBTX, USBRX); // tx, rx
00026 Serial bt(A5, A4); // tx, rx
00027 Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
00028 Ticker repeticion;
00029 
00030 DigitalOut led(LED1);
00031 DigitalOut step(D13);
00032 DigitalOut dir(D12);
00033 DigitalOut enable(D11);
00034 DigitalOut dirAMotor(D4);
00035 PwmOut     dirBMotor(A3);
00036 InterruptIn encoderA(A2);
00037 DigitalOut rele(D3);
00038 HCSR04  usensor(D7,D8);
00039 DigitalIn final1(D9);
00040 InterruptIn final2(D10);
00041 AnalogIn Vout(A0);
00042 AnalogIn Aout(A1);
00043 
00044 enum estados {inicio,movder,calentar,movizq1,centro,movizq2,soplado};
00045 estados estado;
00046 
00047 
00048 void medir_corriente()
00049 {
00050 
00051     float corriente=((Aout.read()*5)-2.5)/0.185;
00052 
00053     pc.printf("Intensidad medida = %.4f A \n", corriente);
00054     bt.printf("Intensidad medida = %.4f A \n", corriente);
00055 
00056 }
00057 
00058 int dato=0;
00059 
00060 void estadoinicio()
00061 {
00062     {
00063         if(bt.readable()) { //Mira si hay algun dato pendiente en el bt
00064             dato=bt.getc();  //Lee un caracter del bt
00065 
00066         }
00067         if (dato=='1') {            //Si el dato es un 1 comienza el proceso
00068 
00069             estado = movder;
00070 
00071 
00072         } else {      //Si el dato es cualquier otra cosa no empieza a hacer nada
00073 
00074             pc.printf("Reposo\n");
00075         }
00076     }
00077 }
00078 
00079 
00080 void estadomovderecha()
00081 {
00082     if(final1==0) {
00083         enable=1; //activar el motor
00084         dir=1; //direccion hacia la derecha
00085         //step=1; // avanzar
00086         step=1;
00087         wait_us(100);
00088         step=0;
00089         wait_us(900);
00090         pc.printf("Estado: Moviendo derecha\n");
00091         gOled.printf("Transportando\n");
00092     } else {
00093         enable=0;
00094         estado=calentar;
00095         gOled.clearDisplay();
00096     }
00097 }
00098 
00099 void estadocalentar()
00100 {
00101     float tension=Vout.read()*3.3;
00102     //pc.printf("Valor voltios %.4f leido flat\n",tension);
00103 
00104     float resistencia= ((3.3*100000.0)/tension)-100000.0;
00105 
00106     float temp= 3950.0/(log(resistencia/100000.0)+(3950.0/298.0));
00107     gOled.printf("Calentando a: %.4f\n",temp);
00108 
00109     if (temp<35.0) {
00110         //rele.write(1);
00111         rele=1;
00112         pc.printf("Estado: Rele activado\n");
00113 
00114     } else {
00115         rele=0;
00116         estado=movizq1;
00117         //gOled.clearDisplay();
00118     }
00119 }
00120 
00121 void estadomovizq1()
00122 {
00123     unsigned int dist;
00124     usensor.start();
00125     dist=usensor.get_dist_cm();
00126 
00127     if(dist>20) {
00128         enable=1; //activar el motor
00129         dir=0; //direccion hacia la derecha
00130         step=1; // avanzar
00131         wait_us(100);
00132         step=0;
00133         wait_us(900);
00134         pc.printf("Estado: Moviendo izquierda1\n");
00135         gOled.printf("Transportando\n");
00136     } else {
00137         enable=0;
00138         estado=centro;
00139         gOled.clearDisplay();
00140     }
00141 }
00142 
00143 
00144 void estadocentro()
00145 {
00146     {
00147         if(bt.readable()) { //Mira si hay algun dato pendiente en el bt
00148             dato=bt.getc();  //Lee un caracter del bt
00149 
00150         }
00151         if (dato=='2') {            //Si el dato es un 1 comienza el proceso
00152 
00153             estado = movder;
00154             gOled.printf("Pieza incorrecta\n");
00155 
00156         } else if(dato=='3') {      //Si el dato es una b enciende el led
00157 
00158             estado = movizq2;
00159             gOled.printf("Pieza correcta\n");
00160         }
00161     }
00162 }
00163 
00164 void estadomovizq2()
00165 {
00166     if(final2==0) {
00167         enable=1; //activar el motor
00168         dir=1; //direccion hacia la derecha
00169         step=1; // avanzar
00170         wait_us(100);
00171         step=0;
00172         wait_us(900);
00173         pc.printf("Estado: Moviendo izquierda2\n");
00174         gOled.printf("Transportando\n");
00175     } else {
00176         enable=0;
00177         estado=soplado;
00178         gOled.clearDisplay();
00179     }
00180 }
00181 
00182 void estadosoplado()
00183 {
00184 
00185     dirAMotor=0;
00186     pc.printf("Soplando pieza");
00187     gOled.printf("Soplando");
00188     wait(5);
00189     estado=inicio;
00190 
00191 }
00192 
00193 
00194 
00195 int main()
00196 {
00197     pc.baud(115200);
00198     estado = inicio;
00199     gOled.begin();
00200     gOled.clearDisplay();
00201 
00202     repeticion.attach(&medir_corriente, 5.0); //Medir la intensidad cada tres segundos
00203 
00204 
00205     while(1) {
00206         switch (estado) {
00207             case inicio:
00208                 estadoinicio();
00209                 break;
00210             case movder:
00211                 estadomovderecha();
00212                 break;
00213             case calentar:
00214                 estadocalentar();
00215                 break;
00216             case movizq1:
00217                 estadomovizq1();
00218                 break;
00219             case centro:
00220                 estadocentro();
00221                 break;
00222             case movizq2:
00223                 estadomovizq2();
00224                 break;
00225             case soplado:
00226                 estadosoplado();
00227                 break;
00228             default:
00229                 break;
00230 
00231         }
00232     }
00233 }
00234