humotion / src / server / middleware_ros.cpp @ b6e7118f
History | View | Annotate | Download (6.771 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 <string> |
| 31 |
|
| 32 |
#include "humotion/server/middleware_ros.h" |
| 33 |
|
| 34 |
using humotion::server::MiddlewareROS;
|
| 35 |
|
| 36 |
//! constructor
|
| 37 |
MiddlewareROS::MiddlewareROS(std::string scope, Controller *c)
|
| 38 |
: Middleware(scope, c) {
|
| 39 |
|
| 40 |
printf("> using ROS middleware\n");
|
| 41 |
|
| 42 |
// start ros core?
|
| 43 |
if (ros::isInitialized()) {
|
| 44 |
// oh another process is doing ros comm, fine, we do not need to call it
|
| 45 |
tick_necessary_ = false;
|
| 46 |
} else {
|
| 47 |
// we have to take care of ros
|
| 48 |
printf("> no active ros middleware, calling ros::init() "
|
| 49 |
"and we will call tick() periodically!\n");
|
| 50 |
std::string node_name = "humotion_server__"+ scope; |
| 51 |
node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
|
| 52 |
printf("> registering on ROS as node %s\n", node_name.c_str());
|
| 53 |
ros::M_string no_remapping; |
| 54 |
ros::init(no_remapping, node_name); |
| 55 |
tick_necessary_ = true;
|
| 56 |
} |
| 57 |
|
| 58 |
// create node handle
|
| 59 |
ros::NodeHandle n; |
| 60 |
ros::NodeHandle pnh("~");
|
| 61 |
|
| 62 |
// set up subscribers
|
| 63 |
mouth_target_subscriber_ = n.subscribe(scope + "/humotion/mouth/target", 150, |
| 64 |
&MiddlewareROS::incoming_mouth_target , |
| 65 |
this, ros::TransportHints().unreliable());
|
| 66 |
gaze_target_subscriber_ = n.subscribe(scope + "/humotion/gaze/target", 150, |
| 67 |
&MiddlewareROS::incoming_gaze_target, |
| 68 |
this, ros::TransportHints().unreliable());
|
| 69 |
|
| 70 |
// set up dynamic reconfiguration service
|
| 71 |
attach_to_reconfiguration_server(pnh); |
| 72 |
} |
| 73 |
|
| 74 |
//! callback for incoming dynamic reconfigure requests:
|
| 75 |
void MiddlewareROS::dynamic_reconfigure_callback(const humotion::humotionConfig &config, |
| 76 |
uint32_t level) {
|
| 77 |
/*// ignore incoming requests as long cam is not set up properly
|
| 78 |
if (!hasValidHandle()) {
|
| 79 |
return;
|
| 80 |
}
|
| 81 |
|
| 82 |
// use some tricks to iterate through all config entries:
|
| 83 |
std::vector<ximea_camera::xiAPIConfig::AbstractParamDescriptionConstPtr>::const_iterator _i;
|
| 84 |
for (_i = config.__getParamDescriptions__().begin();
|
| 85 |
_i != config.__getParamDescriptions__().end(); ++_i) {
|
| 86 |
boost::any val;
|
| 87 |
boost::shared_ptr<const ximea_camera::xiAPIConfig::AbstractParamDescription>
|
| 88 |
description = *_i;
|
| 89 |
|
| 90 |
// fetch actual value:
|
| 91 |
description->getValue(config, val);
|
| 92 |
|
| 93 |
// std::cout << description->name << " " << description->type << "\n";
|
| 94 |
|
| 95 |
// copy data to ximea api:
|
| 96 |
if (description->type == "double") {
|
| 97 |
dynamicReconfigureFloat(description->name.c_str(),
|
| 98 |
static_cast<float>(boost::any_cast<double>(val)));
|
| 99 |
} else if (description->type == "bool") {
|
| 100 |
dynamicReconfigureInt(description->name.c_str(), (boost::any_cast<bool>(val))?1:0);
|
| 101 |
} else if (description->type == "int") {
|
| 102 |
dynamicReconfigureInt(description->name.c_str(), boost::any_cast<int>(val));
|
| 103 |
} else {
|
| 104 |
std::cerr << "ERROR: unsupported config type " << description->type << "\n";
|
| 105 |
}
|
| 106 |
}*/
|
| 107 |
} |
| 108 |
|
| 109 |
//! attach to dynamic reconfigure server
|
| 110 |
void MiddlewareROS::attach_to_reconfiguration_server(ros::NodeHandle priv_nodehandle) {
|
| 111 |
ROS_DEBUG("connecting to dynamic reconfiguration server");
|
| 112 |
ros::NodeHandle reconf_node(priv_nodehandle, "configuration");
|
| 113 |
|
| 114 |
reconf_server_ = new dynamic_reconfigure::Server<humotion::humotionConfig>(reconf_node);
|
| 115 |
reconf_server_->setCallback(boost::bind( |
| 116 |
&MiddlewareROS::dynamic_reconfigure_callback, this, _1, _2));
|
| 117 |
} |
| 118 |
|
| 119 |
//! destructor
|
| 120 |
MiddlewareROS::~MiddlewareROS() {
|
| 121 |
} |
| 122 |
|
| 123 |
//! connection ok?
|
| 124 |
//! \return true if conn is alive
|
| 125 |
bool MiddlewareROS::ok() {
|
| 126 |
return ros::ok();
|
| 127 |
} |
| 128 |
|
| 129 |
//! do a single tick
|
| 130 |
void MiddlewareROS::tick() {
|
| 131 |
if (tick_necessary_) {
|
| 132 |
ros::spinOnce(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
//! callback to handle incoming mouth target
|
| 137 |
void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg) { |
| 138 |
// printf("> incoming mouth_target\n");
|
| 139 |
MouthState mouth_state; |
| 140 |
|
| 141 |
mouth_state.position_left = msg->position.left; |
| 142 |
mouth_state.position_center = msg->position.center; |
| 143 |
mouth_state.position_right = msg->position.right; |
| 144 |
|
| 145 |
mouth_state.opening_left = msg->opening.left; |
| 146 |
mouth_state.opening_center = msg->opening.center; |
| 147 |
mouth_state.opening_right = msg->opening.right; |
| 148 |
|
| 149 |
controller_->set_mouth_target(mouth_state); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
//! callback to handle incoming gaze target
|
| 154 |
void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg) { |
| 155 |
GazeState gaze_state; |
| 156 |
// printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f] %d = %s\n",
|
| 157 |
// msg->pan, msg->tilt, msg->roll, msg->type,
|
| 158 |
// msg->type==humotion::gaze::GAZETYPE_ABSOLUTE?"ABSOLUTE":"RELATIVE");
|
| 159 |
|
| 160 |
gaze_state.pan = msg->pan; |
| 161 |
gaze_state.tilt = msg->tilt; |
| 162 |
gaze_state.roll = msg->roll; |
| 163 |
gaze_state.vergence = msg->vergence; |
| 164 |
|
| 165 |
gaze_state.pan_offset = msg->pan_offset; |
| 166 |
gaze_state.tilt_offset = msg->tilt_offset; |
| 167 |
gaze_state.roll_offset = msg->roll_offset; |
| 168 |
|
| 169 |
gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper; |
| 170 |
gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower; |
| 171 |
|
| 172 |
gaze_state.eyebrow_left = msg->eyebrow_left; |
| 173 |
gaze_state.eyebrow_right = msg->eyebrow_right; |
| 174 |
|
| 175 |
gaze_state.eyeblink_request_left = msg->eyeblink_request_left; |
| 176 |
gaze_state.eyeblink_request_right = msg->eyeblink_request_right; |
| 177 |
|
| 178 |
if (msg->gaze_type == humotion::gaze::GAZETYPE_ABSOLUTE) {
|
| 179 |
gaze_state.gaze_type = GazeState::GAZETYPE_ABSOLUTE; |
| 180 |
} else {
|
| 181 |
gaze_state.gaze_type = GazeState::GAZETYPE_RELATIVE; |
| 182 |
} |
| 183 |
|
| 184 |
gaze_state.timestamp.set(msg->gaze_timestamp.sec, msg->gaze_timestamp.nsec); |
| 185 |
|
| 186 |
controller_->set_gaze_target(gaze_state); |
| 187 |
} |
| 188 |
|