humotion / src / server / middleware_rsb.cpp @ fcc5e139
History | View | Annotate | Download (5.039 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 | #include "server/middleware_rsb.h" |
||
29 | |||
30 | #ifdef RSB_SUPPORT
|
||
31 | |||
32 | #define BOOST_SIGNALS_NO_DEPRECATION_WARNING 1 |
||
33 | #include <rsb/Listener.h> |
||
34 | #include <rsb/patterns/RemoteServer.h> |
||
35 | #include <rsb/MetaData.h> |
||
36 | #include <rsb/converter/Repository.h> |
||
37 | #include <rsb/converter/ProtocolBufferConverter.h> |
||
38 | #include <rsb/Factory.h> |
||
39 | |||
40 | |||
41 | |||
42 | using namespace std; |
||
43 | using namespace boost; |
||
44 | using namespace humotion; |
||
45 | using namespace humotion::server; |
||
46 | using namespace rsb; |
||
47 | using namespace rsb::patterns; |
||
48 | |||
49 | //! constructor
|
||
50 | MiddlewareRSB::MiddlewareRSB(string scope, Controller *c) : Middleware(scope, c){
|
||
51 | dff8ce51 | Simon Schulz | printf("> using RSB middleware\n");
|
52 | 8c6c1163 | Simon Schulz | printf("> registering converters\n");
|
53 | |||
54 | //converter for GazeTarget
|
||
55 | rsb::converter::Converter<string>::Ptr gazeTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::HumotionGazeTarget>()); |
||
56 | rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
|
||
57 | |||
58 | //converter for MouthTarget
|
||
59 | rsb::converter::Converter<string>::Ptr mouthTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>()); |
||
60 | rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
|
||
61 | |||
62 | //first get a factory instance that is used to create RSB domain objects
|
||
63 | Factory &factory = getFactory(); |
||
64 | |||
65 | //create listeners
|
||
66 | mouth_target_listener = factory.createListener(base_scope + "/humotion/mouth/target");
|
||
67 | mouth_target_listener->addHandler(HandlerPtr(new DataFunctionHandler<rst::robot::MouthTarget> (boost::bind(&MiddlewareRSB::incoming_mouth_target, this, _1)))); |
||
68 | |||
69 | gaze_target_listener = factory.createListener(base_scope + "/humotion/gaze/target");
|
||
70 | gaze_target_listener->addHandler(HandlerPtr(new EventFunctionHandler(boost::bind(&MiddlewareRSB::incoming_gaze_target, this, _1)))); |
||
71 | |||
72 | //informer->publish(gaze_target);
|
||
73 | printf("> MiddlewareRSB initialised\n");
|
||
74 | } |
||
75 | |||
76 | |||
77 | //! destructor
|
||
78 | MiddlewareRSB::~MiddlewareRSB(){ |
||
79 | } |
||
80 | |||
81 | //! connection ok?
|
||
82 | //! \return true if conn is alive
|
||
83 | bool MiddlewareRSB::ok(){
|
||
84 | return true; |
||
85 | } |
||
86 | |||
87 | //! do a single tick
|
||
88 | void MiddlewareRSB::tick(){
|
||
89 | //nothing to do
|
||
90 | } |
||
91 | |||
92 | //! callback to handle incoming mouth target
|
||
93 | void MiddlewareRSB::incoming_mouth_target(boost::shared_ptr<rst::robot::MouthTarget> msg){
|
||
94 | //printf("> incoming mouth_target\n");
|
||
95 | MouthState mouth_state; |
||
96 | |||
97 | mouth_state.position_left = msg->position_left(); |
||
98 | mouth_state.position_center = msg->position_center(); |
||
99 | mouth_state.position_right = msg->position_right(); |
||
100 | |||
101 | mouth_state.opening_left = msg->opening_left(); |
||
102 | mouth_state.opening_center = msg->opening_center(); |
||
103 | mouth_state.opening_right = msg->opening_right(); |
||
104 | |||
105 | controller->set_mouth_target(mouth_state); |
||
106 | } |
||
107 | |||
108 | //! callback to handle incoming gaze target
|
||
109 | void MiddlewareRSB::incoming_gaze_target(rsb::EventPtr event){
|
||
110 | //printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f]\n", msg->pan(), msg->tilt(), msg->roll());
|
||
111 | |||
112 | boost::shared_ptr<void> ev_data = event->getData();
|
||
113 | rst::robot::HumotionGazeTarget *msg = (rst::robot::HumotionGazeTarget*) ev_data.get(); |
||
114 | |||
115 | GazeState gaze_state; |
||
116 | |||
117 | if (msg->type() == rst::robot::HumotionGazeTarget::ABSOLUTE){
|
||
118 | gaze_state.type = GazeState::ABSOLUTE; |
||
119 | }else{
|
||
120 | gaze_state.type = GazeState::RELATIVE; |
||
121 | } |
||
122 | |||
123 | gaze_state.pan = msg->pan(); |
||
124 | gaze_state.tilt = msg->tilt(); |
||
125 | gaze_state.roll = msg->roll(); |
||
126 | gaze_state.vergence = msg->vergence(); |
||
127 | |||
128 | gaze_state.pan_offset = msg->pan_offset(); |
||
129 | gaze_state.tilt_offset = msg->tilt_offset(); |
||
130 | gaze_state.roll_offset = msg->roll_offset(); |
||
131 | |||
132 | gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper(); |
||
133 | gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower(); |
||
134 | |||
135 | gaze_state.eyebrow_left = msg->eyebrow_left(); |
||
136 | gaze_state.eyebrow_right = msg->eyebrow_right(); |
||
137 | |||
138 | gaze_state.eyeblink_request_left = msg->eyeblink_request_left(); |
||
139 | gaze_state.eyeblink_request_right = msg->eyeblink_request_right(); |
||
140 | |||
141 | gaze_state.timestamp = event->getMetaData().getCreateTime() / 1000.0; //createTime() returns milliseconds -> convert to seconds |
||
142 | |||
143 | controller->set_gaze_target(gaze_state); |
||
144 | } |
||
145 | |||
146 | #endif |