Revision 0c8d22a5 src/server/controller.cpp

View differences:

src/server/controller.cpp
25 25
* Excellence Initiative.
26 26
*/
27 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
#include "timestamp.h"
35

  
36
using namespace std;
37
using namespace humotion;
38
using namespace humotion::server;
28
#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;
39 41

  
40 42
//! constructor
41 43
Controller::Controller(JointInterface *j) : activated(false) {
......
43 45
}
44 46

  
45 47
//! destructor
46
Controller::~Controller(){
48
Controller::~Controller() {
47 49
}
48 50

  
49 51
//! initialise motion generators
50
void Controller::init_motion_generators(){
51
    //NOTE: the order of these generators is important!
52
void Controller::init_motion_generators() {
53
    // NOTE: the order of these generators is important!
52 54
    //       (i.e. the neck generator must be added after the eye generator!)
53 55

  
54
    //1) eye motion generation:
56
    // eye motion generation:
55 57
    add_motion_generator(new EyeMotionGenerator(joint_interface));
56 58

  
57
    //2) eyelid motion generator
59
    // eyelid motion generator
58 60
    add_motion_generator(new EyelidMotionGenerator(joint_interface));
59 61

  
60
    //3) neck motion generator
62
    // neck motion generator
61 63
    add_motion_generator(new NeckMotionGenerator(joint_interface));
62 64

  
63
    //4) mouth motion generator
65
    // mouth motion generator
64 66
    add_motion_generator(new MouthMotionGenerator(joint_interface));
65 67

  
66
    //5) eyebrow motion generator
68
    // eyebrow motion generator
67 69
    add_motion_generator(new EyebrowMotionGenerator(joint_interface));
68 70
}
69 71

  
70 72
//! add a single motion genrator
71
void Controller::add_motion_generator(MotionGenerator *m){
73
void Controller::add_motion_generator(MotionGenerator *m) {
72 74
    motion_generator_vector.push_back(m);
73 75
}
74 76

  
75 77
//! calculate target angles for all motion generators:
76
void Controller::calculate_targets(){
77
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
78
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++) {
78 81
        (*it)->calculate_targets();
79 82
    }
80 83
}
81 84

  
82 85
//! publish all target angles to the devices:
83 86
//! NOTE: this is done in an extra loop to have a low delay between consequent sets:
84
void Controller::publish_targets(){
85
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
87
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++) {
86 90
        (*it)->publish_targets();
87 91
    }
88 92
}
89 93

  
90
GazeState Controller::relative_gaze_to_absolute_gaze(GazeState relative){
94
humotion::GazeState Controller::relative_gaze_to_absolute_gaze(humotion::GazeState relative) {
91 95
    double pan, tilt, roll;
92
    GazeState absolute_gaze = relative;
96
    humotion::GazeState absolute_gaze = relative;
93 97

  
94
    //incoming gaze state wants to set a relative gaze angle
95
    //in order to calc the new absolute gaze, we need to go back
96
    //in time and find out where the head was pointing at that specific time:
98
    // 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:
97 101
    Timestamp relative_target_timestamp = relative.timestamp;
98 102

  
99
    //check if this timestamp allows a valid conversion:
100
    Timestamp history_begin = joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_first_timestamp();
101
    // Timestamp history_end   = joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_last_timestamp();
102

  
103
    //printf("> incoming: %f, history is %f to %f\n",relative_target_timestamp.to_seconds(), history_begin.to_seconds(), history_end.to_seconds());
104

  
105
    //our history keeps the last n=100 elements in a timestamped list
106
    if ((relative_target_timestamp < history_begin) || (history_begin.is_null())){
107
        //when the incoming data is older than that it makes no sense
108
        //to do any guesswork and try to calculate a valid absolute target
109
        //therefore we will use the last known targets (see below)
110
        //in case we did not see this timestamp before, show a warning:
111
        if (last_known_absolute_timestamp != relative_target_timestamp){
112
            printf("> WARNING: restored/guessed absolute target for unknown timestamp %f [this should not happen]\n", relative_target_timestamp.to_seconds());
103
    // 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());
113 121
            last_known_absolute_target_pan = 0.0;
114 122
            last_known_absolute_target_tilt = 0.0;
115 123
            last_known_absolute_target_roll = 0.0;
116 124
        }
117
    }else{
118
        //all fine, we can reconstruct the absolute target:
119
        //fetch head / camera pose during that timestamp:
120
        double neck_pan  = joint_interface->get_ts_position(JointInterface::ID_NECK_PAN).get_interpolated_value(relative_target_timestamp);
121
        double eye_l_pan = joint_interface->get_ts_position(JointInterface::ID_EYES_LEFT_LR).get_interpolated_value(relative_target_timestamp);
122
        double eye_r_pan = joint_interface->get_ts_position(JointInterface::ID_EYES_RIGHT_LR).get_interpolated_value(relative_target_timestamp);
125
    } 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);
123 134
        last_known_absolute_target_pan       = neck_pan + (eye_l_pan + eye_r_pan)/2.0;
124 135
        //
125
        double neck_tilt = joint_interface->get_ts_position(JointInterface::ID_NECK_TILT).get_interpolated_value(relative_target_timestamp);
126
        double eye_tilt  = joint_interface->get_ts_position(JointInterface::ID_EYES_BOTH_UD).get_interpolated_value(relative_target_timestamp);
136
        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);
127 140
        last_known_absolute_target_tilt      = neck_tilt + eye_tilt;
128 141
        //
129
        last_known_absolute_target_roll      = joint_interface->get_ts_position(JointInterface::ID_NECK_ROLL).get_interpolated_value(relative_target_timestamp);
130
        //safe this timestamp as known:
142
        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:
131 146
        last_known_absolute_timestamp = relative_target_timestamp;
132 147
    }
133 148

  
......
135 150
    tilt = last_known_absolute_target_tilt;
136 151
    roll = last_known_absolute_target_roll;
137 152

  
138
    //substract offsets:
153
    // substract offsets
139 154
    pan  -= relative.pan_offset;
140 155
    tilt -= relative.tilt_offset;
141 156
    roll -= relative.roll_offset;
142 157

  
143 158

  
144
    //build up absolute target:
159
    // build up absolute target
145 160
    absolute_gaze.gaze_type  = GazeState::GAZETYPE_ABSOLUTE;
146 161
    absolute_gaze.pan   = pan + relative.pan;
147 162
    absolute_gaze.tilt  = tilt + relative.tilt;
148 163
    absolute_gaze.roll  = roll + relative.roll;
149
    printf("pan  now = %4.1f, rel=%4.1f ===> %4.2f\n",pan,relative.pan,absolute_gaze.pan);
150
    printf("tilt now = %4.1f, rel=%4.1f ===> %4.2f\n",tilt,relative.tilt,absolute_gaze.tilt);
164
    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);
151 166

  
152
    //FIXME: use ros TF for that calculation...
153
    //see http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%20%28C%2B%2B%29
154
    //ros::Time past = now - ros::Duration(5.0);
155
    //listener.waitForTransform("/turtle2", now,J "/turtle1", past, "/world", ros::Duration(1.0));
156
    //listener.lookupTransform("/turtle2", now, "/turtle1", past, "/world", transform);
157
    //absolute_gaze.dump();
167
    // 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();
158 173

  
159 174
    return absolute_gaze;
160 175
}
161 176

  
162 177
//! activate controller
163
void Controller::set_activated(void){
178
void Controller::set_activated(void) {
164 179
    activated = true;
165 180
}
166 181

  
167 182
//! update gaze target:
168 183
//! \param GazeState with target values for the overall gaze
169
void Controller::set_gaze_target(GazeState new_gaze_target){
170
    if (!activated){
171
        //not yet initialized, ignore incoming targets
184
void Controller::set_gaze_target(humotion::GazeState new_gaze_target) {
185
    if (!activated) {
186
        // not yet initialized, ignore incoming targets
172 187
        return;
173 188
    }
174 189

  
175
    GazeState target_gaze;
176
    //new_gaze_target.dump();
190
    humotion::GazeState target_gaze;
191
    // new_gaze_target.dump();
177 192

  
178
    //relative or absolute gaze update?
179
    if (new_gaze_target.gaze_type == GazeState::GAZETYPE_RELATIVE){
180
        //relative gaze target -> calculate target angles
193
    // relative or absolute gaze update?
194
    if (new_gaze_target.gaze_type == GazeState::GAZETYPE_RELATIVE) {
195
        // relative gaze target -> calculate target angles
181 196
        target_gaze = relative_gaze_to_absolute_gaze(new_gaze_target);
182
    }else{
183
        //already absolute gaze, set this
197
    } else {
198
        // already absolute gaze, set this
184 199
        target_gaze = new_gaze_target;
185 200
    }
186 201

  
187

  
188
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
202
    Controller::motion_generator_vector_t::iterator it;
203
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
189 204
        (*it)->set_gaze_target(target_gaze);
190 205
    }
191 206
}
192 207

  
193 208
//! update mouth state:
194 209
//! \param MouthState with target values for the mouth joints
195
void Controller::set_mouth_target(MouthState s){
196
    if (!activated){
197
        //not yet initialized, ignore incoming targets
210
void Controller::set_mouth_target(MouthState s) {
211
    if (!activated) {
212
        // not yet initialized, ignore incoming targets
198 213
        return;
199 214
    }
200 215

  
201
    for(motion_generator_vector_t::iterator it = motion_generator_vector.begin(); it<motion_generator_vector.end(); it++){
216
    Controller::motion_generator_vector_t::iterator it;
217
    for (it = motion_generator_vector.begin(); it < motion_generator_vector.end(); it++) {
202 218
        (*it)->set_mouth_target(s);
203 219
    }
204 220
}

Also available in: Unified diff