humotion / src / client / middleware_ros.cpp @ 4b77b008
History | View | Annotate | Download (4.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 | 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 | ea068cf1 | sschulz | tick_necessary_ = true;
|
46 | 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 | ea068cf1 | sschulz | tick_necessary_ = false;
|
54 | 8c6c1163 | Simon Schulz | } |
55 | |||
56 | 0c8d22a5 | sschulz | // create node handle
|
57 | 8c6c1163 | Simon Schulz | ros::NodeHandle n; |
58 | |||
59 | 0c8d22a5 | sschulz | // set up publishers
|
60 | ea068cf1 | sschulz | mouth_target_publisher_ = n.advertise<humotion::mouth>(base_scope_ + |
61 | 0c8d22a5 | sschulz | "/humotion/mouth/target", 100); |
62 | ea068cf1 | sschulz | gaze_target_publisher_ = n.advertise<humotion::gaze>(base_scope_ + |
63 | 0c8d22a5 | sschulz | "/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 | ea068cf1 | sschulz | 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 | ea068cf1 | sschulz | msg.position.left = mouth_state_.position_left; |
93 | msg.position.center = mouth_state_.position_center; |
||
94 | msg.position.right = mouth_state_.position_right; |
||
95 | 8c6c1163 | Simon Schulz | |
96 | ea068cf1 | sschulz | msg.opening.left = mouth_state_.opening_left; |
97 | msg.opening.center = mouth_state_.opening_center; |
||
98 | msg.opening.right = mouth_state_.opening_right; |
||
99 | 8c6c1163 | Simon Schulz | |
100 | |||
101 | 0c8d22a5 | sschulz | // add position to send queue
|
102 | ea068cf1 | sschulz | mouth_target_publisher_.publish(msg); |
103 | 8c6c1163 | Simon Schulz | |
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 | ea068cf1 | sschulz | msg.pan = gaze_state_.pan; |
117 | msg.tilt = gaze_state_.tilt; |
||
118 | msg.roll = gaze_state_.roll; |
||
119 | msg.vergence = gaze_state_.vergence; |
||
120 | 8c6c1163 | Simon Schulz | |
121 | ea068cf1 | sschulz | msg.pan_offset = gaze_state_.pan_offset; |
122 | msg.tilt_offset = gaze_state_.tilt_offset; |
||
123 | msg.roll_offset = gaze_state_.roll_offset; |
||
124 | 8c6c1163 | Simon Schulz | |
125 | ea068cf1 | sschulz | msg.eyelid_opening_upper = gaze_state_.eyelid_opening_upper; |
126 | msg.eyelid_opening_lower = gaze_state_.eyelid_opening_lower; |
||
127 | 8c6c1163 | Simon Schulz | |
128 | ea068cf1 | sschulz | msg.eyebrow_left = gaze_state_.eyebrow_left; |
129 | msg.eyebrow_right = gaze_state_.eyebrow_right; |
||
130 | 8c6c1163 | Simon Schulz | |
131 | ea068cf1 | sschulz | msg.eyeblink_request_left = gaze_state_.eyeblink_request_left; |
132 | msg.eyeblink_request_right = gaze_state_.eyeblink_request_right; |
||
133 | 8c6c1163 | Simon Schulz | |
134 | ea068cf1 | 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 | ea068cf1 | sschulz | 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 | ea068cf1 | sschulz | gaze_target_publisher_.publish(msg); |
144 | 8c6c1163 | Simon Schulz | |
145 | 0c8d22a5 | sschulz | // allow ros to handle data
|
146 | 8c6c1163 | Simon Schulz | tick(); |
147 | } |