|
@@ -26,6 +26,8 @@
|
26
|
26
|
#define PRIORITY_TRECEIVEFROMMON 25
|
27
|
27
|
#define PRIORITY_TSTARTROBOT 20
|
28
|
28
|
#define PRIORITY_TCAMERA 21
|
|
29
|
+#define PRIORITY_TWATCHDOG 35
|
|
30
|
+#define PRIORITY_TBATTERY 20
|
29
|
31
|
|
30
|
32
|
/*
|
31
|
33
|
* Some remarks:
|
|
@@ -135,7 +137,7 @@ void Tasks::Init() {
|
135
|
137
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
136
|
138
|
exit(EXIT_FAILURE);
|
137
|
139
|
}
|
138
|
|
- if (err = rt_task_create(&th_battery, "th_battery", 0, PRIORITY_TMOVE, 0)) {
|
|
140
|
+ if (err = rt_task_create(&th_battery, "th_battery", 0, PRIORITY_TBATTERY, 0)) {
|
139
|
141
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
140
|
142
|
exit(EXIT_FAILURE);
|
141
|
143
|
}
|
|
@@ -143,15 +145,15 @@ void Tasks::Init() {
|
143
|
145
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
144
|
146
|
exit(EXIT_FAILURE);
|
145
|
147
|
}
|
146
|
|
- if (err = rt_task_create(&th_watchdog, "th_watchdog", 0, PRIORITY_TMOVE, 0)) {
|
|
148
|
+ if (err = rt_task_create(&th_watchdog, "th_watchdog", 0, PRIORITY_TWATCHDOG, 0)) {
|
147
|
149
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
148
|
150
|
exit(EXIT_FAILURE);
|
149
|
151
|
}
|
150
|
|
- if (err = rt_task_create(&th_camera, "th_camera", 0, PRIORITY_TMOVE, 0)) {
|
|
152
|
+ if (err = rt_task_create(&th_camera, "th_camera", 0, PRIORITY_TCAMERA, 0)) {
|
151
|
153
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
152
|
154
|
exit(EXIT_FAILURE);
|
153
|
155
|
}
|
154
|
|
- if (err = rt_task_create(&th_startCamera, "th_startCamera", 0, PRIORITY_TMOVE, 0)) {
|
|
156
|
+ if (err = rt_task_create(&th_startCamera, "th_startCamera", 0, PRIORITY_TCAMERA, 0)) {
|
155
|
157
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
156
|
158
|
exit(EXIT_FAILURE);
|
157
|
159
|
}
|
|
@@ -284,6 +286,7 @@ void Tasks::SendToMonTask(void* arg) {
|
284
|
286
|
*/
|
285
|
287
|
void Tasks::ReceiveFromMonTask(void *arg) {
|
286
|
288
|
Message *msgRcv;
|
|
289
|
+ int err;
|
287
|
290
|
|
288
|
291
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
289
|
292
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
@@ -292,31 +295,70 @@ void Tasks::ReceiveFromMonTask(void *arg) {
|
292
|
295
|
/**************************************************************************************/
|
293
|
296
|
/* The task receiveFromMon starts here */
|
294
|
297
|
/**************************************************************************************/
|
295
|
|
- rt_sem_p(&sem_serverOk, TM_INFINITE);
|
296
|
|
- cout << "Received message from monitor activated" << endl << flush;
|
|
298
|
+ while(1){
|
|
299
|
+ rt_sem_p(&sem_serverOk, TM_INFINITE);
|
|
300
|
+ cout << "Received message from monitor activated" << endl << flush;
|
|
301
|
+ while (1) {
|
|
302
|
+ msgRcv = monitor.Read();
|
|
303
|
+ cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
|
297
|
304
|
|
298
|
|
- while (1) {
|
299
|
|
- msgRcv = monitor.Read();
|
300
|
|
- cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
|
301
|
|
-
|
302
|
|
- if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
|
303
|
|
- delete(msgRcv);
|
304
|
|
- exit(-1);
|
305
|
|
- } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
|
306
|
|
- rt_sem_v(&sem_openComRobot);
|
307
|
|
- } else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
|
308
|
|
- rt_sem_v(&sem_startRobot);
|
309
|
|
- } else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
|
310
|
|
- msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
|
311
|
|
- msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
|
312
|
|
- msgRcv->CompareID(MESSAGE_ROBOT_GO_RIGHT) ||
|
313
|
|
- msgRcv->CompareID(MESSAGE_ROBOT_STOP)) {
|
314
|
|
-
|
315
|
|
- rt_mutex_acquire(&mutex_move, TM_INFINITE);
|
316
|
|
- move = msgRcv->GetID();
|
317
|
|
- rt_mutex_release(&mutex_move);
|
|
305
|
+ if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
|
|
306
|
+ cout << "On est dans le message monitor lost" << endl << flush;
|
|
307
|
+
|
|
308
|
+ rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
309
|
+ int cpMove = MESSAGE_ROBOT_STOP;
|
|
310
|
+ robot.Write(new Message((MessageID)cpMove));
|
|
311
|
+ rt_mutex_release(&mutex_robot);
|
|
312
|
+ robot.Close();
|
|
313
|
+ monitor.Close();
|
|
314
|
+ rt_mutex_acquire(&mutex_move, TM_INFINITE);
|
|
315
|
+ move = MESSAGE_ROBOT_STOP;
|
|
316
|
+ rt_mutex_release(&mutex_move);
|
|
317
|
+ rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
318
|
+ robotStarted = 0;
|
|
319
|
+ rt_mutex_release(&mutex_robotStarted);
|
|
320
|
+ rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
|
|
321
|
+ watchdog = 0;
|
|
322
|
+ rt_mutex_release(&mutex_watchdog);
|
|
323
|
+ //Close Camrera + etatCamera
|
|
324
|
+ /* if (err = rt_task_delete(&th_server)) {
|
|
325
|
+ cerr << "Error task delete: " << strerror(-err) << endl << flush;
|
|
326
|
+ //exit(EXIT_FAILURE);
|
|
327
|
+ } */
|
|
328
|
+ if (err = rt_task_create(&th_server, "th_server", 0, PRIORITY_TSERVER, 0)) {
|
|
329
|
+ cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
330
|
+ exit(EXIT_FAILURE);
|
|
331
|
+ }
|
|
332
|
+ if (err = rt_task_start(&th_server, (void(*)(void*)) & Tasks::ServerTask, this)) {
|
|
333
|
+ cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
334
|
+ exit(EXIT_FAILURE);
|
|
335
|
+ }
|
|
336
|
+ rt_sem_v(&sem_barrier);
|
|
337
|
+ Kill_StartWD();
|
|
338
|
+ break;
|
|
339
|
+ } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
|
|
340
|
+ rt_sem_v(&sem_openComRobot);
|
|
341
|
+ } else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
|
|
342
|
+ rt_sem_v(&sem_startRobot);
|
|
343
|
+ } else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
|
|
344
|
+ msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
|
|
345
|
+ msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
|
|
346
|
+ msgRcv->CompareID(MESSAGE_ROBOT_GO_RIGHT) ||
|
|
347
|
+ msgRcv->CompareID(MESSAGE_ROBOT_STOP)) {
|
|
348
|
+ rt_mutex_acquire(&mutex_move, TM_INFINITE);
|
|
349
|
+ move = msgRcv->GetID();
|
|
350
|
+ rt_mutex_release(&mutex_move);
|
|
351
|
+ cout << "On est dans le message mouvement" << endl << flush;
|
|
352
|
+ }
|
|
353
|
+ else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITH_WD)){
|
|
354
|
+ rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
|
|
355
|
+ watchdog = 1;
|
|
356
|
+ rt_mutex_release(&mutex_watchdog);
|
|
357
|
+ rt_sem_v(&sem_startRobot);
|
|
358
|
+ cout << "On a start le robot avec Watchdog" << endl << flush;
|
|
359
|
+ }
|
|
360
|
+ delete(msgRcv); // mus be deleted manually, no consumer
|
318
|
361
|
}
|
319
|
|
- delete(msgRcv); // mus be deleted manually, no consumer
|
320
|
362
|
}
|
321
|
363
|
}
|
322
|
364
|
|
|
@@ -357,6 +399,7 @@ void Tasks::OpenComRobot(void *arg) {
|
357
|
399
|
* @brief Thread starting the communication with the robot.
|
358
|
400
|
*/
|
359
|
401
|
void Tasks::StartRobotTask(void *arg) {
|
|
402
|
+ int etatWD;
|
360
|
403
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
361
|
404
|
// Synchronization barrier (waiting that all tasks are starting)
|
362
|
405
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
@@ -365,23 +408,47 @@ void Tasks::StartRobotTask(void *arg) {
|
365
|
408
|
/* The task startRobot starts here */
|
366
|
409
|
/**************************************************************************************/
|
367
|
410
|
while (1) {
|
368
|
|
-
|
|
411
|
+
|
369
|
412
|
Message * msgSend;
|
370
|
413
|
rt_sem_p(&sem_startRobot, TM_INFINITE);
|
371
|
|
- cout << "Start robot without watchdog (";
|
|
414
|
+ cout << "Start robot (";
|
372
|
415
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
373
|
|
- msgSend = robot.Write(robot.StartWithoutWD());
|
374
|
|
- rt_mutex_release(&mutex_robot);
|
375
|
|
- cout << msgSend->GetID();
|
376
|
|
- cout << ")" << endl;
|
|
416
|
+ rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
|
|
417
|
+ etatWD = watchdog;
|
|
418
|
+ rt_mutex_release(&mutex_watchdog);
|
|
419
|
+ cout << "Watchdog = " << etatWD << endl << flush;
|
|
420
|
+ if (etatWD == 0){
|
|
421
|
+ cout << "Dans le if without WD" << etatWD << endl << flush;
|
|
422
|
+ msgSend = robot.Write(robot.StartWithoutWD());
|
|
423
|
+ rt_mutex_release(&mutex_robot);
|
|
424
|
+ cout << msgSend->GetID();
|
|
425
|
+ cout << ")" << endl;
|
377
|
426
|
|
378
|
|
- cout << "Movement answer: " << msgSend->ToString() << endl << flush;
|
379
|
|
- WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
427
|
+ cout << "Movement answer: " << msgSend->ToString() << endl << flush;
|
|
428
|
+ WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
380
|
429
|
|
381
|
|
- if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
|
382
|
|
- rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
383
|
|
- robotStarted = 1;
|
384
|
|
- rt_mutex_release(&mutex_robotStarted);
|
|
430
|
+ if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
|
|
431
|
+ rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
432
|
+ robotStarted = 1;
|
|
433
|
+ rt_mutex_release(&mutex_robotStarted);
|
|
434
|
+ }
|
|
435
|
+ }
|
|
436
|
+ else{
|
|
437
|
+ cout << "Dans le if with WD" << etatWD << endl << flush;
|
|
438
|
+ msgSend = robot.Write(robot.StartWithWD());
|
|
439
|
+ rt_mutex_release(&mutex_robot);
|
|
440
|
+ cout << msgSend->GetID();
|
|
441
|
+ cout << ")" << endl;
|
|
442
|
+
|
|
443
|
+ cout << "Movement answer: " << msgSend->ToString() << endl << flush;
|
|
444
|
+ WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
445
|
+
|
|
446
|
+ if (msgSend->GetID() == MESSAGE_ANSWER_ACK) {
|
|
447
|
+ rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
448
|
+ robotStarted = 1;
|
|
449
|
+ rt_mutex_release(&mutex_robotStarted);
|
|
450
|
+ rt_sem_v(&sem_startWD);
|
|
451
|
+ }
|
385
|
452
|
}
|
386
|
453
|
}
|
387
|
454
|
}
|
|
@@ -393,7 +460,10 @@ void Tasks::MoveTask(void *arg) {
|
393
|
460
|
int rs;
|
394
|
461
|
int cpMove;
|
395
|
462
|
int previousMove = -1;
|
|
463
|
+ int cpt = 0;
|
|
464
|
+
|
396
|
465
|
|
|
466
|
+ Message * response;
|
397
|
467
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
398
|
468
|
// Synchronization barrier (waiting that all tasks are starting)
|
399
|
469
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
@@ -418,8 +488,27 @@ void Tasks::MoveTask(void *arg) {
|
418
|
488
|
cout << " move: " << cpMove;
|
419
|
489
|
|
420
|
490
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
421
|
|
- robot.Write(new Message((MessageID)cpMove));
|
|
491
|
+ response = robot.Write(new Message((MessageID)cpMove));
|
422
|
492
|
rt_mutex_release(&mutex_robot);
|
|
493
|
+
|
|
494
|
+ if (response->CompareID(MESSAGE_ANSWER_ACK)){
|
|
495
|
+ cpt = 0;
|
|
496
|
+ }
|
|
497
|
+ else{
|
|
498
|
+ cpt++;
|
|
499
|
+ if (cpt == 3){
|
|
500
|
+ Message * respToMon = new Message(MESSAGE_ANSWER_COM_ERROR);
|
|
501
|
+ WriteInQueue(&q_messageToMon,respToMon);
|
|
502
|
+ rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
503
|
+ robotStarted = 0;
|
|
504
|
+ rt_mutex_release(&mutex_robotStarted);
|
|
505
|
+ rt_mutex_acquire(&mutex_watchdog, TM_INFINITE);
|
|
506
|
+ watchdog = 0;
|
|
507
|
+ rt_mutex_release(&mutex_watchdog);
|
|
508
|
+ Kill_StartWD();
|
|
509
|
+ cpt = 0;
|
|
510
|
+ }
|
|
511
|
+ }
|
423
|
512
|
previousMove = cpMove;
|
424
|
513
|
}
|
425
|
514
|
}
|
|
@@ -428,6 +517,26 @@ void Tasks::MoveTask(void *arg) {
|
428
|
517
|
}
|
429
|
518
|
|
430
|
519
|
/**
|
|
520
|
+ * @brief Kills and restart the thread watchdog
|
|
521
|
+ */
|
|
522
|
+void Tasks::Kill_StartWD(){
|
|
523
|
+ int err;
|
|
524
|
+ if (err = rt_task_delete(&th_watchdog)) {
|
|
525
|
+ cerr << "Error task delete: " << strerror(-err) << endl << flush;
|
|
526
|
+ exit(EXIT_FAILURE);
|
|
527
|
+ }
|
|
528
|
+ if (err = rt_task_create(&th_watchdog, "th_watchdog", 0, PRIORITY_TWATCHDOG, 0)) {
|
|
529
|
+ cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
530
|
+ exit(EXIT_FAILURE);
|
|
531
|
+ }
|
|
532
|
+ if (err = rt_task_start(&th_watchdog, (void(*)(void*)) & Tasks::ReloadWatchdogTask, this)) {
|
|
533
|
+ cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
534
|
+ exit(EXIT_FAILURE);
|
|
535
|
+ }
|
|
536
|
+ rt_sem_broadcast(&sem_barrier);
|
|
537
|
+}
|
|
538
|
+
|
|
539
|
+/**
|
431
|
540
|
* Write a message in a given queue
|
432
|
541
|
* @param queue Queue identifier
|
433
|
542
|
* @param msg Message to be stored
|
|
@@ -504,11 +613,11 @@ void Tasks::ReloadWatchdogTask(void *arg) {
|
504
|
613
|
|
505
|
614
|
//We wait for the signal startWd
|
506
|
615
|
rt_sem_p(&sem_startWD, TM_INFINITE);
|
507
|
|
-
|
|
616
|
+ cout << "thread WD started" << endl << flush;
|
508
|
617
|
/**************************************************************************************/
|
509
|
618
|
/* The task starts here */
|
510
|
619
|
/**************************************************************************************/
|
511
|
|
- rt_task_set_periodic(NULL, TM_NOW, 50000000);
|
|
620
|
+ rt_task_set_periodic(NULL, TM_NOW, 1000000000);
|
512
|
621
|
|
513
|
622
|
while(1){
|
514
|
623
|
rt_task_wait_period(NULL);
|
|
@@ -519,7 +628,7 @@ void Tasks::ReloadWatchdogTask(void *arg) {
|
519
|
628
|
cout << "Periodic watchdog reload" << endl << flush;
|
520
|
629
|
|
521
|
630
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
522
|
|
- responseBattery = robot.Write(new Message(MESSAGE_ROBOT_RELOAD_WD));
|
|
631
|
+ robot.Write(new Message(MESSAGE_ROBOT_RELOAD_WD));
|
523
|
632
|
rt_mutex_release(&mutex_robot);
|
524
|
633
|
}
|
525
|
634
|
}
|