version antes de terminar automatizacion.

Dependencies:   mbed Servo TextLCD

main.cpp

Committer:
oggonei
Date:
2018-11-22
Revision:
0:e7637a78fcf4

File content as of revision 0:e7637a78fcf4:

#include "mbed.h"
#include "Servo.h"
#include "TextLCD.h"

Serial command(USBTX,USBRX);   
TextLCD lcd(PB_3, PB_5, PB_4, PB_10, PA_8, PA_9); // rs, e, d4-d7
DigitalOut trigger6(PC_10);
DigitalIn  echo6(PC_11);
DigitalOut trigger5(PC_12);
DigitalIn  echo5(PD_2);
DigitalOut trigger4(PC_0);
DigitalIn  echo4(PC_3);
DigitalOut trigger3(PC_1);
DigitalIn  echo3(PC_2);
DigitalOut trigger2(PA_4);
DigitalIn  echo2(PB_0);
DigitalOut trigger1(PA_1);
DigitalIn  echo1(PA_0);
DigitalOut trigger0(PA_13);
DigitalIn  echo0(PA_14);
DigitalOut mder(PC_8); //bandeja derecha
DigitalOut miz(PC_9);  //bandeja inquierda
DigitalOut stepsy(D3);
DigitalOut diry(D6);
DigitalOut ene(D8);
Servo myservo(PA_10);
int distance = 0;
int correction = 0;
Timer sonar;
int x=0,j=0,puestos=0;

int main()
{
    sonar.reset();
    sonar.start();
    while (echo6==2) {};
    sonar.stop();
    correction = sonar.read_us();
    command.baud(9600);
    myservo = 0.22;
    lcd.printf("Parking!\nBienvenido");
    
    inicio:
    if(command.readable()>0)
        {
            x=command.getc();
            if(x=='a') // automatico
                {
                    wait(0.4);
                    lcd.cls();
                    lcd.printf("Mando automatico");
                    command.printf("Entra automatico");
                    wait(0.3);
                    automatico:
                    lcd.cls();
                    lcd.printf("buscando\nCarro");
                    trigger0 = 1;
                    sonar.reset();
                    wait_us(10.0);
                    trigger0 = 0;
                    while (echo0==0) {};
                    sonar.start();
                    while (echo0==1) {};
                    sonar.stop();
                    distance = (sonar.read_us()-correction)/58.0;
                        if(distance<8)
                        {
                            lcd.cls();
                            lcd.printf("Espere\nPor Favor");
                            wait(0.5);
                            lcd.cls();
                            lcd.printf("Buscando \nPuesto");
                            //command.printf("rojo %d",distance);
                            wait(1);
                            trigger1 = 1;
                            sonar.reset();
                            wait_us(10.0);
                            trigger1 = 0;
                            while (echo1==0) {};
                            sonar.start();
                            while (echo1==1) {};
                            sonar.stop();
                            distance = (sonar.read_us()-correction)/58.0;
                            command.printf("distancia: %d",distance);
                            if(distance<8)
                                {   
                                    lcd.cls();
                                    lcd.printf("Puesto 1\nOcupado");
                                    wait(1);
                                    //command.printf("sensores 2");
                                    wait(0.1);
                                    trigger2 = 1;
                                    sonar.reset();
                                    wait_us(10.0);
                                    trigger2 = 0;
                                    while (echo2==0) {};
                                    sonar.start();
                                    while (echo2==1) {};
                                    sonar.stop();
                                    distance = (sonar.read_us()-correction)/58.0;
                                    if(distance<8)
                                        {
                                            lcd.cls();
                                            lcd.printf("Puesto 2\nOcupado");
                                            wait(1);
                                            //command.printf("sensores 3");
                                            wait(0.1);
                                            trigger3 = 1;
                                            sonar.reset();
                                            wait_us(10.0);
                                            trigger3 = 0;
                                            while (echo3==0) {};
                                            sonar.start();
                                            while (echo3==1) {};
                                            sonar.stop();
                                            distance = (sonar.read_us()-correction)/58.0;
                                            if(distance<8)
                                                {
                                                    lcd.cls();
                                                    lcd.printf("Puesto 3\nOcupado");
                                                    wait(1);
                                                    wait(0.1);
                                                    trigger4 = 1;
                                                    sonar.reset();
                                                    wait_us(10.0);
                                                    trigger4 = 0;
                                                    while (echo4==0) {};
                                                    sonar.start();
                                                    while (echo4==1) {};
                                                    sonar.stop();
                                                    distance = (sonar.read_us()-correction)/58.0;
                                                    if(distance<8)
                                                        {
                                                                lcd.cls();
                                                                lcd.printf("Puesto 4\nOcupado");
                                                                wait(1);
                                                                //command.printf("sensores 5");
                                                                wait(0.1);
                                                                trigger5 = 1;
                                                                sonar.reset();
                                                                wait_us(10.0);
                                                                trigger5 = 0;
                                                                while (echo5==0) {};
                                                                sonar.start();
                                                                while (echo5==1) {};
                                                                sonar.stop();
                                                                distance = (sonar.read_us()-correction)/58.0;
                                                                if(distance<8)
                                                                    {
                                                                        lcd.cls();
                                                                        lcd.printf("Puesto 5\nOcupado");
                                                                        wait(1);
                                                                        trigger6 = 1;
                                                                        sonar.reset();
                                                                        wait_us(10.0);
                                                                        trigger6 = 0;
                                                                        while (echo6==0) {};
                                                                        sonar.start();
                                                                        while (echo6==1) {};
                                                                        sonar.stop();
                                                                        distance = (sonar.read_us()-correction)/58.0;
                                                                            if(distance<8)
                                                                            {
                                                                                lcd.cls();
                                                                                lcd.printf("Puesto 6\nOcupado");
                                                                                wait(1);
                                                                                lcd.cls();
                                                                                lcd.printf("Parqueadero \nLleno");
                                                                                wait(1);                                                                                
                                                                            }
                                                                            else
                                                                            {
                                                                                lcd.cls();
                                                                                lcd.printf("Puesto 6\nDisponible");
                                                                            }
                                                                    }
                                                                    else
                                                                    {
                                                                        lcd.cls();
                                                                        lcd.printf("Puesto 5\nDisponible");
                                                                    }
                                                        }
                                                        else
                                                        {
                                                              lcd.cls();
                                                              lcd.printf("Puesto 4\nDisponible");
                                                        }
                                                }
                                                else//3er puesto
                                                {
                                                    lcd.cls();
                                                    lcd.printf("Puesto 3\nDisponible");
                                                    wait(0.3);
                                                    diry=0;
                                                    ene= 0;
                                                    for(j=0;j<=5900;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                    //saca estiba
                                                    mder=1;
                                                    miz=0;
                                                    wait(1.5);
                                                    mder=0;
                                                    wait(0.1);
                                                    diry=0;
                                                    ene= 0;
                                                    for(j=0;j<=700;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                    mder=0;
                                                    miz=1;
                                                    wait(1.5);
                                                    miz=0;
                                                    wait(0.1);
                                                    diry=1;
                                                    ene= 0;
                                                    for(j=0;j<=6750;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                    wait(0.1);
                                                    myservo = 0.4;
                                                    wait(0.1);
                                                    myservo = 0.6;
                                                    wait(0.1);
                                                    myservo = 0.8;
                                                    wait(0.1);
                                                    myservo = 0.9;
                                                    lcd.cls();
                                                    lcd.printf("Ingrese\nPor Favor");
                                                    wait(3);
                                                    myservo = 0.7;
                                                    wait(0.1);
                                                    myservo = 0.5;
                                                    wait(0.1);
                                                    myservo = 0.3;
                                                    wait(0.1);
                                                    myservo = 0.22;
                                                    lcd.cls();
                                                    lcd.printf("Parqueando");
                                                    wait(0.3);
                                                    diry=0;
                                                    ene= 0;
                                                    for(j=0;j<=6800;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                    mder=1;
                                                    miz=0;
                                                    wait(1.5);
                                                    mder=0;
                                                    wait(0.1);
                                                    diry=1;
                                                    ene= 0;
                                                    for(j=0;j<=1200;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                    mder=0;
                                                    miz=1;
                                                    wait(1.5);
                                                    miz=0;
                                                    diry=1;
                                                    ene= 0;
                                                    for(j=0;j<=5550;j++)
                                                    {
                                                        stepsy=0;
                                                        wait_us(2);
                                                        stepsy=1;
                                                        wait_us(1800);
                                                    }
                                                }
                                        }
                                        else //2do puesto
                                        {
                                            lcd.cls();
                                            lcd.printf("Puesto 2\nDisponible");
                                            wait(0.3);
                                            diry=0;
                                            ene= 0;
                                            for(j=0;j<=2700;j++)//pasos que sube
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                            //saca estiba
                                            mder=1;
                                            miz=0;
                                            wait(1.5);
                                            mder=0;
                                            wait(0.1);
                                            diry=0;
                                            ene= 0;
                                            for(j=0;j<=700;j++)// pasos que suben
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                            mder=0;
                                            miz=1;
                                            wait(1.5);
                                            miz=0;
                                            wait(0.1);
                                            diry=1;
                                            ene= 0;
                                            for(j=0;j<=3400;j++)//pasos que baja
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                            wait(0.1);
                                            myservo = 0.4;
                                            wait(0.1);
                                            myservo = 0.6;
                                            wait(0.1);
                                            myservo = 0.8;
                                            wait(0.1);
                                            myservo = 0.9;
                                            lcd.cls();
                                            lcd.printf("Ingrese\nPor Favor");
                                            wait(3);
                                            myservo = 0.7;
                                            wait(0.1);
                                            myservo = 0.5;
                                            wait(0.1);
                                            myservo = 0.3;
                                            wait(0.1);
                                            myservo = 0.22;
                                            lcd.cls();
                                            lcd.printf("Parqueando");
                                            wait(0.3);
                                            diry=0;
                                            ene= 0;
                                            for(j=0;j<=3700;j++)//pasos que sube con carro
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                            mder=1;
                                            miz=0;
                                            wait(1.5);
                                            mder=0;
                                            wait(0.1);
                                            diry=1;
                                            ene= 0;
                                            for(j=0;j<=1200;j++)//pasos que baja carro
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                            mder=0;
                                            miz=1;
                                            wait(1.5);
                                            miz=0;
                                            diry=1;
                                            ene= 0;
                                            for(j=0;j<=25000;j++)//pasos que baja
                                            {
                                                stepsy=0;
                                                wait_us(2);
                                                stepsy=1;
                                                wait_us(1800);
                                            }
                                        }     
                                    }
                                else//1 puesto
                                {
                                        lcd.cls();
                                        lcd.printf("Puesto 1\nDisponible");
                                        wait(0.3);
                                        //saca estiba
                                        mder=1;
                                        miz=0;
                                        wait(1.5);
                                        mder=0;
                                        wait(0.1);
                                        diry=0;
                                        ene= 0;
                                        for(j=0;j<=700;j++)
                                        {
                                            stepsy=0;
                                            wait_us(2);
                                            stepsy=1;
                                            wait_us(1800);
                                        }
                                        mder=0;
                                        miz=1;
                                        wait(1.5);
                                        miz=0;
                                        wait(0.1);
                                        diry=1;
                                        ene= 0;
                                        for(j=0;j<=700;j++)
                                        {
                                            stepsy=0;
                                            wait_us(2);
                                            stepsy=1;
                                            wait_us(1800);
                                        }
                                        wait(0.1);
                                        myservo = 0.4;
                                        wait(0.1);
                                        myservo = 0.6;
                                        wait(0.1);
                                        myservo = 0.8;
                                        wait(0.1);
                                        myservo = 0.9;
                                        lcd.cls();
                                        lcd.printf("Ingrese\nPor Favor");
                                        wait(3);
                                        myservo = 0.7;
                                        wait(0.1);
                                        myservo = 0.5;
                                        wait(0.1);
                                        myservo = 0.3;
                                        wait(0.1);
                                        myservo = 0.22;
                                        lcd.cls();
                                        lcd.printf("Parqueando");
                                        wait(0.3);
                                        diry=0;
                                        ene= 0;
                                        for(j=0;j<=700;j++)
                                        {
                                            stepsy=0;
                                            wait_us(2);
                                            stepsy=1;
                                            wait_us(1800);
                                        }
                                        mder=1;
                                        miz=0;
                                        wait(1.5);
                                        mder=0;
                                        wait(0.1);
                                        diry=1;
                                        ene= 0;
                                        for(j=0;j<=600;j++)
                                        {
                                            stepsy=0;
                                            wait_us(2);
                                            stepsy=1;
                                            wait_us(1800);
                                        }
                                        mder=0;
                                        miz=1;
                                        wait(1.5);
                                        miz=0;
                                        diry=1;
                                        ene= 0;
                                        for(j=0;j<=100;j++)
                                        {
                                            stepsy=0;
                                            wait_us(2);
                                            stepsy=1;
                                            wait_us(1800);
                                        }
                                }
                        }
                        else
                        wait(0.5);
                        goto automatico;          
                } 
            else if(x=='m') //manual
                {
                command.printf("Entra a manual");
                lcd.cls();
                lcd.printf("Mando Manual!");
                inicio2:
                wait(0.2);
                if(command.readable()>0)
                    {
                        x=command.getc();
                        if(x=='1')
                        {
                            lcd.cls();
                            lcd.printf("Sube!\n");
                            wait_us(2);
                            diry=0;
                            ene= 0;
                            for(j=0;j<=1000;j++)
                            {
                                stepsy=0;
                                wait_us(2);
                                stepsy=1;
                                wait_us(1800);
                            }
                            goto inicio2;
                        }
                        else if(x=='2')
                        {
                            lcd.cls();
                            lcd.printf("Baja!\n");
                            wait_us(2);
                            diry=1;
                            ene= 0;
                            for(j=0;j<=1000;j++)
                            {
                                stepsy=0;
                                wait_us(2);
                                stepsy=1;
                                wait_us(1800);
                            }
                            goto inicio2;
                        }
                        else if(x=='3')
                            {
                                //mover motor derecha
                                lcd.cls();
                                command.printf("Gira derecha");
                                mder=1;
                                miz=0;
                                wait(0.5);
                                mder=0;
                                goto inicio2;
                            }
                        else if(x=='4')
                            {
                                //mover motor izquierda
                                lcd.cls();
                                command.printf("Gira izquierda");
                                mder=0;
                                miz=1;
                                wait(0.5);
                                miz=0;
                                goto inicio2;
                            }
                        else if(x=='5')
                            {
                                lcd.cls();
                                lcd.printf("Cerrado!\n");
                                //abre seguro
                                myservo = 0.22;   
                                goto inicio2; 
                            }
                        else if(x=='6')
                            {
                                //cierra seguro  
                                lcd.cls();
                                lcd.printf("Abierto!\n"); 
                                myservo = 0.9;
                                goto inicio2;
                            }
                            
                        else if(x=='7')//sensores
                            {
                                //command.printf("sensores 6");
                                wait(0.1);
                                lcd.cls();
                                lcd.printf("Censando!\n");
                                trigger6 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger6 = 0;
                                while (echo6==0) {};
                                sonar.start();
                                while (echo6==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                    if(distance<8)
                                    {
                                        command.printf("\nb");
                                        puestos++;
                                        //command.printf("rojo %d",distance);
                                    }
                                    else
                                    {
                                        command.printf("\nc");
                                        //command.printf("verde %d",distance);
                                    }
                                //command.printf("sensores 5");
                                wait(0.1);
                                trigger5 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger5 = 0;
                                while (echo5==0) {};
                                sonar.start();
                                while (echo5==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                if(distance<8)
                                    {
                                        command.printf("\nd");
                                        //command.printf("rojo");
                                        puestos++;
                                    }
                                    else
                                    {
                                        command.printf("\ne");
                                        //command.printf("verde");
                                    }
                                //command.printf("sensores 4");
                                wait(0.1);
                                trigger4 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger4 = 0;
                                while (echo4==0) {};
                                sonar.start();
                                while (echo4==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                if(distance<8)
                                    {
                                        puestos++;
                                        command.printf("\nf");
                                        //command.printf("rojo");
                                    }
                                    else
                                    {
                                        command.printf("\ng");
                                        //command.printf("verde");
                                    }
                                //command.printf("sensores 3");
                                wait(0.1);
                                trigger3 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger3 = 0;
                                while (echo3==0) {};
                                sonar.start();
                                while (echo3==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                if(distance<8)
                                    {
                                        puestos++;
                                        command.printf("\nh");
                                        //command.printf("rojo");
                                    }
                                    else
                                    {
                                        command.printf("\ni");
                                        //command.printf("verde");
                                    }
                                //command.printf("sensores 2");
                                wait(0.1);
                                trigger2 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger2 = 0;
                                while (echo2==0) {};
                                sonar.start();
                                while (echo2==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                if(distance<8)
                                    {
                                        puestos++;
                                        command.printf("\nj");
                                        //command.printf("rojo");
                                    }
                                    else
                                    {
                                        command.printf("\nk");
                                        //command.printf("verde");
                                    }
                                //command.printf("sensores 1");
                                wait(0.1);
                                trigger1 = 1;
                                sonar.reset();
                                wait_us(10.0);
                                trigger1 = 0;
                                while (echo1==0) {};
                                sonar.start();
                                while (echo1==1) {};
                                sonar.stop();
                                distance = (sonar.read_us()-correction)/58.0;
                                if(distance<8)
                                    {
                                        puestos++;
                                        command.printf("\nl");
                                        //command.printf("rojo");
                                    }
                                    else
                                    {
                                        command.printf("\nm");
                                        //command.printf("verde");
                                    }
                                lcd.cls();
                                puestos=6-puestos;
                                lcd.printf("Puestos libres:\n %d",puestos);
                                switch(puestos)
                                {
                                    case 1:
                                        command.printf("\nn");
                                        wait(0.1);
                                        break;
                                    case 2:
                                        command.printf("\no");
                                        wait(0.1);
                                        break;
                                    case 3:                                   
                                        command.printf("\np");
                                        wait(0.1);
                                        break;
                                    case 4:
                                        command.printf("\nq");
                                        wait(0.1);
                                        break;
                                    case 5:       
                                        command.printf("\nr");
                                        wait(0.1);
                                        break;
                                    case 6:
                                        command.printf("\ns");
                                        wait(0.1);
                                        break;
                                    default:
                                        break;
                                }
                                wait(0.1);
                                puestos=0;
                            }
                            goto inicio2;
                    }
                    else
                    goto inicio2;  
                }   
        }
        else
        goto inicio;
}