Statistics
| Branch: | Tag: | Revision:

humotion / src / server / controller.cpp @ f10ec142

History | View | Annotate | Download (6.47 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/controller.h"
29
#include "server/eye_motion_generator.h"
30
#include "server/eyelid_motion_generator.h"
31
#include "server/eyebrow_motion_generator.h"
32
#include "server/neck_motion_generator.h"
33
#include "server/mouth_motion_generator.h"
34
35
using namespace std;
36
using namespace humotion;
37
using namespace humotion::server;
38
39
//! constructor
40 5d7ba19c Simon Schulz
Controller::Controller(JointInterface *j) : activated(false) {
41 8c6c1163 Simon Schulz
    joint_interface = j;
42
}
43
44
//! destructor
45
Controller::~Controller(){
46
}
47
48
//! initialise motion generators
49
void Controller::init_motion_generators(){
50
    //NOTE: the order of these generators is important!
51
    //       (i.e. the neck generator must be added after the eye generator!)
52
53
    //1) eye motion generation:
54
    add_motion_generator(new EyeMotionGenerator(joint_interface));
55
56
    //2) eyelid motion generator
57
    add_motion_generator(new EyelidMotionGenerator(joint_interface));
58
59
    //3) neck motion generator
60
    add_motion_generator(new NeckMotionGenerator(joint_interface));
61
62
    //4) mouth motion generator
63
    add_motion_generator(new MouthMotionGenerator(joint_interface));
64
65
    //5) eyebrow motion generator
66
    add_motion_generator(new EyebrowMotionGenerator(joint_interface));
67
}
68
69
//! add a single motion genrator
70
void Controller::add_motion_generator(MotionGenerator *m){
71
    motion_generator_vector.push_back(m);
72
}
73
74
//! calculate target angles for all motion generators:
75
void Controller::calculate_targets(){
76
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
77
        (*it)->calculate_targets();
78
    }
79
}
80
81
//! publish all target angles to the devices:
82
//! NOTE: this is done in an extra loop to have a low delay between consequent sets:
83
void Controller::publish_targets(){
84
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
85
        (*it)->publish_targets();
86
    }
87
}
88
89 89374d69 Simon Schulz
GazeState Controller::relative_gaze_to_absolute_gaze(GazeState relative){
90
    GazeState absolute_gaze = relative;
91
92
    //incoming gaze state wants to set a relative gaze angle
93
    //in order to calc the new absolute gaze, we need to go back
94
    //in time and find out where the head was pointing at that specific time:
95
    double relative_target_timestamp = relative.timestamp;
96
97
    //fetch head / camera pose during that timestamp:
98
    double neck_pan  = joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_interpolated_value(relative_target_timestamp);
99
    double eye_l_pan = joint_interface->get_ts_position(JointInterface::ID_EYES_LEFT_LR).get_interpolated_value(relative_target_timestamp);
100
    double eye_r_pan = joint_interface->get_ts_position(JointInterface::ID_EYES_RIGHT_LR).get_interpolated_value(relative_target_timestamp);
101
    double pan       = neck_pan + (eye_l_pan + eye_r_pan)/2.0;
102
    //
103
    double neck_tilt = joint_interface->get_ts_position(JointInterface::ID_NECK_TILT).get_interpolated_value(relative_target_timestamp);
104
    double eye_tilt  = joint_interface->get_ts_position(JointInterface::ID_EYES_BOTH_UD).get_interpolated_value(relative_target_timestamp);
105
    double tilt      = neck_tilt + eye_tilt;
106
    //
107
    double roll      = joint_interface->get_ts_position(JointInterface::ID_NECK_ROLL).get_interpolated_value(relative_target_timestamp);
108
109 d82702d2 Sebastian Meyer zu Borgsen
    //substract offsets:
110
    pan -= relative.pan_offset;
111
    tilt -= relative.tilt_offset;
112
    roll -= relative.roll_offset;
113
114 3d980d8f Sebastian Meyer zu Borgsen
115 89374d69 Simon Schulz
    //build up absolute target:
116 1c758459 Simon Schulz
    absolute_gaze.gaze_type  = GazeState::GAZETYPE_ABSOLUTE;
117 89374d69 Simon Schulz
    absolute_gaze.pan   = pan + relative.pan;
118
    absolute_gaze.tilt  = tilt + relative.tilt;
119
    absolute_gaze.roll  = roll + relative.roll;
120 f10ec142 Sebastian Meyer zu Borgsen
    //printf("pan now = %4.1f, rel=%4.1f ===> %4.2f\n",pan,relative.pan,absolute_gaze.pan);
121
    //printf("tilt now = %4.1f, rel=%4.1f ===> %4.2f\n",tilt,relative.tilt,absolute_gaze.tilt);
122 89374d69 Simon Schulz
123
    //FIXME: use ros TF for that calculation...
124
    //see http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%20%28C%2B%2B%29
125
    //ros::Time past = now - ros::Duration(5.0);
126
    //listener.waitForTransform("/turtle2", now,J "/turtle1", past, "/world", ros::Duration(1.0));
127
    //listener.lookupTransform("/turtle2", now, "/turtle1", past, "/world", transform);
128 d82702d2 Sebastian Meyer zu Borgsen
    absolute_gaze.dump();
129 89374d69 Simon Schulz
    return absolute_gaze;
130
}
131
132 5d7ba19c Simon Schulz
//! activate controller
133
void Controller::set_activated(void){
134
    activated = true;
135
}
136 8c6c1163 Simon Schulz
137
//! update gaze target:
138
//! \param GazeState with target values for the overall gaze
139
void Controller::set_gaze_target(GazeState new_gaze_target){
140 5d7ba19c Simon Schulz
    if (!activated){
141
        //not yet initialized, ignore incoming targets
142
        return;
143
    }
144
145 89374d69 Simon Schulz
    GazeState target_gaze;
146 fdb6f148 Sebastian Meyer zu Borgsen
    //new_gaze_target.dump();
147 89374d69 Simon Schulz
148
    //relative or absolute gaze update?
149 1c758459 Simon Schulz
    if (new_gaze_target.gaze_type == GazeState::GAZETYPE_RELATIVE){
150 89374d69 Simon Schulz
        //relative gaze target -> calculate target angles
151
        target_gaze = relative_gaze_to_absolute_gaze(new_gaze_target);
152
    }else{
153
        //already absolute gaze, set this
154
        target_gaze = new_gaze_target;
155
    }
156
157
158 8c6c1163 Simon Schulz
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
159 89374d69 Simon Schulz
        (*it)->set_gaze_target(target_gaze);
160 8c6c1163 Simon Schulz
    }
161
}
162
163
//! update mouth state:
164
//! \param MouthState with target values for the mouth joints
165
void Controller::set_mouth_target(MouthState s){
166 5d7ba19c Simon Schulz
    if (!activated){
167
        //not yet initialized, ignore incoming targets
168
        return;
169
    }
170
171 8c6c1163 Simon Schulz
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
172
        (*it)->set_mouth_target(s);
173
    }
174
}