![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Projecte final de Sistemes Encastats
Dependencies: Hc05 RawSerialPc mbed-rtos mbed
Diff: main.cpp
- Revision:
- 1:c603de57c8b6
- Parent:
- 0:be638f6ee353
- Child:
- 2:1ac2d1debc92
--- a/main.cpp Wed Dec 16 14:48:28 2015 +0000 +++ b/main.cpp Wed Dec 16 16:06:25 2015 +0000 @@ -1,8 +1,11 @@ #include "mbed.h" #include "rtos.h" #include <string> +// ----------------------------- +// ---- NO TOCAR DESDE AQUI ---- +// ----------------------------- -#define longString 128 +#define longString 128 //longString es la longitud màxima del string class Pc : public RawSerial { public: @@ -38,12 +41,27 @@ }; class Hc05 : public RawSerial { + private: + int mode, vel, ang; + char strLlegit[longString]; public: //Mutex mutex; Hc05(PinName tx, PinName rx) : RawSerial(tx, rx){ baud(115200); + mode = vel = ang = 0; + iniString(); }; + void iniString(){ + strLlegit[0] = 'S'; + strLlegit[1] = 't'; + strLlegit[2] = 'r'; + strLlegit[3] = 'i'; + strLlegit[4] = 'n'; + strLlegit[5] = 'g'; + strLlegit[6] = '\0'; + }; + void enviaString(char* str){ int i = 0; while((i < longString) && (str[i] != '\0')) { @@ -54,19 +72,19 @@ }; - bool llegirString(char* str){ + bool llegirString(){ if(readable()){ char c = getc(); int i = 0; //mutex.lock(); while((i < longString) && (c != 13)) { if(c!='@'){ - str[i] = c; + strLlegit[i] = c; i++; } c = getc(); } - str[i] = '\0'; + strLlegit[i] = '\0'; //mutex.unlock(); return true; @@ -74,52 +92,73 @@ return false; }; - void tractaString(char* str, int& mode, int& vel, int& ang){ - if ((str[0] == 'a') && (str[1] == 'v') && (str[2] == 'a') && (str[3] == 'n')) + void tractaString(){ + if ((strLlegit[0] == 'a') && (strLlegit[1] == 'v') && (strLlegit[2] == 'a') && (strLlegit[3] == 'n')) mode = 1; - if ((str[0] == 'r') && (str[1] == 'o') && (str[2] == 't') && (str[3] == 'a')) + if ((strLlegit[0] == 'r') && (strLlegit[1] == 'o') && (strLlegit[2] == 't') && (strLlegit[3] == 'a')) mode = 2; - if ((str[0] == 's') && (str[1] == 't') && (str[2] == 'o') && (str[3] == 'p')) + if ((strLlegit[0] == 's') && (strLlegit[1] == 't') && (strLlegit[2] == 'o') && (strLlegit[3] == 'p')){ mode = 0; + vel = 0; + ang = 0; + } if ((mode == 1) || (mode==2)){ - char svel[4], sang[4]; - for(int i = 0; i<=3;i++){ - sang[i] = str[i+5]; - svel[i] = str[i+9]; + char svel[5], sang[5]; + for(int i = 0; i<=4;i++){ + sang[i] = strLlegit[i+5]; + svel[i] = strLlegit[i+10]; } - sang[3] = svel[3] = '\0'; + sang[4] = svel[4] = '\0'; vel = atoi(svel); ang = atoi(sang); } }; - + + int getMode(){return mode;}; + int getVel(){return vel;}; + int getAng(){return ang;}; + void getStringLlegit(char* str){ + int i = 0; + while((i < longString) && (strLlegit[i] != '\0')) { + str[i] = strLlegit[i]; + i++; + } + str[i] = '\0'; + }; }; +// ---------------------------- +// ---- NO TOCAR FINS AQUI ---- +// ---------------------------- -Pc pc(USBTX, USBRX); -Hc05 bt(D8, D2); -char prova[longString] = "hola"; -char enviar[longString] = "enviar"; -int mode, vel, ang; +Pc pc(USBTX, USBRX); //Per provar si funciona, no cal al programa +Hc05 bt(D8, D2); //Crea l'objecte bt, connectat als pins D8 i D2 (és la connexió sèrie BT) +char prova[longString] = "hola"; //Per provar si funciona, no cal al programa - + +//Aquest serà el meu fil, que va llegint cada 300ms del HC-05 void llegir_thread(void const *args) { while (true) { - //pc.enviaString(prova); - bt.llegirString(prova); - bt.tractaString(prova, mode, vel, ang); + bt.llegirString(); + bt.getStringLlegit(prova); //Per provar si funciona, no cal al programa + bt.tractaString(); Thread::wait(300); } } - + +//Aquest serà el programa principal int main() { - mode = vel = ang = 0; - Thread thread(llegir_thread); + + Thread thread(llegir_thread); //Inicia el fil llegir_thread while (true) { - pc.enviaString(prova); - if(vel==50) - pc.enviaString("VEL=50"); - - Thread::wait(1000); + pc.enviaString(prova); //Per provar si funciona, no cal al programa + if(bt.getAng()<0) //Per provar si funciona, no cal al programa + pc.enviaString("ANGLE<0"); //Per provar si funciona, no cal al programa + if(bt.getVel()== 60) //Per provar si funciona, no cal al programa + pc.enviaString("VEL=60"); //Per provar si funciona, no cal al programa + bt.getMode(); //Retorna 0 si ha llegit l'ordre parar, 1 per avançar, 2 per rotar, -1 si hi ha error de connexio + bt.getVel(); //Retorna un enter amb la velocitat (de 0 a 100) + bt.getAng(); //Retorna un enter amb l'angle (de -180º a 180º) + Thread::wait(1000); } } \ No newline at end of file