Looking for Caresses
SoRo project to let Miro interact as a pet would do
 All Classes Functions Variables Pages
B_approach.cpp
1 #include <iostream>
2 #include <string>
3 #include <numeric>
4 #include "../header/B_approach.h"
5 
11 B_approach::B_approach(ros::NodeHandle nh, ros::Publisher pubPlat){
12 
13  sonarMsgs = boost::circular_buffer<float>(averageNum);
14  counter = 0;
15  this->nh = nh;
16  this->pubPlat = pubPlat;
17 }
18 
23 void B_approach::sonarCallback(const sensor_msgs::Range &sensor_range)
24 {
25  look_caresses_pkg::platform_control msg;
26  sonarMsgs.push_front(sensor_range.range);
27 
28  if (sonarMsgs.full()){ //wait to have "averageNum" values of range
29 
30  // average between more than 1 value to avoid the errors of the sonar (sometimes returns zeros)
31  float sum = std::accumulate(sonarMsgs.begin(), sonarMsgs.end(), 0.0); //0 init the sum to 0
32  float average = sum/averageNum;
33  ROS_INFO("[B] Sonar Range average: %f", average);
34 
35  if (average > 0.15) { // in meters
36  msg.body_vel.linear.x = 30; // good choiche for the linear velocity
37  msg.body_vel.angular.z = 0.05; // because miro turns unwanted
38  }
39  else { // miro must not move
40  msg.body_vel.linear.x = 0;
41  msg.body_vel.angular.z = 0;
42  counter ++;
43  }
44  } else { // miro must not move
45  msg.body_vel.linear.x = 0;
46  msg.body_vel.angular.z = 0;
47  }
48  pubPlat.publish(msg);
49 }
50 
57 {
58  counter = 0;
59  sonarMsgs.erase(sonarMsgs.begin(), sonarMsgs.end());
60 
61  ROS_INFO("[B] Started");
62 
63  subTopics();
64 
65  // Publish the kinematic of the head to position the head up for detecting sonar range
66  look_caresses_pkg::platform_control plat_msgs_headup;
67  float body_config_headup[4] = {0.0, 0.4, 0, -0.1};
68  float body_config_speed_headup[4] = {0.0, -1.0, -1.0, -1.0};
69  for (int i =0; i<4; i++){
70  plat_msgs_headup.body_config[i] = body_config_headup[i];
71  plat_msgs_headup.body_config_speed[i] = body_config_speed_headup[i];
72  }
73 
74  pubPlat.publish(plat_msgs_headup);
75 
76 
77  ros::Rate loop_rate2(2);
78  while (ros::ok() && counter<6){
79  ros::spinOnce();
80  loop_rate2.sleep();
81  }
82 
83  //ros::spin();
84  unsubTopics();
85  ROS_INFO("[B] Finished");
86  return 0;
87 }
88 
92 void B_approach::subTopics(){
93  subRange = nh.subscribe("/miro/rob01/sensors/sonar_range", 1000, &B_approach::sonarCallback, this);
94 }
95 
96 
100 void B_approach::unsubTopics(){
101  subRange.shutdown();
102 }
int main()
main for approachin phase. MiRo goes straight until the sensor detect something (ideally the user han...
Definition: B_approach.cpp:56
B_approach(ros::NodeHandle nh, ros::Publisher pubPlat)
Costructor for phase B task.
Definition: B_approach.cpp:11