Statistics
| Branch: | Tag: | Revision:

humotion / src / server / middleware_ros.cpp @ 8d293778

History | View | Annotate | Download (8.965 KB)

1
/*
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

    
30
#include <std_msgs/Float32.h>
31
#include <string>
32

    
33
#include "humotion/server/middleware_ros.h"
34

    
35
using humotion::server::MiddlewareROS;
36

    
37
//! constructor
38
MiddlewareROS::MiddlewareROS(std::string scope, Controller *c)
39
    : Middleware(scope, c), nh_(scope + "/humotion") {
40
    ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "will use ros middleware");
41
    debug_initialized_ = false;
42

    
43
    // start ros core?
44
    if (ros::isInitialized()) {
45
        // oh another process is doing ros comm, fine, we do not need to call it
46
        tick_necessary_ = false;
47
    } else {
48
        // we have to take care of ros
49
        ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "no active ros middleware, will call ros::init() "
50
               "now and we will call tick() periodically!\n");
51

    
52
        std::string node_name = scope;
53
        node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
54

    
55
        ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "registering on ROS as node " << node_name.c_str());
56

    
57
        ros::M_string no_remapping;
58
        ros::init(no_remapping, node_name);
59
        tick_necessary_ = true;
60
    }
61

    
62
    // create node handle
63
    ros::NodeHandle pnh("~");
64

    
65
    // set up subscribers
66
    mouth_target_subscriber_ = nh_.subscribe("mouth/target", 150,
67
                                          &MiddlewareROS::incoming_mouth_target ,
68
                                          this, ros::TransportHints().unreliable());
69
    gaze_target_subscriber_  = nh_.subscribe("gaze/target",  150,
70
                                          &MiddlewareROS::incoming_gaze_target,
71
                                          this, ros::TransportHints().unreliable());
72

    
73
    // set up dynamic reconfiguration service
74
    attach_to_reconfiguration_server(pnh);
75
}
76

    
77
//! callback for incoming dynamic reconfigure requests:
78
void MiddlewareROS::dynamic_reconfigure_callback(const humotion::humotionConfig &dyn_config,
79
                                                  uint32_t level) {
80
    // fetch config
81
    humotion::server::Config *config = controller_->get_config();
82

    
83
    // debug
84
    config->publish_internals = dyn_config.publish_internals;
85

    
86
    // 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
    config->use_neck_target_instead_of_position_eye =
92
            dyn_config.use_neck_target_instead_of_position_eye;
93

    
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
    // parameters for eyelids
111
    config->eyelids_follow_eyemotion = dyn_config.eyelids_follow_eyemotion;
112

    
113
    // 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
}
120

    
121
//! attach to dynamic reconfigure server
122
void MiddlewareROS::attach_to_reconfiguration_server(ros::NodeHandle priv_nodehandle) {
123
    ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "connecting to dynamic reconfiguration server");
124

    
125
    ros::NodeHandle reconf_node(priv_nodehandle, "humotion/configuration");
126

    
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
}
131

    
132
//! destructor
133
MiddlewareROS::~MiddlewareROS() {
134
}
135

    
136
//! connection ok?
137
//! \return true if conn is alive
138
bool MiddlewareROS::ok() {
139
    return ros::ok();
140
}
141

    
142
//! do a single tick
143
void MiddlewareROS::tick() {
144
    if (tick_necessary_) {
145
        ros::spinOnce();
146
    }
147
}
148

    
149
//! 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
//! callback to handle incoming mouth  target
197
void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg) {
198
    // printf("> incoming mouth_target\n");
199
    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
    controller_->set_mouth_target(mouth_state);
210
}
211

    
212

    
213
//! callback to handle incoming gaze  target
214
void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg) {
215
    GazeState gaze_state;
216
    // 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

    
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
    if (msg->gaze_type == humotion::gaze::GAZETYPE_ABSOLUTE) {
239
        gaze_state.gaze_type = GazeState::GAZETYPE_ABSOLUTE;
240
    } else {
241
        gaze_state.gaze_type = GazeState::GAZETYPE_RELATIVE;
242
    }
243

    
244
    gaze_state.timestamp.set(msg->gaze_timestamp.sec, msg->gaze_timestamp.nsec);
245

    
246
    controller_->set_gaze_target(gaze_state);
247
}
248