humotion / src / server / controller.cpp @ 51a1f01d
History | View | Annotate | Download (10.701 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 | 4b77b008 | Florian Lier | #include <string> |
29 | |||
30 | 0c8d22a5 | sschulz | #include "humotion/server/controller.h" |
31 | #include "humotion/server/eye_motion_generator.h" |
||
32 | #include "humotion/server/eyebrow_motion_generator.h" |
||
33 | #include "humotion/server/eyelid_motion_generator.h" |
||
34 | #include "humotion/server/mouth_motion_generator.h" |
||
35 | #include "humotion/server/neck_motion_generator.h" |
||
36 | #include "humotion/timestamp.h" |
||
37 | |||
38 | 4b77b008 | Florian Lier | |
39 | 0c8d22a5 | sschulz | // using namespace std;
|
40 | // using namespace humotion;
|
||
41 | // using namespace humotion::server;
|
||
42 | |||
43 | using humotion::server::Controller;
|
||
44 | 6d13138a | sschulz | using humotion::server::Config;
|
45 | 2aa96942 | sschulz | using humotion::server::debug_data_t;
|
46 | 8c6c1163 | Simon Schulz | |
47 | //! constructor
|
||
48 | ea068cf1 | sschulz | Controller::Controller(JointInterface *j) { |
49 | activated_ = false;
|
||
50 | joint_interface_ = j; |
||
51 | 6d13138a | sschulz | |
52 | config_ = new Config();
|
||
53 | 8c6c1163 | Simon Schulz | } |
54 | |||
55 | //! destructor
|
||
56 | 0c8d22a5 | sschulz | Controller::~Controller() { |
57 | 8c6c1163 | Simon Schulz | } |
58 | |||
59 | //! initialise motion generators
|
||
60 | 0c8d22a5 | sschulz | void Controller::init_motion_generators() {
|
61 | // NOTE: the order of these generators is important!
|
||
62 | 8c6c1163 | Simon Schulz | // (i.e. the neck generator must be added after the eye generator!)
|
63 | |||
64 | 0c8d22a5 | sschulz | // eye motion generation:
|
65 | 6d13138a | sschulz | add_motion_generator(new EyeMotionGenerator(joint_interface_, config_));
|
66 | 8c6c1163 | Simon Schulz | |
67 | 0c8d22a5 | sschulz | // eyelid motion generator
|
68 | 6d13138a | sschulz | add_motion_generator(new EyelidMotionGenerator(joint_interface_, config_));
|
69 | 8c6c1163 | Simon Schulz | |
70 | 0c8d22a5 | sschulz | // neck motion generator
|
71 | 6d13138a | sschulz | add_motion_generator(new NeckMotionGenerator(joint_interface_, config_));
|
72 | 8c6c1163 | Simon Schulz | |
73 | 0c8d22a5 | sschulz | // mouth motion generator
|
74 | 6d13138a | sschulz | add_motion_generator(new MouthMotionGenerator(joint_interface_, config_));
|
75 | 8c6c1163 | Simon Schulz | |
76 | 0c8d22a5 | sschulz | // eyebrow motion generator
|
77 | 6d13138a | sschulz | add_motion_generator(new EyebrowMotionGenerator(joint_interface_, config_));
|
78 | 8c6c1163 | Simon Schulz | } |
79 | |||
80 | //! add a single motion genrator
|
||
81 | 0c8d22a5 | sschulz | void Controller::add_motion_generator(MotionGenerator *m) {
|
82 | ea068cf1 | sschulz | motion_generator_vector_.push_back(m); |
83 | 8c6c1163 | Simon Schulz | } |
84 | |||
85 | //! calculate target angles for all motion generators:
|
||
86 | 0c8d22a5 | sschulz | void Controller::calculate_targets() {
|
87 | Controller::motion_generator_vector_t::iterator it; |
||
88 | ea068cf1 | sschulz | for (it = motion_generator_vector_.begin(); it < motion_generator_vector_.end(); it++) {
|
89 | 2aa96942 | sschulz | MotionGenerator *mg = *it; |
90 | // calculate targets
|
||
91 | mg->calculate_targets(); |
||
92 | 8c6c1163 | Simon Schulz | } |
93 | } |
||
94 | |||
95 | 2aa96942 | sschulz | debug_data_t Controller::get_debug_data() { |
96 | debug_data_t debug_data; |
||
97 | |||
98 | Controller::motion_generator_vector_t::iterator it; |
||
99 | for (it = motion_generator_vector_.begin(); it < motion_generator_vector_.end(); it++) {
|
||
100 | MotionGenerator *mg = *it; |
||
101 | // fetch and append debug data
|
||
102 | debug_data_t dataset = mg->get_debug_data(); |
||
103 | debug_data.insert(dataset.begin(), dataset.end()); |
||
104 | } |
||
105 | |||
106 | 4b77b008 | Florian Lier | // fetch data from controller as well
|
107 | debug_data_t controller_dataset = debug_data_; |
||
108 | debug_data.insert(controller_dataset.begin(), controller_dataset.end()); |
||
109 | |||
110 | 2aa96942 | sschulz | return debug_data;
|
111 | } |
||
112 | |||
113 | 4b77b008 | Florian Lier | //! store debug data
|
114 | void Controller::store_debug_data(std::string name, float value) { |
||
115 | debug_data_[name] = value; |
||
116 | } |
||
117 | |||
118 | 8c6c1163 | Simon Schulz | //! publish all target angles to the devices:
|
119 | //! NOTE: this is done in an extra loop to have a low delay between consequent sets:
|
||
120 | 0c8d22a5 | sschulz | void Controller::publish_targets() {
|
121 | Controller::motion_generator_vector_t::iterator it; |
||
122 | ea068cf1 | sschulz | for (it = motion_generator_vector_.begin(); it < motion_generator_vector_.end(); it++) {
|
123 | 8c6c1163 | Simon Schulz | (*it)->publish_targets(); |
124 | } |
||
125 | } |
||
126 | |||
127 | 0c8d22a5 | sschulz | humotion::GazeState Controller::relative_gaze_to_absolute_gaze(humotion::GazeState relative) { |
128 | caf7373f | Simon Schulz | double pan, tilt, roll;
|
129 | 4b77b008 | Florian Lier | double neck_pan = 0.0; |
130 | double neck_tilt = 0.0; |
||
131 | |||
132 | 0c8d22a5 | sschulz | humotion::GazeState absolute_gaze = relative; |
133 | 89374d69 | Simon Schulz | |
134 | 0c8d22a5 | sschulz | // incoming gaze state wants to set a relative gaze angle
|
135 | // in order to calc the new absolute gaze, we need to go back
|
||
136 | // in time and find out where the head was pointing at that specific time:
|
||
137 | 32327f15 | Simon Schulz | Timestamp relative_target_timestamp = relative.timestamp; |
138 | 89374d69 | Simon Schulz | |
139 | 0c8d22a5 | sschulz | // check if this timestamp allows a valid conversion:
|
140 | Timestamp history_begin = |
||
141 | ea068cf1 | sschulz | joint_interface_->get_ts_position(JointInterface::ID_NECK_PAN).get_first_timestamp(); |
142 | dbf66483 | Florian Lier | Timestamp history_end = |
143 | joint_interface_->get_ts_position(JointInterface::ID_NECK_PAN).get_last_timestamp(); |
||
144 | 0c8d22a5 | sschulz | |
145 | // printf("> incoming: %f, history is %f to %f\n",relative_target_timestamp.to_seconds(),
|
||
146 | // history_begin.to_seconds(), history_end.to_seconds());
|
||
147 | |||
148 | // our history keeps the last n elements in a timestamped list
|
||
149 | if ((relative_target_timestamp < history_begin) || (history_begin.is_null())) {
|
||
150 | // when the incoming data is older than that it makes no sense
|
||
151 | // to do any guesswork and try to calculate a valid absolute target
|
||
152 | // therefore we will use the last known targets (see below)
|
||
153 | // in case we did not see this timestamp before, show a warning:
|
||
154 | ea068cf1 | sschulz | if (last_known_absolute_timestamp_ != relative_target_timestamp) {
|
155 | dbf66483 | Florian Lier | printf("> WARNING: restored/guessed absolute target for unknown timestamp %f (tsmap = [%f - %f])"
|
156 | "[this should only happen during startup]\n", relative_target_timestamp.to_seconds(),
|
||
157 | history_begin.to_seconds(), |
||
158 | history_end.to_seconds() |
||
159 | ); |
||
160 | ea068cf1 | sschulz | last_known_absolute_target_pan_ = 0.0; |
161 | last_known_absolute_target_tilt_ = 0.0; |
||
162 | last_known_absolute_target_roll_ = 0.0; |
||
163 | caf7373f | Simon Schulz | } |
164 | 0c8d22a5 | sschulz | } else {
|
165 | // all fine, we can reconstruct the absolute target
|
||
166 | // fetch head / camera pose during that timestamp
|
||
167 | 4b77b008 | Florian Lier | neck_pan = joint_interface_->get_ts_position( |
168 | 0c8d22a5 | sschulz | JointInterface::ID_NECK_PAN).get_interpolated_value(relative_target_timestamp); |
169 | ea068cf1 | sschulz | double eye_l_pan = joint_interface_->get_ts_position(
|
170 | 0c8d22a5 | sschulz | JointInterface::ID_EYES_LEFT_LR).get_interpolated_value(relative_target_timestamp); |
171 | ea068cf1 | sschulz | double eye_r_pan = joint_interface_->get_ts_position(
|
172 | 0c8d22a5 | sschulz | JointInterface::ID_EYES_RIGHT_LR).get_interpolated_value(relative_target_timestamp); |
173 | ea068cf1 | sschulz | last_known_absolute_target_pan_ = neck_pan + (eye_l_pan + eye_r_pan)/2.0; |
174 | caf7373f | Simon Schulz | //
|
175 | 4b77b008 | Florian Lier | neck_tilt = joint_interface_->get_ts_position( |
176 | 0c8d22a5 | sschulz | JointInterface::ID_NECK_TILT).get_interpolated_value(relative_target_timestamp); |
177 | ea068cf1 | sschulz | double eye_tilt = joint_interface_->get_ts_position(
|
178 | 0c8d22a5 | sschulz | JointInterface::ID_EYES_BOTH_UD).get_interpolated_value(relative_target_timestamp); |
179 | ea068cf1 | sschulz | last_known_absolute_target_tilt_ = neck_tilt + eye_tilt; |
180 | caf7373f | Simon Schulz | //
|
181 | ea068cf1 | sschulz | last_known_absolute_target_roll_ = joint_interface_->get_ts_position( |
182 | 0c8d22a5 | sschulz | JointInterface::ID_NECK_ROLL).get_interpolated_value(relative_target_timestamp); |
183 | |||
184 | // safe this timestamp as known:
|
||
185 | ea068cf1 | sschulz | last_known_absolute_timestamp_ = relative_target_timestamp; |
186 | caf7373f | Simon Schulz | } |
187 | |||
188 | ea068cf1 | sschulz | pan = last_known_absolute_target_pan_; |
189 | tilt = last_known_absolute_target_tilt_; |
||
190 | roll = last_known_absolute_target_roll_; |
||
191 | 89374d69 | Simon Schulz | |
192 | 0c8d22a5 | sschulz | // substract offsets
|
193 | caf7373f | Simon Schulz | pan -= relative.pan_offset; |
194 | d82702d2 | Sebastian Meyer zu Borgsen | tilt -= relative.tilt_offset; |
195 | roll -= relative.roll_offset; |
||
196 | |||
197 | 0c8d22a5 | sschulz | // build up absolute target
|
198 | 1c758459 | Simon Schulz | absolute_gaze.gaze_type = GazeState::GAZETYPE_ABSOLUTE; |
199 | 89374d69 | Simon Schulz | absolute_gaze.pan = pan + relative.pan; |
200 | absolute_gaze.tilt = tilt + relative.tilt; |
||
201 | absolute_gaze.roll = roll + relative.roll; |
||
202 | 6de0fc2e | sschulz | // printf("pan now = %4.1f, rel=%4.1f ===> %4.2f\n", pan, relative.pan, absolute_gaze.pan);
|
203 | // printf("tilt now = %4.1f, rel=%4.1f ===> %4.2f\n", tilt, relative.tilt, absolute_gaze.tilt);
|
||
204 | 4b77b008 | Florian Lier | |
205 | dbf66483 | Florian Lier | // update the timestamp. this gaze target is now absolute, set appropriate timestamp
|
206 | //printf("%f %f %f 333\n", absolute_gaze.pan, absolute_gaze.tilt, absolute_gaze.roll);
|
||
207 | //absolute_gaze.tilt = 0.0;
|
||
208 | //absolute_gaze.roll = 0.0;
|
||
209 | |||
210 | //absolute_gaze.roll = absolute_gaze.pan; //0.012;
|
||
211 | absolute_gaze.timestamp = Timestamp::now(); |
||
212 | |||
213 | 4b77b008 | Florian Lier | // store debug data:
|
214 | 45091351 | sschulz | // this is the position we had at the ts of the relative target
|
215 | dbf66483 | Florian Lier | store_debug_data("controller/pan_neck_position_at_relative_ts", neck_pan);
|
216 | store_debug_data("controller/pan_overall_position_at_relative_ts", last_known_absolute_target_pan_);
|
||
217 | 45091351 | sschulz | // this is the relative movement that was requested
|
218 | store_debug_data("controller/pan_target_relative", relative.pan);
|
||
219 | // this is the calculated overall target
|
||
220 | store_debug_data("controller/pan_target", absolute_gaze.pan);
|
||
221 | 6de0fc2e | sschulz | |
222 | 0c8d22a5 | sschulz | // FIXME: use ros TF for that calculation...
|
223 | // see http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%20%28C%2B%2B%29
|
||
224 | // ros::Time past = now - ros::Duration(5.0);
|
||
225 | // listener.waitForTransform("/turtle2", now,J "/turtle1", past, "/world", ros::Duration(1.0));
|
||
226 | // listener.lookupTransform("/turtle2", now, "/turtle1", past, "/world", transform);
|
||
227 | // absolute_gaze.dump();
|
||
228 | caf7373f | Simon Schulz | |
229 | 89374d69 | Simon Schulz | return absolute_gaze;
|
230 | } |
||
231 | |||
232 | 5d7ba19c | Simon Schulz | //! activate controller
|
233 | 0c8d22a5 | sschulz | void Controller::set_activated(void) { |
234 | ea068cf1 | sschulz | activated_ = true;
|
235 | 5d7ba19c | Simon Schulz | } |
236 | 8c6c1163 | Simon Schulz | |
237 | //! update gaze target:
|
||
238 | //! \param GazeState with target values for the overall gaze
|
||
239 | 0c8d22a5 | sschulz | void Controller::set_gaze_target(humotion::GazeState new_gaze_target) {
|
240 | ea068cf1 | sschulz | if (!activated_) {
|
241 | 0c8d22a5 | sschulz | // not yet initialized, ignore incoming targets
|
242 | 5d7ba19c | Simon Schulz | return;
|
243 | } |
||
244 | |||
245 | 0c8d22a5 | sschulz | humotion::GazeState target_gaze; |
246 | // new_gaze_target.dump();
|
||
247 | 89374d69 | Simon Schulz | |
248 | 0c8d22a5 | sschulz | // relative or absolute gaze update?
|
249 | if (new_gaze_target.gaze_type == GazeState::GAZETYPE_RELATIVE) {
|
||
250 | // relative gaze target -> calculate target angles
|
||
251 | 89374d69 | Simon Schulz | target_gaze = relative_gaze_to_absolute_gaze(new_gaze_target); |
252 | 0c8d22a5 | sschulz | } else {
|
253 | // already absolute gaze, set this
|
||
254 | 89374d69 | Simon Schulz | target_gaze = new_gaze_target; |
255 | } |
||
256 | |||
257 | 0c8d22a5 | sschulz | Controller::motion_generator_vector_t::iterator it; |
258 | ea068cf1 | sschulz | for (it = motion_generator_vector_.begin(); it < motion_generator_vector_.end(); it++) {
|
259 | 89374d69 | Simon Schulz | (*it)->set_gaze_target(target_gaze); |
260 | 8c6c1163 | Simon Schulz | } |
261 | } |
||
262 | |||
263 | //! update mouth state:
|
||
264 | //! \param MouthState with target values for the mouth joints
|
||
265 | 0c8d22a5 | sschulz | void Controller::set_mouth_target(MouthState s) {
|
266 | ea068cf1 | sschulz | if (!activated_) {
|
267 | 0c8d22a5 | sschulz | // not yet initialized, ignore incoming targets
|
268 | 5d7ba19c | Simon Schulz | return;
|
269 | } |
||
270 | |||
271 | 0c8d22a5 | sschulz | Controller::motion_generator_vector_t::iterator it; |
272 | ea068cf1 | sschulz | for (it = motion_generator_vector_.begin(); it < motion_generator_vector_.end(); it++) {
|
273 | 8c6c1163 | Simon Schulz | (*it)->set_mouth_target(s); |
274 | } |
||
275 | } |
||
276 | 277050c7 | sschulz | |
277 | //! access the configuration
|
||
278 | Config* Controller::get_config() { |
||
279 | return config_;
|
||
280 | } |