Cambios

Dependencies:   mbed Adafruit_GFX SeeedShieldBot BluetoothSerial

Files at this revision

API Documentation at this revision

Comitter:
aitor98
Date:
Sun Dec 20 22:27:31 2020 +0000
Commit message:
Cambiosss

Changed in this revision

Adafruit_GFX.lib Show annotated file Show diff for this revision Revisions of this file
BluetoothSerial.lib Show annotated file Show diff for this revision Revisions of this file
SeeedShieldBot.lib Show annotated file Show diff for this revision Revisions of this file
hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_GFX.lib	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/nkhorman/code/Adafruit_GFX/#7fb1d4d3525d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BluetoothSerial.lib	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/yihui/code/BluetoothSerial/#f56002898ee8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SeeedShieldBot.lib	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/components/code/SeeedShieldBot/#9fd1722259a1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.cpp	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,98 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "hcsr04.h"
+
+
+HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
+    trigger(TrigPin), echo(EchoPin)
+{
+    pulsetime.stop();
+    pulsetime.reset();
+    echo.rise(this,&HCSR04::isr_rise);
+    echo.fall(this,&HCSR04::isr_fall);
+    trigger=0;
+}
+
+HCSR04::~HCSR04()
+{
+}
+
+void HCSR04::isr_rise(void)
+{
+    pulsetime.start();
+}
+void HCSR04::start(void)
+{
+    trigger=1;
+    wait_us(10);
+    trigger=0;
+}
+
+void HCSR04::isr_fall(void)
+{
+    pulsetime.stop();
+    pulsedur = pulsetime.read_us();
+    distance= (pulsedur*343)/20000;
+    pulsetime.reset();
+}
+
+void HCSR04::rise (void (*fptr)(void))
+{
+    echo.rise(fptr);
+}
+void HCSR04::fall (void (*fptr)(void))
+{
+    echo.fall(fptr);
+}
+
+unsigned int HCSR04::get_dist_cm()
+{
+    return distance;
+}
+unsigned int HCSR04::get_pulse_us()
+{
+    return pulsedur;
+}
+
+
+
+/*******************************************************
+   Here is a sample code usage
+********************************************************* 
+#include "hcsr04.h"
+HCSR04  usensor(p25,p6);
+int main()
+{
+    unsigned char count=0;
+    while(1) {
+        usensor.start();
+        wait_ms(500); 
+        dist=usensor.get_dist_cm();
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.printf("cm:%ld",dist );
+ 
+        count++;
+        lcd.locate(0,1);
+        lcd.printf("Distance =%d",count);
+        
+    }
+*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.h	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,68 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * For more details on the sensor :
+ * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MBED_HCSR04_H
+#define MBED_HCSR04_H
+
+#include "mbed.h"
+
+/** HCSR04 Class(es)
+ */
+
+class HCSR04
+{
+public:
+    /** Create a HCSR04 object connected to the specified pin
+    * @param pin i/o pin to connect to
+    */
+    HCSR04(PinName TrigPin,PinName EchoPin);
+    ~HCSR04();
+
+    /** Return the distance from obstacle in cm
+    * @param distance in cms and returns -1, in case of failure
+    */
+    unsigned int get_dist_cm(void);
+    /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver.
+    * @param pulse duration in microseconds.
+    */
+    unsigned int get_pulse_us(void);
+    /** Generates the trigger pulse of 10us on the trigger PIN.
+    */
+    void start(void );
+    void isr_rise(void);
+    void isr_fall(void);
+    void fall (void (*fptr)(void));
+    void rise (void (*fptr)(void));
+
+
+
+private:
+
+    Timer pulsetime;
+    DigitalOut  trigger;
+    InterruptIn echo;
+    unsigned int pulsedur;
+    unsigned int distance;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,234 @@
+#include "mbed.h"
+#include "hcsr04.h"
+#include "Adafruit_SSD1306.h"
+/*#include "estadomovder.h"
+#include "estadoinicio.h"
+#include "estadocalentar.h"
+#include "estadomovizq1.h"
+#include "estadocentro.h"
+#include "estadomovizq2.h"
+#include "estadosoplado.h"*/
+
+
+class I2CPreInit : public I2C
+{
+public:
+    I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
+    {
+        frequency(100000);
+        start();
+    };
+};
+
+I2CPreInit gI2C(PB_9,PB_8);
+
+Serial pc(USBTX, USBRX); // tx, rx
+Serial bt(A5, A4); // tx, rx
+Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
+Ticker repeticion;
+
+DigitalOut led(LED1);
+DigitalOut step(D13);
+DigitalOut dir(D12);
+DigitalOut enable(D11);
+DigitalOut dirAMotor(D4);
+PwmOut     dirBMotor(A3);
+InterruptIn encoderA(A2);
+DigitalOut rele(D3);
+HCSR04  usensor(D7,D8);
+DigitalIn final1(D9);
+InterruptIn final2(D10);
+AnalogIn Vout(A0);
+AnalogIn Aout(A1);
+
+enum estados {inicio,movder,calentar,movizq1,centro,movizq2,soplado};
+estados estado;
+
+
+void medir_corriente()
+{
+
+    float corriente=((Aout.read()*5)-2.5)/0.185;
+
+    pc.printf("Intensidad medida = %.4f A \n", corriente);
+    bt.printf("Intensidad medida = %.4f A \n", corriente);
+
+}
+
+int dato=0;
+
+void estadoinicio()
+{
+    {
+        if(bt.readable()) { //Mira si hay algun dato pendiente en el bt
+            dato=bt.getc();  //Lee un caracter del bt
+
+        }
+        if (dato=='1') {            //Si el dato es un 1 comienza el proceso
+
+            estado = movder;
+
+
+        } else {      //Si el dato es cualquier otra cosa no empieza a hacer nada
+
+            pc.printf("Reposo\n");
+        }
+    }
+}
+
+
+void estadomovderecha()
+{
+    if(final1==0) {
+        enable=1; //activar el motor
+        dir=1; //direccion hacia la derecha
+        //step=1; // avanzar
+        step=1;
+        wait_us(100);
+        step=0;
+        wait_us(900);
+        pc.printf("Estado: Moviendo derecha\n");
+        gOled.printf("Transportando\n");
+    } else {
+        enable=0;
+        estado=calentar;
+        gOled.clearDisplay();
+    }
+}
+
+void estadocalentar()
+{
+    float tension=Vout.read()*3.3;
+    //pc.printf("Valor voltios %.4f leido flat\n",tension);
+
+    float resistencia= ((3.3*100000.0)/tension)-100000.0;
+
+    float temp= 3950.0/(log(resistencia/100000.0)+(3950.0/298.0));
+    gOled.printf("Calentando a: %.4f\n",temp);
+
+    if (temp<35.0) {
+        //rele.write(1);
+        rele=1;
+        pc.printf("Estado: Rele activado\n");
+
+    } else {
+        rele=0;
+        estado=movizq1;
+        //gOled.clearDisplay();
+    }
+}
+
+void estadomovizq1()
+{
+    unsigned int dist;
+    usensor.start();
+    dist=usensor.get_dist_cm();
+
+    if(dist>20) {
+        enable=1; //activar el motor
+        dir=0; //direccion hacia la derecha
+        step=1; // avanzar
+        wait_us(100);
+        step=0;
+        wait_us(900);
+        pc.printf("Estado: Moviendo izquierda1\n");
+        gOled.printf("Transportando\n");
+    } else {
+        enable=0;
+        estado=centro;
+        gOled.clearDisplay();
+    }
+}
+
+
+void estadocentro()
+{
+    {
+        if(bt.readable()) { //Mira si hay algun dato pendiente en el bt
+            dato=bt.getc();  //Lee un caracter del bt
+
+        }
+        if (dato=='2') {            //Si el dato es un 1 comienza el proceso
+
+            estado = movder;
+            gOled.printf("Pieza incorrecta\n");
+
+        } else if(dato=='3') {      //Si el dato es una b enciende el led
+
+            estado = movizq2;
+            gOled.printf("Pieza correcta\n");
+        }
+    }
+}
+
+void estadomovizq2()
+{
+    if(final2==0) {
+        enable=1; //activar el motor
+        dir=1; //direccion hacia la derecha
+        step=1; // avanzar
+        wait_us(100);
+        step=0;
+        wait_us(900);
+        pc.printf("Estado: Moviendo izquierda2\n");
+        gOled.printf("Transportando\n");
+    } else {
+        enable=0;
+        estado=soplado;
+        gOled.clearDisplay();
+    }
+}
+
+void estadosoplado()
+{
+
+    dirAMotor=0;
+    pc.printf("Soplando pieza");
+    gOled.printf("Soplando");
+    wait(5);
+    estado=inicio;
+
+}
+
+
+
+int main()
+{
+    pc.baud(115200);
+    estado = inicio;
+    gOled.begin();
+    gOled.clearDisplay();
+
+    repeticion.attach(&medir_corriente, 5.0); //Medir la intensidad cada tres segundos
+
+
+    while(1) {
+        switch (estado) {
+            case inicio:
+                estadoinicio();
+                break;
+            case movder:
+                estadomovderecha();
+                break;
+            case calentar:
+                estadocalentar();
+                break;
+            case movizq1:
+                estadomovizq1();
+                break;
+            case centro:
+                estadocentro();
+                break;
+            case movizq2:
+                estadomovizq2();
+                break;
+            case soplado:
+                estadosoplado();
+                break;
+            default:
+                break;
+
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Dec 20 22:27:31 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file