|
|
|
@ -22,12 +22,12 @@
|
|
|
|
|
#define PRIORITY_TSERVER 30
|
|
|
|
|
#define PRIORITY_TOPENCOMROBOT 20
|
|
|
|
|
#define PRIORITY_TMOVE 20
|
|
|
|
|
#define PRIORITY_TSENDTOMON 23
|
|
|
|
|
#define PRIORITY_TSENDTOMON 22
|
|
|
|
|
#define PRIORITY_TRECEIVEFROMMON 25
|
|
|
|
|
#define PRIORITY_TSTARTROBOT 20
|
|
|
|
|
#define PRIORITY_TCAMERA 21
|
|
|
|
|
#define PRIORITY_TBATTERY 19
|
|
|
|
|
#define PRIORITY_TSTARTCAMERA 22
|
|
|
|
|
#define PRIORITY_TBATTERY 25
|
|
|
|
|
#define PRIORITY_TSTARTCAMERA 25
|
|
|
|
|
#define PRIORITY_TROBOTRELOADMESSAGE 25
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -46,12 +46,50 @@
|
|
|
|
|
*
|
|
|
|
|
* 6- When you want to write something in terminal, use cout and terminate with endl and flush
|
|
|
|
|
*
|
|
|
|
|
* 7- Good luck ! Thanks
|
|
|
|
|
* 7- Good luck !
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Initialisation des structures de l'application (tâches, mutex,
|
|
|
|
|
* semaphore, etc.)
|
|
|
|
|
void Tasks::RobotLossCounter(void* arg){
|
|
|
|
|
int state;
|
|
|
|
|
|
|
|
|
|
// wait for connexion establishement
|
|
|
|
|
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_connexionEstablished, TM_INFINITE);
|
|
|
|
|
cout << "Starting to monitor packet losses " << endl << flush;
|
|
|
|
|
rt_task_set_periodic(NULL, TM_NOW, 100*1000*1000);
|
|
|
|
|
while(1){
|
|
|
|
|
rt_task_wait_period(NULL);
|
|
|
|
|
|
|
|
|
|
rt_mutex_acquire(&mutex_compteurRobotLoss, TM_INFINITE);
|
|
|
|
|
if(compteurRobotLoss >= 3){
|
|
|
|
|
// send info to monitor
|
|
|
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_COM_ERROR)); // TODO : c'est probablement faux mais on a suivi la doc *sig*
|
|
|
|
|
|
|
|
|
|
// close robot
|
|
|
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
|
|
|
state = robot.Close();
|
|
|
|
|
rt_mutex_release(&mutex_robot);
|
|
|
|
|
rt_mutex_acquire(&mutex_robotStarted, TM_INFINITE);
|
|
|
|
|
robotStarted = 0;
|
|
|
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send info to monitor
|
|
|
|
|
if (state < 0) {
|
|
|
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_NACK));
|
|
|
|
|
} else {
|
|
|
|
|
WriteInQueue(&q_messageToMon, new Message(MESSAGE_ANSWER_ACK));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(&mutex_compteurRobotLoss);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
Message* Tasks::parseMessage(Message* msg){
|
|
|
|
@ -152,14 +190,6 @@ void Tasks::Init() {
|
|
|
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
if (err = rt_mutex_create(&mutex_battery, NULL)) {
|
|
|
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
if (err = rt_mutex_create(&mutex_image, NULL)) {
|
|
|
|
|
cerr << "Error mutex create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
cout << "Mutexes created successfully" << endl << flush;
|
|
|
|
|
|
|
|
|
|
/**************************************************************************************/
|
|
|
|
@ -234,6 +264,10 @@ void Tasks::Init() {
|
|
|
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
/*if (err = rt_task_create(&th_lossCounter, "th_lossCounter", 0, PRIORITY_TLOSSCOUNTER, 0)) {
|
|
|
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}*/
|
|
|
|
|
if (err = rt_task_create(&th_camera, "th_camera", 0, PRIORITY_TCAMERA, 0)) {
|
|
|
|
|
cerr << "Error task create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
@ -252,14 +286,12 @@ void Tasks::Init() {
|
|
|
|
|
/**************************************************************************************/
|
|
|
|
|
/* Message queues creation */
|
|
|
|
|
/**************************************************************************************/
|
|
|
|
|
if ((err = rt_queue_create(&q_messageToMon, "q_messageToMon", sizeof (Message*)*100, Q_UNLIMITED, Q_FIFO)) < 0) {
|
|
|
|
|
if ((err = rt_queue_create(&q_messageToMon, "q_messageToMon", sizeof (Message*)*50, Q_UNLIMITED, Q_FIFO)) < 0) {
|
|
|
|
|
cerr << "Error msg queue create: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
cout << "Queues created successfully" << endl << flush;
|
|
|
|
|
|
|
|
|
|
camera = new Camera(sm,5);
|
|
|
|
|
cout << "New camera ! " << endl << flush;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -305,6 +337,11 @@ void Tasks::Run() {
|
|
|
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*if (err = rt_task_start(&th_lossCounter, (void(*)(void*)) & Tasks::RobotLossCounter, this)) {
|
|
|
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}*/
|
|
|
|
|
if (err = rt_task_start(&th_robotReloadMessage, (void(*)(void*)) & Tasks::RobotReloadMessage, this)) {
|
|
|
|
|
cerr << "Error task start: " << strerror(-err) << endl << flush;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
@ -424,33 +461,18 @@ void Tasks::ReceiveFromMonTask(void *arg) {
|
|
|
|
|
rt_mutex_release(&mutex_move);
|
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_CAM_OPEN)){
|
|
|
|
|
rt_sem_v(&sem_startCamera);
|
|
|
|
|
} else if (msgRcv->CompareID(MESSAGE_CAM_CLOSE)){
|
|
|
|
|
rt_sem_v(&sem_startCamera);
|
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_ASK_ARENA)){
|
|
|
|
|
cout << "Enabling search arena" << endl << flush;
|
|
|
|
|
rt_mutex_acquire(&mutex_search_arena, TM_INFINITE);
|
|
|
|
|
searchArena = 1;
|
|
|
|
|
rt_mutex_release(&mutex_search_arena);
|
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
|
|
|
getImage = 0;
|
|
|
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
cout << "Search arena true " << endl << flush;
|
|
|
|
|
} else if(msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM) || msgRcv->CompareID(MESSAGE_CAM_ARENA_INFIRM)){
|
|
|
|
|
rt_mutex_acquire(&mutex_answer_arena,TM_INFINITE);
|
|
|
|
|
arenaConfirm = msgRcv->CompareID(MESSAGE_CAM_ARENA_CONFIRM)? 1:0; // A vérifier que le ternaire est utile
|
|
|
|
|
cout << "Statut de l'arene" << arenaConfirm << endl << flush;
|
|
|
|
|
rt_mutex_release(&mutex_answer_arena);
|
|
|
|
|
rt_sem_v(&sem_answerSync);
|
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_START)){
|
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
|
|
|
robotPos = 1;
|
|
|
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
|
|
|
cout << "Robot Pos true " << endl << flush;
|
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_CAM_POSITION_COMPUTE_STOP)){
|
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
|
|
|
robotPos = 0;
|
|
|
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
|
|
|
cout << "Robot Pos false" << endl << flush;
|
|
|
|
|
}else if(msgRcv->CompareID(MESSAGE_MONITOR_LOST)){
|
|
|
|
|
cout << "MONITOR LOST" << endl << flush;
|
|
|
|
|
|
|
|
|
@ -526,7 +548,7 @@ void Tasks::StartRobotTask(void *arg) {
|
|
|
|
|
rt_sem_p(&sem_startRobot, TM_INFINITE);
|
|
|
|
|
cout << "Start robot without watchdog (";
|
|
|
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
|
|
|
msgSend = parseMessage(robot.Write(robot.StartWithoutWD()));
|
|
|
|
|
msgSend = parseMessage(robot.Write(robot.StartWithoutWD())); // TODO : convert Message MessageID
|
|
|
|
|
rt_mutex_release(&mutex_robot);
|
|
|
|
|
cout << msgSend->GetID();
|
|
|
|
|
cout << ")" << endl;
|
|
|
|
@ -637,43 +659,32 @@ void Tasks::StartCameraTask(void *arg) {
|
|
|
|
|
|
|
|
|
|
Message * msgSend;
|
|
|
|
|
Message * msg;
|
|
|
|
|
bool status;
|
|
|
|
|
rt_sem_p(&sem_startCamera, TM_INFINITE);
|
|
|
|
|
cout << "Start Camera ";
|
|
|
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
|
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
|
|
|
if(!cameraStarted){
|
|
|
|
|
status = camera->Open();
|
|
|
|
|
cout << "Camera Opened" << endl << flush; // Toggle a modifier ?
|
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
|
|
|
getImage = 1;
|
|
|
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
}else{
|
|
|
|
|
camera->Close();
|
|
|
|
|
status = true;
|
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
|
|
|
getImage = 0;
|
|
|
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
cout << "Camera Closed" << endl << flush;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool status = camera.Open();
|
|
|
|
|
if(!status){
|
|
|
|
|
msgSend = new Message(MESSAGE_ANSWER_NACK);
|
|
|
|
|
WriteInQueue(&q_messageToMon, msgSend); // msgSend will be deleted by sendToMon
|
|
|
|
|
cout << "NACK !" << endl << flush ;
|
|
|
|
|
cout << msgSend->ToString() << endl << flush;
|
|
|
|
|
cout << "NACK1 ! " << endl << flush ;
|
|
|
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
|
|
|
cout << "NACK2 ! " << endl << flush ;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
msg = new Message(MESSAGE_ANSWER_ACK);
|
|
|
|
|
cout << "ACK3 ! " << endl << flush ;
|
|
|
|
|
cout << msg->ToString() << endl << flush;
|
|
|
|
|
cameraStarted = !cameraStarted;
|
|
|
|
|
cout << cameraStarted << endl << flush;
|
|
|
|
|
cout << "ACK4 ! " << endl << flush ;
|
|
|
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
|
cout << "ACK ! " << endl << flush ;
|
|
|
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
|
|
|
cout << "ff" << endl << flush ;
|
|
|
|
|
cameraStarted = 1;
|
|
|
|
|
cout << "Cam success! " << endl <<flush;
|
|
|
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Cam success! " << endl <<flush;
|
|
|
|
|
cout << "Fin de start cam" << endl << flush;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -738,7 +749,7 @@ void Tasks::BatteryTask(void *arg) {
|
|
|
|
|
rt_mutex_release(&mutex_robotStarted);
|
|
|
|
|
if (rs == 1) {
|
|
|
|
|
rt_mutex_acquire(&mutex_robot, TM_INFINITE);
|
|
|
|
|
Message* batteryReply = parseMessage(robot.Write(robot.GetBattery()));
|
|
|
|
|
Message* batteryReply = parseMessage(robot.Write(robot.GetBattery())); // TODO : convert Message MessageID
|
|
|
|
|
string batteryLevel = batteryReply->ToString();
|
|
|
|
|
rt_mutex_release(&mutex_robot);
|
|
|
|
|
WriteInQueue(&q_messageToMon, batteryReply);
|
|
|
|
@ -749,17 +760,13 @@ void Tasks::BatteryTask(void *arg) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief task in charge of managing the camera.
|
|
|
|
|
*/
|
|
|
|
|
void Tasks::CameraTask(void *arg){
|
|
|
|
|
int cam;
|
|
|
|
|
int grab;
|
|
|
|
|
Img* image;
|
|
|
|
|
Arena arenaSaved,arena ;
|
|
|
|
|
std::list<Position> robotPosition;
|
|
|
|
|
MessageImg* imgToSend;
|
|
|
|
|
|
|
|
|
|
int cam;
|
|
|
|
|
cout << "Start " << __PRETTY_FUNCTION__ << endl << flush;
|
|
|
|
|
// Synchronization barrier (waiting that all tasks are starting)
|
|
|
|
|
rt_sem_p(&sem_barrier, TM_INFINITE);
|
|
|
|
@ -769,82 +776,44 @@ void Tasks::CameraTask(void *arg){
|
|
|
|
|
rt_mutex_acquire(&mutex_cameraStarted, TM_INFINITE);
|
|
|
|
|
cam = cameraStarted;
|
|
|
|
|
rt_mutex_release(&mutex_cameraStarted);
|
|
|
|
|
|
|
|
|
|
if(!cam){
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(getImage){
|
|
|
|
|
cout << "Traitement de l'image" << endl << flush ;
|
|
|
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
|
|
|
// Lancer le traitement périodique
|
|
|
|
|
|
|
|
|
|
Img img = camera->Grab();
|
|
|
|
|
image = img.Copy();
|
|
|
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
|
rt_mutex_acquire(&mutex_search_arena, TM_INFINITE);
|
|
|
|
|
arena = image->SearchArena(); // Mise a jour de l'arene tout le temps pour la position du robot mais draw si search arena = true
|
|
|
|
|
if(searchArena){
|
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
|
|
|
getImage = 0;
|
|
|
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
// chopper l'arene
|
|
|
|
|
if(arena.IsEmpty()){
|
|
|
|
|
cout << "Arena empty" << endl << flush;
|
|
|
|
|
// envoi d'un no ack au moniteur
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
cout << "Draw Arena" << endl << flush;
|
|
|
|
|
image->DrawArena(arena);
|
|
|
|
|
// envoi de l'arene pour vérif
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
image->DrawArena(arenaSaved);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
|
|
|
if(robotPos){
|
|
|
|
|
// chopper le robot
|
|
|
|
|
cout << "Searching robot positions" << endl << flush ;
|
|
|
|
|
robotPosition = image->SearchRobot(arena);
|
|
|
|
|
|
|
|
|
|
//Traitement du robot
|
|
|
|
|
if(!robotPosition.empty()){ // Robot position != null
|
|
|
|
|
image->DrawAllRobots(robotPosition);
|
|
|
|
|
}
|
|
|
|
|
for (auto position : robotPosition)
|
|
|
|
|
{
|
|
|
|
|
WriteInQueue(&q_messageToMon, new MessagePosition(MESSAGE_CAM_POSITION,position)); // Envoi de toutes les positions
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
|
|
|
// Envoi de l'image
|
|
|
|
|
cout << "Envoi de l'image" << endl << flush ;
|
|
|
|
|
imgToSend = new MessageImg(MESSAGE_CAM_IMAGE,image);
|
|
|
|
|
WriteInQueue(&q_messageToMon, imgToSend);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rt_mutex_acquire(&mutex_answer_arena, TM_INFINITE);
|
|
|
|
|
rt_mutex_acquire(&mutex_camera, TM_INFINITE);
|
|
|
|
|
// Lancer le traitement périodique
|
|
|
|
|
Img img = camera.Grab();
|
|
|
|
|
Arena arena;
|
|
|
|
|
rt_mutex_release(&mutex_camera);
|
|
|
|
|
rt_mutex_acquire(&mutex_search_arena, TM_INFINITE);
|
|
|
|
|
if(searchArena){
|
|
|
|
|
rt_sem_p(&sem_answerSync, TM_INFINITE);
|
|
|
|
|
if(arenaConfirm){
|
|
|
|
|
// Arena OK
|
|
|
|
|
cout << "Arena Saved" << endl << flush;
|
|
|
|
|
arenaSaved = arena;
|
|
|
|
|
}else{
|
|
|
|
|
// Arena not OK
|
|
|
|
|
cout << "Arena Dumped" << endl << flush;
|
|
|
|
|
// Rien ?
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_acquire(&mutex_image, TM_INFINITE);
|
|
|
|
|
getImage = 1; // changer en booleen pour synchroniser plutot que ça
|
|
|
|
|
rt_mutex_release(&mutex_image);
|
|
|
|
|
searchArena = 0;
|
|
|
|
|
// chopper l'arene
|
|
|
|
|
cout << "Search arena pls ! " << endl << flush;
|
|
|
|
|
arena = img.SearchArena();
|
|
|
|
|
// searchArena = 0; // toggle?
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(&mutex_answer_arena);
|
|
|
|
|
rt_mutex_release(&mutex_search_arena);
|
|
|
|
|
|
|
|
|
|
rt_mutex_acquire(&mutex_robot_pos, TM_INFINITE);
|
|
|
|
|
if(robotPos){
|
|
|
|
|
// chopper le robot
|
|
|
|
|
cout << "Pos du robot pls !" ;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(&mutex_robot_pos);
|
|
|
|
|
if(arena.IsEmpty()){
|
|
|
|
|
cout << "Arena empty" << endl << flush;
|
|
|
|
|
// envoi d'un no ack au moniteur
|
|
|
|
|
}else{
|
|
|
|
|
cout << "Draw Arena" << endl << flush;
|
|
|
|
|
img.DrawArena(arena);
|
|
|
|
|
|
|
|
|
|
// envoi de l'arene pour vérif
|
|
|
|
|
}
|
|
|
|
|
cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARENE" << endl << flush;
|
|
|
|
|
WriteInQueue(&q_messageToMon, new MessageImg(MESSAGE_CAM_IMAGE,&img));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|