humotion / src / server / middleware_ros.cpp @ 0c8d22a5
History | View | Annotate | Download (4.685 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 | 8c6c1163 | Simon Schulz | |
40 | 5d7ba19c | Simon Schulz | printf("> using ROS middleware\n");
|
41 | 708960ff | Simon Schulz | |
42 | 0c8d22a5 | sschulz | // start ros core?
|
43 | if (ros::isInitialized()) {
|
||
44 | // oh another process is doing ros comm, fine, we do not need to call it
|
||
45 | 8c6c1163 | Simon Schulz | tick_necessary = false;
|
46 | 0c8d22a5 | sschulz | } 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 | 708960ff | Simon Schulz | node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
|
52 | 0c8d22a5 | sschulz | printf("> registering on ROS as node %s\n", node_name.c_str());
|
53 | 8c6c1163 | Simon Schulz | ros::M_string no_remapping; |
54 | ros::init(no_remapping, node_name); |
||
55 | tick_necessary = true;
|
||
56 | } |
||
57 | |||
58 | 0c8d22a5 | sschulz | // create node handle
|
59 | 8c6c1163 | Simon Schulz | ros::NodeHandle n; |
60 | |||
61 | 0c8d22a5 | sschulz | // set up subscribers
|
62 | mouth_target_subscriber = n.subscribe(scope + "/humotion/mouth/target", 150, |
||
63 | &MiddlewareROS::incoming_mouth_target , |
||
64 | this, ros::TransportHints().unreliable());
|
||
65 | gaze_target_subscriber = n.subscribe(scope + "/humotion/gaze/target", 150, |
||
66 | &MiddlewareROS::incoming_gaze_target, |
||
67 | this, ros::TransportHints().unreliable());
|
||
68 | 8c6c1163 | Simon Schulz | } |
69 | |||
70 | //! destructor
|
||
71 | 0c8d22a5 | sschulz | MiddlewareROS::~MiddlewareROS() { |
72 | 8c6c1163 | Simon Schulz | } |
73 | |||
74 | //! connection ok?
|
||
75 | //! \return true if conn is alive
|
||
76 | 0c8d22a5 | sschulz | bool MiddlewareROS::ok() {
|
77 | 8c6c1163 | Simon Schulz | return ros::ok();
|
78 | } |
||
79 | |||
80 | //! do a single tick
|
||
81 | 0c8d22a5 | sschulz | void MiddlewareROS::tick() {
|
82 | if (tick_necessary) {
|
||
83 | 8c6c1163 | Simon Schulz | ros::spinOnce(); |
84 | } |
||
85 | } |
||
86 | |||
87 | //! callback to handle incoming mouth target
|
||
88 | 0c8d22a5 | sschulz | void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg) { |
89 | // printf("> incoming mouth_target\n");
|
||
90 | 8c6c1163 | Simon Schulz | MouthState mouth_state; |
91 | |||
92 | mouth_state.position_left = msg->position.left; |
||
93 | mouth_state.position_center = msg->position.center; |
||
94 | mouth_state.position_right = msg->position.right; |
||
95 | |||
96 | mouth_state.opening_left = msg->opening.left; |
||
97 | mouth_state.opening_center = msg->opening.center; |
||
98 | mouth_state.opening_right = msg->opening.right; |
||
99 | |||
100 | controller->set_mouth_target(mouth_state); |
||
101 | } |
||
102 | |||
103 | |||
104 | //! callback to handle incoming gaze target
|
||
105 | 0c8d22a5 | sschulz | void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg) { |
106 | 8c6c1163 | Simon Schulz | GazeState gaze_state; |
107 | 0c8d22a5 | sschulz | // printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f] %d = %s\n",
|
108 | // msg->pan, msg->tilt, msg->roll, msg->type,
|
||
109 | // msg->type==humotion::gaze::GAZETYPE_ABSOLUTE?"ABSOLUTE":"RELATIVE");
|
||
110 | 8c6c1163 | Simon Schulz | |
111 | gaze_state.pan = msg->pan; |
||
112 | gaze_state.tilt = msg->tilt; |
||
113 | gaze_state.roll = msg->roll; |
||
114 | gaze_state.vergence = msg->vergence; |
||
115 | |||
116 | gaze_state.pan_offset = msg->pan_offset; |
||
117 | gaze_state.tilt_offset = msg->tilt_offset; |
||
118 | gaze_state.roll_offset = msg->roll_offset; |
||
119 | |||
120 | gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper; |
||
121 | gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower; |
||
122 | |||
123 | gaze_state.eyebrow_left = msg->eyebrow_left; |
||
124 | gaze_state.eyebrow_right = msg->eyebrow_right; |
||
125 | |||
126 | gaze_state.eyeblink_request_left = msg->eyeblink_request_left; |
||
127 | gaze_state.eyeblink_request_right = msg->eyeblink_request_right; |
||
128 | |||
129 | 0c8d22a5 | sschulz | if (msg->gaze_type == humotion::gaze::GAZETYPE_ABSOLUTE) {
|
130 | 1c758459 | Simon Schulz | gaze_state.gaze_type = GazeState::GAZETYPE_ABSOLUTE; |
131 | 0c8d22a5 | sschulz | } else {
|
132 | 1c758459 | Simon Schulz | gaze_state.gaze_type = GazeState::GAZETYPE_RELATIVE; |
133 | } |
||
134 | 32327f15 | Simon Schulz | |
135 | gaze_state.timestamp.set(msg->gaze_timestamp.sec, msg->gaze_timestamp.nsec); |
||
136 | 8c6c1163 | Simon Schulz | |
137 | controller->set_gaze_target(gaze_state); |
||
138 | } |