robo_bluetooth
Dependencies: MMA8451Q_ext TextLCD mbed
main.cpp@0:29436844c427, 2017-06-16 (annotated)
- Committer:
- marcosvtt
- Date:
- Fri Jun 16 17:31:00 2017 +0000
- Revision:
- 0:29436844c427
robozin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcosvtt | 0:29436844c427 | 1 | /* |
marcosvtt | 0:29436844c427 | 2 | MAPA DE CANAIS |
marcosvtt | 0:29436844c427 | 3 | Bluetooth_TX E1 |
marcosvtt | 0:29436844c427 | 4 | Bluetooth_RX E0 |
marcosvtt | 0:29436844c427 | 5 | Motor_Amarelo B0 |
marcosvtt | 0:29436844c427 | 6 | Motor_Laranja B1 |
marcosvtt | 0:29436844c427 | 7 | Motor_Verde B2 |
marcosvtt | 0:29436844c427 | 8 | Motor_Azul B3 |
marcosvtt | 0:29436844c427 | 9 | Servo_R D4 |
marcosvtt | 0:29436844c427 | 10 | Servo_L A12 |
marcosvtt | 0:29436844c427 | 11 | |
marcosvtt | 0:29436844c427 | 12 | |
marcosvtt | 0:29436844c427 | 13 | */ |
marcosvtt | 0:29436844c427 | 14 | |
marcosvtt | 0:29436844c427 | 15 | // D4 = D1 |
marcosvtt | 0:29436844c427 | 16 | // D5 = D3 |
marcosvtt | 0:29436844c427 | 17 | // D6 = D2 |
marcosvtt | 0:29436844c427 | 18 | // D7 = D0 |
marcosvtt | 0:29436844c427 | 19 | // RS = D5 |
marcosvtt | 0:29436844c427 | 20 | // E = A13 |
marcosvtt | 0:29436844c427 | 21 | |
marcosvtt | 0:29436844c427 | 22 | #include "mbed.h" |
marcosvtt | 0:29436844c427 | 23 | #include "MMA8451Q.h" |
marcosvtt | 0:29436844c427 | 24 | #include "TextLCD.h" |
marcosvtt | 0:29436844c427 | 25 | |
marcosvtt | 0:29436844c427 | 26 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
marcosvtt | 0:29436844c427 | 27 | |
marcosvtt | 0:29436844c427 | 28 | #define SERVO_L_MAX 800 |
marcosvtt | 0:29436844c427 | 29 | #define SERVO_L_MIN 0 |
marcosvtt | 0:29436844c427 | 30 | #define SERVO_R_MAX 800 |
marcosvtt | 0:29436844c427 | 31 | #define SERVO_R_MIN 0 |
marcosvtt | 0:29436844c427 | 32 | #define MIN_PULSO_SERVO_US 700 |
marcosvtt | 0:29436844c427 | 33 | |
marcosvtt | 0:29436844c427 | 34 | |
marcosvtt | 0:29436844c427 | 35 | |
marcosvtt | 0:29436844c427 | 36 | |
marcosvtt | 0:29436844c427 | 37 | Serial bluetooth(PTE0, PTE1); |
marcosvtt | 0:29436844c427 | 38 | Serial pc(USBTX, USBRX); |
marcosvtt | 0:29436844c427 | 39 | |
marcosvtt | 0:29436844c427 | 40 | TextLCD LCD(PTD5, PTA13,PTD1, PTD3, PTD2, PTD0); |
marcosvtt | 0:29436844c427 | 41 | MMA8451Q acelerometro(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
marcosvtt | 0:29436844c427 | 42 | |
marcosvtt | 0:29436844c427 | 43 | PwmOut motor_L2(PTD4); |
marcosvtt | 0:29436844c427 | 44 | PwmOut motor_L1(PTA12); |
marcosvtt | 0:29436844c427 | 45 | PwmOut motor_R1(PTA4); |
marcosvtt | 0:29436844c427 | 46 | PwmOut motor_R2(PTA5); |
marcosvtt | 0:29436844c427 | 47 | |
marcosvtt | 0:29436844c427 | 48 | PwmOut Servo_L(PTD5); |
marcosvtt | 0:29436844c427 | 49 | PwmOut Servo_R(PTA13); |
marcosvtt | 0:29436844c427 | 50 | |
marcosvtt | 0:29436844c427 | 51 | DigitalOut Led_Vermelho(LED_RED); |
marcosvtt | 0:29436844c427 | 52 | DigitalOut Led_Verde(LED_GREEN); |
marcosvtt | 0:29436844c427 | 53 | DigitalOut Led_Azul(LED_BLUE); |
marcosvtt | 0:29436844c427 | 54 | |
marcosvtt | 0:29436844c427 | 55 | long count = 0; |
marcosvtt | 0:29436844c427 | 56 | |
marcosvtt | 0:29436844c427 | 57 | float velocidade = 1; |
marcosvtt | 0:29436844c427 | 58 | |
marcosvtt | 0:29436844c427 | 59 | float d_x; |
marcosvtt | 0:29436844c427 | 60 | float d_y; |
marcosvtt | 0:29436844c427 | 61 | float d_z; |
marcosvtt | 0:29436844c427 | 62 | float old_x = 0; |
marcosvtt | 0:29436844c427 | 63 | float old_y = 0; |
marcosvtt | 0:29436844c427 | 64 | float old_z = 0; |
marcosvtt | 0:29436844c427 | 65 | |
marcosvtt | 0:29436844c427 | 66 | |
marcosvtt | 0:29436844c427 | 67 | void nome_integrantes(){ |
marcosvtt | 0:29436844c427 | 68 | LCD.printf("nome do robo"); |
marcosvtt | 0:29436844c427 | 69 | wait(1); |
marcosvtt | 0:29436844c427 | 70 | LCD.cls(); |
marcosvtt | 0:29436844c427 | 71 | LCD.printf("Adriana Peter"); |
marcosvtt | 0:29436844c427 | 72 | wait(1); |
marcosvtt | 0:29436844c427 | 73 | LCD.cls(); |
marcosvtt | 0:29436844c427 | 74 | LCD.printf("Julia Dias"); |
marcosvtt | 0:29436844c427 | 75 | wait(1); |
marcosvtt | 0:29436844c427 | 76 | LCD.cls(); |
marcosvtt | 0:29436844c427 | 77 | LCD.printf("Marcos Torres"); |
marcosvtt | 0:29436844c427 | 78 | wait(1); |
marcosvtt | 0:29436844c427 | 79 | LCD.cls(); |
marcosvtt | 0:29436844c427 | 80 | LCD.printf("Thiago Vinicius"); |
marcosvtt | 0:29436844c427 | 81 | wait(1); |
marcosvtt | 0:29436844c427 | 82 | LCD.cls(); |
marcosvtt | 0:29436844c427 | 83 | |
marcosvtt | 0:29436844c427 | 84 | } |
marcosvtt | 0:29436844c427 | 85 | |
marcosvtt | 0:29436844c427 | 86 | void abre_arma(){ |
marcosvtt | 0:29436844c427 | 87 | Servo_L.pulsewidth_us(MIN_PULSO_SERVO_US + SERVO_L_MAX); |
marcosvtt | 0:29436844c427 | 88 | Servo_R.pulsewidth_us(MIN_PULSO_SERVO_US + SERVO_R_MIN); |
marcosvtt | 0:29436844c427 | 89 | } |
marcosvtt | 0:29436844c427 | 90 | |
marcosvtt | 0:29436844c427 | 91 | void fecha_arma(){ |
marcosvtt | 0:29436844c427 | 92 | Servo_L.pulsewidth_us(MIN_PULSO_SERVO_US + SERVO_L_MIN); |
marcosvtt | 0:29436844c427 | 93 | Servo_R.pulsewidth_us(MIN_PULSO_SERVO_US + SERVO_R_MAX); |
marcosvtt | 0:29436844c427 | 94 | } |
marcosvtt | 0:29436844c427 | 95 | |
marcosvtt | 0:29436844c427 | 96 | char b(){ |
marcosvtt | 0:29436844c427 | 97 | char cmd; |
marcosvtt | 0:29436844c427 | 98 | bluetooth.scanf("%c", &cmd); |
marcosvtt | 0:29436844c427 | 99 | |
marcosvtt | 0:29436844c427 | 100 | return cmd; |
marcosvtt | 0:29436844c427 | 101 | } |
marcosvtt | 0:29436844c427 | 102 | |
marcosvtt | 0:29436844c427 | 103 | void movimentos(){ |
marcosvtt | 0:29436844c427 | 104 | char comando; |
marcosvtt | 0:29436844c427 | 105 | comando = b(); |
marcosvtt | 0:29436844c427 | 106 | switch (comando){ |
marcosvtt | 0:29436844c427 | 107 | |
marcosvtt | 0:29436844c427 | 108 | case 'F': //frente |
marcosvtt | 0:29436844c427 | 109 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 110 | motor_L2 = velocidade; |
marcosvtt | 0:29436844c427 | 111 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 112 | motor_R2 = velocidade; |
marcosvtt | 0:29436844c427 | 113 | break; |
marcosvtt | 0:29436844c427 | 114 | |
marcosvtt | 0:29436844c427 | 115 | case 'B': //ré |
marcosvtt | 0:29436844c427 | 116 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 117 | motor_L1 = velocidade; |
marcosvtt | 0:29436844c427 | 118 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 119 | motor_R1 = velocidade; |
marcosvtt | 0:29436844c427 | 120 | break; |
marcosvtt | 0:29436844c427 | 121 | |
marcosvtt | 0:29436844c427 | 122 | case 'R': |
marcosvtt | 0:29436844c427 | 123 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 124 | motor_L1 = velocidade; |
marcosvtt | 0:29436844c427 | 125 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 126 | motor_R2 = velocidade; |
marcosvtt | 0:29436844c427 | 127 | break; |
marcosvtt | 0:29436844c427 | 128 | |
marcosvtt | 0:29436844c427 | 129 | case 'L': |
marcosvtt | 0:29436844c427 | 130 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 131 | motor_L2 = velocidade; |
marcosvtt | 0:29436844c427 | 132 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 133 | motor_R1 = velocidade; |
marcosvtt | 0:29436844c427 | 134 | break; |
marcosvtt | 0:29436844c427 | 135 | |
marcosvtt | 0:29436844c427 | 136 | case 'I': //frente esquerda |
marcosvtt | 0:29436844c427 | 137 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 138 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 139 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 140 | motor_R2 = velocidade; |
marcosvtt | 0:29436844c427 | 141 | break; |
marcosvtt | 0:29436844c427 | 142 | |
marcosvtt | 0:29436844c427 | 143 | case 'G': //frente direita |
marcosvtt | 0:29436844c427 | 144 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 145 | motor_L2 = velocidade; |
marcosvtt | 0:29436844c427 | 146 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 147 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 148 | |
marcosvtt | 0:29436844c427 | 149 | break; |
marcosvtt | 0:29436844c427 | 150 | |
marcosvtt | 0:29436844c427 | 151 | case 'J': //re esquerda |
marcosvtt | 0:29436844c427 | 152 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 153 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 154 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 155 | motor_R1 = velocidade; |
marcosvtt | 0:29436844c427 | 156 | break; |
marcosvtt | 0:29436844c427 | 157 | |
marcosvtt | 0:29436844c427 | 158 | case 'H': // re direita |
marcosvtt | 0:29436844c427 | 159 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 160 | motor_L1 = velocidade; |
marcosvtt | 0:29436844c427 | 161 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 162 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 163 | break; |
marcosvtt | 0:29436844c427 | 164 | |
marcosvtt | 0:29436844c427 | 165 | case 'S': // parado |
marcosvtt | 0:29436844c427 | 166 | motor_L1 = 0; |
marcosvtt | 0:29436844c427 | 167 | motor_L2 = 0; |
marcosvtt | 0:29436844c427 | 168 | motor_R1 = 0; |
marcosvtt | 0:29436844c427 | 169 | motor_R2 = 0; |
marcosvtt | 0:29436844c427 | 170 | break; |
marcosvtt | 0:29436844c427 | 171 | |
marcosvtt | 0:29436844c427 | 172 | case 'w': |
marcosvtt | 0:29436844c427 | 173 | fecha_arma(); |
marcosvtt | 0:29436844c427 | 174 | break; |
marcosvtt | 0:29436844c427 | 175 | |
marcosvtt | 0:29436844c427 | 176 | case 'W': |
marcosvtt | 0:29436844c427 | 177 | abre_arma(); |
marcosvtt | 0:29436844c427 | 178 | break; |
marcosvtt | 0:29436844c427 | 179 | |
marcosvtt | 0:29436844c427 | 180 | default: |
marcosvtt | 0:29436844c427 | 181 | if(comando == '0') velocidade = 0; |
marcosvtt | 0:29436844c427 | 182 | else if (comando == '1') velocidade = 0.1; |
marcosvtt | 0:29436844c427 | 183 | else if (comando == '2') velocidade = 0.2; |
marcosvtt | 0:29436844c427 | 184 | else if (comando == '3') velocidade = 0.3; |
marcosvtt | 0:29436844c427 | 185 | else if (comando == '4') velocidade = 0.4; |
marcosvtt | 0:29436844c427 | 186 | else if (comando == '5') velocidade = 0.5; |
marcosvtt | 0:29436844c427 | 187 | else if (comando == '6') velocidade = 0.6; |
marcosvtt | 0:29436844c427 | 188 | else if (comando == '7') velocidade = 0.7; |
marcosvtt | 0:29436844c427 | 189 | else if (comando == '8') velocidade = 0.8; |
marcosvtt | 0:29436844c427 | 190 | else if (comando == '9') velocidade = 0.9; |
marcosvtt | 0:29436844c427 | 191 | else if (comando == 'q') velocidade = 1; |
marcosvtt | 0:29436844c427 | 192 | |
marcosvtt | 0:29436844c427 | 193 | |
marcosvtt | 0:29436844c427 | 194 | |
marcosvtt | 0:29436844c427 | 195 | } |
marcosvtt | 0:29436844c427 | 196 | pc.printf("%c || %i\n", comando, count++); |
marcosvtt | 0:29436844c427 | 197 | } |
marcosvtt | 0:29436844c427 | 198 | |
marcosvtt | 0:29436844c427 | 199 | |
marcosvtt | 0:29436844c427 | 200 | void seta(){ |
marcosvtt | 0:29436844c427 | 201 | d_x = abs(acelerometro.getAccX() - old_x); |
marcosvtt | 0:29436844c427 | 202 | d_y = abs(acelerometro.getAccY() - old_y); |
marcosvtt | 0:29436844c427 | 203 | d_z = abs(acelerometro.getAccZ() - old_z); |
marcosvtt | 0:29436844c427 | 204 | bluetooth.printf("%f | %f | %f\n", d_x, d_y, d_z); |
marcosvtt | 0:29436844c427 | 205 | } |
marcosvtt | 0:29436844c427 | 206 | int main() { |
marcosvtt | 0:29436844c427 | 207 | |
marcosvtt | 0:29436844c427 | 208 | Servo_L.period_ms(20); |
marcosvtt | 0:29436844c427 | 209 | Servo_R.period_ms(20); |
marcosvtt | 0:29436844c427 | 210 | abre_arma(); |
marcosvtt | 0:29436844c427 | 211 | Led_Vermelho = 1; |
marcosvtt | 0:29436844c427 | 212 | Led_Verde = 1; |
marcosvtt | 0:29436844c427 | 213 | Led_Azul = 1; |
marcosvtt | 0:29436844c427 | 214 | while(1) { |
marcosvtt | 0:29436844c427 | 215 | movimentos(); |
marcosvtt | 0:29436844c427 | 216 | seta(); |
marcosvtt | 0:29436844c427 | 217 | } |
marcosvtt | 0:29436844c427 | 218 | |
marcosvtt | 0:29436844c427 | 219 | } |