Maquina de estados

Dependencies:   mbed

Committer:
javiervicente
Date:
Thu Dec 09 17:59:02 2021 +0000
Revision:
0:8a1a447db446
cambio;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
javiervicente 0:8a1a447db446 1 #include "mbed.h"
javiervicente 0:8a1a447db446 2 #include "hcsr04.h"
javiervicente 0:8a1a447db446 3
javiervicente 0:8a1a447db446 4 Serial pc(USBTX, USBRX); // tx, rx
javiervicente 0:8a1a447db446 5 Ticker tickerMideDistancia;
javiervicente 0:8a1a447db446 6 unsigned distancia=1000;;
javiervicente 0:8a1a447db446 7
javiervicente 0:8a1a447db446 8
javiervicente 0:8a1a447db446 9 DigitalIn final1(D10);
javiervicente 0:8a1a447db446 10 DigitalIn final2(D9);
javiervicente 0:8a1a447db446 11 DigitalOut step(D13);
javiervicente 0:8a1a447db446 12 DigitalOut dir(D12);
javiervicente 0:8a1a447db446 13 DigitalOut enable(D11);
javiervicente 0:8a1a447db446 14
javiervicente 0:8a1a447db446 15 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 16 estados estado;
javiervicente 0:8a1a447db446 17
javiervicente 0:8a1a447db446 18 HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin):
javiervicente 0:8a1a447db446 19
javiervicente 0:8a1a447db446 20 void mideDistancia()
javiervicente 0:8a1a447db446 21 {
javiervicente 0:8a1a447db446 22 usensor.start();
javiervicente 0:8a1a447db446 23 }
javiervicente 0:8a1a447db446 24
javiervicente 0:8a1a447db446 25 void paso(int d)
javiervicente 0:8a1a447db446 26 {
javiervicente 0:8a1a447db446 27 dir=d;
javiervicente 0:8a1a447db446 28 step=1;
javiervicente 0:8a1a447db446 29 wait_us(100);
javiervicente 0:8a1a447db446 30 step=0;
javiervicente 0:8a1a447db446 31 wait_us(900);
javiervicente 0:8a1a447db446 32 }
javiervicente 0:8a1a447db446 33
javiervicente 0:8a1a447db446 34 void estadoCerrada()
javiervicente 0:8a1a447db446 35 {
javiervicente 0:8a1a447db446 36 if(distancia<10) {
javiervicente 0:8a1a447db446 37 enable=1;
javiervicente 0:8a1a447db446 38 estado=abriendose;
javiervicente 0:8a1a447db446 39 pc.printf("Estado Abriendose, distancia %d\n",distancia);
javiervicente 0:8a1a447db446 40 }
javiervicente 0:8a1a447db446 41 }
javiervicente 0:8a1a447db446 42
javiervicente 0:8a1a447db446 43 void estadoAbriendose()
javiervicente 0:8a1a447db446 44 {
javiervicente 0:8a1a447db446 45 if((final1==1)&&(distancia<10)) {
javiervicente 0:8a1a447db446 46 estado=abierta;
javiervicente 0:8a1a447db446 47 enable=0;
javiervicente 0:8a1a447db446 48 } else if((final1==1)&&(distancia>=10)) {
javiervicente 0:8a1a447db446 49 enable=1;
javiervicente 0:8a1a447db446 50 estado=cerrandose;
javiervicente 0:8a1a447db446 51 } else {
javiervicente 0:8a1a447db446 52 paso(1);
javiervicente 0:8a1a447db446 53 pc.printf("paso\n");
javiervicente 0:8a1a447db446 54
javiervicente 0:8a1a447db446 55 }
javiervicente 0:8a1a447db446 56 }
javiervicente 0:8a1a447db446 57
javiervicente 0:8a1a447db446 58 void estadoCerrandose(){
javiervicente 0:8a1a447db446 59 if(distancia<10) {
javiervicente 0:8a1a447db446 60 estado=abriendose;
javiervicente 0:8a1a447db446 61 enable=1;
javiervicente 0:8a1a447db446 62 } else if(final2==1) {
javiervicente 0:8a1a447db446 63 estado=cerrada;
javiervicente 0:8a1a447db446 64 enable=0;
javiervicente 0:8a1a447db446 65 } else {
javiervicente 0:8a1a447db446 66 paso(0);
javiervicente 0:8a1a447db446 67 pc.printf("paso\n");
javiervicente 0:8a1a447db446 68 }
javiervicente 0:8a1a447db446 69 }
javiervicente 0:8a1a447db446 70
javiervicente 0:8a1a447db446 71 void estadoAbierta()
javiervicente 0:8a1a447db446 72 {
javiervicente 0:8a1a447db446 73 if (distancia>=10) {
javiervicente 0:8a1a447db446 74 enable=1;
javiervicente 0:8a1a447db446 75 estado=cerrandose;
javiervicente 0:8a1a447db446 76 }
javiervicente 0:8a1a447db446 77 }
javiervicente 0:8a1a447db446 78
javiervicente 0:8a1a447db446 79 int main()
javiervicente 0:8a1a447db446 80 {
javiervicente 0:8a1a447db446 81 pc.baud(115200);
javiervicente 0:8a1a447db446 82 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 83 estado=cerrada;
javiervicente 0:8a1a447db446 84 pc.printf("Estado cerrada\n");
javiervicente 0:8a1a447db446 85 while(1) {
javiervicente 0:8a1a447db446 86 distancia=usensor.get_dist_cm();
javiervicente 0:8a1a447db446 87 switch ( estado ) {
javiervicente 0:8a1a447db446 88 case cerrada:
javiervicente 0:8a1a447db446 89 estadoCerrada();
javiervicente 0:8a1a447db446 90 break;
javiervicente 0:8a1a447db446 91 case abriendose:
javiervicente 0:8a1a447db446 92 estadoAbriendose();
javiervicente 0:8a1a447db446 93 break;
javiervicente 0:8a1a447db446 94 case abierta:
javiervicente 0:8a1a447db446 95 estadoAbierta();
javiervicente 0:8a1a447db446 96 break;
javiervicente 0:8a1a447db446 97 case cerrandose:
javiervicente 0:8a1a447db446 98 estadoCerrandose();
javiervicente 0:8a1a447db446 99 break;
javiervicente 0:8a1a447db446 100 default:
javiervicente 0:8a1a447db446 101 break;
javiervicente 0:8a1a447db446 102 }
javiervicente 0:8a1a447db446 103 }
javiervicente 0:8a1a447db446 104 }