humotion / src / client / middleware_ros.cpp @ 0c8d22a5
History | View | Annotate | Download (4.299 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 | 0c8d22a5 | sschulz | #include <boost/algorithm/string/classification.hpp> |
29 | #include <string> |
||
30 | 8c6c1163 | Simon Schulz | |
31 | 0c8d22a5 | sschulz | #include "humotion/client/middleware_ros.h" |
32 | 1c758459 | Simon Schulz | #include "humotion/gaze.h" |
33 | 0c8d22a5 | sschulz | #include "humotion/mouth.h" |
34 | |||
35 | 8c6c1163 | Simon Schulz | |
36 | 0c8d22a5 | sschulz | // using namespace std;
|
37 | // using namespace boost;
|
||
38 | // using namespace humotion;
|
||
39 | using humotion::client::MiddlewareROS;
|
||
40 | 8c6c1163 | Simon Schulz | |
41 | //! constructor
|
||
42 | 0c8d22a5 | sschulz | MiddlewareROS::MiddlewareROS(std::string scope) : Middleware(scope) {
|
43 | // start ros core
|
||
44 | if (!ros::isInitialized()) {
|
||
45 | 8c6c1163 | Simon Schulz | tick_necessary = true;
|
46 | 0c8d22a5 | sschulz | std::string node_name = "humotion_client__"+ base_scope; |
47 | f62f3b26 | icub | node_name.erase(std::remove(node_name.begin(), node_name.end(), '/'), node_name.end());
|
48 | 8c6c1163 | Simon Schulz | |
49 | ros::M_string no_remapping; |
||
50 | ros::init(no_remapping, node_name); |
||
51 | 0c8d22a5 | sschulz | } else {
|
52 | // another ros thread already takes care of spinning
|
||
53 | 8c6c1163 | Simon Schulz | tick_necessary = false;
|
54 | } |
||
55 | |||
56 | 0c8d22a5 | sschulz | // create node handle
|
57 | 8c6c1163 | Simon Schulz | ros::NodeHandle n; |
58 | |||
59 | 0c8d22a5 | sschulz | // set up publishers
|
60 | mouth_target_publisher = n.advertise<humotion::mouth>(base_scope + |
||
61 | "/humotion/mouth/target", 100); |
||
62 | gaze_target_publisher = n.advertise<humotion::gaze>(base_scope + |
||
63 | "/humotion/gaze/target", 100); |
||
64 | 8c6c1163 | Simon Schulz | } |
65 | |||
66 | //! destructor
|
||
67 | 0c8d22a5 | sschulz | MiddlewareROS::~MiddlewareROS() { |
68 | 8c6c1163 | Simon Schulz | } |
69 | |||
70 | //! connection ok?
|
||
71 | //! \return true if conn is alive
|
||
72 | 0c8d22a5 | sschulz | bool MiddlewareROS::ok() {
|
73 | 8c6c1163 | Simon Schulz | return ros::ok();
|
74 | } |
||
75 | |||
76 | //! do a single tick
|
||
77 | 0c8d22a5 | sschulz | void MiddlewareROS::tick() {
|
78 | if (tick_necessary) {
|
||
79 | 8c6c1163 | Simon Schulz | ros::spinOnce(); |
80 | } |
||
81 | } |
||
82 | |||
83 | |||
84 | //! send mouth target to server
|
||
85 | 0c8d22a5 | sschulz | void MiddlewareROS::send_mouth_target() {
|
86 | // build target packet
|
||
87 | 8c6c1163 | Simon Schulz | humotion::mouth msg; |
88 | |||
89 | 0c8d22a5 | sschulz | // set timestamp
|
90 | 8c6c1163 | Simon Schulz | msg.header.stamp = ros::Time::now(); |
91 | |||
92 | msg.position.left = mouth_state.position_left; |
||
93 | msg.position.center = mouth_state.position_center; |
||
94 | msg.position.right = mouth_state.position_right; |
||
95 | |||
96 | msg.opening.left = mouth_state.opening_left; |
||
97 | msg.opening.center = mouth_state.opening_center; |
||
98 | msg.opening.right = mouth_state.opening_right; |
||
99 | |||
100 | |||
101 | 0c8d22a5 | sschulz | // add position to send queue
|
102 | 8c6c1163 | Simon Schulz | mouth_target_publisher.publish(msg); |
103 | |||
104 | 0c8d22a5 | sschulz | // allow ros to handle data
|
105 | 8c6c1163 | Simon Schulz | tick(); |
106 | } |
||
107 | |||
108 | //! send mouth target to server
|
||
109 | 0c8d22a5 | sschulz | void MiddlewareROS::send_gaze_target() {
|
110 | // build target packet
|
||
111 | 8c6c1163 | Simon Schulz | humotion::gaze msg; |
112 | |||
113 | 0c8d22a5 | sschulz | // set timestamp
|
114 | 8c6c1163 | Simon Schulz | msg.header.stamp = ros::Time::now(); |
115 | |||
116 | msg.pan = gaze_state.pan; |
||
117 | msg.tilt = gaze_state.tilt; |
||
118 | msg.roll = gaze_state.roll; |
||
119 | msg.vergence = gaze_state.vergence; |
||
120 | |||
121 | msg.pan_offset = gaze_state.pan_offset; |
||
122 | msg.tilt_offset = gaze_state.tilt_offset; |
||
123 | msg.roll_offset = gaze_state.roll_offset; |
||
124 | |||
125 | msg.eyelid_opening_upper = gaze_state.eyelid_opening_upper; |
||
126 | msg.eyelid_opening_lower = gaze_state.eyelid_opening_lower; |
||
127 | |||
128 | msg.eyebrow_left = gaze_state.eyebrow_left; |
||
129 | msg.eyebrow_right = gaze_state.eyebrow_right; |
||
130 | |||
131 | msg.eyeblink_request_left = gaze_state.eyeblink_request_left; |
||
132 | msg.eyeblink_request_right = gaze_state.eyeblink_request_right; |
||
133 | |||
134 | 0c8d22a5 | sschulz | if (gaze_state.gaze_type == GazeState::GAZETYPE_ABSOLUTE) {
|
135 | 1c758459 | Simon Schulz | msg.gaze_type = humotion::gaze::GAZETYPE_ABSOLUTE; |
136 | 0c8d22a5 | sschulz | } else {
|
137 | 1c758459 | Simon Schulz | msg.gaze_type = humotion::gaze::GAZETYPE_RELATIVE; |
138 | 89374d69 | Simon Schulz | } |
139 | d21cdd06 | Simon Schulz | msg.gaze_timestamp.sec = gaze_state.timestamp.sec; |
140 | msg.gaze_timestamp.nsec = gaze_state.timestamp.nsec; |
||
141 | 8c6c1163 | Simon Schulz | |
142 | 0c8d22a5 | sschulz | // add position to send queue
|
143 | 8c6c1163 | Simon Schulz | gaze_target_publisher.publish(msg); |
144 | |||
145 | 0c8d22a5 | sschulz | // allow ros to handle data
|
146 | 8c6c1163 | Simon Schulz | tick(); |
147 | } |