semio_ros  0.10.6
semio_deixis_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <semio_msgs_ros/DeicticRecognitionResult.h>
4 #include <semio_msgs_ros/DeicticTargets.h>
5 
8 
9 //! Simple ROS wrapper around semio::DeicticRecognizer
11 {
12 public:
13  //! ROS message for the result of deictic recognition for all humanoids
14  typedef semio_msgs_ros::DeicticRecognitionResult _DeicticRecognitionResultMsg;
15  //! ROS message for the result of deictic recognition for a single humanoid
16  typedef semio_msgs_ros::DeicticRecognitionHumanoidItem _DeicticRecognitionHumanoidItemMsg;
17  //! ROS message for the result of deictic recognition for a single humanoid joint
18  typedef semio_msgs_ros::DeicticRecognitionSourceItem _DeicticRecognitionSourceItemMsg;
19  //! ROS message for the result of deictic recognition for a single deictic target
20  typedef semio_msgs_ros::DeicticRecognitionTopNItem _DeicticRecognitionTopNItemMsg;
21  //! ROS message for a vector of deictic targets
22  typedef semio_msgs_ros::DeicticTargets _DeicticTargetsMsg;
23  //! ROS message for a single deictic target
24  typedef semio_msgs_ros::DeicticTarget _DeicticTargetMsg;
25 
26  //! NodeHandle copy used to interface with ROS
27  ros::NodeHandle nh_rel_;
28  //! Deictic recognition result publisher
29  ros::Publisher result_pub_;
30  //! Deictic targets subscriber
31  ros::Subscriber targets_sub_;
32 
33  //! Pointer to the input source for humanoids
35  //! Semio deictic recognizer
37 
38  /**
39  @param nh_rel @copybrief nh_rel_
40  @param humanoid_source_ptr @copybrief humanoid_source_ptr_
41  */
42  SemioDeixisNode( ros::NodeHandle & nh_rel, semio::HumanoidSource::Ptr humanoid_source_ptr )
43  :
44  nh_rel_( nh_rel ),
45  result_pub_( nh_rel_.advertise<_DeicticRecognitionResultMsg>( "result", 100 ) ),
46  targets_sub_( nh_rel_.subscribe( "targets", 10, &SemioDeixisNode::targetsCB, this ) ),
47  humanoid_source_ptr_( humanoid_source_ptr )
48  {
49  typedef std::function<void(std::string const &)> _ParamFunc;
50  typedef std::pair<std::string, _ParamFunc> _ParamOp;
51  for( auto const & param_op : {
52  _ParamOp(
53  "filter/max_size",
54  [this]( std::string const & param_name ){
55  this->deictic_recognizer_.setFilterMaxSize( this->nh_rel_.param<int>( std::string( param_name ), 0 ) );
56  } ),
57  _ParamOp( "filter/max_duration",
58  [this]( std::string const & param_name ){
59  this->deictic_recognizer_.setFilterMaxDuration( this->nh_rel_.param<double>( std::string( param_name ), 0 ) );
60  } ) } )
61  {
62  if( nh_rel_.hasParam( param_op.first ) ) param_op.second( param_op.first );
63  }
64  }
65 
66  //! Main loop
67  void spin()
68  {
69  ros::Rate loop_rate( 30 );
70 
71  while( ros::ok() )
72  {
73  //! - Trigger ROS callbacks
74  ros::spinOnce();
75 
76  //! - Pass humanoids to deictic recognizer
77  deictic_recognizer_.getHumanoids() = humanoid_source_ptr_->update();
78  //! - Calculate deictic recognition result
79  semio::DeicticRecognitionResult const & result( deictic_recognizer_.calculateResult() );
80 
81  //----------
82  //! - Convert deictic recognition result to ROS message
83  _DeicticRecognitionResultMsg result_msg;
84 
85  result_msg.humanoids.reserve( result.size() );
86 
87  for( auto const & humanoid_item : result )
88  {
89  semio::DeixisSourceMap const & sources( humanoid_item.second );
90 
91  _DeicticRecognitionHumanoidItemMsg humanoid_msg;
92  humanoid_msg.id = static_cast<uint32_t>( humanoid_item.first );
93  humanoid_msg.sources.reserve( sources.size() );
94 
95  for( auto const & source_item : sources )
96  {
97  semio::DeixisTopNList const & top_n_list( source_item.second );
98 
99  _DeicticRecognitionSourceItemMsg source_msg;
100  source_msg.name = source_item.first;
101  source_msg.top_n_list.reserve( top_n_list.size() );
102 
103  for( auto const & list_item : top_n_list.values() )
104  {
105  _DeicticRecognitionTopNItemMsg top_n_item_msg;
106  top_n_item_msg.likelihood = list_item.getValue();
107  top_n_item_msg.target_name = list_item.getData();
108 
109  source_msg.top_n_list.push_back( std::move( top_n_item_msg ) );
110  }
111 
112  humanoid_msg.sources.push_back( std::move( source_msg ) );
113  }
114 
115  result_msg.humanoids.push_back( std::move( humanoid_msg ) );
116  }
117  //----------
118 
119  //! - Publish deictic recognition result
120  result_pub_.publish( std::move( result_msg ) );
121 
122  loop_rate.sleep();
123  }
124  }
125 
126  //! ROS callback for deictic targets
127  /**
128  @param msg_ptr ConstPtr to the deictic target message
129  */
130  void targetsCB( _DeicticTargetsMsg::ConstPtr const & msg_ptr )
131  {
132  auto const & targets_msg = *msg_ptr;
133 
134  //----------
135  //! - Convert deictic target message to semio::DeicticTargetArray
136  semio::DeicticTargetArray deictic_targets;
137 
138  for( auto const & target_msg : targets_msg.targets )
139  {
140  auto const & name = target_msg.name;
141  auto const & position = target_msg.position;
142 
143  deictic_targets.emplace( name, Eigen::Vector3d( position.x, position.y, position.z ) );
144  }
145  //----------
146 
147  //! - Update deictic recognizer's list of targets
148  deictic_recognizer_.getTargets() = deictic_targets;
149  }
150 };
151 
152 int main( int argc, char ** argv )
153 {
154  ros::init( argc, argv, "semio_deixis_node" );
155  //! - Create NodeHandle with relative namespace
156  ros::NodeHandle nh_rel( "~" );
157 
158  //! - Create semio::ros::HumanoidSourceAdapter
159  semio::ros::HumanoidSourceAdapter humanoid_source_adapter( nh_rel );
160 
161  //! - Create SemioDeixisNode; pass node handle and humanoid source
162  SemioDeixisNode semio_deixis_node( nh_rel, humanoid_source_adapter.getHumanoidSource() );
163  //! - Start main loop SemioDeixisNode::spin()
164  semio_deixis_node.spin();
165 
166  return 0;
167 }
_DataSet & values()
Simple ROS wrapper around semio::DeicticRecognizer.
semio_msgs_ros::DeicticRecognitionResult _DeicticRecognitionResultMsg
ROS message for the result of deictic recognition for all humanoids.
std::set< DeicticTarget > DeicticTargetArray
DeixisHumanoidMap DeicticRecognitionResult
void setFilterMaxSize(size_t const &max_size=30)
semio_msgs_ros::DeicticTarget _DeicticTargetMsg
ROS message for a single deictic target.
HumanoidArray & getHumanoids()
DeicticTargetArray & getTargets()
semio::DeicticRecognizer deictic_recognizer_
Semio deictic recognizer.
semio::HumanoidSource::Ptr humanoid_source_ptr_
Pointer to the input source for humanoids.
DeicticRecognitionResult const & calculateResult()
decltype(std::make_shared< HumanoidSource >()) typedef Ptr
SemioDeixisNode(ros::NodeHandle &nh_rel, semio::HumanoidSource::Ptr humanoid_source_ptr)
void targetsCB(_DeicticTargetsMsg::ConstPtr const &msg_ptr)
ROS callback for deictic targets.
void spin()
Main loop.
Utility class to simplify the creation of semio::HumanoidSource instances.
ros::Publisher result_pub_
Deictic recognition result publisher.
HumanoidSource::Ptr getHumanoidSource(std::string const &source="param")
Create a semio::HumanoidSource.
int main(int argc, char **argv)
semio_msgs_ros::DeicticRecognitionHumanoidItem _DeicticRecognitionHumanoidItemMsg
ROS message for the result of deictic recognition for a single humanoid.
semio_msgs_ros::DeicticRecognitionTopNItem _DeicticRecognitionTopNItemMsg
ROS message for the result of deictic recognition for a single deictic target.
semio_msgs_ros::DeicticRecognitionSourceItem _DeicticRecognitionSourceItemMsg
ROS message for the result of deictic recognition for a single humanoid joint.
ros::Subscriber targets_sub_
Deictic targets subscriber.
semio_msgs_ros::DeicticTargets _DeicticTargetsMsg
ROS message for a vector of deictic targets.
std::map< std::string, DeixisTopNList > DeixisSourceMap
ros::NodeHandle nh_rel_
NodeHandle copy used to interface with ROS.
void setFilterMaxDuration(double const &max_duration=0.5)