semio_ros  0.10.6
semio_attention_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <semio_msgs_ros/AttentionRecognitionResult.h>
4 #include <semio_msgs_ros/AttentionTargets.h>
5 
8 
9 //! Simple ROS wrapper around semio::AttentionRecognizer
11 {
12 public:
13  //! ROS message for the result of attention recognition for all humanoids
14  typedef semio_msgs_ros::AttentionRecognitionResult _AttentionRecognitionResultMsg;
15  //! ROS message for the result of attention recognition for a single humanoid
16  typedef semio_msgs_ros::AttentionRecognitionHumanoidItem _AttentionRecognitionHumanoidItemMsg;
17  //! ROS message for the result of attention recognition for a single humanoid joint
18  typedef semio_msgs_ros::AttentionRecognitionJointItem _AttentionRecognitionJointItemMsg;
19  //! ROS message for the result of attention recognition for a single attention target
20  typedef semio_msgs_ros::AttentionRecognitionTopNItem _AttentionRecognitionTopNItemMsg;
21  //! ROS message for a vector of attention targets
22  typedef semio_msgs_ros::AttentionTargets _AttentionTargetsMsg;
23  //! ROS message for a single attention target
24  typedef semio_msgs_ros::AttentionTarget _AttentionTargetMsg;
25 
26  //! NodeHandle copy used to interface with ROS
27  ros::NodeHandle nh_rel_;
28  //! Attention recognition result publisher
29  ros::Publisher result_pub_;
30  //! Attention targets subscriber
31  ros::Subscriber targets_sub_;
32 
33  //! Pointer to the input source for humanoids
35  //! Semio attention recognizer
37 
38  /**
39  @param nh_rel @copybrief nh_rel_
40  @param humanoid_source_ptr @copybrief humanoid_source_ptr_
41  */
42  SemioAttentionNode( ros::NodeHandle & nh_rel, semio::HumanoidSource::Ptr humanoid_source_ptr )
43  :
44  nh_rel_( nh_rel ),
45  result_pub_( nh_rel_.advertise<_AttentionRecognitionResultMsg>( "result", 100 ) ),
46  targets_sub_( nh_rel_.subscribe( "targets", 10, &SemioAttentionNode::targetsCB, this ) ),
47  humanoid_source_ptr_( humanoid_source_ptr )
48  {
49  //
50  }
51 
52  //! Main loop
53  void spin()
54  {
55  ros::Rate loop_rate( 30 );
56 
57  while( ros::ok() )
58  {
59  //! - Trigger ROS callbacks
60  ros::spinOnce();
61 
62  //! - Pass humanoids to attention recognizer
63  attention_recognizer_.getHumanoids() = humanoid_source_ptr_->update();
64  //! - Calculate attention recognition result
65  semio::AttentionRecognitionResult const & result = attention_recognizer_.calculateResult();
66 
67  //----------
68  //! - Convert attention recognition result to ROS message
69  _AttentionRecognitionResultMsg result_msg;
70 
71  result_msg.humanoids.reserve( result.size() );
72 
73  for( auto const & humanoid_item : result )
74  {
75  semio::AttentionSourceMap const & joints( humanoid_item.second );
76 
77  _AttentionRecognitionHumanoidItemMsg humanoid_msg;
78  humanoid_msg.id = static_cast<uint32_t>( humanoid_item.first );
79  humanoid_msg.joints.reserve( joints.size() );
80 
81  for( auto const & joint_item : joints )
82  {
83  semio::AttentionTopNList const & top_n_list( joint_item.second );
84 
85  _AttentionRecognitionJointItemMsg joint_msg;
86  joint_msg.id = static_cast<uint32_t>( joint_item.first );
87  joint_msg.top_n_list.reserve( top_n_list.size() );
88 
89  for( auto const & list_item : top_n_list.values() )
90  {
91  _AttentionRecognitionTopNItemMsg top_n_item_msg;
92  top_n_item_msg.likelihood = list_item.getValue();
93  top_n_item_msg.target_name = list_item.getData();
94 
95  joint_msg.top_n_list.push_back( std::move( top_n_item_msg ) );
96  }
97 
98  humanoid_msg.joints.push_back( std::move( joint_msg ) );
99  }
100 
101  result_msg.humanoids.push_back( std::move( humanoid_msg ) );
102  }
103  //----------
104 
105  //! - Publish attention recognition result
106  result_pub_.publish( std::move( result_msg ) );
107 
108  loop_rate.sleep();
109  }
110  }
111 
112  //! ROS callback for attention targets
113  /**
114  @param msg_ptr ConstPtr to the attention target message
115  */
116  void targetsCB( _AttentionTargetsMsg::ConstPtr const & msg_ptr )
117  {
118  auto const & targets_msg = *msg_ptr;
119 
120  //----------
121  //! - Convert attention target message to semio::AttentionTargetArray
122  semio::AttentionTargetArray attention_targets;
123 
124  for( auto const & target_msg : targets_msg.targets )
125  {
126  auto const & name = target_msg.name;
127  auto const & position = target_msg.position;
128 
129  attention_targets.emplace( name, Eigen::Vector3d( position.x, position.y, position.z ) );
130  }
131  //----------
132 
133  //! - Update attention recognizer's list of targets
134  attention_recognizer_.getTargets() = attention_targets;
135  }
136 };
137 
138 int main( int argc, char ** argv )
139 {
140  ros::init( argc, argv, "semio_attention_node" );
141  //! - Create NodeHandle with relative namespace
142  ros::NodeHandle nh_rel( "~" );
143 
144  //! - Create semio::ros::HumanoidSourceAdapter
145  semio::ros::HumanoidSourceAdapter humanoid_source_adapter( nh_rel );
146 
147  //! - Create SemioAttentionNode; pass node handle and humanoid source
148  SemioAttentionNode semio_attention_node( nh_rel, humanoid_source_adapter.getHumanoidSource() );
149  //! - Start main loop SemioAttentionNode::spin()
150  semio_attention_node.spin();
151 
152  return 0;
153 }
_DataSet & values()
void targetsCB(_AttentionTargetsMsg::ConstPtr const &msg_ptr)
ROS callback for attention targets.
AttentionHumanoidMap AttentionRecognitionResult
std::set< AttentionTarget > AttentionTargetArray
std::map< HumanoidJoint::JointType, AttentionTopNList > AttentionSourceMap
AttentionRecognitionResult const & calculateResult()
AttentionTargetArray & getTargets()
HumanoidArray & getHumanoids()
void spin()
Main loop.
semio::AttentionRecognizer attention_recognizer_
Semio attention recognizer.
semio_msgs_ros::AttentionTarget _AttentionTargetMsg
ROS message for a single attention target.
SemioAttentionNode(ros::NodeHandle &nh_rel, semio::HumanoidSource::Ptr humanoid_source_ptr)
semio_msgs_ros::AttentionRecognitionResult _AttentionRecognitionResultMsg
ROS message for the result of attention recognition for all humanoids.
ros::NodeHandle nh_rel_
NodeHandle copy used to interface with ROS.
semio_msgs_ros::AttentionRecognitionHumanoidItem _AttentionRecognitionHumanoidItemMsg
ROS message for the result of attention recognition for a single humanoid.
decltype(std::make_shared< HumanoidSource >()) typedef Ptr
semio_msgs_ros::AttentionTargets _AttentionTargetsMsg
ROS message for a vector of attention targets.
ros::Subscriber targets_sub_
Attention targets subscriber.
semio_msgs_ros::AttentionRecognitionTopNItem _AttentionRecognitionTopNItemMsg
ROS message for the result of attention recognition for a single attention target.
Utility class to simplify the creation of semio::HumanoidSource instances.
Simple ROS wrapper around semio::AttentionRecognizer.
HumanoidSource::Ptr getHumanoidSource(std::string const &source="param")
Create a semio::HumanoidSource.
int main(int argc, char **argv)
semio_msgs_ros::AttentionRecognitionJointItem _AttentionRecognitionJointItemMsg
ROS message for the result of attention recognition for a single humanoid joint.
semio::HumanoidSource::Ptr humanoid_source_ptr_
Pointer to the input source for humanoids.
ros::Publisher result_pub_
Attention recognition result publisher.