Statistics
| Branch: | Tag: | Revision:

humotion / src / server / middleware_ros.cpp @ 6de0fc2e

History | View | Annotate | Download (8.965 KB)

1 8c6c1163 Simon Schulz
/*
2
* This file is part of humotion
3
*
4
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/humotion
6
*
7
* This file may be licensed under the terms of the
8
* GNU Lesser General Public License Version 3 (the ``LGPL''),
9
* or (at your option) any later version.
10
*
11
* Software distributed under the License is distributed
12
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
* express or implied. See the LGPL for the specific language
14
* governing rights and limitations.
15
*
16
* You should have received a copy of the LGPL along with this
17
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
18
* or write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
*
21
* The development of this software was supported by the
22
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
23
* The Excellence Cluster EXC 277 is a grant of the Deutsche
24
* Forschungsgemeinschaft (DFG) in the context of the German
25
* Excellence Initiative.
26
*/
27
28
#include <boost/algorithm/string/classification.hpp>
29 0c8d22a5 sschulz
30 2aa96942 sschulz
#include <std_msgs/Float32.h>
31 0c8d22a5 sschulz
#include <string>
32
33
#include "humotion/server/middleware_ros.h"
34
35
using humotion::server::MiddlewareROS;
36 8c6c1163 Simon Schulz
37
//! constructor
38 0c8d22a5 sschulz
MiddlewareROS::MiddlewareROS(std::string scope, Controller *c)
39 2aa96942 sschulz
    : Middleware(scope, c), nh_(scope + "/humotion") {
40 277050c7 sschulz
    ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "will use ros middleware");
41 2aa96942 sschulz
    debug_initialized_ = false;
42 708960ff Simon Schulz
43 0c8d22a5 sschulz
    // start ros core?
44
    if (ros::isInitialized()) {
45
        // oh another process is doing ros comm, fine, we do not need to call it
46 ea068cf1 sschulz
        tick_necessary_ = false;
47 0c8d22a5 sschulz
    } else {
48
        // we have to take care of ros
49 277050c7 sschulz
        ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "no active ros middleware, will call ros::init() "
50
               "now and we will call tick() periodically!\n");
51
52 5cd4364c sschulz
        std::string node_name = scope;
53 708960ff Simon Schulz
        node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
54 277050c7 sschulz
55
        ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "registering on ROS as node " << node_name.c_str());
56
57 8c6c1163 Simon Schulz
        ros::M_string no_remapping;
58
        ros::init(no_remapping, node_name);
59 ea068cf1 sschulz
        tick_necessary_ = true;
60 8c6c1163 Simon Schulz
    }
61
62 0c8d22a5 sschulz
    // create node handle
63 ea068cf1 sschulz
    ros::NodeHandle pnh("~");
64 8c6c1163 Simon Schulz
65 0c8d22a5 sschulz
    // set up subscribers
66 2aa96942 sschulz
    mouth_target_subscriber_ = nh_.subscribe("mouth/target", 150,
67 0c8d22a5 sschulz
                                          &MiddlewareROS::incoming_mouth_target ,
68
                                          this, ros::TransportHints().unreliable());
69 2aa96942 sschulz
    gaze_target_subscriber_  = nh_.subscribe("gaze/target",  150,
70 0c8d22a5 sschulz
                                          &MiddlewareROS::incoming_gaze_target,
71
                                          this, ros::TransportHints().unreliable());
72 ea068cf1 sschulz
73
    // set up dynamic reconfiguration service
74
    attach_to_reconfiguration_server(pnh);
75
}
76
77
//! callback for incoming dynamic reconfigure requests:
78 277050c7 sschulz
void MiddlewareROS::dynamic_reconfigure_callback(const humotion::humotionConfig &dyn_config,
79 ea068cf1 sschulz
                                                  uint32_t level) {
80 277050c7 sschulz
    // fetch config
81
    humotion::server::Config *config = controller_->get_config();
82
83 2aa96942 sschulz
    // debug
84
    config->publish_internals = dyn_config.publish_internals;
85
86 277050c7 sschulz
    // saccade detection thresholds
87
    config->threshold_velocity_eye_saccade = dyn_config.threshold_velocity_eye_saccade;
88
    config->threshold_angle_neck_saccade = dyn_config.threshold_angle_neck_saccade;
89
    config->threshold_angle_omr_limit = dyn_config.threshold_angle_omr_limit;
90
91 2aa96942 sschulz
    config->use_neck_target_instead_of_position_eye =
92
            dyn_config.use_neck_target_instead_of_position_eye;
93 277050c7 sschulz
94
    // neck motion generation configuration
95
    config->scale_velocity_neck = dyn_config.scale_velocity_neck;
96
    config->scale_acceleration_neck = dyn_config.scale_acceleration_neck;
97
    config->limit_velocity_neck = dyn_config.limit_velocity_neck;
98
    config->limit_acceleration_neck = dyn_config.limit_acceleration_neck;
99
100
    // eye motion generation configuration
101
    config->scale_velocity_eye = dyn_config.scale_velocity_eye;
102
    config->scale_acceleration_eye = dyn_config.scale_acceleration_eye;
103
    config->limit_velocity_eye = dyn_config.limit_velocity_eye;
104
    config->limit_acceleration_eye = dyn_config.limit_acceleration_eye;
105
106
    // parameters fo the breathing pattern
107
    config->breath_period = dyn_config.breath_period;
108
    config->breath_amplitude = dyn_config.breath_amplitude;
109
110 f311c844 sschulz
    // parameters for eyelids
111
    config->eyelids_follow_eyemotion = dyn_config.eyelids_follow_eyemotion;
112
113 277050c7 sschulz
    // parameters for eye blinking
114
    config->eyeblink_duration = dyn_config.eyeblink_duration;
115
    config->eyeblink_periodic_distribution_lower = dyn_config.eyeblink_periodic_distribution_lower;
116
    config->eyeblink_periodic_distribution_upper = dyn_config.eyeblink_periodic_distribution_upper;
117
    config->eyeblink_probability_after_saccade = dyn_config.eyeblink_probability_after_saccade;
118
    config->eyeblink_blocked_time = dyn_config.eyeblink_blocked_time;
119 ea068cf1 sschulz
}
120
121
//! attach to dynamic reconfigure server
122
void MiddlewareROS::attach_to_reconfiguration_server(ros::NodeHandle priv_nodehandle) {
123 277050c7 sschulz
    ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "connecting to dynamic reconfiguration server");
124
125 6d13138a sschulz
    ros::NodeHandle reconf_node(priv_nodehandle, "humotion/configuration");
126 ea068cf1 sschulz
127
    reconf_server_ = new dynamic_reconfigure::Server<humotion::humotionConfig>(reconf_node);
128
    reconf_server_->setCallback(boost::bind(
129
                                    &MiddlewareROS::dynamic_reconfigure_callback, this, _1, _2));
130 8c6c1163 Simon Schulz
}
131
132
//! destructor
133 0c8d22a5 sschulz
MiddlewareROS::~MiddlewareROS() {
134 8c6c1163 Simon Schulz
}
135
136
//! connection ok?
137
//! \return true if conn is alive
138 0c8d22a5 sschulz
bool MiddlewareROS::ok() {
139 8c6c1163 Simon Schulz
    return ros::ok();
140
}
141
142
//! do a single tick
143 0c8d22a5 sschulz
void MiddlewareROS::tick() {
144 ea068cf1 sschulz
    if (tick_necessary_) {
145 8c6c1163 Simon Schulz
        ros::spinOnce();
146
    }
147
}
148
149 2aa96942 sschulz
//! init debug dataset publishers
150
void MiddlewareROS::debug_initialize() {
151
    if (debug_initialized_) {
152
        return;
153
    }
154
155
    debug_initialized_ = true;
156
}
157
158
//! publish a debug dataset
159
void MiddlewareROS::publish_debug_dataset(debug_data_t debug_data) {
160
    if (!controller_->get_config()->publish_internals) {
161
        // no debugging data enabled, return
162
        return;
163
    }
164
165
    debug_initialize();
166
167
    // iterate over all debug variables and publish them
168
    debug_data_t::iterator it;
169
    for (it = debug_data.begin(); it != debug_data.end(); it++) {
170
        publish_debug_data(it->first, it->second);
171
    }
172
}
173
174
void MiddlewareROS::publish_debug_data(std::string name, float value) {
175
    debug_topic_map_t::iterator it = debug_topic_map.find(name);
176
    if (it == debug_topic_map.end()) {
177
        // we have no publisher for this dataset, create one:
178
        ROS_DEBUG_STREAM("creating debug output stream " << name);
179
        ros::Publisher pub =  nh_.advertise<std_msgs::Float32>("debug_data/" + name, 1000);
180
181
        std::pair<debug_topic_map_t::iterator, bool> ret;
182
        ret = debug_topic_map.insert(std::pair<std::string, ros::Publisher>(name, pub));
183
        it = ret.first;
184
    }
185
186
    // in any case, it is now the publisher we want  so publish data now
187
    std_msgs::Float32 msg;
188
    msg.data = value;
189
190
    // std::cout  << "DEBUG HUMOTION " << name << " " << value << std::endl;
191
192
    // send it
193
    it->second.publish(msg);
194
}
195
196 8c6c1163 Simon Schulz
//! callback to handle incoming mouth  target
197 0c8d22a5 sschulz
void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg) {
198
    // printf("> incoming mouth_target\n");
199 8c6c1163 Simon Schulz
    MouthState mouth_state;
200
201
    mouth_state.position_left   = msg->position.left;
202
    mouth_state.position_center = msg->position.center;
203
    mouth_state.position_right  = msg->position.right;
204
205
    mouth_state.opening_left   = msg->opening.left;
206
    mouth_state.opening_center = msg->opening.center;
207
    mouth_state.opening_right  = msg->opening.right;
208
209 ea068cf1 sschulz
    controller_->set_mouth_target(mouth_state);
210 8c6c1163 Simon Schulz
}
211
212
213
//! callback to handle incoming gaze  target
214 0c8d22a5 sschulz
void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg) {
215 8c6c1163 Simon Schulz
    GazeState gaze_state;
216 0c8d22a5 sschulz
    // printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f] %d = %s\n",
217
    //          msg->pan, msg->tilt, msg->roll, msg->type,
218
    //          msg->type==humotion::gaze::GAZETYPE_ABSOLUTE?"ABSOLUTE":"RELATIVE");
219 8c6c1163 Simon Schulz
220
    gaze_state.pan  = msg->pan;
221
    gaze_state.tilt = msg->tilt;
222
    gaze_state.roll = msg->roll;
223
    gaze_state.vergence = msg->vergence;
224
225
    gaze_state.pan_offset  = msg->pan_offset;
226
    gaze_state.tilt_offset = msg->tilt_offset;
227
    gaze_state.roll_offset = msg->roll_offset;
228
229
    gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper;
230
    gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower;
231
232
    gaze_state.eyebrow_left = msg->eyebrow_left;
233
    gaze_state.eyebrow_right = msg->eyebrow_right;
234
235
    gaze_state.eyeblink_request_left = msg->eyeblink_request_left;
236
    gaze_state.eyeblink_request_right = msg->eyeblink_request_right;
237
238 0c8d22a5 sschulz
    if (msg->gaze_type == humotion::gaze::GAZETYPE_ABSOLUTE) {
239 1c758459 Simon Schulz
        gaze_state.gaze_type = GazeState::GAZETYPE_ABSOLUTE;
240 0c8d22a5 sschulz
    } else {
241 1c758459 Simon Schulz
        gaze_state.gaze_type = GazeState::GAZETYPE_RELATIVE;
242
    }
243 32327f15 Simon Schulz
244
    gaze_state.timestamp.set(msg->gaze_timestamp.sec, msg->gaze_timestamp.nsec);
245 8c6c1163 Simon Schulz
246 ea068cf1 sschulz
    controller_->set_gaze_target(gaze_state);
247 8c6c1163 Simon Schulz
}