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