Andres Cano
/
STM32_read_analog
juego 2
main.cpp@1:17e8d7967b5a, 2018-11-16 (annotated)
- Committer:
- OnacSerdna
- Date:
- Fri Nov 16 12:15:24 2018 +0000
- Revision:
- 1:17e8d7967b5a
- Parent:
- 0:adbdc52e1846
- Child:
- 2:205af2fe4225
caarrrro;
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 | 1:17e8d7967b5a | 6 | Ticker cambio; |
OnacSerdna | 0:adbdc52e1846 | 7 | MPU6050 Wire(PB_9,PB_8); |
OnacSerdna | 0:adbdc52e1846 | 8 | InterruptIn iz(PA_14); |
OnacSerdna | 0:adbdc52e1846 | 9 | InterruptIn de(PA_13); |
OnacSerdna | 0:adbdc52e1846 | 10 | #define uint8_t unsigned char |
OnacSerdna | 1:17e8d7967b5a | 11 | float q[3]; |
OnacSerdna | 0:adbdc52e1846 | 12 | int pos; |
OnacSerdna | 0:adbdc52e1846 | 13 | float vel=200; |
OnacSerdna | 0:adbdc52e1846 | 14 | int b; |
OnacSerdna | 1:17e8d7967b5a | 15 | uint8_t cam[3]={0b01100000,0b00011000,0b00000110}; |
OnacSerdna | 0:adbdc52e1846 | 16 | uint8_t Matr[9]={0,0,0,0,0,0,0,0,0}; |
OnacSerdna | 0:adbdc52e1846 | 17 | uint8_t entorno={0b10000001}; |
OnacSerdna | 0:adbdc52e1846 | 18 | uint8_t carro={0b00011000}; |
OnacSerdna | 0:adbdc52e1846 | 19 | uint8_t obs=0b01100000; |
OnacSerdna | 0:adbdc52e1846 | 20 | void derecha(); |
OnacSerdna | 0:adbdc52e1846 | 21 | void izquierda(); |
OnacSerdna | 0:adbdc52e1846 | 22 | void colision(); |
OnacSerdna | 0:adbdc52e1846 | 23 | void juego(); |
OnacSerdna | 0:adbdc52e1846 | 24 | void accelerometro(); |
OnacSerdna | 1:17e8d7967b5a | 25 | void cambiof(); |
OnacSerdna | 0:adbdc52e1846 | 26 | int con; |
OnacSerdna | 1:17e8d7967b5a | 27 | int az; |
OnacSerdna | 0:adbdc52e1846 | 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 | 0:adbdc52e1846 | 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 | 0:adbdc52e1846 | 45 | |
OnacSerdna | 0:adbdc52e1846 | 46 | void inicializar(){ |
OnacSerdna | 0:adbdc52e1846 | 47 | |
OnacSerdna | 0:adbdc52e1846 | 48 | a.baud(9600); |
OnacSerdna | 0:adbdc52e1846 | 49 | sendSPI(0x0c,1); |
OnacSerdna | 0:adbdc52e1846 | 50 | sendSPI(0x09,0); |
OnacSerdna | 0:adbdc52e1846 | 51 | sendSPI(0x0A,0x01); |
OnacSerdna | 0:adbdc52e1846 | 52 | sendSPI(0x0F,1); |
OnacSerdna | 0:adbdc52e1846 | 53 | wait (0.5); |
OnacSerdna | 0:adbdc52e1846 | 54 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 55 | wait(0.5); |
OnacSerdna | 0:adbdc52e1846 | 56 | sendSPI(0xB,0x7); |
OnacSerdna | 0:adbdc52e1846 | 57 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 58 | vel=200; |
OnacSerdna | 0:adbdc52e1846 | 59 | carro=0b00011000; |
OnacSerdna | 0:adbdc52e1846 | 60 | } |
OnacSerdna | 0:adbdc52e1846 | 61 | |
OnacSerdna | 0:adbdc52e1846 | 62 | void imprimir() |
OnacSerdna | 0:adbdc52e1846 | 63 | { |
OnacSerdna | 0:adbdc52e1846 | 64 | |
OnacSerdna | 0:adbdc52e1846 | 65 | |
OnacSerdna | 0:adbdc52e1846 | 66 | int c_d; //carro, obstaculo y entorno |
OnacSerdna | 0:adbdc52e1846 | 67 | int c_e; //carro y entorno |
OnacSerdna | 0:adbdc52e1846 | 68 | int din; |
OnacSerdna | 0:adbdc52e1846 | 69 | for(int i=1;i<=7;i++){ |
OnacSerdna | 0:adbdc52e1846 | 70 | sendSPI(i,entorno); |
OnacSerdna | 0:adbdc52e1846 | 71 | } |
OnacSerdna | 0:adbdc52e1846 | 72 | for(int j=1;j<=10;j++){ |
OnacSerdna | 0:adbdc52e1846 | 73 | din=(obs>>b) | entorno ; |
OnacSerdna | 0:adbdc52e1846 | 74 | c_d= carro | din; |
OnacSerdna | 0:adbdc52e1846 | 75 | c_e=carro | entorno; |
OnacSerdna | 0:adbdc52e1846 | 76 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 77 | if(j<=7){ |
OnacSerdna | 0:adbdc52e1846 | 78 | sendSPI(j,din); |
OnacSerdna | 0:adbdc52e1846 | 79 | } |
OnacSerdna | 0:adbdc52e1846 | 80 | |
OnacSerdna | 0:adbdc52e1846 | 81 | if(j>1 && j<=8) |
OnacSerdna | 0:adbdc52e1846 | 82 | sendSPI(j-1,din); |
OnacSerdna | 0:adbdc52e1846 | 83 | |
OnacSerdna | 0:adbdc52e1846 | 84 | if(j>2 && j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 85 | sendSPI(j-2,entorno); |
OnacSerdna | 0:adbdc52e1846 | 86 | } |
OnacSerdna | 0:adbdc52e1846 | 87 | if(j>=8&&j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 88 | sendSPI(8,c_d); |
OnacSerdna | 0:adbdc52e1846 | 89 | } |
OnacSerdna | 0:adbdc52e1846 | 90 | if(j>10){ |
OnacSerdna | 0:adbdc52e1846 | 91 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 92 | } |
OnacSerdna | 0:adbdc52e1846 | 93 | |
OnacSerdna | 0:adbdc52e1846 | 94 | if( (obs>>b)==carro && j==8 ){ |
OnacSerdna | 0:adbdc52e1846 | 95 | colision(); |
OnacSerdna | 0:adbdc52e1846 | 96 | } |
OnacSerdna | 0:adbdc52e1846 | 97 | |
OnacSerdna | 0:adbdc52e1846 | 98 | wait_ms(vel); |
OnacSerdna | 0:adbdc52e1846 | 99 | con++; |
OnacSerdna | 0:adbdc52e1846 | 100 | if(con==10){ |
OnacSerdna | 0:adbdc52e1846 | 101 | vel=vel-10;} |
OnacSerdna | 0:adbdc52e1846 | 102 | } |
OnacSerdna | 0:adbdc52e1846 | 103 | |
OnacSerdna | 0:adbdc52e1846 | 104 | } |
OnacSerdna | 0:adbdc52e1846 | 105 | |
OnacSerdna | 0:adbdc52e1846 | 106 | int main() { |
OnacSerdna | 1:17e8d7967b5a | 107 | cambio.attach(&cambiof,0.2); |
OnacSerdna | 0:adbdc52e1846 | 108 | while(1){ |
OnacSerdna | 1:17e8d7967b5a | 109 | accelerometro(); |
OnacSerdna | 0:adbdc52e1846 | 110 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 111 | } |
OnacSerdna | 0:adbdc52e1846 | 112 | } |
OnacSerdna | 0:adbdc52e1846 | 113 | |
OnacSerdna | 1:17e8d7967b5a | 114 | |
OnacSerdna | 0:adbdc52e1846 | 115 | void juego(){ |
OnacSerdna | 0:adbdc52e1846 | 116 | inicializar(); |
OnacSerdna | 0:adbdc52e1846 | 117 | de.fall(&derecha); |
OnacSerdna | 0:adbdc52e1846 | 118 | iz.fall(&izquierda); |
OnacSerdna | 0:adbdc52e1846 | 119 | a.printf("inicio\n"); |
OnacSerdna | 0:adbdc52e1846 | 120 | while(1){ |
OnacSerdna | 0:adbdc52e1846 | 121 | con=0; |
OnacSerdna | 0:adbdc52e1846 | 122 | b=2*(rand()%3); |
OnacSerdna | 0:adbdc52e1846 | 123 | a.printf("numero %d\n",b); |
OnacSerdna | 0:adbdc52e1846 | 124 | imprimir(); |
OnacSerdna | 0:adbdc52e1846 | 125 | } |
OnacSerdna | 0:adbdc52e1846 | 126 | } |
OnacSerdna | 0:adbdc52e1846 | 127 | |
OnacSerdna | 0:adbdc52e1846 | 128 | void derecha() |
OnacSerdna | 0:adbdc52e1846 | 129 | { |
OnacSerdna | 0:adbdc52e1846 | 130 | if(carro==0b00011000 || carro==0b01100000) |
OnacSerdna | 0:adbdc52e1846 | 131 | carro=carro>>2; |
OnacSerdna | 0:adbdc52e1846 | 132 | a.printf("entra der \n"); |
OnacSerdna | 0:adbdc52e1846 | 133 | return; |
OnacSerdna | 0:adbdc52e1846 | 134 | } |
OnacSerdna | 0:adbdc52e1846 | 135 | void izquierda() |
OnacSerdna | 0:adbdc52e1846 | 136 | { |
OnacSerdna | 0:adbdc52e1846 | 137 | if( carro==0b00000110 || carro==0b00011000 ) |
OnacSerdna | 0:adbdc52e1846 | 138 | carro=carro<<2; |
OnacSerdna | 0:adbdc52e1846 | 139 | a.printf("entra izq \n"); |
OnacSerdna | 0:adbdc52e1846 | 140 | return; |
OnacSerdna | 0:adbdc52e1846 | 141 | } |
OnacSerdna | 0:adbdc52e1846 | 142 | |
OnacSerdna | 0:adbdc52e1846 | 143 | void colision () |
OnacSerdna | 0:adbdc52e1846 | 144 | { |
OnacSerdna | 0:adbdc52e1846 | 145 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 146 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 147 | sendSPI(3,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 148 | sendSPI(4,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 149 | sendSPI(5,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 150 | sendSPI(6,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 151 | wait(2); |
OnacSerdna | 0:adbdc52e1846 | 152 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 153 | } |
OnacSerdna | 0:adbdc52e1846 | 154 | |
OnacSerdna | 0:adbdc52e1846 | 155 | void accelerometro(){ |
OnacSerdna | 1:17e8d7967b5a | 156 | |
OnacSerdna | 1:17e8d7967b5a | 157 | Wire.getAccelero(q); |
OnacSerdna | 1:17e8d7967b5a | 158 | a.printf("accelero: \t x= %f,\t y= %f, \t z= %f \n",q[0],q[1],q[2]); |
OnacSerdna | 1:17e8d7967b5a | 159 | int accelero[3]; |
OnacSerdna | 1:17e8d7967b5a | 160 | //Wire.getAcceleroRaw(accelero); |
OnacSerdna | 0:adbdc52e1846 | 161 | int valor=accelero[1]; |
OnacSerdna | 0:adbdc52e1846 | 162 | if(valor<=(2000) && valor>=(1000)){ |
OnacSerdna | 1:17e8d7967b5a | 163 | az=1;} |
OnacSerdna | 0:adbdc52e1846 | 164 | if(valor<(1000) && valor>=(-1000)){ |
OnacSerdna | 1:17e8d7967b5a | 165 | az=2;} |
OnacSerdna | 0:adbdc52e1846 | 166 | if(valor<(-1000) && valor>=(-2000)){ |
OnacSerdna | 1:17e8d7967b5a | 167 | az=3;} |
OnacSerdna | 1:17e8d7967b5a | 168 | a.printf("holi : \t %d \n",az); |
OnacSerdna | 0:adbdc52e1846 | 169 | wait(0.8); |
OnacSerdna | 0:adbdc52e1846 | 170 | |
OnacSerdna | 0:adbdc52e1846 | 171 | } |
OnacSerdna | 1:17e8d7967b5a | 172 | |
OnacSerdna | 1:17e8d7967b5a | 173 | void cambiof(){ |
OnacSerdna | 1:17e8d7967b5a | 174 | |
OnacSerdna | 1:17e8d7967b5a | 175 | switch (az){ |
OnacSerdna | 1:17e8d7967b5a | 176 | case 1: carro=cam[1]; |
OnacSerdna | 1:17e8d7967b5a | 177 | break; |
OnacSerdna | 1:17e8d7967b5a | 178 | case 2: carro=cam[2]; |
OnacSerdna | 1:17e8d7967b5a | 179 | break; |
OnacSerdna | 1:17e8d7967b5a | 180 | case 3: carro=cam[3]; |
OnacSerdna | 1:17e8d7967b5a | 181 | break; |
OnacSerdna | 1:17e8d7967b5a | 182 | } |
OnacSerdna | 1:17e8d7967b5a | 183 | |
OnacSerdna | 1:17e8d7967b5a | 184 | |
OnacSerdna | 1:17e8d7967b5a | 185 | } |