person.cpp
Go to the documentation of this file.
1 // Copyright 2022 PAL Robotics S.L.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the PAL Robotics S.L. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 #include <std_msgs/String.h>
30 #include <std_msgs/Bool.h>
31 
32 #include "geometry_msgs/TransformStamped.h"
33 #include "hri/person.h"
34 
35 #include "hri/hri.h"
36 #include "hri_msgs/EngagementLevel.h"
37 #include "std_msgs/Float32.h"
38 
39 using namespace std;
40 using namespace hri;
41 
42 Person::Person(ID id, const HRIListener* listener, ros::NodeHandle& nh,
43  tf2_ros::Buffer* tf_buffer_ptr, const std::string& reference_frame)
44  : FeatureTracker{ id, nh }
45  , listener_(listener)
46  , _anonymous(false)
47  , _engagement_status(nullptr)
48  , _alias("")
49  , _loc_confidence(0.)
50  , _tf_buffer_ptr(tf_buffer_ptr)
51  , _reference_frame(reference_frame)
52 {
53 }
54 
55 
57 {
58  ROS_DEBUG_STREAM("Deleting person " << id_);
59 }
60 
62 {
63  ns_ = "/humans/persons/" + id_;
64  ROS_DEBUG_STREAM("New person detected: " << ns_);
65 
66  face_id_subscriber_ = node_.subscribe<std_msgs::String>(
67  ns_ + "/face_id", 1, [&](const std_msgs::StringConstPtr msg) { face_id = msg->data; });
68 
69  body_id_subscriber_ = node_.subscribe<std_msgs::String>(
70  ns_ + "/body_id", 1, [&](const std_msgs::StringConstPtr msg) { body_id = msg->data; });
71 
72  voice_id_subscriber_ = node_.subscribe<std_msgs::String>(
73  ns_ + "/voice_id", 1,
74  [&](const std_msgs::StringConstPtr msg) { voice_id = msg->data; });
75 
76  anonymous_subscriber_ = node_.subscribe<std_msgs::Bool>(
77  ns_ + "/anonymous", 1,
78  [&](const std_msgs::BoolConstPtr msg) { _anonymous = msg->data; });
79 
80  alias_subscriber_ = node_.subscribe<std_msgs::String>(
81  ns_ + "/alias", 1, [&](const std_msgs::StringConstPtr msg) { _alias = msg->data; });
82 
84  ns_ + "/engagement_status", 1,
85  [&](const hri_msgs::EngagementLevelConstPtr msg) { _engagement_status = msg; });
86 
87  loc_confidence_subscriber_ = node_.subscribe<std_msgs::Float32>(
88  ns_ + "/location_confidence", 1,
89  [&](const std_msgs::Float32ConstPtr msg) { _loc_confidence = msg->data; });
90 }
91 
93 {
94  if (listener_->getFaces().count(face_id) != 0)
95  return listener_->getFaces()[face_id];
96  else
97  return FaceWeakConstPtr();
98 }
99 
101 {
102  if (listener_->getBodies().count(body_id) != 0)
103  return listener_->getBodies()[body_id];
104  else
105  return BodyWeakConstPtr();
106 }
107 
109 {
110  if (listener_->getVoices().count(voice_id) != 0)
111  return listener_->getVoices()[voice_id];
112  else
113  return VoiceWeakConstPtr();
114 }
115 
116 boost::optional<EngagementLevel> Person::engagement_status() const
117 {
118  if (!_engagement_status)
119  {
120  return boost::optional<EngagementLevel>();
121  }
122 
123  switch (_engagement_status->level)
124  {
125  case hri_msgs::EngagementLevel::UNKNOWN:
126  return boost::optional<EngagementLevel>();
135  default:
136  // we should handle all the possible engagement values
137  assert(false);
138  return boost::optional<EngagementLevel>();
139  }
140 }
141 
142 boost::optional<geometry_msgs::TransformStamped> Person::transform() const
143 {
144  if (_loc_confidence == 0)
145  {
146  return boost::optional<geometry_msgs::TransformStamped>();
147  }
148 
149  try
150  {
153 
154  return transform;
155  }
156  catch (tf2::LookupException)
157  {
158  ROS_WARN_STREAM("failed to transform person frame " << frame() << " to " << _reference_frame
159  << ". Are the frames published?");
160  return boost::optional<geometry_msgs::TransformStamped>();
161  }
162 }
163 
std::string ns_
Definition: base.h:119
void init() override
Definition: person.cpp:61
ros::NodeHandle node_
Definition: base.h:121
hri_msgs::EngagementLevelConstPtr _engagement_status
Definition: person.h:143
ID voice_id
Definition: person.h:121
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::weak_ptr< const Voice > VoiceWeakConstPtr
Definition: voice.h:150
ros::Subscriber face_id_subscriber_
Definition: person.h:149
boost::optional< geometry_msgs::TransformStamped > transform() const
Definition: person.cpp:142
EngagementLevel
Definition: person.h:54
tf2_ros::Buffer * _tf_buffer_ptr
Definition: person.h:157
std::weak_ptr< const Face > FaceWeakConstPtr
Definition: face.h:209
float _loc_confidence
Definition: person.h:145
std::weak_ptr< const Body > BodyWeakConstPtr
Definition: body.h:138
ros::Subscriber loc_confidence_subscriber_
Definition: person.h:155
virtual ~Person()
Definition: person.cpp:56
boost::optional< EngagementLevel > engagement_status() const
Definition: person.cpp:116
ros::Subscriber engagement_subscriber_
Definition: person.h:154
ros::Subscriber alias_subscriber_
Definition: person.h:153
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
VoiceWeakConstPtr voice() const
Definition: person.cpp:108
BodyWeakConstPtr body() const
Definition: person.cpp:100
const HRIListener * listener_
Definition: person.h:129
std::map< ID, FaceWeakConstPtr > getFaces() const
Returns the list of currently detected faces, mapped to their IDs.
Definition: hri.cpp:62
ros::Subscriber anonymous_subscriber_
Definition: person.h:152
ros::Subscriber voice_id_subscriber_
Definition: person.h:151
ID _alias
Definition: person.h:139
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
std::string _reference_frame
Definition: person.h:147
bool _anonymous
Definition: person.h:141
Definition: base.h:38
std::map< ID, BodyWeakConstPtr > getBodies() const
Returns the list of currently detected bodies, mapped to their IDs.
Definition: hri.cpp:76
static const ros::Duration PERSON_TF_TIMEOUT(0.01)
std::string frame() const
Definition: person.h:78
ID id() const
Definition: base.h:88
ros::Subscriber body_id_subscriber_
Definition: person.h:150
ID body_id
Definition: person.h:120
Main entry point to libhri. This is most likely what you want to use. Use example: ...
Definition: hri.h:70
FaceWeakConstPtr face() const
Definition: person.cpp:92
ID face_id
Definition: person.h:119
std::string ID
Definition: base.h:40
std::map< ID, VoiceWeakConstPtr > getVoices() const
Returns the list of currently detected voices, mapped to their IDs.
Definition: hri.cpp:90


libhri
Author(s): Séverin Lemaignan
autogenerated on Wed Apr 19 2023 02:30:41