Andres Cano
/
STM32_read_analog
juego 2
main.cpp@0:adbdc52e1846, 2018-11-16 (annotated)
- Committer:
- OnacSerdna
- Date:
- Fri Nov 16 04:25:15 2018 +0000
- Revision:
- 0:adbdc52e1846
- Child:
- 1:17e8d7967b5a
Codigo de juego
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 | 0:adbdc52e1846 | 10 | int pos; |
OnacSerdna | 0:adbdc52e1846 | 11 | float vel=200; |
OnacSerdna | 0:adbdc52e1846 | 12 | int b; |
OnacSerdna | 0:adbdc52e1846 | 13 | uint8_t Matr[9]={0,0,0,0,0,0,0,0,0}; |
OnacSerdna | 0:adbdc52e1846 | 14 | uint8_t entorno={0b10000001}; |
OnacSerdna | 0:adbdc52e1846 | 15 | uint8_t carro={0b00011000}; |
OnacSerdna | 0:adbdc52e1846 | 16 | uint8_t obs=0b01100000; |
OnacSerdna | 0:adbdc52e1846 | 17 | void derecha(); |
OnacSerdna | 0:adbdc52e1846 | 18 | void izquierda(); |
OnacSerdna | 0:adbdc52e1846 | 19 | void colision(); |
OnacSerdna | 0:adbdc52e1846 | 20 | void juego(); |
OnacSerdna | 0:adbdc52e1846 | 21 | void accelerometro(); |
OnacSerdna | 0:adbdc52e1846 | 22 | int con; |
OnacSerdna | 0:adbdc52e1846 | 23 | |
OnacSerdna | 0:adbdc52e1846 | 24 | void sendSPI(uint8_t d1, uint8_t d2) |
OnacSerdna | 0:adbdc52e1846 | 25 | { |
OnacSerdna | 0:adbdc52e1846 | 26 | deviceM.unlock(); |
OnacSerdna | 0:adbdc52e1846 | 27 | ssel=0; |
OnacSerdna | 0:adbdc52e1846 | 28 | deviceM.write(d1); |
OnacSerdna | 0:adbdc52e1846 | 29 | deviceM.write(d2); |
OnacSerdna | 0:adbdc52e1846 | 30 | ssel=1; |
OnacSerdna | 0:adbdc52e1846 | 31 | deviceM.lock(); |
OnacSerdna | 0:adbdc52e1846 | 32 | } |
OnacSerdna | 0:adbdc52e1846 | 33 | |
OnacSerdna | 0:adbdc52e1846 | 34 | void borrar() |
OnacSerdna | 0:adbdc52e1846 | 35 | { |
OnacSerdna | 0:adbdc52e1846 | 36 | for(int i;i<=8;i++) { |
OnacSerdna | 0:adbdc52e1846 | 37 | sendSPI(i,0); |
OnacSerdna | 0:adbdc52e1846 | 38 | } |
OnacSerdna | 0:adbdc52e1846 | 39 | } |
OnacSerdna | 0:adbdc52e1846 | 40 | |
OnacSerdna | 0:adbdc52e1846 | 41 | void inicializar(){ |
OnacSerdna | 0:adbdc52e1846 | 42 | |
OnacSerdna | 0:adbdc52e1846 | 43 | a.baud(9600); |
OnacSerdna | 0:adbdc52e1846 | 44 | sendSPI(0x0c,1); |
OnacSerdna | 0:adbdc52e1846 | 45 | sendSPI(0x09,0); |
OnacSerdna | 0:adbdc52e1846 | 46 | sendSPI(0x0A,0x01); |
OnacSerdna | 0:adbdc52e1846 | 47 | sendSPI(0x0F,1); |
OnacSerdna | 0:adbdc52e1846 | 48 | wait (0.5); |
OnacSerdna | 0:adbdc52e1846 | 49 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 50 | wait(0.5); |
OnacSerdna | 0:adbdc52e1846 | 51 | sendSPI(0xB,0x7); |
OnacSerdna | 0:adbdc52e1846 | 52 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 53 | vel=200; |
OnacSerdna | 0:adbdc52e1846 | 54 | carro=0b00011000; |
OnacSerdna | 0:adbdc52e1846 | 55 | } |
OnacSerdna | 0:adbdc52e1846 | 56 | |
OnacSerdna | 0:adbdc52e1846 | 57 | void imprimir() |
OnacSerdna | 0:adbdc52e1846 | 58 | { |
OnacSerdna | 0:adbdc52e1846 | 59 | |
OnacSerdna | 0:adbdc52e1846 | 60 | |
OnacSerdna | 0:adbdc52e1846 | 61 | int c_d; //carro, obstaculo y entorno |
OnacSerdna | 0:adbdc52e1846 | 62 | int c_e; //carro y entorno |
OnacSerdna | 0:adbdc52e1846 | 63 | int din; |
OnacSerdna | 0:adbdc52e1846 | 64 | for(int i=1;i<=7;i++){ |
OnacSerdna | 0:adbdc52e1846 | 65 | sendSPI(i,entorno); |
OnacSerdna | 0:adbdc52e1846 | 66 | } |
OnacSerdna | 0:adbdc52e1846 | 67 | for(int j=1;j<=10;j++){ |
OnacSerdna | 0:adbdc52e1846 | 68 | din=(obs>>b) | entorno ; |
OnacSerdna | 0:adbdc52e1846 | 69 | c_d= carro | din; |
OnacSerdna | 0:adbdc52e1846 | 70 | c_e=carro | entorno; |
OnacSerdna | 0:adbdc52e1846 | 71 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 72 | if(j<=7){ |
OnacSerdna | 0:adbdc52e1846 | 73 | sendSPI(j,din); |
OnacSerdna | 0:adbdc52e1846 | 74 | } |
OnacSerdna | 0:adbdc52e1846 | 75 | |
OnacSerdna | 0:adbdc52e1846 | 76 | if(j>1 && j<=8) |
OnacSerdna | 0:adbdc52e1846 | 77 | sendSPI(j-1,din); |
OnacSerdna | 0:adbdc52e1846 | 78 | |
OnacSerdna | 0:adbdc52e1846 | 79 | if(j>2 && j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 80 | sendSPI(j-2,entorno); |
OnacSerdna | 0:adbdc52e1846 | 81 | } |
OnacSerdna | 0:adbdc52e1846 | 82 | if(j>=8&&j<=9){ |
OnacSerdna | 0:adbdc52e1846 | 83 | sendSPI(8,c_d); |
OnacSerdna | 0:adbdc52e1846 | 84 | } |
OnacSerdna | 0:adbdc52e1846 | 85 | if(j>10){ |
OnacSerdna | 0:adbdc52e1846 | 86 | sendSPI(8,c_e); |
OnacSerdna | 0:adbdc52e1846 | 87 | } |
OnacSerdna | 0:adbdc52e1846 | 88 | |
OnacSerdna | 0:adbdc52e1846 | 89 | if( (obs>>b)==carro && j==8 ){ |
OnacSerdna | 0:adbdc52e1846 | 90 | colision(); |
OnacSerdna | 0:adbdc52e1846 | 91 | } |
OnacSerdna | 0:adbdc52e1846 | 92 | |
OnacSerdna | 0:adbdc52e1846 | 93 | wait_ms(vel); |
OnacSerdna | 0:adbdc52e1846 | 94 | con++; |
OnacSerdna | 0:adbdc52e1846 | 95 | if(con==10){ |
OnacSerdna | 0:adbdc52e1846 | 96 | vel=vel-10;} |
OnacSerdna | 0:adbdc52e1846 | 97 | } |
OnacSerdna | 0:adbdc52e1846 | 98 | |
OnacSerdna | 0:adbdc52e1846 | 99 | } |
OnacSerdna | 0:adbdc52e1846 | 100 | |
OnacSerdna | 0:adbdc52e1846 | 101 | int main() { |
OnacSerdna | 0:adbdc52e1846 | 102 | while(1){ |
OnacSerdna | 0:adbdc52e1846 | 103 | //accelerometro(); |
OnacSerdna | 0:adbdc52e1846 | 104 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 105 | } |
OnacSerdna | 0:adbdc52e1846 | 106 | } |
OnacSerdna | 0:adbdc52e1846 | 107 | |
OnacSerdna | 0:adbdc52e1846 | 108 | void juego(){ |
OnacSerdna | 0:adbdc52e1846 | 109 | inicializar(); |
OnacSerdna | 0:adbdc52e1846 | 110 | de.fall(&derecha); |
OnacSerdna | 0:adbdc52e1846 | 111 | iz.fall(&izquierda); |
OnacSerdna | 0:adbdc52e1846 | 112 | a.printf("inicio\n"); |
OnacSerdna | 0:adbdc52e1846 | 113 | while(1){ |
OnacSerdna | 0:adbdc52e1846 | 114 | con=0; |
OnacSerdna | 0:adbdc52e1846 | 115 | b=2*(rand()%3); |
OnacSerdna | 0:adbdc52e1846 | 116 | a.printf("numero %d\n",b); |
OnacSerdna | 0:adbdc52e1846 | 117 | imprimir(); |
OnacSerdna | 0:adbdc52e1846 | 118 | } |
OnacSerdna | 0:adbdc52e1846 | 119 | } |
OnacSerdna | 0:adbdc52e1846 | 120 | |
OnacSerdna | 0:adbdc52e1846 | 121 | void derecha() |
OnacSerdna | 0:adbdc52e1846 | 122 | { |
OnacSerdna | 0:adbdc52e1846 | 123 | if(carro==0b00011000 || carro==0b01100000) |
OnacSerdna | 0:adbdc52e1846 | 124 | carro=carro>>2; |
OnacSerdna | 0:adbdc52e1846 | 125 | a.printf("entra der \n"); |
OnacSerdna | 0:adbdc52e1846 | 126 | return; |
OnacSerdna | 0:adbdc52e1846 | 127 | } |
OnacSerdna | 0:adbdc52e1846 | 128 | void izquierda() |
OnacSerdna | 0:adbdc52e1846 | 129 | { |
OnacSerdna | 0:adbdc52e1846 | 130 | if( carro==0b00000110 || carro==0b00011000 ) |
OnacSerdna | 0:adbdc52e1846 | 131 | carro=carro<<2; |
OnacSerdna | 0:adbdc52e1846 | 132 | a.printf("entra izq \n"); |
OnacSerdna | 0:adbdc52e1846 | 133 | return; |
OnacSerdna | 0:adbdc52e1846 | 134 | } |
OnacSerdna | 0:adbdc52e1846 | 135 | |
OnacSerdna | 0:adbdc52e1846 | 136 | void colision () |
OnacSerdna | 0:adbdc52e1846 | 137 | { |
OnacSerdna | 0:adbdc52e1846 | 138 | borrar(); |
OnacSerdna | 0:adbdc52e1846 | 139 | sendSPI(0x0F,0); |
OnacSerdna | 0:adbdc52e1846 | 140 | sendSPI(3,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 141 | sendSPI(4,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 142 | sendSPI(5,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 143 | sendSPI(6,0b01111110); |
OnacSerdna | 0:adbdc52e1846 | 144 | wait(2); |
OnacSerdna | 0:adbdc52e1846 | 145 | juego(); |
OnacSerdna | 0:adbdc52e1846 | 146 | } |
OnacSerdna | 0:adbdc52e1846 | 147 | |
OnacSerdna | 0:adbdc52e1846 | 148 | void accelerometro(){ |
OnacSerdna | 0:adbdc52e1846 | 149 | int holi; |
OnacSerdna | 0:adbdc52e1846 | 150 | Wire.setAcceleroRange('MPU6050_ACCELERO_RANGE_16G'); |
OnacSerdna | 0:adbdc52e1846 | 151 | int accelero[3]; Wire.getAcceleroRaw(accelero); |
OnacSerdna | 0:adbdc52e1846 | 152 | int valor=accelero[1]; |
OnacSerdna | 0:adbdc52e1846 | 153 | if(valor<=(2000) && valor>=(1000)){ |
OnacSerdna | 0:adbdc52e1846 | 154 | holi=1;} |
OnacSerdna | 0:adbdc52e1846 | 155 | if(valor<(1000) && valor>=(-1000)){ |
OnacSerdna | 0:adbdc52e1846 | 156 | holi=2;} |
OnacSerdna | 0:adbdc52e1846 | 157 | if(valor<(-1000) && valor>=(-2000)){ |
OnacSerdna | 0:adbdc52e1846 | 158 | holi=3;} |
OnacSerdna | 0:adbdc52e1846 | 159 | a.printf("holi : \t %d \n",holi); |
OnacSerdna | 0:adbdc52e1846 | 160 | wait(0.8); |
OnacSerdna | 0:adbdc52e1846 | 161 | |
OnacSerdna | 0:adbdc52e1846 | 162 | } |