Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Committer:
Thierry19
Date:
Sat Apr 11 19:20:22 2015 +0000
Revision:
50:48e8da5fc1ae
Parent:
47:f372ca93d6c1
Child:
51:299408ceee3a
Tonnes de modifications, actuellement le programme est plein de traces et de tests.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
llarose 1:601d2922ff06 1 /* S5 Projet - Conception d'un systeme embarque reseaute
llarose 1:601d2922ff06 2 * main.cpp
groygirard 6:fd1bf5563299 3 *
llarose 1:601d2922ff06 4 * @author Equipe de projet 2
groygirard 6:fd1bf5563299 5 *
llarose 1:601d2922ff06 6 */
groygirard 14:bda91cead7f2 7
groygirard 6:fd1bf5563299 8 // System libraries
llarose 1:601d2922ff06 9 #include "mbed.h"
llarose 1:601d2922ff06 10 #include "rtos.h"
groygirard 14:bda91cead7f2 11
groygirard 6:fd1bf5563299 12 // Proprietary libraries
groygirard 8:51f6c8f59449 13 #include "Cible.h"
groygirard 6:fd1bf5563299 14 #include "CountDown.h"
llarose 1:601d2922ff06 15 #include "FlexSensor.h"
groygirard 6:fd1bf5563299 16 #include "MMA8452Q.h"
llarose 22:cccb77300fd5 17 #include "Structure.h"
llarose 22:cccb77300fd5 18 #include "Xbee.h"
groygirard 14:bda91cead7f2 19
llarose 1:601d2922ff06 20 #define GO 0x01
Thierry19 50:48e8da5fc1ae 21 #define ACTIVATE_ACCEL 1
groygirard 14:bda91cead7f2 22
groygirard 12:ebb08773dbdb 23 Serial m_pc(USBTX, USBRX);
llarose 22:cccb77300fd5 24 LocalFileSystem local("local"); // file system for config.txt
groygirard 14:bda91cead7f2 25
groygirard 6:fd1bf5563299 26 //PROTOTYPES DE FONCTION
llarose 1:601d2922ff06 27 void gunner(void const* args);
llarose 1:601d2922ff06 28 void rps(void const* args);
llarose 1:601d2922ff06 29 void airGuitar(void const* args);
llarose 17:1a634bb615f3 30 void flex(void const* args);
groygirard 15:dc18545822b3 31 void analyze_sensor_data(void const* args);
groygirard 26:5700cde2350b 32 void configure_GUNNER();
groygirard 26:5700cde2350b 33 void configure_RPS();
groygirard 26:5700cde2350b 34 void configure_GUITAR();
llarose 22:cccb77300fd5 35 void ReadConfig();
groygirard 25:184915935d68 36 void timer2_init(void);
Thierry19 50:48e8da5fc1ae 37 void GetGameMode(void const* args);
groygirard 14:bda91cead7f2 38
groygirard 12:ebb08773dbdb 39 uint8_t play = 0;
Thierry19 50:48e8da5fc1ae 40 uint8_t windup = 0;
groygirard 14:bda91cead7f2 41
pete1801 40:cf5a55961d8c 42 FlexSensor flexSensors(p18, p19, p20); // flex sensor 1.
groygirard 6:fd1bf5563299 43 Accel accel;
groygirard 25:184915935d68 44 flex_t flex_data;
Thierry19 50:48e8da5fc1ae 45 //RtosTimer *sync;
Thierry19 50:48e8da5fc1ae 46 GameMode_e mode = RPS;
llarose 22:cccb77300fd5 47 short PanId;
pete1801 45:20f5867f15de 48 char GantID;
pete1801 39:558cd5780490 49 Xbee xbee;
groygirard 14:bda91cead7f2 50
groygirard 15:dc18545822b3 51 Thread *threads[3];
groygirard 6:fd1bf5563299 52 Thread* gunner_thread_ptr = NULL; // Possiblement mettre dans un tableau
groygirard 6:fd1bf5563299 53 Thread* rps_thread_ptr = NULL; // avec des position codees
groygirard 6:fd1bf5563299 54 Thread* airguitar_thread_ptr = NULL; // dans des define. guillaume
llarose 17:1a634bb615f3 55 Thread* flex_thread_ptr = NULL; // Lecture des entrées analogiques
pete1801 39:558cd5780490 56 Thread* Thread_Send_Data_To_Fixe = NULL;// Thread pour envoyer les données des sensors au fixe
Thierry19 50:48e8da5fc1ae 57 Thread* xbeeReceive = NULL;
Thierry19 50:48e8da5fc1ae 58 Thread* threadGetGameMode = NULL;
groygirard 14:bda91cead7f2 59
groygirard 25:184915935d68 60 extern "C" void TIMER2_IRQHandler(void)
groygirard 25:184915935d68 61 {
groygirard 25:184915935d68 62 /*Flag du decodage - s'execute a chaque fronts, descendants et montants*/
groygirard 25:184915935d68 63 if ((LPC_TIM2->IR & 0x20) == 0x20) {
groygirard 25:184915935d68 64 LPC_TIM2->IR |= 0x20; // clear Timer2 interrupt register
groygirard 29:9a932d354ae3 65 threads[mode]->signal_set(0x02);
groygirard 25:184915935d68 66 }
groygirard 25:184915935d68 67 }
groygirard 14:bda91cead7f2 68
Thierry19 50:48e8da5fc1ae 69 //Fonction qui part le thread
Thierry19 50:48e8da5fc1ae 70 void startXbeeReceive(void const* args)
Thierry19 50:48e8da5fc1ae 71 {
Thierry19 50:48e8da5fc1ae 72 xbee.Recevoir();
Thierry19 50:48e8da5fc1ae 73 }
groygirard 14:bda91cead7f2 74
Thierry19 50:48e8da5fc1ae 75 int main(void const* args)
llarose 1:601d2922ff06 76 {
pete1801 30:389d09853cd1 77 m_pc.printf("\r\n==== PROGRAM START MOBILE ====\r\n");
groygirard 6:fd1bf5563299 78 // Initializing the accelerometer
groygirard 26:5700cde2350b 79
llarose 22:cccb77300fd5 80 ReadConfig(); //read config file
pete1801 30:389d09853cd1 81 m_pc.printf("PANID %x\r\n", PanId);
pete1801 39:558cd5780490 82 xbee = Xbee(PanId, p13, p14); //set PAN ID
Thierry19 50:48e8da5fc1ae 83 xbeeReceive = new Thread(startXbeeReceive);
Thierry19 50:48e8da5fc1ae 84 threadGetGameMode = new Thread(GetGameMode);
groygirard 26:5700cde2350b 85
groygirard 26:5700cde2350b 86 #if ACTIVATE_ACCEL
groygirard 6:fd1bf5563299 87 accel = Accel();
groygirard 6:fd1bf5563299 88 accel.init_MMA8452();
groygirard 25:184915935d68 89 timer2_init();
groygirard 26:5700cde2350b 90 #endif
groygirard 15:dc18545822b3 91
Thierry19 50:48e8da5fc1ae 92 gunner_thread_ptr = new Thread(gunner);
Thierry19 50:48e8da5fc1ae 93 flex_thread_ptr = new Thread(flex);
Thierry19 50:48e8da5fc1ae 94 rps_thread_ptr = new Thread(rps);
Thierry19 50:48e8da5fc1ae 95 airguitar_thread_ptr = new Thread(airGuitar);
Thierry19 50:48e8da5fc1ae 96
groygirard 29:9a932d354ae3 97 threads[GUNNER] = gunner_thread_ptr;
groygirard 29:9a932d354ae3 98 threads[RPS] = rps_thread_ptr;
groygirard 29:9a932d354ae3 99 threads[AirGuitar] = airguitar_thread_ptr;
groygirard 29:9a932d354ae3 100
Thierry19 50:48e8da5fc1ae 101 while(true) {
Thierry19 50:48e8da5fc1ae 102 }
groygirard 14:bda91cead7f2 103
llarose 1:601d2922ff06 104 }
groygirard 14:bda91cead7f2 105
groygirard 25:184915935d68 106 void timer2_init(void)
groygirard 12:ebb08773dbdb 107 {
groygirard 25:184915935d68 108 LPC_PINCON->PINSEL0 |= 0xc00; // set P0.5 to CAP2.1
groygirard 25:184915935d68 109 LPC_SC->PCONP |= (1 << 22); // Timer2 power on
groygirard 25:184915935d68 110 LPC_SC->PCLKSEL1 |= (1 << 12); // Divide CCLK by 1 for Timer2
groygirard 25:184915935d68 111 LPC_TIM2->CCR |= 0x30; // set cap2.1 rising-edge/falling-edge and interrupt
groygirard 25:184915935d68 112 LPC_TIM2->TCR |= (1 << 0); // start Timer2
groygirard 25:184915935d68 113 LPC_TIM2->EMR = 0x20; //
groygirard 25:184915935d68 114 LPC_TIM2->IR |= 0xFFFFFFFF;
groygirard 25:184915935d68 115 NVIC_EnableIRQ(TIMER2_IRQn);
llarose 1:601d2922ff06 116 }
groygirard 14:bda91cead7f2 117
llarose 17:1a634bb615f3 118 void flex(void const* args)
llarose 17:1a634bb615f3 119 {
llarose 17:1a634bb615f3 120 flexSensors.Run();
llarose 17:1a634bb615f3 121 }
llarose 17:1a634bb615f3 122
pete1801 46:5fdd45fef08d 123 void GetGameMode(void const* args)
pete1801 46:5fdd45fef08d 124 {
pete1801 46:5fdd45fef08d 125 while (true) {
Thierry19 44:0862bc49ffa7 126 // attente et lecture d'un événement digital
Thierry19 50:48e8da5fc1ae 127 osEvent evtD = xbee.mailbox_TypeDeJeu.get(25);
Thierry19 44:0862bc49ffa7 128 if (evtD.status == osEventMail) {
Thierry19 44:0862bc49ffa7 129 Fixe_Vers_Mobile *mail = (Fixe_Vers_Mobile*)evtD.value.p;
Thierry19 44:0862bc49ffa7 130 mode = mail->game;
Thierry19 50:48e8da5fc1ae 131 m_pc.printf("Mode \r\n %c", mode);
Thierry19 44:0862bc49ffa7 132 // Envoyer la structure
Thierry19 44:0862bc49ffa7 133 xbee.mailbox_TypeDeJeu.free(mail);
Thierry19 50:48e8da5fc1ae 134 switch(mode) {
Thierry19 50:48e8da5fc1ae 135 case 0:
Thierry19 50:48e8da5fc1ae 136 m_pc.printf("Led Gunner Start! \n\r");
Thierry19 50:48e8da5fc1ae 137 configure_GUNNER();
Thierry19 50:48e8da5fc1ae 138 break;
Thierry19 50:48e8da5fc1ae 139 case 1:
Thierry19 50:48e8da5fc1ae 140 m_pc.printf("RPS Start! \n\r");
Thierry19 50:48e8da5fc1ae 141 configure_RPS();
Thierry19 50:48e8da5fc1ae 142 break;
Thierry19 50:48e8da5fc1ae 143 case 2:
Thierry19 50:48e8da5fc1ae 144 m_pc.printf("Air Guitar Start! \n\r");
Thierry19 50:48e8da5fc1ae 145 configure_GUITAR();
Thierry19 50:48e8da5fc1ae 146 break;
Thierry19 50:48e8da5fc1ae 147 default:
Thierry19 50:48e8da5fc1ae 148 break;
Thierry19 50:48e8da5fc1ae 149 }
Thierry19 50:48e8da5fc1ae 150 m_pc.printf("\n\r Mode recu: %x", mode);
Thierry19 44:0862bc49ffa7 151 }
Thierry19 44:0862bc49ffa7 152 }
Thierry19 44:0862bc49ffa7 153 }
Thierry19 44:0862bc49ffa7 154
groygirard 26:5700cde2350b 155
llarose 1:601d2922ff06 156 void gunner(void const* args)
groygirard 12:ebb08773dbdb 157 {
groygirard 12:ebb08773dbdb 158 // local variables
Thierry19 50:48e8da5fc1ae 159 while(mode == GUNNER) {
Thierry19 50:48e8da5fc1ae 160 m_pc.printf("Gunner thread");
groygirard 29:9a932d354ae3 161 Thread::signal_wait(0x02);
groygirard 29:9a932d354ae3 162 flex_data = flexSensors.get_flex_values();
pete1801 45:20f5867f15de 163 while(flex_data.index != 0) {
pete1801 45:20f5867f15de 164 wait(10);
pete1801 45:20f5867f15de 165 flex_data = flexSensors.get_flex_values();
pete1801 45:20f5867f15de 166 }
Thierry19 50:48e8da5fc1ae 167 Mobile_Vers_Fixe *mail = new Mobile_Vers_Fixe;
groygirard 33:ab4ff35d27ea 168 mail->flexSensor = flex_data;
pete1801 45:20f5867f15de 169 mail->gants = GantID;
groygirard 33:ab4ff35d27ea 170 mail->accelData.x = 0x00;
groygirard 33:ab4ff35d27ea 171 mail->accelData.y = 0x00;
groygirard 33:ab4ff35d27ea 172 mail->accelData.z = 0x01;
Thierry19 50:48e8da5fc1ae 173 xbee.EnvoyerStructure(mail);
llarose 1:601d2922ff06 174 }
llarose 1:601d2922ff06 175 }
groygirard 14:bda91cead7f2 176
groygirard 26:5700cde2350b 177 void configure_GUNNER()
llarose 17:1a634bb615f3 178 {
groygirard 29:9a932d354ae3 179 accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); // z plan transient motion detection
llarose 17:1a634bb615f3 180 }
llarose 17:1a634bb615f3 181
llarose 1:601d2922ff06 182 void rps(void const* args)
llarose 1:601d2922ff06 183 {
Thierry19 50:48e8da5fc1ae 184 Mobile_Vers_Fixe *mailGauche = new Mobile_Vers_Fixe;
Thierry19 50:48e8da5fc1ae 185 mailGauche->flexSensor.index = true;
Thierry19 50:48e8da5fc1ae 186 mailGauche->flexSensor.annulaire = true;
Thierry19 50:48e8da5fc1ae 187 mailGauche->flexSensor.majeur = true;
Thierry19 50:48e8da5fc1ae 188 mailGauche->gants = 'G'; // Gauche ou Droit
Thierry19 50:48e8da5fc1ae 189 mailGauche->accelData.x = 0x01;
Thierry19 50:48e8da5fc1ae 190 mailGauche->accelData.y = 0x00;
Thierry19 50:48e8da5fc1ae 191 mailGauche->accelData.z = 0x01;
Thierry19 50:48e8da5fc1ae 192
Thierry19 50:48e8da5fc1ae 193 /*
Thierry19 50:48e8da5fc1ae 194 while (1) {
Thierry19 50:48e8da5fc1ae 195 m_pc.printf("Sending test \r\n");
Thierry19 50:48e8da5fc1ae 196 wait(1);
Thierry19 50:48e8da5fc1ae 197 xbee.EnvoyerStructure(mailGauche);
Thierry19 50:48e8da5fc1ae 198 //mailbox_Mobile_Vers_Fixe.put(mail);
Thierry19 50:48e8da5fc1ae 199 }
Thierry19 50:48e8da5fc1ae 200 */
groygirard 6:fd1bf5563299 201 // local variables
Thierry19 50:48e8da5fc1ae 202
Thierry19 50:48e8da5fc1ae 203 while(mode == RPS) {
groygirard 25:184915935d68 204 Thread::signal_wait(0x02);
groygirard 25:184915935d68 205 windup++;
groygirard 25:184915935d68 206 m_pc.printf("Decide in : %d \n\r", windup);
groygirard 26:5700cde2350b 207 if(windup >= 3) {
groygirard 25:184915935d68 208 windup = 0;
groygirard 25:184915935d68 209 Thread::wait(500);
Thierry19 50:48e8da5fc1ae 210 //flex_data = flexSensors.get_flex_values();
Thierry19 50:48e8da5fc1ae 211 /*Mobile_Vers_Fixe *mail = new Mobile_Vers_Fixe;
Thierry19 50:48e8da5fc1ae 212 mail->flexSensor.index = false;
Thierry19 50:48e8da5fc1ae 213 mail->flexSensor.annulaire = false;
Thierry19 50:48e8da5fc1ae 214 mail->flexSensor.majeur = false;
Thierry19 50:48e8da5fc1ae 215 mail->gants = 'D'; // Gauche ou Droit
groygirard 33:ab4ff35d27ea 216 mail->accelData.x = 0x01;
groygirard 33:ab4ff35d27ea 217 mail->accelData.y = 0x00;
Thierry19 50:48e8da5fc1ae 218 mail->accelData.z = 0x00;*/
Thierry19 50:48e8da5fc1ae 219 m_pc.printf("Envoie de mail");
Thierry19 50:48e8da5fc1ae 220
Thierry19 50:48e8da5fc1ae 221 xbee.EnvoyerStructure(mailGauche);
groygirard 25:184915935d68 222 // send data frame to the fixed mbed for analyze
groygirard 25:184915935d68 223 }
groygirard 25:184915935d68 224 accel.clear_TRANSIENT_INTERRUPT();
llarose 1:601d2922ff06 225 }
llarose 1:601d2922ff06 226 }
groygirard 14:bda91cead7f2 227
pete1801 45:20f5867f15de 228
groygirard 28:6f9c5af9e272 229
groygirard 26:5700cde2350b 230 void configure_RPS()
groygirard 26:5700cde2350b 231 {
groygirard 26:5700cde2350b 232 accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08);
llarose 1:601d2922ff06 233 }
groygirard 14:bda91cead7f2 234
llarose 1:601d2922ff06 235 void airGuitar(void const* args)
llarose 1:601d2922ff06 236 {
groygirard 6:fd1bf5563299 237 // local variables
groygirard 14:bda91cead7f2 238
groygirard 6:fd1bf5563299 239 while(true) {
llarose 1:601d2922ff06 240 Thread::signal_wait(GO);
llarose 1:601d2922ff06 241 // code...
llarose 1:601d2922ff06 242 }
llarose 1:601d2922ff06 243 }
groygirard 14:bda91cead7f2 244
groygirard 26:5700cde2350b 245 void configure_GUITAR()
groygirard 15:dc18545822b3 246 {
Thierry19 50:48e8da5fc1ae 247 accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); // z plan transient motion detection
groygirard 15:dc18545822b3 248 }
llarose 22:cccb77300fd5 249
llarose 22:cccb77300fd5 250 //read config file
llarose 22:cccb77300fd5 251 void ReadConfig()
llarose 22:cccb77300fd5 252 {
llarose 22:cccb77300fd5 253 FILE* file = fopen("/local/config.txt","r");
groygirard 26:5700cde2350b 254 if (file != NULL) {
llarose 22:cccb77300fd5 255 char buffer[2];
pete1801 45:20f5867f15de 256
llarose 22:cccb77300fd5 257 fscanf(file, "%x", &buffer); //panID = 2 char
llarose 22:cccb77300fd5 258 PanId = buffer[1] << 8 | buffer[0]; //set PAN ID global variable
Thierry19 50:48e8da5fc1ae 259
Thierry19 50:48e8da5fc1ae 260 fscanf(file, "%x", &buffer);
pete1801 47:f372ca93d6c1 261 GantID = buffer[0];
Thierry19 50:48e8da5fc1ae 262
llarose 22:cccb77300fd5 263 fclose(file); //close file
groygirard 26:5700cde2350b 264 } else { //if file is not found
llarose 22:cccb77300fd5 265 m_pc.printf("ERROR AT CONFIG FILE \r\n");
llarose 22:cccb77300fd5 266 }
groygirard 27:0c0dfdf8d953 267 }