humotion / src / client / middleware_ros.cpp @ 2c4bbc89
History | View | Annotate | Download (4.022 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 "client/middleware_ros.h" |
29 |
|
30 |
#ifdef ROS_SUPPORT
|
31 |
|
32 |
#include <humotion/mouth.h> |
33 |
#include <humotion/gaze.h> |
34 |
#include <boost/range/algorithm/remove_if.hpp> |
35 |
#include <boost/algorithm/string/classification.hpp> |
36 |
|
37 |
using namespace std; |
38 |
using namespace boost; |
39 |
using namespace humotion; |
40 |
using namespace humotion::client; |
41 |
|
42 |
//! constructor
|
43 |
MiddlewareROS::MiddlewareROS(string scope) : Middleware(scope){
|
44 |
//start ros core
|
45 |
if (!ros::isInitialized()){
|
46 |
tick_necessary = true;
|
47 |
string node_name = "humotion_client__"+ base_scope; |
48 |
node_name.erase(boost::remove_if(node_name, boost::is_any_of("/")), node_name.end());
|
49 |
|
50 |
ros::M_string no_remapping; |
51 |
ros::init(no_remapping, node_name); |
52 |
}else{
|
53 |
//another ros thread takes care of spinning
|
54 |
tick_necessary = false;
|
55 |
} |
56 |
|
57 |
//create node handle
|
58 |
ros::NodeHandle n; |
59 |
|
60 |
//set up publishers:
|
61 |
mouth_target_publisher = n.advertise<humotion::mouth>(base_scope + "/humotion/mouth/target", 100); |
62 |
gaze_target_publisher = n.advertise<humotion::gaze>(base_scope + "/humotion/gaze/target", 100); |
63 |
|
64 |
} |
65 |
|
66 |
//! destructor
|
67 |
MiddlewareROS::~MiddlewareROS(){ |
68 |
} |
69 |
|
70 |
//! connection ok?
|
71 |
//! \return true if conn is alive
|
72 |
bool MiddlewareROS::ok(){
|
73 |
return ros::ok();
|
74 |
} |
75 |
|
76 |
//! do a single tick
|
77 |
void MiddlewareROS::tick(){
|
78 |
if (tick_necessary){
|
79 |
ros::spinOnce(); |
80 |
} |
81 |
} |
82 |
|
83 |
|
84 |
//! send mouth target to server
|
85 |
void MiddlewareROS::send_mouth_target(){
|
86 |
//build target packet:
|
87 |
humotion::mouth msg; |
88 |
|
89 |
//set timestamp
|
90 |
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 |
//add position to send queue
|
102 |
mouth_target_publisher.publish(msg); |
103 |
|
104 |
//allow ros to handle data
|
105 |
tick(); |
106 |
} |
107 |
|
108 |
//! send mouth target to server
|
109 |
void MiddlewareROS::send_gaze_target(){
|
110 |
//build target packet:
|
111 |
humotion::gaze msg; |
112 |
|
113 |
//set timestamp
|
114 |
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 |
if (gaze_state.type == GazeState::ABSOLUTE){
|
135 |
msg.type = humotion::gaze::ABSOLUTE; |
136 |
}else{
|
137 |
msg.type = humotion::gaze::RELATIVE; |
138 |
} |
139 |
|
140 |
//add position to send queue
|
141 |
gaze_target_publisher.publish(msg); |
142 |
|
143 |
//allow ros to handle data
|
144 |
tick(); |
145 |
} |
146 |
|
147 |
#endif
|