humotion / src / server / middleware_ros.cpp @ 95dd1012
History | View | Annotate | Download (7.33 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 | #include <string>  | 
      ||
| 31 | |||
| 32 | #include "humotion/server/middleware_ros.h"  | 
      ||
| 33 | |||
| 34 | using humotion::server::MiddlewareROS;
 | 
      ||
| 35 | 8c6c1163 | Simon Schulz | |
| 36 | //! constructor
 | 
      ||
| 37 | 0c8d22a5 | sschulz | MiddlewareROS::MiddlewareROS(std::string scope, Controller *c)
 | 
      
| 38 |     : Middleware(scope, c) {
 | 
      ||
| 39 | 277050c7 | sschulz | ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "will use ros middleware");  | 
      
| 40 | 708960ff | Simon Schulz | |
| 41 | 0c8d22a5 | sschulz |     // start ros core?
 | 
      
| 42 |     if (ros::isInitialized()) {
 | 
      ||
| 43 |         // oh another process is doing ros comm, fine, we do not need to call it
 | 
      ||
| 44 | ea068cf1 | sschulz |         tick_necessary_ = false;
 | 
      
| 45 | 0c8d22a5 | sschulz |     } else {
 | 
      
| 46 |         // we have to take care of ros
 | 
      ||
| 47 | 277050c7 | sschulz | ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "no active ros middleware, will call ros::init() "  | 
      
| 48 |                "now and we will call tick() periodically!\n");
 | 
      ||
| 49 | |||
| 50 | 5cd4364c | sschulz |         std::string node_name = scope;
 | 
      
| 51 | 708960ff | Simon Schulz |         node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
 | 
      
| 52 | 277050c7 | sschulz | |
| 53 | ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "registering on ROS as node " << node_name.c_str());  | 
      ||
| 54 | |||
| 55 | 8c6c1163 | Simon Schulz | ros::M_string no_remapping;  | 
      
| 56 | ros::init(no_remapping, node_name);  | 
      ||
| 57 | ea068cf1 | sschulz |         tick_necessary_ = true;
 | 
      
| 58 | 8c6c1163 | Simon Schulz | }  | 
      
| 59 | |||
| 60 | 0c8d22a5 | sschulz |     // create node handle
 | 
      
| 61 | 8c6c1163 | Simon Schulz | ros::NodeHandle n;  | 
      
| 62 | ea068cf1 | sschulz |     ros::NodeHandle pnh("~");
 | 
      
| 63 | 8c6c1163 | Simon Schulz | |
| 64 | 0c8d22a5 | sschulz |     // set up subscribers
 | 
      
| 65 | ea068cf1 | sschulz | mouth_target_subscriber_ = n.subscribe(scope + "/humotion/mouth/target", 150,  | 
      
| 66 | 0c8d22a5 | sschulz | &MiddlewareROS::incoming_mouth_target ,  | 
      
| 67 |                                           this, ros::TransportHints().unreliable());
 | 
      ||
| 68 | ea068cf1 | sschulz | gaze_target_subscriber_ = n.subscribe(scope + "/humotion/gaze/target", 150,  | 
      
| 69 | 0c8d22a5 | sschulz | &MiddlewareROS::incoming_gaze_target,  | 
      
| 70 |                                           this, ros::TransportHints().unreliable());
 | 
      ||
| 71 | ea068cf1 | sschulz | |
| 72 |     // set up dynamic reconfiguration service
 | 
      ||
| 73 | attach_to_reconfiguration_server(pnh);  | 
      ||
| 74 | }  | 
      ||
| 75 | |||
| 76 | //! callback for incoming dynamic reconfigure requests:
 | 
      ||
| 77 | 277050c7 | sschulz | void MiddlewareROS::dynamic_reconfigure_callback(const humotion::humotionConfig &dyn_config,  | 
      
| 78 | ea068cf1 | sschulz |                                                   uint32_t level) {
 | 
      
| 79 | 277050c7 | sschulz |     // fetch config
 | 
      
| 80 | humotion::server::Config *config = controller_->get_config();  | 
      ||
| 81 | |||
| 82 |     // saccade detection thresholds
 | 
      ||
| 83 | config->threshold_velocity_eye_saccade = dyn_config.threshold_velocity_eye_saccade;  | 
      ||
| 84 | config->threshold_angle_neck_saccade = dyn_config.threshold_angle_neck_saccade;  | 
      ||
| 85 | config->threshold_angle_omr_limit = dyn_config.threshold_angle_omr_limit;  | 
      ||
| 86 | |||
| 87 | |||
| 88 |     // neck motion generation configuration
 | 
      ||
| 89 | config->scale_velocity_neck = dyn_config.scale_velocity_neck;  | 
      ||
| 90 | config->scale_acceleration_neck = dyn_config.scale_acceleration_neck;  | 
      ||
| 91 | config->limit_velocity_neck = dyn_config.limit_velocity_neck;  | 
      ||
| 92 | config->limit_acceleration_neck = dyn_config.limit_acceleration_neck;  | 
      ||
| 93 | |||
| 94 |     // eye motion generation configuration
 | 
      ||
| 95 | config->scale_velocity_eye = dyn_config.scale_velocity_eye;  | 
      ||
| 96 | config->scale_acceleration_eye = dyn_config.scale_acceleration_eye;  | 
      ||
| 97 | config->limit_velocity_eye = dyn_config.limit_velocity_eye;  | 
      ||
| 98 | config->limit_acceleration_eye = dyn_config.limit_acceleration_eye;  | 
      ||
| 99 | |||
| 100 |     // parameters fo the breathing pattern
 | 
      ||
| 101 | config->breath_period = dyn_config.breath_period;  | 
      ||
| 102 | config->breath_amplitude = dyn_config.breath_amplitude;  | 
      ||
| 103 | |||
| 104 | f311c844 | sschulz |     // parameters for eyelids
 | 
      
| 105 | config->eyelids_follow_eyemotion = dyn_config.eyelids_follow_eyemotion;  | 
      ||
| 106 | |||
| 107 | 277050c7 | sschulz |     // parameters for eye blinking
 | 
      
| 108 | config->eyeblink_duration = dyn_config.eyeblink_duration;  | 
      ||
| 109 | config->eyeblink_periodic_distribution_lower = dyn_config.eyeblink_periodic_distribution_lower;  | 
      ||
| 110 | config->eyeblink_periodic_distribution_upper = dyn_config.eyeblink_periodic_distribution_upper;  | 
      ||
| 111 | config->eyeblink_probability_after_saccade = dyn_config.eyeblink_probability_after_saccade;  | 
      ||
| 112 | config->eyeblink_blocked_time = dyn_config.eyeblink_blocked_time;  | 
      ||
| 113 | ea068cf1 | sschulz | }  | 
      
| 114 | |||
| 115 | //! attach to dynamic reconfigure server
 | 
      ||
| 116 | void MiddlewareROS::attach_to_reconfiguration_server(ros::NodeHandle priv_nodehandle) {
 | 
      ||
| 117 | 277050c7 | sschulz | ROS_DEBUG_STREAM_NAMED("MiddlewareROS", "connecting to dynamic reconfiguration server");  | 
      
| 118 | |||
| 119 | 6d13138a | sschulz |     ros::NodeHandle reconf_node(priv_nodehandle, "humotion/configuration");
 | 
      
| 120 | ea068cf1 | sschulz | |
| 121 |     reconf_server_ = new dynamic_reconfigure::Server<humotion::humotionConfig>(reconf_node);
 | 
      ||
| 122 | reconf_server_->setCallback(boost::bind(  | 
      ||
| 123 |                                     &MiddlewareROS::dynamic_reconfigure_callback, this, _1, _2));
 | 
      ||
| 124 | 8c6c1163 | Simon Schulz | }  | 
      
| 125 | |||
| 126 | //! destructor
 | 
      ||
| 127 | 0c8d22a5 | sschulz | MiddlewareROS::~MiddlewareROS() {
 | 
      
| 128 | 8c6c1163 | Simon Schulz | }  | 
      
| 129 | |||
| 130 | //! connection ok?
 | 
      ||
| 131 | //! \return true if conn is alive
 | 
      ||
| 132 | 0c8d22a5 | sschulz | bool MiddlewareROS::ok() {
 | 
      
| 133 | 8c6c1163 | Simon Schulz |     return ros::ok();
 | 
      
| 134 | }  | 
      ||
| 135 | |||
| 136 | //! do a single tick
 | 
      ||
| 137 | 0c8d22a5 | sschulz | void MiddlewareROS::tick() {
 | 
      
| 138 | ea068cf1 | sschulz |     if (tick_necessary_) {
 | 
      
| 139 | 8c6c1163 | Simon Schulz | ros::spinOnce();  | 
      
| 140 | }  | 
      ||
| 141 | }  | 
      ||
| 142 | |||
| 143 | //! callback to handle incoming mouth  target
 | 
      ||
| 144 | 0c8d22a5 | sschulz | void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg) {  | 
      
| 145 |     // printf("> incoming mouth_target\n");
 | 
      ||
| 146 | 8c6c1163 | Simon Schulz | MouthState mouth_state;  | 
      
| 147 | |||
| 148 | mouth_state.position_left = msg->position.left;  | 
      ||
| 149 | mouth_state.position_center = msg->position.center;  | 
      ||
| 150 | mouth_state.position_right = msg->position.right;  | 
      ||
| 151 | |||
| 152 | mouth_state.opening_left = msg->opening.left;  | 
      ||
| 153 | mouth_state.opening_center = msg->opening.center;  | 
      ||
| 154 | mouth_state.opening_right = msg->opening.right;  | 
      ||
| 155 | |||
| 156 | ea068cf1 | sschulz | controller_->set_mouth_target(mouth_state);  | 
      
| 157 | 8c6c1163 | Simon Schulz | }  | 
      
| 158 | |||
| 159 | |||
| 160 | //! callback to handle incoming gaze  target
 | 
      ||
| 161 | 0c8d22a5 | sschulz | void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg) {  | 
      
| 162 | 8c6c1163 | Simon Schulz | GazeState gaze_state;  | 
      
| 163 | 0c8d22a5 | sschulz |     // printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f] %d = %s\n",
 | 
      
| 164 |     //          msg->pan, msg->tilt, msg->roll, msg->type,
 | 
      ||
| 165 |     //          msg->type==humotion::gaze::GAZETYPE_ABSOLUTE?"ABSOLUTE":"RELATIVE");
 | 
      ||
| 166 | 8c6c1163 | Simon Schulz | |
| 167 | gaze_state.pan = msg->pan;  | 
      ||
| 168 | gaze_state.tilt = msg->tilt;  | 
      ||
| 169 | gaze_state.roll = msg->roll;  | 
      ||
| 170 | gaze_state.vergence = msg->vergence;  | 
      ||
| 171 | |||
| 172 | gaze_state.pan_offset = msg->pan_offset;  | 
      ||
| 173 | gaze_state.tilt_offset = msg->tilt_offset;  | 
      ||
| 174 | gaze_state.roll_offset = msg->roll_offset;  | 
      ||
| 175 | |||
| 176 | gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper;  | 
      ||
| 177 | gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower;  | 
      ||
| 178 | |||
| 179 | gaze_state.eyebrow_left = msg->eyebrow_left;  | 
      ||
| 180 | gaze_state.eyebrow_right = msg->eyebrow_right;  | 
      ||
| 181 | |||
| 182 | gaze_state.eyeblink_request_left = msg->eyeblink_request_left;  | 
      ||
| 183 | gaze_state.eyeblink_request_right = msg->eyeblink_request_right;  | 
      ||
| 184 | |||
| 185 | 0c8d22a5 | sschulz |     if (msg->gaze_type == humotion::gaze::GAZETYPE_ABSOLUTE) {
 | 
      
| 186 | 1c758459 | Simon Schulz | gaze_state.gaze_type = GazeState::GAZETYPE_ABSOLUTE;  | 
      
| 187 | 0c8d22a5 | sschulz |     } else {
 | 
      
| 188 | 1c758459 | Simon Schulz | gaze_state.gaze_type = GazeState::GAZETYPE_RELATIVE;  | 
      
| 189 | }  | 
      ||
| 190 | 32327f15 | Simon Schulz | |
| 191 | gaze_state.timestamp.set(msg->gaze_timestamp.sec, msg->gaze_timestamp.nsec);  | 
      ||
| 192 | 8c6c1163 | Simon Schulz | |
| 193 | ea068cf1 | sschulz | controller_->set_gaze_target(gaze_state);  | 
      
| 194 | 8c6c1163 | Simon Schulz | }  |