hlrc / client / cpp / src / MiddlewareROS.cpp @ 5628de6c
History | View | Annotate | Download (8.653 KB)
1 | 0c286af0 | Simon Schulz | /*
|
---|---|---|---|
2 | * This file is part of hlrc
|
||
3 | *
|
||
4 | * Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
||
5 | * http://opensource.cit-ec.de/projects/hlrc
|
||
6 | *
|
||
7 | * This file may be licensed under the terms of the
|
||
8 | * GNU General Public License Version 3 (the ``GPL''),
|
||
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 GPL for the specific language
|
||
14 | * governing rights and limitations.
|
||
15 | *
|
||
16 | * You should have received a copy of the GPL along with this
|
||
17 | * program. If not, go to http://www.gnu.org/licenses/gpl.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 | |||
29 | #ifdef ROS_SUPPORT
|
||
30 | |||
31 | #include "MiddlewareROS.h" |
||
32 | #include <stdio.h> |
||
33 | #include <boost/algorithm/string.hpp> |
||
34 | #include <boost/range/algorithm_ext/erase.hpp> |
||
35 | #include <boost/range/algorithm/remove_if.hpp> |
||
36 | |||
37 | using namespace std; |
||
38 | |||
39 | MiddlewareROS::MiddlewareROS(string scope) : Middleware(scope) {
|
||
40 | printf("> new MiddlewareROS() on base scope '%s'\n",base_scope.c_str());
|
||
41 | init(); |
||
42 | } |
||
43 | |||
44 | template <typename actionspec> |
||
45 | actionlib::SimpleActionClient<actionspec> * create_action_client(string scope){
|
||
46 | printf("> starting SimpleActionClient on %s\n",scope.c_str());
|
||
47 | |||
48 | actionlib::SimpleActionClient<actionspec> *ac = new actionlib::SimpleActionClient<actionspec>(scope, true); |
||
49 | |||
50 | if (!ac->waitForServer(ros::Duration(1))){ |
||
51 | char buf[256]; |
||
52 | snprintf(buf, 256, "ERROR: action service %s not ready", scope.c_str()); |
||
53 | throw runtime_error(buf);
|
||
54 | } |
||
55 | return ac;
|
||
56 | } |
||
57 | |||
58 | void MiddlewareROS::init(void){ |
||
59 | printf("> MiddlewareROS::init()\n");
|
||
60 | |||
61 | string scope = base_scope + "/set"; |
||
62 | |||
63 | string node_name = "hlrc_client__"+ base_scope; |
||
64 | node_name.erase(boost::remove_if(node_name, boost::is_any_of("/")), node_name.end());
|
||
65 | |||
66 | printf("> registering on ROS as node %s, scope = %s\n",node_name.c_str(), scope.c_str());
|
||
67 | if (!ros::isInitialized()){
|
||
68 | ros::M_string no_remapping; |
||
69 | ros::init(no_remapping, node_name); |
||
70 | } |
||
71 | |||
72 | //create node handle
|
||
73 | //ros_node_handle = new ros::NodeHandle();
|
||
74 | //node_handle = boost::shared_ptr<ros::NodeHandle>(new ros::NodeHandle());
|
||
75 | |||
76 | printf("> setting up ROS action clients...\n");
|
||
77 | animation_ac = create_action_client<hlrc_server::animationAction>(scope + "/animation");
|
||
78 | utterance_ac = create_action_client<hlrc_server::utteranceAction>(scope + "/utterance");
|
||
79 | current_emotionstate_ac = create_action_client<hlrc_server::emotionstateAction>(scope + "/currentEmotion");
|
||
80 | default_emotionstate_ac = create_action_client<hlrc_server::emotionstateAction>(scope + "/defaultEmotion");
|
||
81 | gazetarget_ac = create_action_client<hlrc_server::gazetargetAction>(scope + "/gaze");
|
||
82 | mouthtarget_ac = create_action_client<hlrc_server::mouthtargetAction>(scope + "/mouth");
|
||
83 | speech_ac = create_action_client<hlrc_server::speechAction>(scope + "/speech");
|
||
84 | |||
85 | printf("> init done.\n");
|
||
86 | } |
||
87 | |||
88 | |||
89 | void MiddlewareROS::publish_emotion(string scope_target, RobotEmotion e, bool blocking){ |
||
90 | hlrc_server::emotionstateGoal goal; |
||
91 | |||
92 | switch(e.value){
|
||
93 | default:
|
||
94 | printf("> WANRING: invalid emotion id %d. defaulting to NEUTRAL\n",e.value);
|
||
95 | //fall through:
|
||
96 | case(RobotEmotion::NEUTRAL):
|
||
97 | goal.value = hlrc_server::emotionstateGoal::NEUTRAL; |
||
98 | break;
|
||
99 | case(RobotEmotion::HAPPY):
|
||
100 | goal.value = hlrc_server::emotionstateGoal::HAPPY; |
||
101 | break;
|
||
102 | case(RobotEmotion::SAD):
|
||
103 | goal.value = hlrc_server::emotionstateGoal::SAD; |
||
104 | break;
|
||
105 | case(RobotEmotion::ANGRY):
|
||
106 | goal.value = hlrc_server::emotionstateGoal::ANGRY; |
||
107 | break;
|
||
108 | case(RobotEmotion::SURPRISED):
|
||
109 | goal.value = hlrc_server::emotionstateGoal::SURPRISED; |
||
110 | break;
|
||
111 | case(RobotEmotion::FEAR):
|
||
112 | goal.value = hlrc_server::emotionstateGoal::FEAR; |
||
113 | break;
|
||
114 | } |
||
115 | |||
116 | goal.duration = e.time_ms; |
||
117 | |||
118 | actionlib::SimpleActionClient<hlrc_server::emotionstateAction> *ac; |
||
119 | if (scope_target == "defaultEmotion"){ |
||
120 | ac = default_emotionstate_ac; |
||
121 | }else{
|
||
122 | ac = current_emotionstate_ac; |
||
123 | } |
||
124 | |||
125 | //send
|
||
126 | ac->sendGoal(goal); |
||
127 | |||
128 | if (blocking){
|
||
129 | bool finished_before_timeout = ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
|
||
130 | if (!finished_before_timeout){
|
||
131 | printf("> ERROR: blocking call timed out!\n");
|
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | |||
137 | void MiddlewareROS::publish_current_emotion(RobotEmotion e, bool blocking){ |
||
138 | publish_emotion("currentEmotion", e, blocking);
|
||
139 | } |
||
140 | |||
141 | void MiddlewareROS::publish_default_emotion(RobotEmotion e, bool blocking){ |
||
142 | publish_emotion("defaultEmotion", e, blocking);
|
||
143 | } |
||
144 | |||
145 | void MiddlewareROS::publish_gaze_target(RobotGaze target, bool blocking){ |
||
146 | hlrc_server::gazetargetGoal goal; |
||
147 | |||
148 | goal.pan = target.pan; |
||
149 | goal.tilt = target.tilt; |
||
150 | goal.roll = target.roll; |
||
151 | goal.vergence = target.vergence; |
||
152 | |||
153 | goal.tilt_offset = target.tilt_offset; |
||
154 | goal.pan_offset = target.pan_offset; |
||
155 | |||
156 | 62aeb8ce | Simon Schulz | //timestamp:
|
157 | 3877047d | Simon Schulz | goal.gaze_timestamp.sec = target.gaze_timestamp.sec; |
158 | goal.gaze_timestamp.nsec = target.gaze_timestamp.nsec; |
||
159 | 62aeb8ce | Simon Schulz | |
160 | c5a47e56 | Simon Schulz | if (target.gaze_type == RobotGaze::ABSOLUTE){
|
161 | 62aeb8ce | Simon Schulz | goal.gaze_type = hlrc_server::gazetargetGoal::GAZETARGET_ABSOLUTE; |
162 | }else{
|
||
163 | goal.gaze_type = hlrc_server::gazetargetGoal::GAZETARGET_RELATIVE; |
||
164 | } |
||
165 | |||
166 | 0c286af0 | Simon Schulz | //send
|
167 | gazetarget_ac->sendGoal(goal); |
||
168 | |||
169 | if (blocking){
|
||
170 | bool finished_before_timeout = gazetarget_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
|
||
171 | if (!finished_before_timeout){
|
||
172 | printf("> ERROR: blocking call timed out!\n");
|
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | |||
178 | void MiddlewareROS::publish_mouth_target(RobotMouth target, bool blocking){ |
||
179 | hlrc_server::mouthtargetGoal goal; |
||
180 | |||
181 | goal.position_left = target.position_left; |
||
182 | goal.position_center = target.position_center; |
||
183 | goal.position_right = target.position_right; |
||
184 | |||
185 | goal.opening_left = target.opening_left; |
||
186 | goal.opening_center = target.opening_center; |
||
187 | goal.opening_right = target.opening_right; |
||
188 | |||
189 | |||
190 | //send
|
||
191 | mouthtarget_ac->sendGoal(goal); |
||
192 | |||
193 | if (blocking){
|
||
194 | bool finished_before_timeout = mouthtarget_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
|
||
195 | if (!finished_before_timeout){
|
||
196 | printf("> ERROR: blocking call timed out!\n");
|
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | void MiddlewareROS::publish_head_animation(RobotHeadAnimation a, bool blocking){ |
||
202 | hlrc_server::animationGoal goal; |
||
203 | |||
204 | switch(a.value){
|
||
205 | default:
|
||
206 | printf("> WANRING: invalid animation id %d. defaulting to IDLE",a.value);
|
||
207 | //fall through:
|
||
208 | case(RobotHeadAnimation::IDLE):
|
||
209 | goal.target = hlrc_server::animationGoal::IDLE; |
||
210 | break;
|
||
211 | case(RobotHeadAnimation::HEAD_NOD):
|
||
212 | goal.target = hlrc_server::animationGoal::HEAD_NOD; |
||
213 | break;
|
||
214 | case(RobotHeadAnimation::HEAD_SHAKE):
|
||
215 | goal.target = hlrc_server::animationGoal::HEAD_SHAKE; |
||
216 | break;
|
||
217 | case(RobotHeadAnimation::EYEBLINK_L):
|
||
218 | goal.target = hlrc_server::animationGoal::EYEBLINK_L; |
||
219 | break;
|
||
220 | case(RobotHeadAnimation::EYEBLINK_R):
|
||
221 | goal.target = hlrc_server::animationGoal::EYEBLINK_R; |
||
222 | break;
|
||
223 | case(RobotHeadAnimation::EYEBLINK_BOTH):
|
||
224 | goal.target = hlrc_server::animationGoal::EYEBLINK_BOTH; |
||
225 | break;
|
||
226 | case(RobotHeadAnimation::EYEBROWS_RAISE):
|
||
227 | goal.target = hlrc_server::animationGoal::EYEBROWS_RAISE; |
||
228 | break;
|
||
229 | case(RobotHeadAnimation::EYEBROWS_LOWER):
|
||
230 | goal.target = hlrc_server::animationGoal::EYEBROWS_LOWER; |
||
231 | break;
|
||
232 | } |
||
233 | |||
234 | goal.repetitions = a.repetitions; |
||
235 | goal.scale = a.scale; |
||
236 | goal.duration_each = a.time_ms; |
||
237 | |||
238 | //send
|
||
239 | animation_ac->sendGoal(goal); |
||
240 | |||
241 | if (blocking){
|
||
242 | bool finished_before_timeout = animation_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
|
||
243 | if (!finished_before_timeout){
|
||
244 | printf("> ERROR: blocking call timed out!\n");
|
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | void MiddlewareROS::publish_speech(string text, bool blocking){ |
||
250 | hlrc_server::speechGoal goal; |
||
251 | |||
252 | goal.text = text; |
||
253 | |||
254 | //send
|
||
255 | speech_ac->sendGoal(goal); |
||
256 | |||
257 | if (blocking){
|
||
258 | bool finished_before_timeout = speech_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
|
||
259 | if (!finished_before_timeout){
|
||
260 | printf("> ERROR: blocking call timed out!\n");
|
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | #endif |