Statistics
| Branch: | Tag: | Revision:

humotion / src / server / controller.cpp @ 0c8d22a5

History | View | Annotate | Download (8.608 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 0c8d22a5 sschulz
#include "humotion/server/controller.h"
29
#include "humotion/server/eye_motion_generator.h"
30
#include "humotion/server/eyebrow_motion_generator.h"
31
#include "humotion/server/eyelid_motion_generator.h"
32
#include "humotion/server/mouth_motion_generator.h"
33
#include "humotion/server/neck_motion_generator.h"
34
#include "humotion/timestamp.h"
35
36
// using namespace std;
37
// using namespace humotion;
38
// using namespace humotion::server;
39
40
using humotion::server::Controller;
41 8c6c1163 Simon Schulz
42
//! constructor
43 5d7ba19c Simon Schulz
Controller::Controller(JointInterface *j) : activated(false) {
44 8c6c1163 Simon Schulz
    joint_interface = j;
45
}
46
47
//! destructor
48 0c8d22a5 sschulz
Controller::~Controller() {
49 8c6c1163 Simon Schulz
}
50
51
//! initialise motion generators
52 0c8d22a5 sschulz
void Controller::init_motion_generators() {
53
    // NOTE: the order of these generators is important!
54 8c6c1163 Simon Schulz
    //       (i.e. the neck generator must be added after the eye generator!)
55
56 0c8d22a5 sschulz
    // eye motion generation:
57 8c6c1163 Simon Schulz
    add_motion_generator(new EyeMotionGenerator(joint_interface));
58
59 0c8d22a5 sschulz
    // eyelid motion generator
60 8c6c1163 Simon Schulz
    add_motion_generator(new EyelidMotionGenerator(joint_interface));
61
62 0c8d22a5 sschulz
    // neck motion generator
63 8c6c1163 Simon Schulz
    add_motion_generator(new NeckMotionGenerator(joint_interface));
64
65 0c8d22a5 sschulz
    // mouth motion generator
66 8c6c1163 Simon Schulz
    add_motion_generator(new MouthMotionGenerator(joint_interface));
67
68 0c8d22a5 sschulz
    // eyebrow motion generator
69 8c6c1163 Simon Schulz
    add_motion_generator(new EyebrowMotionGenerator(joint_interface));
70
}
71
72
//! add a single motion genrator
73 0c8d22a5 sschulz
void Controller::add_motion_generator(MotionGenerator *m) {
74 8c6c1163 Simon Schulz
    motion_generator_vector.push_back(m);
75
}
76
77
//! calculate target angles for all motion generators:
78 0c8d22a5 sschulz
void Controller::calculate_targets() {
79
    Controller::motion_generator_vector_t::iterator it;
80
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
81 8c6c1163 Simon Schulz
        (*it)->calculate_targets();
82
    }
83
}
84
85
//! publish all target angles to the devices:
86
//! NOTE: this is done in an extra loop to have a low delay between consequent sets:
87 0c8d22a5 sschulz
void Controller::publish_targets() {
88
    Controller::motion_generator_vector_t::iterator it;
89
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
90 8c6c1163 Simon Schulz
        (*it)->publish_targets();
91
    }
92
}
93
94 0c8d22a5 sschulz
humotion::GazeState Controller::relative_gaze_to_absolute_gaze(humotion::GazeState relative) {
95 caf7373f Simon Schulz
    double pan, tilt, roll;
96 0c8d22a5 sschulz
    humotion::GazeState absolute_gaze = relative;
97 89374d69 Simon Schulz
98 0c8d22a5 sschulz
    // incoming gaze state wants to set a relative gaze angle
99
    // in order to calc the new absolute gaze, we need to go back
100
    // in time and find out where the head was pointing at that specific time:
101 32327f15 Simon Schulz
    Timestamp relative_target_timestamp = relative.timestamp;
102 89374d69 Simon Schulz
103 0c8d22a5 sschulz
    // check if this timestamp allows a valid conversion:
104
    Timestamp history_begin =
105
            joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_first_timestamp();
106
    // Timestamp history_end   =
107
    //       joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_last_timestamp();
108
109
    // printf("> incoming: %f, history is %f to %f\n",relative_target_timestamp.to_seconds(),
110
    // history_begin.to_seconds(), history_end.to_seconds());
111
112
    // our history keeps the last n elements in a timestamped list
113
    if ((relative_target_timestamp < history_begin) || (history_begin.is_null())) {
114
        // when the incoming data is older than that it makes no sense
115
        // to do any guesswork and try to calculate a valid absolute target
116
        // therefore we will use the last known targets (see below)
117
        // in case we did not see this timestamp before, show a warning:
118
        if (last_known_absolute_timestamp != relative_target_timestamp) {
119
            printf("> WARNING: restored/guessed absolute target for unknown timestamp %f "
120
                   "[this should not happen]\n", relative_target_timestamp.to_seconds());
121 c0b29ddc Sebastian Meyer zu Borgsen
            last_known_absolute_target_pan = 0.0;
122
            last_known_absolute_target_tilt = 0.0;
123
            last_known_absolute_target_roll = 0.0;
124 caf7373f Simon Schulz
        }
125 0c8d22a5 sschulz
    } else {
126
        // all fine, we can reconstruct the absolute target
127
        // fetch head / camera pose during that timestamp
128
        double neck_pan  = joint_interface->get_ts_position(
129
                JointInterface::ID_NECK_PAN).get_interpolated_value(relative_target_timestamp);
130
        double eye_l_pan = joint_interface->get_ts_position(
131
                JointInterface::ID_EYES_LEFT_LR).get_interpolated_value(relative_target_timestamp);
132
        double eye_r_pan = joint_interface->get_ts_position(
133
                JointInterface::ID_EYES_RIGHT_LR).get_interpolated_value(relative_target_timestamp);
134 caf7373f Simon Schulz
        last_known_absolute_target_pan       = neck_pan + (eye_l_pan + eye_r_pan)/2.0;
135
        //
136 0c8d22a5 sschulz
        double neck_tilt = joint_interface->get_ts_position(
137
                JointInterface::ID_NECK_TILT).get_interpolated_value(relative_target_timestamp);
138
        double eye_tilt  = joint_interface->get_ts_position(
139
                JointInterface::ID_EYES_BOTH_UD).get_interpolated_value(relative_target_timestamp);
140 caf7373f Simon Schulz
        last_known_absolute_target_tilt      = neck_tilt + eye_tilt;
141
        //
142 0c8d22a5 sschulz
        last_known_absolute_target_roll      = joint_interface->get_ts_position(
143
                JointInterface::ID_NECK_ROLL).get_interpolated_value(relative_target_timestamp);
144
145
        // safe this timestamp as known:
146 caf7373f Simon Schulz
        last_known_absolute_timestamp = relative_target_timestamp;
147
    }
148
149
    pan  = last_known_absolute_target_pan;
150
    tilt = last_known_absolute_target_tilt;
151
    roll = last_known_absolute_target_roll;
152 89374d69 Simon Schulz
153 0c8d22a5 sschulz
    // substract offsets
154 caf7373f Simon Schulz
    pan  -= relative.pan_offset;
155 d82702d2 Sebastian Meyer zu Borgsen
    tilt -= relative.tilt_offset;
156
    roll -= relative.roll_offset;
157
158 3d980d8f Sebastian Meyer zu Borgsen
159 0c8d22a5 sschulz
    // build up absolute target
160 1c758459 Simon Schulz
    absolute_gaze.gaze_type  = GazeState::GAZETYPE_ABSOLUTE;
161 89374d69 Simon Schulz
    absolute_gaze.pan   = pan + relative.pan;
162
    absolute_gaze.tilt  = tilt + relative.tilt;
163
    absolute_gaze.roll  = roll + relative.roll;
164 0c8d22a5 sschulz
    printf("pan  now = %4.1f, rel=%4.1f ===> %4.2f\n", pan, relative.pan, absolute_gaze.pan);
165
    printf("tilt now = %4.1f, rel=%4.1f ===> %4.2f\n", tilt, relative.tilt, absolute_gaze.tilt);
166 89374d69 Simon Schulz
167 0c8d22a5 sschulz
    // FIXME: use ros TF for that calculation...
168
    // see http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%20%28C%2B%2B%29
169
    // ros::Time past = now - ros::Duration(5.0);
170
    // listener.waitForTransform("/turtle2", now,J "/turtle1", past, "/world", ros::Duration(1.0));
171
    // listener.lookupTransform("/turtle2", now, "/turtle1", past, "/world", transform);
172
    // absolute_gaze.dump();
173 caf7373f Simon Schulz
174 89374d69 Simon Schulz
    return absolute_gaze;
175
}
176
177 5d7ba19c Simon Schulz
//! activate controller
178 0c8d22a5 sschulz
void Controller::set_activated(void) {
179 5d7ba19c Simon Schulz
    activated = true;
180
}
181 8c6c1163 Simon Schulz
182
//! update gaze target:
183
//! \param GazeState with target values for the overall gaze
184 0c8d22a5 sschulz
void Controller::set_gaze_target(humotion::GazeState new_gaze_target) {
185
    if (!activated) {
186
        // not yet initialized, ignore incoming targets
187 5d7ba19c Simon Schulz
        return;
188
    }
189
190 0c8d22a5 sschulz
    humotion::GazeState target_gaze;
191
    // new_gaze_target.dump();
192 89374d69 Simon Schulz
193 0c8d22a5 sschulz
    // relative or absolute gaze update?
194
    if (new_gaze_target.gaze_type == GazeState::GAZETYPE_RELATIVE) {
195
        // relative gaze target -> calculate target angles
196 89374d69 Simon Schulz
        target_gaze = relative_gaze_to_absolute_gaze(new_gaze_target);
197 0c8d22a5 sschulz
    } else {
198
        // already absolute gaze, set this
199 89374d69 Simon Schulz
        target_gaze = new_gaze_target;
200
    }
201
202 0c8d22a5 sschulz
    Controller::motion_generator_vector_t::iterator it;
203
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
204 89374d69 Simon Schulz
        (*it)->set_gaze_target(target_gaze);
205 8c6c1163 Simon Schulz
    }
206
}
207
208
//! update mouth state:
209
//! \param MouthState with target values for the mouth joints
210 0c8d22a5 sschulz
void Controller::set_mouth_target(MouthState s) {
211
    if (!activated) {
212
        // not yet initialized, ignore incoming targets
213 5d7ba19c Simon Schulz
        return;
214
    }
215
216 0c8d22a5 sschulz
    Controller::motion_generator_vector_t::iterator it;
217
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
218 8c6c1163 Simon Schulz
        (*it)->set_mouth_target(s);
219
    }
220
}