![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Cambios
Dependencies: mbed Adafruit_GFX SeeedShieldBot BluetoothSerial
main.cpp@1:035ceace4dc3, 2020-12-21 (annotated)
- Committer:
- arturodeusto
- Date:
- Mon Dec 21 12:33:29 2020 +0000
- Revision:
- 1:035ceace4dc3
- Parent:
- 0:92bf762d49fa
- Child:
- 2:8e21f1f358bc
casi funciona
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aitor98 | 0:92bf762d49fa | 1 | #include "mbed.h" |
aitor98 | 0:92bf762d49fa | 2 | #include "hcsr04.h" |
aitor98 | 0:92bf762d49fa | 3 | #include "Adafruit_SSD1306.h" |
aitor98 | 0:92bf762d49fa | 4 | /*#include "estadomovder.h" |
aitor98 | 0:92bf762d49fa | 5 | #include "estadoinicio.h" |
aitor98 | 0:92bf762d49fa | 6 | #include "estadocalentar.h" |
aitor98 | 0:92bf762d49fa | 7 | #include "estadomovizq1.h" |
aitor98 | 0:92bf762d49fa | 8 | #include "estadocentro.h" |
aitor98 | 0:92bf762d49fa | 9 | #include "estadomovizq2.h" |
aitor98 | 0:92bf762d49fa | 10 | #include "estadosoplado.h"*/ |
aitor98 | 0:92bf762d49fa | 11 | |
aitor98 | 0:92bf762d49fa | 12 | |
aitor98 | 0:92bf762d49fa | 13 | class I2CPreInit : public I2C |
aitor98 | 0:92bf762d49fa | 14 | { |
aitor98 | 0:92bf762d49fa | 15 | public: |
aitor98 | 0:92bf762d49fa | 16 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
aitor98 | 0:92bf762d49fa | 17 | { |
aitor98 | 0:92bf762d49fa | 18 | frequency(100000); |
aitor98 | 0:92bf762d49fa | 19 | start(); |
aitor98 | 0:92bf762d49fa | 20 | }; |
aitor98 | 0:92bf762d49fa | 21 | }; |
aitor98 | 0:92bf762d49fa | 22 | |
aitor98 | 0:92bf762d49fa | 23 | I2CPreInit gI2C(PB_9,PB_8); |
aitor98 | 0:92bf762d49fa | 24 | |
aitor98 | 0:92bf762d49fa | 25 | Serial pc(USBTX, USBRX); // tx, rx |
aitor98 | 0:92bf762d49fa | 26 | Serial bt(A5, A4); // tx, rx |
aitor98 | 0:92bf762d49fa | 27 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
aitor98 | 0:92bf762d49fa | 28 | Ticker repeticion; |
aitor98 | 0:92bf762d49fa | 29 | |
aitor98 | 0:92bf762d49fa | 30 | DigitalOut led(LED1); |
aitor98 | 0:92bf762d49fa | 31 | DigitalOut step(D13); |
aitor98 | 0:92bf762d49fa | 32 | DigitalOut dir(D12); |
aitor98 | 0:92bf762d49fa | 33 | DigitalOut enable(D11); |
aitor98 | 0:92bf762d49fa | 34 | DigitalOut dirAMotor(D4); |
aitor98 | 0:92bf762d49fa | 35 | PwmOut dirBMotor(A3); |
aitor98 | 0:92bf762d49fa | 36 | InterruptIn encoderA(A2); |
aitor98 | 0:92bf762d49fa | 37 | DigitalOut rele(D3); |
aitor98 | 0:92bf762d49fa | 38 | HCSR04 usensor(D7,D8); |
aitor98 | 0:92bf762d49fa | 39 | DigitalIn final1(D9); |
aitor98 | 0:92bf762d49fa | 40 | InterruptIn final2(D10); |
aitor98 | 0:92bf762d49fa | 41 | AnalogIn Vout(A0); |
aitor98 | 0:92bf762d49fa | 42 | AnalogIn Aout(A1); |
aitor98 | 0:92bf762d49fa | 43 | |
aitor98 | 0:92bf762d49fa | 44 | enum estados {inicio,movder,calentar,movizq1,centro,movizq2,soplado}; |
aitor98 | 0:92bf762d49fa | 45 | estados estado; |
aitor98 | 0:92bf762d49fa | 46 | |
aitor98 | 0:92bf762d49fa | 47 | |
aitor98 | 0:92bf762d49fa | 48 | void medir_corriente() |
aitor98 | 0:92bf762d49fa | 49 | { |
aitor98 | 0:92bf762d49fa | 50 | |
aitor98 | 0:92bf762d49fa | 51 | float corriente=((Aout.read()*5)-2.5)/0.185; |
aitor98 | 0:92bf762d49fa | 52 | |
aitor98 | 0:92bf762d49fa | 53 | pc.printf("Intensidad medida = %.4f A \n", corriente); |
aitor98 | 0:92bf762d49fa | 54 | bt.printf("Intensidad medida = %.4f A \n", corriente); |
aitor98 | 0:92bf762d49fa | 55 | |
aitor98 | 0:92bf762d49fa | 56 | } |
aitor98 | 0:92bf762d49fa | 57 | |
aitor98 | 0:92bf762d49fa | 58 | int dato=0; |
aitor98 | 0:92bf762d49fa | 59 | |
aitor98 | 0:92bf762d49fa | 60 | void estadoinicio() |
aitor98 | 0:92bf762d49fa | 61 | { |
aitor98 | 0:92bf762d49fa | 62 | { |
aitor98 | 0:92bf762d49fa | 63 | if(bt.readable()) { //Mira si hay algun dato pendiente en el bt |
aitor98 | 0:92bf762d49fa | 64 | dato=bt.getc(); //Lee un caracter del bt |
aitor98 | 0:92bf762d49fa | 65 | |
aitor98 | 0:92bf762d49fa | 66 | } |
aitor98 | 0:92bf762d49fa | 67 | if (dato=='1') { //Si el dato es un 1 comienza el proceso |
aitor98 | 0:92bf762d49fa | 68 | |
aitor98 | 0:92bf762d49fa | 69 | estado = movder; |
arturodeusto | 1:035ceace4dc3 | 70 | //gOled.printf("Transportando\n"); |
arturodeusto | 1:035ceace4dc3 | 71 | bt.printf("Transportando\n"); |
aitor98 | 0:92bf762d49fa | 72 | |
aitor98 | 0:92bf762d49fa | 73 | |
aitor98 | 0:92bf762d49fa | 74 | } else { //Si el dato es cualquier otra cosa no empieza a hacer nada |
aitor98 | 0:92bf762d49fa | 75 | } |
aitor98 | 0:92bf762d49fa | 76 | } |
aitor98 | 0:92bf762d49fa | 77 | } |
aitor98 | 0:92bf762d49fa | 78 | |
aitor98 | 0:92bf762d49fa | 79 | |
aitor98 | 0:92bf762d49fa | 80 | void estadomovderecha() |
aitor98 | 0:92bf762d49fa | 81 | { |
aitor98 | 0:92bf762d49fa | 82 | if(final1==0) { |
aitor98 | 0:92bf762d49fa | 83 | enable=1; //activar el motor |
aitor98 | 0:92bf762d49fa | 84 | dir=1; //direccion hacia la derecha |
aitor98 | 0:92bf762d49fa | 85 | //step=1; // avanzar |
aitor98 | 0:92bf762d49fa | 86 | step=1; |
aitor98 | 0:92bf762d49fa | 87 | wait_us(100); |
aitor98 | 0:92bf762d49fa | 88 | step=0; |
aitor98 | 0:92bf762d49fa | 89 | wait_us(900); |
aitor98 | 0:92bf762d49fa | 90 | } else { |
aitor98 | 0:92bf762d49fa | 91 | enable=0; |
aitor98 | 0:92bf762d49fa | 92 | estado=calentar; |
arturodeusto | 1:035ceace4dc3 | 93 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 94 | //gOled.printf("Calentando\n"); |
arturodeusto | 1:035ceace4dc3 | 95 | bt.printf("Calentando\n"); |
arturodeusto | 1:035ceace4dc3 | 96 | |
aitor98 | 0:92bf762d49fa | 97 | } |
aitor98 | 0:92bf762d49fa | 98 | } |
aitor98 | 0:92bf762d49fa | 99 | |
aitor98 | 0:92bf762d49fa | 100 | void estadocalentar() |
aitor98 | 0:92bf762d49fa | 101 | { |
arturodeusto | 1:035ceace4dc3 | 102 | rele=1; |
aitor98 | 0:92bf762d49fa | 103 | float tension=Vout.read()*3.3; |
aitor98 | 0:92bf762d49fa | 104 | //pc.printf("Valor voltios %.4f leido flat\n",tension); |
aitor98 | 0:92bf762d49fa | 105 | |
aitor98 | 0:92bf762d49fa | 106 | float resistencia= ((3.3*100000.0)/tension)-100000.0; |
aitor98 | 0:92bf762d49fa | 107 | |
aitor98 | 0:92bf762d49fa | 108 | float temp= 3950.0/(log(resistencia/100000.0)+(3950.0/298.0)); |
arturodeusto | 1:035ceace4dc3 | 109 | bt.printf("Calentando a: %.4f\n",temp); |
arturodeusto | 1:035ceace4dc3 | 110 | //gOled.printf("Calentando a: %.4f\n",temp); |
aitor98 | 0:92bf762d49fa | 111 | |
arturodeusto | 1:035ceace4dc3 | 112 | if (temp<308.0) { |
aitor98 | 0:92bf762d49fa | 113 | //rele.write(1); |
aitor98 | 0:92bf762d49fa | 114 | |
aitor98 | 0:92bf762d49fa | 115 | } else { |
aitor98 | 0:92bf762d49fa | 116 | rele=0; |
aitor98 | 0:92bf762d49fa | 117 | estado=movizq1; |
aitor98 | 0:92bf762d49fa | 118 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 119 | //gOled.printf("Transportando\n"); |
arturodeusto | 1:035ceace4dc3 | 120 | bt.printf("Transportando\n"); |
aitor98 | 0:92bf762d49fa | 121 | } |
aitor98 | 0:92bf762d49fa | 122 | } |
aitor98 | 0:92bf762d49fa | 123 | |
aitor98 | 0:92bf762d49fa | 124 | void estadomovizq1() |
aitor98 | 0:92bf762d49fa | 125 | { |
aitor98 | 0:92bf762d49fa | 126 | unsigned int dist; |
aitor98 | 0:92bf762d49fa | 127 | usensor.start(); |
arturodeusto | 1:035ceace4dc3 | 128 | wait(0.01); |
aitor98 | 0:92bf762d49fa | 129 | dist=usensor.get_dist_cm(); |
aitor98 | 0:92bf762d49fa | 130 | |
aitor98 | 0:92bf762d49fa | 131 | if(dist>20) { |
aitor98 | 0:92bf762d49fa | 132 | enable=1; //activar el motor |
aitor98 | 0:92bf762d49fa | 133 | dir=0; //direccion hacia la derecha |
aitor98 | 0:92bf762d49fa | 134 | step=1; // avanzar |
aitor98 | 0:92bf762d49fa | 135 | wait_us(100); |
aitor98 | 0:92bf762d49fa | 136 | step=0; |
aitor98 | 0:92bf762d49fa | 137 | wait_us(900); |
arturodeusto | 1:035ceace4dc3 | 138 | |
aitor98 | 0:92bf762d49fa | 139 | } else { |
aitor98 | 0:92bf762d49fa | 140 | enable=0; |
aitor98 | 0:92bf762d49fa | 141 | estado=centro; |
arturodeusto | 1:035ceace4dc3 | 142 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 143 | //gOled.printf("Comprobar\n"); |
arturodeusto | 1:035ceace4dc3 | 144 | bt.printf("Centro\n"); |
aitor98 | 0:92bf762d49fa | 145 | } |
aitor98 | 0:92bf762d49fa | 146 | } |
aitor98 | 0:92bf762d49fa | 147 | |
aitor98 | 0:92bf762d49fa | 148 | |
aitor98 | 0:92bf762d49fa | 149 | void estadocentro() |
aitor98 | 0:92bf762d49fa | 150 | { |
aitor98 | 0:92bf762d49fa | 151 | { |
aitor98 | 0:92bf762d49fa | 152 | if(bt.readable()) { //Mira si hay algun dato pendiente en el bt |
aitor98 | 0:92bf762d49fa | 153 | dato=bt.getc(); //Lee un caracter del bt |
aitor98 | 0:92bf762d49fa | 154 | |
aitor98 | 0:92bf762d49fa | 155 | } |
aitor98 | 0:92bf762d49fa | 156 | if (dato=='2') { //Si el dato es un 1 comienza el proceso |
aitor98 | 0:92bf762d49fa | 157 | |
aitor98 | 0:92bf762d49fa | 158 | estado = movder; |
arturodeusto | 1:035ceace4dc3 | 159 | //gOled.printf("Pieza incorrecta\n"); |
arturodeusto | 1:035ceace4dc3 | 160 | bt.printf("Pieza incorrecta\n"); |
arturodeusto | 1:035ceace4dc3 | 161 | |
aitor98 | 0:92bf762d49fa | 162 | |
aitor98 | 0:92bf762d49fa | 163 | } else if(dato=='3') { //Si el dato es una b enciende el led |
aitor98 | 0:92bf762d49fa | 164 | |
aitor98 | 0:92bf762d49fa | 165 | estado = movizq2; |
arturodeusto | 1:035ceace4dc3 | 166 | //gOled.printf("Pieza correcta\n"); |
arturodeusto | 1:035ceace4dc3 | 167 | bt.printf("Pieza correcta\n"); |
aitor98 | 0:92bf762d49fa | 168 | } |
aitor98 | 0:92bf762d49fa | 169 | } |
aitor98 | 0:92bf762d49fa | 170 | } |
aitor98 | 0:92bf762d49fa | 171 | |
aitor98 | 0:92bf762d49fa | 172 | void estadomovizq2() |
aitor98 | 0:92bf762d49fa | 173 | { |
aitor98 | 0:92bf762d49fa | 174 | if(final2==0) { |
aitor98 | 0:92bf762d49fa | 175 | enable=1; //activar el motor |
arturodeusto | 1:035ceace4dc3 | 176 | dir=0; //direccion hacia la derecha |
aitor98 | 0:92bf762d49fa | 177 | step=1; // avanzar |
aitor98 | 0:92bf762d49fa | 178 | wait_us(100); |
aitor98 | 0:92bf762d49fa | 179 | step=0; |
aitor98 | 0:92bf762d49fa | 180 | wait_us(900); |
aitor98 | 0:92bf762d49fa | 181 | } else { |
aitor98 | 0:92bf762d49fa | 182 | enable=0; |
aitor98 | 0:92bf762d49fa | 183 | estado=soplado; |
arturodeusto | 1:035ceace4dc3 | 184 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 185 | //gOled.printf("Soplando\n"); |
arturodeusto | 1:035ceace4dc3 | 186 | bt.printf("Soplando\n"); |
aitor98 | 0:92bf762d49fa | 187 | } |
aitor98 | 0:92bf762d49fa | 188 | } |
aitor98 | 0:92bf762d49fa | 189 | |
aitor98 | 0:92bf762d49fa | 190 | void estadosoplado() |
aitor98 | 0:92bf762d49fa | 191 | { |
arturodeusto | 1:035ceace4dc3 | 192 | dirAMotor=0; |
arturodeusto | 1:035ceace4dc3 | 193 | dirBMotor=1; |
arturodeusto | 1:035ceace4dc3 | 194 | wait(5); |
aitor98 | 0:92bf762d49fa | 195 | dirAMotor=0; |
arturodeusto | 1:035ceace4dc3 | 196 | dirBMotor=0; |
aitor98 | 0:92bf762d49fa | 197 | estado=inicio; |
arturodeusto | 1:035ceace4dc3 | 198 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 199 | //gOled.printf("Reposo\n"); |
arturodeusto | 1:035ceace4dc3 | 200 | bt.printf("Reposo\n"); |
arturodeusto | 1:035ceace4dc3 | 201 | |
aitor98 | 0:92bf762d49fa | 202 | |
aitor98 | 0:92bf762d49fa | 203 | } |
aitor98 | 0:92bf762d49fa | 204 | |
aitor98 | 0:92bf762d49fa | 205 | |
aitor98 | 0:92bf762d49fa | 206 | |
aitor98 | 0:92bf762d49fa | 207 | int main() |
aitor98 | 0:92bf762d49fa | 208 | { |
aitor98 | 0:92bf762d49fa | 209 | pc.baud(115200); |
aitor98 | 0:92bf762d49fa | 210 | estado = inicio; |
arturodeusto | 1:035ceace4dc3 | 211 | //gOled.begin(); |
arturodeusto | 1:035ceace4dc3 | 212 | //gOled.clearDisplay(); |
arturodeusto | 1:035ceace4dc3 | 213 | //gOled.printf("Reposo\n"); |
arturodeusto | 1:035ceace4dc3 | 214 | bt.printf("Reposo\n"); |
aitor98 | 0:92bf762d49fa | 215 | |
aitor98 | 0:92bf762d49fa | 216 | repeticion.attach(&medir_corriente, 5.0); //Medir la intensidad cada tres segundos |
aitor98 | 0:92bf762d49fa | 217 | |
aitor98 | 0:92bf762d49fa | 218 | |
aitor98 | 0:92bf762d49fa | 219 | while(1) { |
aitor98 | 0:92bf762d49fa | 220 | switch (estado) { |
aitor98 | 0:92bf762d49fa | 221 | case inicio: |
aitor98 | 0:92bf762d49fa | 222 | estadoinicio(); |
aitor98 | 0:92bf762d49fa | 223 | break; |
aitor98 | 0:92bf762d49fa | 224 | case movder: |
aitor98 | 0:92bf762d49fa | 225 | estadomovderecha(); |
aitor98 | 0:92bf762d49fa | 226 | break; |
aitor98 | 0:92bf762d49fa | 227 | case calentar: |
aitor98 | 0:92bf762d49fa | 228 | estadocalentar(); |
aitor98 | 0:92bf762d49fa | 229 | break; |
aitor98 | 0:92bf762d49fa | 230 | case movizq1: |
aitor98 | 0:92bf762d49fa | 231 | estadomovizq1(); |
aitor98 | 0:92bf762d49fa | 232 | break; |
aitor98 | 0:92bf762d49fa | 233 | case centro: |
aitor98 | 0:92bf762d49fa | 234 | estadocentro(); |
aitor98 | 0:92bf762d49fa | 235 | break; |
aitor98 | 0:92bf762d49fa | 236 | case movizq2: |
aitor98 | 0:92bf762d49fa | 237 | estadomovizq2(); |
aitor98 | 0:92bf762d49fa | 238 | break; |
aitor98 | 0:92bf762d49fa | 239 | case soplado: |
aitor98 | 0:92bf762d49fa | 240 | estadosoplado(); |
aitor98 | 0:92bf762d49fa | 241 | break; |
aitor98 | 0:92bf762d49fa | 242 | default: |
aitor98 | 0:92bf762d49fa | 243 | break; |
aitor98 | 0:92bf762d49fa | 244 | |
aitor98 | 0:92bf762d49fa | 245 | } |
aitor98 | 0:92bf762d49fa | 246 | } |
aitor98 | 0:92bf762d49fa | 247 | } |
aitor98 | 0:92bf762d49fa | 248 |