hlrc / client / cpp / src / MiddlewareRSB.cpp @ 00c26660
History | View | Annotate | Download (8.124 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 RSB_SUPPORT
|
||
30 | |||
31 | #include "MiddlewareRSB.h" |
||
32 | #include <rsb/converter/Repository.h> |
||
33 | #include <rsb/converter/ProtocolBufferConverter.h> |
||
34 | |||
35 | #include <rst/robot/EmotionState.pb.h> |
||
36 | #include <rst/robot/Animation.pb.h> |
||
37 | #include <rst/robot/GazeTarget.pb.h> |
||
38 | #include <rst/robot/MouthTarget.pb.h> |
||
39 | #include <rst/audition/Utterance.pb.h> |
||
40 | #include <boost/algorithm/string.hpp> |
||
41 | #include <boost/range/algorithm_ext/erase.hpp> |
||
42 | |||
43 | using namespace std; |
||
44 | using namespace rsb; |
||
45 | using namespace rsb::patterns; |
||
46 | |||
47 | MiddlewareRSB::MiddlewareRSB(string scope) : Middleware(scope) {
|
||
48 | printf("> new MiddlewareRSB() on base scope '%s'\n",base_scope.c_str());
|
||
49 | init(); |
||
50 | } |
||
51 | |||
52 | void MiddlewareRSB::init(void){ |
||
53 | printf("> MiddlewareRSB::init() registering converters\n");
|
||
54 | |||
55 | try{
|
||
56 | //converter for EmotionState
|
||
57 | rsb::converter::Converter<string>::Ptr emotionStateConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::EmotionState>()); |
||
58 | rsb::converter::converterRepository<string>()->registerConverter(emotionStateConverter);
|
||
59 | |||
60 | //converter for Utterance
|
||
61 | //rsb::converter::Converter<string>::Ptr UtteranceConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::Utterance>());
|
||
62 | //rsb::converter::converterRepository<string>()->registerConverter(UtteranceConverter);
|
||
63 | |||
64 | //converter for GazeTarget
|
||
65 | rsb::converter::Converter<string>::Ptr gazeTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::GazeTarget>()); |
||
66 | rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
|
||
67 | |||
68 | //converter for MouthTarget
|
||
69 | rsb::converter::Converter<string>::Ptr mouthTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>()); |
||
70 | rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
|
||
71 | |||
72 | |||
73 | //converter for Animation
|
||
74 | rsb::converter::Converter<string>::Ptr animationConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::Animation>()); |
||
75 | rsb::converter::converterRepository<string>()->registerConverter(animationConverter);
|
||
76 | |||
77 | }catch(std::invalid_argument e){
|
||
78 | printf("> converters already registered\n");
|
||
79 | } |
||
80 | |||
81 | //first get a factory instance that is used to create RSB domain objects
|
||
82 | Factory &factory = getFactory(); |
||
83 | |||
84 | //get server
|
||
85 | string scope = base_scope + "/set/"; |
||
86 | hlrc_server = factory.createRemoteServer(scope); |
||
87 | |||
88 | printf("> init done\n");
|
||
89 | |||
90 | } |
||
91 | |||
92 | void MiddlewareRSB::publish_emotion(string scope_target, RobotEmotion e, bool blocking){ |
||
93 | boost::shared_ptr<rst::robot::EmotionState> request(new rst::robot::EmotionState());
|
||
94 | |||
95 | switch(e.value){
|
||
96 | default:
|
||
97 | printf("> WANRING: invalid emotion id %d. defaulting to NEUTRAL\n",e.value);
|
||
98 | //fall through:
|
||
99 | case(RobotEmotion::NEUTRAL):
|
||
100 | request->set_value(rst::robot::EmotionState::NEUTRAL); |
||
101 | break;
|
||
102 | case(RobotEmotion::HAPPY):
|
||
103 | request->set_value(rst::robot::EmotionState::HAPPY); |
||
104 | break;
|
||
105 | case(RobotEmotion::SAD):
|
||
106 | request->set_value(rst::robot::EmotionState::SAD); |
||
107 | break;
|
||
108 | case(RobotEmotion::ANGRY):
|
||
109 | request->set_value(rst::robot::EmotionState::ANGRY); |
||
110 | break;
|
||
111 | case(RobotEmotion::SURPRISED):
|
||
112 | request->set_value(rst::robot::EmotionState::SURPRISED); |
||
113 | break;
|
||
114 | case(RobotEmotion::FEAR):
|
||
115 | request->set_value(rst::robot::EmotionState::FEAR); |
||
116 | break;
|
||
117 | } |
||
118 | |||
119 | request->set_duration(e.time_ms); |
||
120 | |||
121 | if (blocking){
|
||
122 | hlrc_server->call<rst::robot::EmotionState>(scope_target, request); |
||
123 | }else{
|
||
124 | hlrc_server->callAsync<rst::robot::EmotionState>(scope_target, request); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | |||
129 | void MiddlewareRSB::publish_current_emotion(RobotEmotion e, bool blocking){ |
||
130 | publish_emotion("currentEmotion", e, blocking);
|
||
131 | } |
||
132 | |||
133 | void MiddlewareRSB::publish_default_emotion(RobotEmotion e, bool blocking){ |
||
134 | publish_emotion("defaultEmotion", e, blocking);
|
||
135 | } |
||
136 | |||
137 | void MiddlewareRSB::publish_gaze_target(RobotGaze target, bool blocking){ |
||
138 | boost::shared_ptr<rst::robot::GazeTarget> request(new rst::robot::GazeTarget());
|
||
139 | |||
140 | request->set_pan(target.pan); |
||
141 | request->set_tilt(target.tilt); |
||
142 | request->set_roll(target.roll); |
||
143 | request->set_vergence(target.vergence); |
||
144 | request->set_pan_offset(target.pan_offset); |
||
145 | request->set_tilt_offset(target.tilt_offset); |
||
146 | |||
147 | if (blocking){
|
||
148 | hlrc_server->call<rst::robot::GazeTarget>("gaze", request);
|
||
149 | }else{
|
||
150 | hlrc_server->callAsync<rst::robot::GazeTarget>("gaze", request);
|
||
151 | } |
||
152 | } |
||
153 | |||
154 | void MiddlewareRSB::publish_mouth_target(RobotMouth target, bool blocking){ |
||
155 | boost::shared_ptr<rst::robot::MouthTarget> request(new rst::robot::MouthTarget());
|
||
156 | |||
157 | request->set_position_left( target.position_left); |
||
158 | request->set_position_center(target.position_center); |
||
159 | request->set_position_right( target.position_right); |
||
160 | |||
161 | request->set_opening_left( target.opening_left); |
||
162 | request->set_opening_center(target.opening_center); |
||
163 | request->set_opening_right( target.opening_right); |
||
164 | |||
165 | if (blocking){
|
||
166 | hlrc_server->call<rst::robot::MouthTarget>("mouth", request);
|
||
167 | }else{
|
||
168 | hlrc_server->callAsync<rst::robot::MouthTarget>("mouth", request);
|
||
169 | } |
||
170 | } |
||
171 | |||
172 | void MiddlewareRSB::publish_head_animation(RobotHeadAnimation a, bool blocking){ |
||
173 | boost::shared_ptr<rst::robot::Animation> request(new rst::robot::Animation());
|
||
174 | |||
175 | switch(a.value){
|
||
176 | default:
|
||
177 | printf("> WANRING: invalid animation id %d. defaulting to IDLE",a.value);
|
||
178 | //fall through:
|
||
179 | case(RobotHeadAnimation::IDLE):
|
||
180 | request->set_target(rst::robot::Animation::IDLE); |
||
181 | break;
|
||
182 | case(RobotHeadAnimation::HEAD_NOD):
|
||
183 | request->set_target(rst::robot::Animation::HEAD_NOD); |
||
184 | break;
|
||
185 | case(RobotHeadAnimation::HEAD_SHAKE):
|
||
186 | request->set_target(rst::robot::Animation::HEAD_SHAKE); |
||
187 | break;
|
||
188 | case(RobotHeadAnimation::EYEBLINK_L):
|
||
189 | request->set_target(rst::robot::Animation::EYEBLINK_L); |
||
190 | break;
|
||
191 | case(RobotHeadAnimation::EYEBLINK_R):
|
||
192 | request->set_target(rst::robot::Animation::EYEBLINK_R); |
||
193 | break;
|
||
194 | case(RobotHeadAnimation::EYEBLINK_BOTH):
|
||
195 | request->set_target(rst::robot::Animation::EYEBLINK_BOTH); |
||
196 | break;
|
||
197 | case(RobotHeadAnimation::EYEBROWS_RAISE):
|
||
198 | request->set_target(rst::robot::Animation::EYEBROWS_RAISE); |
||
199 | break;
|
||
200 | case(RobotHeadAnimation::EYEBROWS_LOWER):
|
||
201 | request->set_target(rst::robot::Animation::EYEBROWS_LOWER); |
||
202 | break;
|
||
203 | } |
||
204 | |||
205 | request->set_repetitions(a.repetitions); |
||
206 | request->set_scale(a.scale); |
||
207 | request->set_duration_each(a.time_ms); |
||
208 | |||
209 | if (blocking){
|
||
210 | hlrc_server->call<rst::robot::Animation>("animation", request);
|
||
211 | }else{
|
||
212 | hlrc_server->callAsync<rst::robot::Animation>("animation", request);
|
||
213 | } |
||
214 | } |
||
215 | |||
216 | void MiddlewareRSB::publish_speech(string text, bool blocking){ |
||
217 | //say it
|
||
218 | boost::shared_ptr<std::string> request(new string(text)); |
||
219 | |||
220 | if (blocking){
|
||
221 | hlrc_server->call<std::string>("speech", request); |
||
222 | }else{
|
||
223 | hlrc_server->callAsync<std::string>("speech", request); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | #endif |