diff --git a/software/raspberry/superviseur-robot/tasks.cpp b/software/raspberry/superviseur-robot/tasks.cpp index 3be95f9..d2c617b 100644 --- a/software/raspberry/superviseur-robot/tasks.cpp +++ b/software/raspberry/superviseur-robot/tasks.cpp @@ -75,7 +75,10 @@ void Tasks::Init() { cerr << "Error mutex create: " << strerror(-err) << endl << flush; exit(EXIT_FAILURE); } - + /*if (err = rt_mutex_create(&mutex_robot_on, NULL)) { + cerr << "Error mutex create: " << strerror(-err) << endl << flush; + exit(EXIT_FAILURE); + }*/ cout << "Mutexes created successfully" << endl << flush; @@ -279,6 +282,8 @@ void Tasks::ReceiveFromMonTask(void *arg) { if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) { delete(msgRcv); + cout << "Connection to monitor lost" << endl; + monitor.Close(); //exit(-1); } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) { @@ -344,39 +349,44 @@ void Tasks::StartRobotTaskWithoutWatchdog(void *arg) { cout << "Start " << __PRETTY_FUNCTION__ << endl << flush; // Synchronization barrier (waiting that all tasks are starting) rt_sem_p(&sem_barrier, TM_INFINITE); - + int killBattery=0; /**************************************************************************************/ /* The task startRobot starts here */ /**************************************************************************************/ - while (1) { - Message* p_mess_answer_battery; - Message * msgSend; - rt_sem_p(&sem_startRobotWithoutWatchdog, TM_INFINITE); - cout << "Start robot without watchdog ("; - rt_mutex_acquire(&mutex_robot, TM_INFINITE); - msgSend = robot.Write(robot.StartWithoutWD()); - rt_mutex_release(&mutex_robot); - cout << msgSend->GetID(); - cout << ")" << endl; - - cout << "Movement answer: " << msgSend->ToString() << endl << flush; - WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon + Message* p_mess_answer_battery; + Message * msgSend; + rt_sem_p(&sem_startRobotWithoutWatchdog, TM_INFINITE); + cout << "Start robot without watchdog ("; + + //Boolean to get the battery + rt_mutex_acquire(&mutex_kill_battery, TM_INFINITE); + killBatteryBool=0; + rt_mutex_release(&mutex_kill_battery); + rt_mutex_acquire(&mutex_robot, TM_INFINITE); + msgSend = robot.Write(robot.StartWithoutWD()); + rt_mutex_release(&mutex_robot); + cout << msgSend->GetID(); + cout << ")" << endl; + cout << "Movement answer: " << msgSend->ToString() << endl << flush; + WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon - if (msgSend->GetID() == MESSAGE_ANSWER_ACK) { - rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE); - robotStarted = 1; - rt_mutex_release(&mutex_robotStarted); - rt_task_set_periodic(NULL, TM_NOW, 3000000000); - while (1) { - rt_task_wait_period(NULL); - rt_mutex_acquire(&mutex_robot, TM_INFINITE); - p_mess_answer_battery = robot.Write(robot.GetBattery()); - rt_mutex_release(&mutex_robot); - rt_mutex_acquire(&mutex_monitor, TM_INFINITE); - monitor.Write(p_mess_answer_battery); - rt_mutex_release(&mutex_monitor); - cout << endl << flush; - } + if (msgSend->GetID() == MESSAGE_ANSWER_ACK) { + rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE); + robotStarted = 1; + rt_mutex_release(&mutex_robotStarted); + rt_task_set_periodic(NULL, TM_NOW, 500000000); + while (killBattery==0) { + rt_task_wait_period(NULL); + rt_mutex_acquire(&mutex_robot, TM_INFINITE); + p_mess_answer_battery = robot.Write(robot.GetBattery()); + rt_mutex_release(&mutex_robot); + rt_mutex_acquire(&mutex_monitor, TM_INFINITE); + monitor.Write(p_mess_answer_battery); + rt_mutex_release(&mutex_monitor); + //cout << endl << flush; + rt_mutex_acquire(&mutex_kill_battery, TM_INFINITE); + killBattery=killBatteryBool; + rt_mutex_release(&mutex_kill_battery); } } } @@ -396,44 +406,48 @@ void Tasks::StartRobotTaskWithWatchdog(void *arg) { /**************************************************************************************/ /* The task startRobot starts here */ /**************************************************************************************/ - while (1) { - Message* p_mess_answer_battery; - Message * msgSend; - int cpt=1; - int err; - rt_sem_p(&sem_startRobotWithWatchdog, TM_INFINITE); - cout << "Start robot with watchdog ("; - rt_mutex_acquire(&mutex_robot, TM_INFINITE); - msgSend = robot.Write(robot.StartWithWD()); - rt_mutex_release(&mutex_robot); - cout << msgSend->GetID(); - cout << ")" << endl; + Message* p_mess_answer_battery; + Message * msgSend; + int cpt=1; + int err; + rt_sem_p(&sem_startRobotWithWatchdog, TM_INFINITE); + cout << "Start robot with watchdog ("; + rt_mutex_acquire(&mutex_robot, TM_INFINITE); + msgSend = robot.Write(robot.StartWithWD()); + rt_mutex_release(&mutex_robot); + + //boolean to ask battery + rt_mutex_acquire(&mutex_kill_battery, TM_INFINITE); + killBatteryBool=0; + rt_mutex_release(&mutex_kill_battery); + + cout << msgSend->GetID(); + cout << ")" << endl; - cout << "Movement answer: " << msgSend->ToString() << endl << flush; - WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon + cout << "Movement answer: " << msgSend->ToString() << endl << flush; + WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon - if (msgSend->GetID() == MESSAGE_ANSWER_ACK) { + if (msgSend->GetID() == MESSAGE_ANSWER_ACK) { - rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE); - robotStarted = 1; - rt_mutex_release(&mutex_robotStarted); - rt_task_set_periodic(NULL, TM_NOW, 300000000); - while (1) { - cpt++; - rt_task_wait_period(NULL); + rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE); + robotStarted = 1; + rt_mutex_release(&mutex_robotStarted); + rt_task_set_periodic(NULL, TM_NOW, 500000000); + while (1) { + cpt++; + if(cpt%2==0){ rt_mutex_acquire(&mutex_robot, TM_INFINITE); - robot.Write(robot.ReloadWD()); + robot.Write(robot.ReloadWD()); rt_mutex_release(&mutex_robot); - if(cpt%10==0){ - rt_mutex_acquire(&mutex_robot, TM_INFINITE); - p_mess_answer_battery = robot.Write(robot.GetBattery()); - rt_mutex_release(&mutex_robot); - rt_mutex_acquire(&mutex_monitor, TM_INFINITE); - monitor.Write(p_mess_answer_battery); - rt_mutex_release(&mutex_monitor); - cout << endl << flush; - } } + rt_task_wait_period(NULL); + rt_mutex_acquire(&mutex_robot, TM_INFINITE); + p_mess_answer_battery = robot.Write(robot.GetBattery()); + rt_mutex_release(&mutex_robot); + rt_mutex_acquire(&mutex_monitor, TM_INFINITE); + monitor.Write(p_mess_answer_battery); + rt_mutex_release(&mutex_monitor); + cout << endl << flush; } } } diff --git a/software/raspberry/superviseur-robot/tasks.h b/software/raspberry/superviseur-robot/tasks.h index 48ac6dd..609aeda 100644 --- a/software/raspberry/superviseur-robot/tasks.h +++ b/software/raspberry/superviseur-robot/tasks.h @@ -65,6 +65,8 @@ private: ComMonitor monitor; ComRobot robot; int robotStarted = 0; + int killBatteryBool = 0; + int killRobotWithoutWd = 0; int move = MESSAGE_ROBOT_STOP; /**********************************************************************/ @@ -85,6 +87,7 @@ private: RT_MUTEX mutex_robot; RT_MUTEX mutex_robotStarted; RT_MUTEX mutex_move; + RT_MUTEX mutex_kill_battery; /**********************************************************************/ /* Semaphores */