Andres Cano
/
STM32_read_analog
juego 2
main.cpp@2:205af2fe4225, 2018-11-17 (annotated)
- Committer:
- OnacSerdna
- Date:
- Sat Nov 17 21:36:40 2018 +0000
- Revision:
- 2:205af2fe4225
- Parent:
- 1:17e8d7967b5a
- Child:
- 3:e936c097685c
v3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OnacSerdna | 0:adbdc52e1846 | 1 | #include "mbed.h" |
OnacSerdna | 0:adbdc52e1846 | 2 | #include "MPU6050.h" |
OnacSerdna | 0:adbdc52e1846 | 3 | SPI deviceM(PB_15, PB_14, PB_13); |
OnacSerdna | 0:adbdc52e1846 | 4 | DigitalOut ssel (PB_12); |
OnacSerdna | 0:adbdc52e1846 | 5 | Serial a(USBTX,USBRX); |
OnacSerdna | 0:adbdc52e1846 | 6 | MPU6050 Wire(PB_9,PB_8); |
OnacSerdna | 0:adbdc52e1846 | 7 | InterruptIn iz(PA_14); |
OnacSerdna | 0:adbdc52e1846 | 8 | InterruptIn de(PA_13); |
OnacSerdna | 0:adbdc52e1846 | 9 | #define uint8_t unsigned char |
OnacSerdna | 1:17e8d7967b5a | 10 | float q[3]; |
OnacSerdna | 0:adbdc52e1846 | 11 | int pos; |
OnacSerdna | 0:adbdc52e1846 | 12 | float vel=200; |
OnacSerdna | 0:adbdc52e1846 | 13 | int b; |
OnacSerdna | 1:17e8d7967b5a | 14 | uint8_t cam[3]={0b01100000,0b00011000,0b00000110}; |
OnacSerdna | 0:adbdc52e1846 | 15 | uint8_t Matr[9]={0,0,0,0,0,0,0,0,0}; |
OnacSerdna | 0:adbdc52e1846 | 16 | uint8_t entorno={0b10000001}; |
OnacSerdna | 0:adbdc52e1846 | 17 | uint8_t carro={0b00011000}; |
OnacSerdna | 0:adbdc52e1846 | 18 | uint8_t obs=0b01100000; |
OnacSerdna | 0:adbdc52e1846 | 19 | void derecha(); |
OnacSerdna | 0:adbdc52e1846 | 20 | void izquierda(); |
OnacSerdna | 0:adbdc52e1846 | 21 | void colision(); |
OnacSerdna | 0:adbdc52e1846 | 22 | void juego(); |
OnacSerdna | 0:adbdc52e1846 | 23 | void accelerometro(); |
OnacSerdna | 2:205af2fe4225 | 24 | void cambio(); |
OnacSerdna | 2:205af2fe4225 | 25 | void vell(); |
OnacSerdna | 2:205af2fe4225 | 26 | int con=0; |
OnacSerdna | 1:17e8d7967b5a | 27 | int az; |
OnacSerdna | 2:205af2fe4225 | 28 | //////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 29 | void sendSPI(uint8_t d1, uint8_t d2) |
OnacSerdna | 0:adbdc52e1846 | 30 | { |
OnacSerdna | 0:adbdc52e1846 | 31 | deviceM.unlock(); |
OnacSerdna | 0:adbdc52e1846 | 32 | ssel=0; |
OnacSerdna | 0:adbdc52e1846 | 33 | deviceM.write(d1); |
OnacSerdna | 0:adbdc52e1846 | 34 | deviceM.write(d2); |
OnacSerdna | 0:adbdc52e1846 | 35 | ssel=1; |
OnacSerdna | 0:adbdc52e1846 | 36 | deviceM.lock(); |
OnacSerdna | 0:adbdc52e1846 | 37 | } |
OnacSerdna | 2:205af2fe4225 | 38 | //////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 39 | void borrar() |
OnacSerdna | 0:adbdc52e1846 | 40 | { |
OnacSerdna | 0:adbdc52e1846 | 41 | for(int i;i<=8;i++) { |
OnacSerdna | 0:adbdc52e1846 | 42 | sendSPI(i,0); |
OnacSerdna | 0:adbdc52e1846 | 43 | } |
OnacSerdna | 0:adbdc52e1846 | 44 | } |
OnacSerdna | 2:205af2fe4225 | 45 | /////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 46 | void inicializar(){ |
OnacSerdna | 0:adbdc52e1846 | 47 | a.baud(9600); |
OnacSerdna | 0:adbdc52e1846 | 48 | sendSPI(0x0c,1); |
OnacSerdna | 0:adbdc52e1846 | 49 | sendSPI(0x09,0); |
OnacSerdna | 0:adbdc52e1846 | 50 | sendSPI(0x0A,0x01); |
OnacSerdna | 0:adbdc52e1846 | 51 | sendSPI(0x0F,1); |
OnacSerdna | 0:adbdc52e1846 | 52 | wait (0.5); |
OnacSerdna | 0:adbdc52e1846 | 53 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 54 | wait(0.5); |
OnacSerdna | 0:adbdc52e1846 | 55 | sendSPI(0xB,0x7); |
OnacSerdna | 0:adbdc52e1846 | 56 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 57 | vel=200; |
OnacSerdna | 0:adbdc52e1846 | 58 | carro=0b00011000; |
OnacSerdna | 0:adbdc52e1846 | 59 | } |
OnacSerdna | 2:205af2fe4225 | 60 | ////////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 61 | void imprimir() |
OnacSerdna | 0:adbdc52e1846 | 62 | { |
OnacSerdna | 0:adbdc52e1846 | 63 | int c_d; //carro, obstaculo y entorno |
OnacSerdna | 0:adbdc52e1846 | 64 | int c_e; //carro y entorno |
OnacSerdna | 0:adbdc52e1846 | 65 | int din; |
OnacSerdna | 0:adbdc52e1846 | 66 | for(int i=1;i<=7;i++){ |
OnacSerdna | 0:adbdc52e1846 | 67 | sendSPI(i,entorno); |
OnacSerdna | 0:adbdc52e1846 | 68 | } |
OnacSerdna | 0:adbdc52e1846 | 69 | for(int j=1;j<=10;j++){ |
OnacSerdna | 0:adbdc52e1846 | 70 | din=(obs>>b) | entorno ; |
OnacSerdna | 0:adbdc52e1846 | 71 | c_d= carro | din; |
OnacSerdna | 0:adbdc52e1846 | 72 | c_e=carro | entorno; |
OnacSerdna | 0:adbdc52e1846 | 73 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 74 | if(j<=7){ |
OnacSerdna | 0:adbdc52e1846 | 75 | sendSPI(j,din); |
OnacSerdna | 0:adbdc52e1846 | 76 | } |
OnacSerdna | 0:adbdc52e1846 | 77 | |
OnacSerdna | 0:adbdc52e1846 | 78 | if(j>1 && j<=8) |
OnacSerdna | 0:adbdc52e1846 | 79 | sendSPI(j-1,din); |
OnacSerdna | 0:adbdc52e1846 | 80 | |
OnacSerdna | 0:adbdc52e1846 | 81 | if(j>2 && j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 82 | sendSPI(j-2,entorno); |
OnacSerdna | 0:adbdc52e1846 | 83 | } |
OnacSerdna | 0:adbdc52e1846 | 84 | if(j>=8&&j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 85 | sendSPI(8,c_d); |
OnacSerdna | 0:adbdc52e1846 | 86 | } |
OnacSerdna | 0:adbdc52e1846 | 87 | if(j>10){ |
OnacSerdna | 0:adbdc52e1846 | 88 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 89 | } |
OnacSerdna | 0:adbdc52e1846 | 90 | |
OnacSerdna | 0:adbdc52e1846 | 91 | if( (obs>>b)==carro && j==8 ){ |
OnacSerdna | 0:adbdc52e1846 | 92 | colision(); |
OnacSerdna | 0:adbdc52e1846 | 93 | } |
OnacSerdna | 2:205af2fe4225 | 94 | accelerometro(); |
OnacSerdna | 2:205af2fe4225 | 95 | cambio(); |
OnacSerdna | 0:adbdc52e1846 | 96 | wait_ms(vel); |
OnacSerdna | 0:adbdc52e1846 | 97 | } |
OnacSerdna | 0:adbdc52e1846 | 98 | } |
OnacSerdna | 2:205af2fe4225 | 99 | ////////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 100 | int main() { |
OnacSerdna | 0:adbdc52e1846 | 101 | while(1){ |
OnacSerdna | 0:adbdc52e1846 | 102 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 103 | } |
OnacSerdna | 0:adbdc52e1846 | 104 | } |
OnacSerdna | 2:205af2fe4225 | 105 | /////////////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 106 | void juego(){ |
OnacSerdna | 0:adbdc52e1846 | 107 | inicializar(); |
OnacSerdna | 2:205af2fe4225 | 108 | //de.rise(&derecha); |
OnacSerdna | 2:205af2fe4225 | 109 | //iz.rise(&izquierda); |
OnacSerdna | 0:adbdc52e1846 | 110 | a.printf("inicio\n"); |
OnacSerdna | 0:adbdc52e1846 | 111 | while(1){ |
OnacSerdna | 0:adbdc52e1846 | 112 | b=2*(rand()%3); |
OnacSerdna | 2:205af2fe4225 | 113 | a.printf("umero %d\n",az); |
OnacSerdna | 2:205af2fe4225 | 114 | imprimir(); |
OnacSerdna | 2:205af2fe4225 | 115 | vell(); |
OnacSerdna | 0:adbdc52e1846 | 116 | } |
OnacSerdna | 0:adbdc52e1846 | 117 | } |
OnacSerdna | 2:205af2fe4225 | 118 | /////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 119 | void derecha() |
OnacSerdna | 0:adbdc52e1846 | 120 | { |
OnacSerdna | 0:adbdc52e1846 | 121 | if(carro==0b00011000 || carro==0b01100000) |
OnacSerdna | 0:adbdc52e1846 | 122 | carro=carro>>2; |
OnacSerdna | 0:adbdc52e1846 | 123 | a.printf("entra der \n"); |
OnacSerdna | 0:adbdc52e1846 | 124 | return; |
OnacSerdna | 0:adbdc52e1846 | 125 | } |
OnacSerdna | 2:205af2fe4225 | 126 | /////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 127 | void izquierda() |
OnacSerdna | 0:adbdc52e1846 | 128 | { |
OnacSerdna | 0:adbdc52e1846 | 129 | if( carro==0b00000110 || carro==0b00011000 ) |
OnacSerdna | 0:adbdc52e1846 | 130 | carro=carro<<2; |
OnacSerdna | 0:adbdc52e1846 | 131 | a.printf("entra izq \n"); |
OnacSerdna | 0:adbdc52e1846 | 132 | return; |
OnacSerdna | 0:adbdc52e1846 | 133 | } |
OnacSerdna | 2:205af2fe4225 | 134 | //////////////////////////////////// |
OnacSerdna | 2:205af2fe4225 | 135 | void cambio(){ |
OnacSerdna | 2:205af2fe4225 | 136 | switch(az){ |
OnacSerdna | 2:205af2fe4225 | 137 | case 1: carro = cam[0];break; |
OnacSerdna | 2:205af2fe4225 | 138 | case 2: carro = cam[1];break; |
OnacSerdna | 2:205af2fe4225 | 139 | case 3: carro = cam[2];break; |
OnacSerdna | 2:205af2fe4225 | 140 | default: break;} |
OnacSerdna | 2:205af2fe4225 | 141 | |
OnacSerdna | 2:205af2fe4225 | 142 | } |
OnacSerdna | 2:205af2fe4225 | 143 | //////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 144 | void colision () |
OnacSerdna | 0:adbdc52e1846 | 145 | { |
OnacSerdna | 0:adbdc52e1846 | 146 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 147 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 148 | sendSPI(3,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 149 | sendSPI(4,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 150 | sendSPI(5,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 151 | sendSPI(6,0b01111110); |
OnacSerdna | 2:205af2fe4225 | 152 | con=0; |
OnacSerdna | 2:205af2fe4225 | 153 | vel=200; |
OnacSerdna | 0:adbdc52e1846 | 154 | wait(2); |
OnacSerdna | 0:adbdc52e1846 | 155 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 156 | } |
OnacSerdna | 2:205af2fe4225 | 157 | ///////////////////////////////////// |
OnacSerdna | 0:adbdc52e1846 | 158 | void accelerometro(){ |
OnacSerdna | 1:17e8d7967b5a | 159 | Wire.getAccelero(q); |
OnacSerdna | 1:17e8d7967b5a | 160 | a.printf("accelero: \t x= %f,\t y= %f, \t z= %f \n",q[0],q[1],q[2]); |
OnacSerdna | 2:205af2fe4225 | 161 | int valor=q[1]; |
OnacSerdna | 2:205af2fe4225 | 162 | if(valor<=(-4) && valor>=(-10)){ |
OnacSerdna | 1:17e8d7967b5a | 163 | az=1;} |
OnacSerdna | 2:205af2fe4225 | 164 | if(valor<(4) && valor>=(-4)){ |
OnacSerdna | 1:17e8d7967b5a | 165 | az=2;} |
OnacSerdna | 2:205af2fe4225 | 166 | if(valor<(10) && valor>=(4)){ |
OnacSerdna | 1:17e8d7967b5a | 167 | az=3;} |
OnacSerdna | 2:205af2fe4225 | 168 | a.printf("az : \t %d \n",az); |
OnacSerdna | 0:adbdc52e1846 | 169 | } |
OnacSerdna | 2:205af2fe4225 | 170 | ///////////////////////////////////// |
OnacSerdna | 2:205af2fe4225 | 171 | void vell(){ |
OnacSerdna | 2:205af2fe4225 | 172 | con++; |
OnacSerdna | 2:205af2fe4225 | 173 | if(con>2 && vel>=20) |
OnacSerdna | 2:205af2fe4225 | 174 | { |
OnacSerdna | 2:205af2fe4225 | 175 | vel=vel-(vel*0.2); |
OnacSerdna | 2:205af2fe4225 | 176 | } |
OnacSerdna | 1:17e8d7967b5a | 177 | } |