Projecte final de Sistemes Encastats
Dependencies: Hc05 RawSerialPc mbed-rtos mbed
main.cpp@3:4a598ab10e87, 2015-12-18 (annotated)
- Committer:
- jcabello7
- Date:
- Fri Dec 18 00:23:49 2015 +0000
- Revision:
- 3:4a598ab10e87
- Parent:
- 2:1ac2d1debc92
- Child:
- 4:0a37a963b724
versi? final amb control de connexio, nom?s falta separar les funcions en llibreries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jcabello7 | 0:be638f6ee353 | 1 | #include "mbed.h" |
jcabello7 | 0:be638f6ee353 | 2 | #include "rtos.h" |
jcabello7 | 3:4a598ab10e87 | 3 | |
jcabello7 | 1:c603de57c8b6 | 4 | // ----------------------------- |
jcabello7 | 1:c603de57c8b6 | 5 | // ---- NO TOCAR DESDE AQUI ---- |
jcabello7 | 1:c603de57c8b6 | 6 | // ----------------------------- |
jcabello7 | 0:be638f6ee353 | 7 | |
jcabello7 | 2:1ac2d1debc92 | 8 | //0, 60, 120, 180 i en negatiu |
jcabello7 | 2:1ac2d1debc92 | 9 | |
jcabello7 | 1:c603de57c8b6 | 10 | #define longString 128 //longString es la longitud màxima del string |
jcabello7 | 0:be638f6ee353 | 11 | |
jcabello7 | 0:be638f6ee353 | 12 | class Pc : public RawSerial { |
jcabello7 | 0:be638f6ee353 | 13 | public: |
jcabello7 | 0:be638f6ee353 | 14 | Pc(PinName tx, PinName rx) : RawSerial(tx, rx){ |
jcabello7 | 0:be638f6ee353 | 15 | baud(115200); |
jcabello7 | 0:be638f6ee353 | 16 | }; |
jcabello7 | 0:be638f6ee353 | 17 | |
jcabello7 | 0:be638f6ee353 | 18 | void enviaString(char* str){ |
jcabello7 | 0:be638f6ee353 | 19 | int i = 0; |
jcabello7 | 0:be638f6ee353 | 20 | while((i < longString) && (str[i] != '\0')) { |
jcabello7 | 0:be638f6ee353 | 21 | putc(str[i]); |
jcabello7 | 0:be638f6ee353 | 22 | i++; |
jcabello7 | 0:be638f6ee353 | 23 | } |
jcabello7 | 0:be638f6ee353 | 24 | putc(10); |
jcabello7 | 0:be638f6ee353 | 25 | }; |
jcabello7 | 0:be638f6ee353 | 26 | |
jcabello7 | 0:be638f6ee353 | 27 | bool llegirString(char* str){ |
jcabello7 | 0:be638f6ee353 | 28 | if(readable()){ |
jcabello7 | 0:be638f6ee353 | 29 | int i= 0; |
jcabello7 | 0:be638f6ee353 | 30 | str[i] = getc(); |
jcabello7 | 0:be638f6ee353 | 31 | while((i < longString) && (str[i] != 13)) { |
jcabello7 | 0:be638f6ee353 | 32 | if (str[i] != '@') |
jcabello7 | 0:be638f6ee353 | 33 | i++; |
jcabello7 | 0:be638f6ee353 | 34 | str[i] = getc(); |
jcabello7 | 0:be638f6ee353 | 35 | } |
jcabello7 | 0:be638f6ee353 | 36 | str[i] = '\0'; |
jcabello7 | 0:be638f6ee353 | 37 | return true; |
jcabello7 | 0:be638f6ee353 | 38 | } |
jcabello7 | 0:be638f6ee353 | 39 | return false; |
jcabello7 | 0:be638f6ee353 | 40 | }; |
jcabello7 | 0:be638f6ee353 | 41 | |
jcabello7 | 0:be638f6ee353 | 42 | |
jcabello7 | 0:be638f6ee353 | 43 | }; |
jcabello7 | 0:be638f6ee353 | 44 | |
jcabello7 | 0:be638f6ee353 | 45 | class Hc05 : public RawSerial { |
jcabello7 | 1:c603de57c8b6 | 46 | private: |
jcabello7 | 1:c603de57c8b6 | 47 | int mode, vel, ang; |
jcabello7 | 3:4a598ab10e87 | 48 | Timer *timer; |
jcabello7 | 1:c603de57c8b6 | 49 | char strLlegit[longString]; |
jcabello7 | 0:be638f6ee353 | 50 | public: |
jcabello7 | 0:be638f6ee353 | 51 | //Mutex mutex; |
jcabello7 | 0:be638f6ee353 | 52 | Hc05(PinName tx, PinName rx) : RawSerial(tx, rx){ |
jcabello7 | 3:4a598ab10e87 | 53 | baud(230400); |
jcabello7 | 1:c603de57c8b6 | 54 | mode = vel = ang = 0; |
jcabello7 | 1:c603de57c8b6 | 55 | iniString(); |
jcabello7 | 3:4a598ab10e87 | 56 | timer = new Timer; |
jcabello7 | 3:4a598ab10e87 | 57 | timer->start(); |
jcabello7 | 0:be638f6ee353 | 58 | }; |
jcabello7 | 0:be638f6ee353 | 59 | |
jcabello7 | 3:4a598ab10e87 | 60 | ~Hc05(){ |
jcabello7 | 3:4a598ab10e87 | 61 | //timer.stop(); |
jcabello7 | 3:4a598ab10e87 | 62 | }; |
jcabello7 | 3:4a598ab10e87 | 63 | |
jcabello7 | 1:c603de57c8b6 | 64 | void iniString(){ |
jcabello7 | 1:c603de57c8b6 | 65 | strLlegit[0] = 'S'; |
jcabello7 | 1:c603de57c8b6 | 66 | strLlegit[1] = 't'; |
jcabello7 | 1:c603de57c8b6 | 67 | strLlegit[2] = 'r'; |
jcabello7 | 1:c603de57c8b6 | 68 | strLlegit[3] = 'i'; |
jcabello7 | 1:c603de57c8b6 | 69 | strLlegit[4] = 'n'; |
jcabello7 | 1:c603de57c8b6 | 70 | strLlegit[5] = 'g'; |
jcabello7 | 1:c603de57c8b6 | 71 | strLlegit[6] = '\0'; |
jcabello7 | 1:c603de57c8b6 | 72 | }; |
jcabello7 | 1:c603de57c8b6 | 73 | |
jcabello7 | 0:be638f6ee353 | 74 | void enviaString(char* str){ |
jcabello7 | 0:be638f6ee353 | 75 | int i = 0; |
jcabello7 | 0:be638f6ee353 | 76 | while((i < longString) && (str[i] != '\0')) { |
jcabello7 | 0:be638f6ee353 | 77 | putc(str[i]); |
jcabello7 | 0:be638f6ee353 | 78 | i++; |
jcabello7 | 0:be638f6ee353 | 79 | } |
jcabello7 | 0:be638f6ee353 | 80 | putc(13); |
jcabello7 | 0:be638f6ee353 | 81 | }; |
jcabello7 | 0:be638f6ee353 | 82 | |
jcabello7 | 0:be638f6ee353 | 83 | |
jcabello7 | 1:c603de57c8b6 | 84 | bool llegirString(){ |
jcabello7 | 0:be638f6ee353 | 85 | if(readable()){ |
jcabello7 | 3:4a598ab10e87 | 86 | |
jcabello7 | 0:be638f6ee353 | 87 | char c = getc(); |
jcabello7 | 0:be638f6ee353 | 88 | int i = 0; |
jcabello7 | 3:4a598ab10e87 | 89 | //timer->reset(); |
jcabello7 | 0:be638f6ee353 | 90 | //mutex.lock(); |
jcabello7 | 0:be638f6ee353 | 91 | while((i < longString) && (c != 13)) { |
jcabello7 | 0:be638f6ee353 | 92 | if(c!='@'){ |
jcabello7 | 1:c603de57c8b6 | 93 | strLlegit[i] = c; |
jcabello7 | 0:be638f6ee353 | 94 | i++; |
jcabello7 | 0:be638f6ee353 | 95 | } |
jcabello7 | 3:4a598ab10e87 | 96 | timer->reset(); |
jcabello7 | 0:be638f6ee353 | 97 | c = getc(); |
jcabello7 | 0:be638f6ee353 | 98 | } |
jcabello7 | 1:c603de57c8b6 | 99 | strLlegit[i] = '\0'; |
jcabello7 | 0:be638f6ee353 | 100 | //mutex.unlock(); |
jcabello7 | 0:be638f6ee353 | 101 | return true; |
jcabello7 | 0:be638f6ee353 | 102 | |
jcabello7 | 0:be638f6ee353 | 103 | } |
jcabello7 | 0:be638f6ee353 | 104 | return false; |
jcabello7 | 0:be638f6ee353 | 105 | }; |
jcabello7 | 0:be638f6ee353 | 106 | |
jcabello7 | 1:c603de57c8b6 | 107 | void tractaString(){ |
jcabello7 | 1:c603de57c8b6 | 108 | if ((strLlegit[0] == 'a') && (strLlegit[1] == 'v') && (strLlegit[2] == 'a') && (strLlegit[3] == 'n')) |
jcabello7 | 0:be638f6ee353 | 109 | mode = 1; |
jcabello7 | 1:c603de57c8b6 | 110 | if ((strLlegit[0] == 'r') && (strLlegit[1] == 'o') && (strLlegit[2] == 't') && (strLlegit[3] == 'a')) |
jcabello7 | 0:be638f6ee353 | 111 | mode = 2; |
jcabello7 | 1:c603de57c8b6 | 112 | if ((strLlegit[0] == 's') && (strLlegit[1] == 't') && (strLlegit[2] == 'o') && (strLlegit[3] == 'p')){ |
jcabello7 | 0:be638f6ee353 | 113 | mode = 0; |
jcabello7 | 1:c603de57c8b6 | 114 | vel = 0; |
jcabello7 | 1:c603de57c8b6 | 115 | ang = 0; |
jcabello7 | 1:c603de57c8b6 | 116 | } |
jcabello7 | 0:be638f6ee353 | 117 | if ((mode == 1) || (mode==2)){ |
jcabello7 | 1:c603de57c8b6 | 118 | char svel[5], sang[5]; |
jcabello7 | 1:c603de57c8b6 | 119 | for(int i = 0; i<=4;i++){ |
jcabello7 | 1:c603de57c8b6 | 120 | sang[i] = strLlegit[i+5]; |
jcabello7 | 1:c603de57c8b6 | 121 | svel[i] = strLlegit[i+10]; |
jcabello7 | 0:be638f6ee353 | 122 | } |
jcabello7 | 1:c603de57c8b6 | 123 | sang[4] = svel[4] = '\0'; |
jcabello7 | 0:be638f6ee353 | 124 | vel = atoi(svel); |
jcabello7 | 0:be638f6ee353 | 125 | ang = atoi(sang); |
jcabello7 | 0:be638f6ee353 | 126 | } |
jcabello7 | 2:1ac2d1debc92 | 127 | if ((strLlegit[0] == 'm') && (strLlegit[1] == 'o') && (strLlegit[2] == 'd') && (strLlegit[3] == 'e') && (strLlegit[5] == 'a') && (strLlegit[6] == 'u') && (strLlegit[7] == 't') && (strLlegit[8] == 'o')) |
jcabello7 | 2:1ac2d1debc92 | 128 | mode = 3; |
jcabello7 | 0:be638f6ee353 | 129 | }; |
jcabello7 | 1:c603de57c8b6 | 130 | |
jcabello7 | 1:c603de57c8b6 | 131 | int getMode(){return mode;}; |
jcabello7 | 1:c603de57c8b6 | 132 | int getVel(){return vel;}; |
jcabello7 | 1:c603de57c8b6 | 133 | int getAng(){return ang;}; |
jcabello7 | 1:c603de57c8b6 | 134 | void getStringLlegit(char* str){ |
jcabello7 | 1:c603de57c8b6 | 135 | int i = 0; |
jcabello7 | 1:c603de57c8b6 | 136 | while((i < longString) && (strLlegit[i] != '\0')) { |
jcabello7 | 1:c603de57c8b6 | 137 | str[i] = strLlegit[i]; |
jcabello7 | 1:c603de57c8b6 | 138 | i++; |
jcabello7 | 1:c603de57c8b6 | 139 | } |
jcabello7 | 1:c603de57c8b6 | 140 | str[i] = '\0'; |
jcabello7 | 1:c603de57c8b6 | 141 | }; |
jcabello7 | 3:4a598ab10e87 | 142 | |
jcabello7 | 3:4a598ab10e87 | 143 | void comprovaConnexio(){ |
jcabello7 | 3:4a598ab10e87 | 144 | //char m[128]; |
jcabello7 | 3:4a598ab10e87 | 145 | //snprintf(m, 128, "Mode: %d", getMode()); |
jcabello7 | 3:4a598ab10e87 | 146 | //Pc::enviaString(m); |
jcabello7 | 3:4a598ab10e87 | 147 | if(getTimer() > 300) |
jcabello7 | 3:4a598ab10e87 | 148 | mode = -1; |
jcabello7 | 3:4a598ab10e87 | 149 | }; |
jcabello7 | 3:4a598ab10e87 | 150 | |
jcabello7 | 3:4a598ab10e87 | 151 | int getTimer(){ |
jcabello7 | 3:4a598ab10e87 | 152 | return timer->read_ms(); |
jcabello7 | 3:4a598ab10e87 | 153 | }; |
jcabello7 | 3:4a598ab10e87 | 154 | |
jcabello7 | 3:4a598ab10e87 | 155 | |
jcabello7 | 0:be638f6ee353 | 156 | }; |
jcabello7 | 1:c603de57c8b6 | 157 | // ---------------------------- |
jcabello7 | 1:c603de57c8b6 | 158 | // ---- NO TOCAR FINS AQUI ---- |
jcabello7 | 1:c603de57c8b6 | 159 | // ---------------------------- |
jcabello7 | 0:be638f6ee353 | 160 | |
jcabello7 | 1:c603de57c8b6 | 161 | Pc pc(USBTX, USBRX); //Per provar si funciona, no cal al programa |
jcabello7 | 2:1ac2d1debc92 | 162 | Hc05 bt(D8, D2); //Crea l'objecte bt, connectat als pins D8 (TX) i D2 (RX) (és la connexió sèrie BT) |
jcabello7 | 1:c603de57c8b6 | 163 | char prova[longString] = "hola"; //Per provar si funciona, no cal al programa |
jcabello7 | 0:be638f6ee353 | 164 | |
jcabello7 | 1:c603de57c8b6 | 165 | |
jcabello7 | 1:c603de57c8b6 | 166 | //Aquest serà el meu fil, que va llegint cada 300ms del HC-05 |
jcabello7 | 0:be638f6ee353 | 167 | void llegir_thread(void const *args) { |
jcabello7 | 0:be638f6ee353 | 168 | while (true) { |
jcabello7 | 3:4a598ab10e87 | 169 | if(bt.llegirString()) |
jcabello7 | 3:4a598ab10e87 | 170 | bt.tractaString(); |
jcabello7 | 3:4a598ab10e87 | 171 | |
jcabello7 | 3:4a598ab10e87 | 172 | bt.comprovaConnexio(); |
jcabello7 | 1:c603de57c8b6 | 173 | bt.getStringLlegit(prova); //Per provar si funciona, no cal al programa |
jcabello7 | 3:4a598ab10e87 | 174 | Thread::wait(100); |
jcabello7 | 3:4a598ab10e87 | 175 | //bt.comprovaConnexio(); |
jcabello7 | 3:4a598ab10e87 | 176 | |
jcabello7 | 0:be638f6ee353 | 177 | } |
jcabello7 | 0:be638f6ee353 | 178 | } |
jcabello7 | 1:c603de57c8b6 | 179 | |
jcabello7 | 1:c603de57c8b6 | 180 | //Aquest serà el programa principal |
jcabello7 | 0:be638f6ee353 | 181 | int main() { |
jcabello7 | 1:c603de57c8b6 | 182 | |
jcabello7 | 1:c603de57c8b6 | 183 | Thread thread(llegir_thread); //Inicia el fil llegir_thread |
jcabello7 | 3:4a598ab10e87 | 184 | char m[128]; |
jcabello7 | 0:be638f6ee353 | 185 | while (true) { |
jcabello7 | 1:c603de57c8b6 | 186 | pc.enviaString(prova); //Per provar si funciona, no cal al programa |
jcabello7 | 1:c603de57c8b6 | 187 | if(bt.getAng()<0) //Per provar si funciona, no cal al programa |
jcabello7 | 1:c603de57c8b6 | 188 | pc.enviaString("ANGLE<0"); //Per provar si funciona, no cal al programa |
jcabello7 | 1:c603de57c8b6 | 189 | if(bt.getVel()== 60) //Per provar si funciona, no cal al programa |
jcabello7 | 1:c603de57c8b6 | 190 | pc.enviaString("VEL=60"); //Per provar si funciona, no cal al programa |
jcabello7 | 3:4a598ab10e87 | 191 | //if(bt.getMode()<0) |
jcabello7 | 3:4a598ab10e87 | 192 | //pc.enviaString("ALERTA! S'ha perdut la connexio"); //Retorna 0 si ha llegit l'ordre parar, 1 per avançar, 2 per rotar, -1 si hi ha error de connexio |
jcabello7 | 3:4a598ab10e87 | 193 | snprintf(m, 128, "Mode: %d | timer: %dms", bt.getMode(), bt.getTimer()); |
jcabello7 | 3:4a598ab10e87 | 194 | pc.enviaString(m); |
jcabello7 | 1:c603de57c8b6 | 195 | bt.getVel(); //Retorna un enter amb la velocitat (de 0 a 100) |
jcabello7 | 1:c603de57c8b6 | 196 | bt.getAng(); //Retorna un enter amb l'angle (de -180º a 180º) |
jcabello7 | 3:4a598ab10e87 | 197 | Thread::wait(500); |
jcabello7 | 0:be638f6ee353 | 198 | } |
jcabello7 | 0:be638f6ee353 | 199 | } |