ajout des taches vison et batterie

This commit is contained in:
Romain Vitrat 2020-03-28 15:50:35 +01:00
parent dfe5df3a48
commit 673cd3390c
2 changed files with 183 additions and 194 deletions

View file

@ -24,10 +24,10 @@
#define PRIORITY_TMOVE 20
#define PRIORITY_TSENDTOMON 22
#define PRIORITY_TRECEIVEFROMMON 25
#define PRIORITY_TSTARTROBOTWITHOUTWATCHDOG 22
#define PRIORITY_TSTARTROBOTWITHWATCHDOG 22
#define PRIORITY_TCAMERA 21
#define PRIORITY_TSTARTROBOTWITHOUTWATCHDOG 24
#define PRIORITY_TSTARTROBOTWITHWATCHDOG 23
#define PRIORITY_TCAMERA 21
#define PRIORITY_TVISION 26
/*
* Some remarks:
@ -75,55 +75,30 @@ void Tasks::Init() {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_killReceiveFromMon, NULL)) {
if (err = rt_mutex_create(&mutex_killBattery, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_killVision, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_acquireImage, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_searchArena, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_drawArena, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_getPosition, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_killSendMon, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_killDetectLostSupRob, NULL)) {
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_mutex_create(&mutex_acquireImage, NULL)) {
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;
/**************************************************************************************/
@ -149,16 +124,14 @@ void Tasks::Init() {
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_sem_create(&sem_allowStartReceive, NULL, 0, S_FIFO)) {
if (err = rt_sem_create(&sem_askBattery, NULL, 0, S_FIFO)) {
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
//Initialization for some specific sem: Allow to run the first time
rt_sem_v(&sem_allowStartReceive);
}
if (err = rt_sem_create(&sem_allowStartCaptureImage, NULL, 0, S_FIFO)) {
cerr << "Error semaphore create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
cout << "Semaphores created successfully" << endl << flush;
/**************************************************************************************/
@ -192,7 +165,14 @@ void Tasks::Init() {
cerr << "Error task create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_task_create(&th_askBattery, "th_askBattery", 0, PRIORITY_TMOVE, 0)) {
cerr << "Error task create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
/* if (err = rt_task_create(&th_vision, "th_vision", 0, PRIORITY_TVISION, 0)) {
cerr << "Error task create: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}*/
cout << "Tasks created successfully" << endl << flush;
/**************************************************************************************/
@ -241,7 +221,14 @@ void Tasks::Run() {
cerr << "Error task start: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
if (err = rt_task_start(&th_askBattery, (void(*)(void*)) & Tasks::AskBattery, this)) {
cerr << "Error task start: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}
/* if (err = rt_task_start(&th_vision, (void(*)(void*)) & Tasks::Vision, this)) {
cerr << "Error task start: " << strerror(-err) << endl << flush;
exit(EXIT_FAILURE);
}*/
cout << "Tasks launched" << endl << flush;
}
@ -318,119 +305,30 @@ void Tasks::SendToMonTask(void* arg) {
*/
void Tasks::ReceiveFromMonTask(void *arg) {
Message *msgRcv;
bool killReceiveFromMonOk=0;
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
// Synchronization barrier (waiting that all tasks are starting)
rt_sem_p(&sem_barrier, TM_INFINITE);
//Wait twin task to die if not first round
rt_sem_p(&sem_allowStartReceive, TM_INFINITE);
//Reinitialize control boolean
rt_mutex_acquire(&mutex_killReceiveFromMon, TM_INFINITE);
killReceiveFromMon=0;
rt_mutex_release(&mutex_killReceiveFromMon);
/**************************************************************************************/
/* The task receiveFromMon starts here */
/**************************************************************************************/
rt_sem_p(&sem_serverOk, TM_INFINITE);
cout << "Received message from monitor activated" << endl << flush;
while (!killReceiveFromMonOk) {
while (1) {
msgRcv = monitor.Read();
cout << "Rcv <= " << msgRcv->ToString() << endl << flush;
if (msgRcv->CompareID(MESSAGE_MONITOR_LOST)) {
delete(msgRcv);
cout << "Connection to monitor lost" << endl;
monitor.Close();
rt_mutex_acquire(&mutex_killReceiveFromMon, TM_INFINITE);
killReceiveFromMon=1;
rt_mutex_release(&mutex_killReceiveFromMon);
rt_mutex_acquire(&mutex_killVision, TM_INFINITE);
killVision=1;
rt_mutex_release(&mutex_killVision);
rt_mutex_acquire(&mutex_killSendMon, TM_INFINITE);
killSendMon=1;
rt_mutex_release(&mutex_killSendMon);
rt_mutex_acquire(&mutex_killBattery, TM_INFINITE);
killBattery=1;
rt_mutex_release(&mutex_killBattery);
rt_mutex_acquire(&mutex_killDetectLostSupRob, TM_INFINITE);
killDetectlostSupRob=1;
rt_mutex_release(&mutex_killDetectLostSupRob);
rt_mutex_acquire(&mutex_acquireImage, TM_INFINITE);
acquireImage=0;
rt_mutex_release(&mutex_acquireImage);
//exit(-1);
} else if (msgRcv->CompareID(MESSAGE_CAM_OPEN)) {
cout << "Command Open Camera Received" << endl << flush;
//start task Vision
exit(-1);
} else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
cout << "Command Open Communication with Robot Received" << endl << flush;
rt_sem_v(&sem_openComRobot);
} else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
cout << "Command Start Robot without Watchdog Received" << endl << flush;
rt_sem_v(&sem_startRobotWithoutWatchdog);
} else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITH_WD)) {
cout << "Command Start Robot with Watchdog Received" << endl << flush;
//start task robot with watchdog
} else if (msgRcv->CompareID(MESSAGE_CAM_ASK_ARENA)) {
cout << "Command Search Arena Received" << endl << flush;
rt_mutex_acquire(&mutex_searchArena, TM_INFINITE);
searchArena=1;
rt_mutex_release(&mutex_searchArena);
} else if (msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_START)) {
cout << "Command Get Root Position Received" << endl << flush;
rt_mutex_acquire(&mutex_getPosition, TM_INFINITE);
getPosition=1;
rt_mutex_release(&mutex_getPosition);
} else if (msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_STOP)) {
cout << "Command Stop Getting Robot Position Received" << endl << flush;
rt_mutex_acquire(&mutex_getPosition, TM_INFINITE);
getPosition=0;
rt_mutex_release(&mutex_getPosition);
} else if (msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM)) {
cout << "Command Confirm Arena Received" << endl << flush;
rt_mutex_acquire(&mutex_drawArena, TM_INFINITE);
drawArena=1;
rt_mutex_release(&mutex_drawArena);
rt_mutex_acquire(&mutex_searchArena, TM_INFINITE);
searchArena=0;
rt_mutex_release(&mutex_searchArena);
rt_mutex_acquire(&mutex_acquireImage, TM_INFINITE);
acquireImage=1;
rt_mutex_release(&mutex_acquireImage);
} else if (msgRcv->CompareID(MESSAGE_CAM_ARENA_INFIRM)) {
cout << "Command Infirm Arena Received" << endl << flush;
rt_mutex_acquire(&mutex_searchArena, TM_INFINITE);
searchArena=0;
rt_mutex_release(&mutex_searchArena);
rt_mutex_acquire(&mutex_acquireImage, TM_INFINITE);
acquireImage=1;
rt_mutex_release(&mutex_acquireImage);
rt_sem_v(&sem_startRobotWithWatchdog);
} else if (msgRcv->CompareID(MESSAGE_ROBOT_GO_FORWARD) ||
msgRcv->CompareID(MESSAGE_ROBOT_GO_BACKWARD) ||
msgRcv->CompareID(MESSAGE_ROBOT_GO_LEFT) ||
@ -441,18 +339,13 @@ void Tasks::ReceiveFromMonTask(void *arg) {
move = msgRcv->GetID();
rt_mutex_release(&mutex_move);
}
delete(msgRcv); // must be deleted manually, no consumer
//Update loop condition
rt_mutex_acquire(&mutex_killReceiveFromMon, TM_INFINITE);
killReceiveFromMonOk=killReceiveFromMon;
rt_mutex_release(&mutex_killReceiveFromMon);
delete(msgRcv); // mus be deleted manually, no consumer
}
rt_sem_v(&sem_allowStartReceive);
}
/**
* @brief Thread opening communication with the robot.
*/
@ -486,29 +379,29 @@ void Tasks::OpenComRobot(void *arg) {
}
}
/**
* @brief Thread starting the communication with the robot.
*//**
* @brief Thread starting the communication with the robot.
*/
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;
int killBatteryOk=0;
Message * msgSend;
/**************************************************************************************/
/* The task startRobot starts here */
/**************************************************************************************/
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_killBattery, TM_INFINITE);
killBattery=0;
killBatteryOk=0;
rt_mutex_release(&mutex_killBattery);
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);
@ -522,86 +415,68 @@ void Tasks::StartRobotTaskWithoutWatchdog(void *arg) {
robotStarted = 1;
rt_mutex_release(&mutex_robotStarted);
rt_task_set_periodic(NULL, TM_NOW, 500000000);
while (killBattery==0) {
while (killBatteryOk==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_sem_v(&sem_askBattery);
rt_mutex_acquire(&mutex_killBattery, TM_INFINITE);
killBattery=killBattery;
killBatteryOk=killBattery;
rt_mutex_release(&mutex_killBattery);
}
}
}
/**
* @brief Thread starting the communication with the robot.
*//**
* @brief Thread starting the communication with the robot.
*/
void Tasks::StartRobotTaskWithWatchdog(void *arg) {
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
// Synchronization barrier (waiting that all tasks are starting)
rt_sem_p(&sem_barrier, TM_INFINITE);
int killBatteryOk=0;
int cpt=1;
Message * msgSend;
/**************************************************************************************/
/* The task startRobot starts here */
/**************************************************************************************/
Message* p_mess_answer_battery;
Message * msgSend;
int cpt=1;
int err;
//Boolean to get the battery
rt_mutex_acquire(&mutex_killBattery, TM_INFINITE);
killBatteryOk=0;
rt_mutex_release(&mutex_killBattery);
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_killBattery, TM_INFINITE);
killBattery=0;
rt_mutex_release(&mutex_killBattery);
cout << msgSend->GetID();
cout << ")" << endl;
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, 500000000);
while (1) {
while (killBatteryOk==0) {
cpt++;
if(cpt%2==0){
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
if(cpt==2){
robot.Write(robot.ReloadWD());
rt_mutex_release(&mutex_robot);
cpt=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_sem_v(&sem_askBattery);
rt_mutex_acquire(&mutex_killBattery, TM_INFINITE);
killBatteryOk=killBattery;
rt_mutex_release(&mutex_killBattery);
}
}
}
/**
* @brief Thread handling control of the robot.
*/
@ -640,6 +515,112 @@ void Tasks::MoveTask(void *arg) {
}
void Tasks::AskBattery(void *arg){
Message* p_mess_answer_battery;
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
// Synchronization barrier (waiting that all tasks are starting)
rt_sem_p(&sem_barrier, TM_INFINITE);
/**************************************************************************************/
/* The task starts here */
/**************************************************************************************/
while(1){
rt_sem_p(&sem_askBattery, TM_INFINITE);
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
p_mess_answer_battery = robot.Write(robot.GetBattery());
rt_mutex_release(&mutex_robot);
WriteInQueue(&q_messageToMon, p_mess_answer_battery);
cout << endl << flush;
}
}
//This task works on a difficult principle that fully make it controllable through
// monitor, accessing booleans variables to set getPosition or searchArena
void Tasks::Vision(void *arg){
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
// Synchronization barrier (waiting that all tasks are starting)
rt_sem_p(&sem_barrier, TM_INFINITE);
rt_sem_p(&sem_askBattery, TM_INFINITE);
int killVisionOk=0;
Camera camera;
int acquireImageOk=1;
int searchArenaOk=0;
int drawArenaOk=0;
int getPositionOk=0;
Arena arena;
list<Position> positionList;
list<Position>::iterator it;
string strPos;
MessagePosition* msgPos;
Message* msgPosMsg;
MessageImg* msgImg;
Message* msgImgMsg;
msgPos->SetID(MESSAGE_CAM_POSITION);
msgImg->SetID(MESSAGE_CAM_IMAGE);
rt_mutex_acquire(&mutex_killVision, TM_INFINITE);
killVision=0;
rt_mutex_release(&mutex_killVision);
camera.Open();
while(killVisionOk==0){
while(acquireImageOk==1){
Img img=camera.Grab();
if(searchArenaOk==1){
rt_mutex_acquire(&mutex_acquireImage, TM_INFINITE);
acquireImage=0;
rt_mutex_release(&mutex_acquireImage);
acquireImageOk=acquireImage;
arena=img.SearchArena();
img.DrawArena(arena);
}
if(drawArenaOk==1){
// img.DrawArena(arena);
rt_mutex_acquire(&mutex_acquireImage, TM_INFINITE);
acquireImage=1;
rt_mutex_release(&mutex_acquireImage);
acquireImageOk=acquireImage;
}
if(getPositionOk==1){
//On démarre la recherche du robot dans la zone définie par l'arène
positionList=img.SearchRobot(arena);
//Définitition et assignation de l'itérateur de parcrous de la liste positionList
it=positionList.begin();
//Définition d'un messagePosition qui va contenir l'information (x,y)
msgPos->SetPosition(*it);
//Transformation en message classique
msgPosMsg=msgPos->Copy();
//Envoi
WriteInQueue(&q_messageToMon,msgPos);
//Dessis du robot sur l'image
img.DrawRobot(*it);
}
//Définition d'un messageImg contenant l'image avec le robot et l'arène dessinés (ou pas)
msgImg->SetImage(&img);
//Transformation en message classique
msgImgMsg=msgImg->Copy();
//Envoi
WriteInQueue(&q_messageToMon,msgImg);
}
}
rt_mutex_acquire(&mutex_searchArena, TM_INFINITE);
searchArena=0;
rt_mutex_release(&mutex_searchArena);
searchArenaOk=1;
rt_mutex_acquire(&mutex_getPosition, TM_INFINITE);
getPosition=0;
rt_mutex_release(&mutex_getPosition);
getPositionOk=1;
}
/**
* Write a message in a given queue
* @param queue Queue identifier

View file

@ -87,6 +87,8 @@ private:
RT_TASK th_startRobotWithoutWatchdog;
RT_TASK th_startRobotWithWatchdog;
RT_TASK th_move;
RT_TASK th_askBattery;
RT_TASK th_vision;
/**********************************************************************/
/* Mutex */
@ -115,7 +117,9 @@ private:
RT_SEM sem_serverOk;
RT_SEM sem_startRobotWithoutWatchdog;
RT_SEM sem_startRobotWithWatchdog;
RT_SEM sem_askBattery;
RT_SEM sem_allowStartReceive;
RT_SEM sem_allowStartCaptureImage;
/**********************************************************************/
/* Message queues */
@ -161,7 +165,11 @@ private:
*/
void MoveTask(void *arg);
void AskBattery(void *arg);
void Vision(void *arg);
/**********************************************************************/
/* Queue services */