Looking for Caresses
SoRo project to let Miro interact as a pet would do
 All Classes Functions Variables Pages
Aa_faceDetection.cpp
1 #include <cstdlib>
2 #include <iostream>
3 #include "../header/Aa_faceDetection.h"
4 
10 Aa_faceDetection::Aa_faceDetection(ros::NodeHandle nh, ros::Publisher pubPlat){
11  detectedLeft = false;
12  detectedFaceConsec = 0;
13  this->nh = nh;
14  this->pubPlat = pubPlat;
15 
16 }
17 
18 
23 void Aa_faceDetection::subImgDetectCallbackRight(const opencv_apps::FaceArrayStamped &msgFacesRight){
24 
25  look_caresses_pkg::platform_control msgVel;
26 
27  if (msgFacesRight.faces.empty()) { //no detection with right camera
28  if(detectedLeft) {
29  //turn slowly clockwise to catch face also with right camera
30  msgVel.body_vel.angular.z = 0.1;
31  detectedFaceConsec = 0; //reset counter time for detect face
32  }
33  else {
34  //turn a little faster clockwise because neither left camera has detected face
35  msgVel.body_vel.angular.z = 0.2;
36  detectedFaceConsec = 0;
37  }
38  }
39  else {
40  if(detectedLeft) {
41  //face detected with both cameras, keep position
42  msgVel.body_vel.angular.z = 0;
43  detectedFaceConsec++; // to check the time the face is in range
44  }
45  else {
46  //turn slowly counterclockwise to catch face also with left camera
47  msgVel.body_vel.angular.z = -0.1;
48  detectedFaceConsec = 0;
49  }
50  }
51 
52  pubPlat.publish(msgVel);
53 }
54 
55 
60 void Aa_faceDetection::subImgDetectCallbackLeft(const opencv_apps::FaceArrayStamped &msgFacesLeft){
61 
62  if (msgFacesLeft.faces.empty()) {
63  detectedLeft = false;
64  }
65  else {
66  detectedLeft = true;
67  }
68 }
69 
70 
76 {
77 
78  detectedLeft = false;
79  detectedFaceConsec = 0;
80 
81  ROS_INFO("[Aa] Started");
82  subTopics();
83 
84  ros::Rate loop_rate(10); //10 hz
85  while (detectedFaceConsec <= 15){ //detect face and keep it in focus for 15*10hz = 1.5 seconds
86  ros::spinOnce();
87  loop_rate.sleep();
88  }
89 
90  look_caresses_pkg::platform_control plat_msgs_saw;
91  ros::Rate loop_rate2(1);
92  for (int i =0; i<5; i++){
93  plat_msgs_saw.sound_index_P2 = rand()%20; //pirate sound
94  pubPlat.publish(plat_msgs_saw);
95  //ros::spinOnce();
96  loop_rate2.sleep();
97  }
98 
99  ROS_INFO("[Aa] I saw you consecutevely for 1.5 seconds");
100 
101  unsubTopics();
102  ROS_INFO("[Aa] Finished");
103  return 0;
104 }
105 
109 void Aa_faceDetection::subTopics(){
110 
111  subImageDetectRight = nh.subscribe("/right/face_detection/faces", 1000, &Aa_faceDetection::subImgDetectCallbackRight, this);
112  subImageDetectLeft = nh.subscribe("/left/face_detection/faces", 1000, &Aa_faceDetection::subImgDetectCallbackLeft, this);
113 
114 }
115 
116 
120 void Aa_faceDetection::unsubTopics(){
121  subImageDetectRight.shutdown();
122  subImageDetectLeft.shutdown();
123 }
124 
int main()
The main for face detection task. It make MiRo turns around z axis until a face is visible in both ca...
Aa_faceDetection(ros::NodeHandle nh, ros::Publisher pubPlat)
Costructor for phase Aa task.