Statistics
| Branch: | Tag: | Revision:

humotion / src / server / eye_motion_generator.cpp @ dbf66483

History | View | Annotate | Download (7.985 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/eye_motion_generator.h"
29
#include "humotion/server/server.h"
30 8c6c1163 Simon Schulz
31 0c8d22a5 sschulz
// using namespace std;
32
// using namespace humotion;
33
// using namespace humotion::server;
34 8c6c1163 Simon Schulz
35 0c8d22a5 sschulz
using humotion::server::EyeMotionGenerator;
36
37 8c6c1163 Simon Schulz
//! constructor
38 6d13138a sschulz
EyeMotionGenerator::EyeMotionGenerator(JointInterface *j, Config *cfg) :
39
    GazeMotionGenerator(j, cfg, 3, 1.0/Server::MOTION_UPDATERATE) {
40 8c6c1163 Simon Schulz
}
41
42
43
//! destructor
44 0c8d22a5 sschulz
EyeMotionGenerator::~EyeMotionGenerator() {
45 8c6c1163 Simon Schulz
}
46
47
//! set up an eyemotion profile
48
//! this will use speed and acceleration calc formulas from literature:
49
//! \param dof id of joint
50
//! \param target angle
51
//! \param current angle
52 730467d3 Simon Schulz
void EyeMotionGenerator::setup_eyemotion(int dof, float target,
53
                                         float current_position,
54 0c8d22a5 sschulz
                                         float current_velocity,
55
                                         humotion::Timestamp timestamp) {
56
    // get distance to target:
57 0d0f5ca1 Simon Schulz
    float distance_abs = fabs(target - current_position);
58 6d13138a sschulz
59 0c8d22a5 sschulz
    // get max speed: factor can be found in encyc britannica:
60
    // "linear.... being 300° per second for 10° and 500° per second for 30°" (max=700)
61 6d13138a sschulz
    float max_velocity = fmin(700.0, 10.0*distance_abs + 200.0);
62
63
    // scale and limit max speed
64
    max_velocity = max_velocity * config->scale_velocity_eye;
65
    max_velocity = fmin(max_velocity, config->limit_velocity_eye);
66
67 0c8d22a5 sschulz
    // max accel: use data from:
68
    // "Speed and Accuracy of Saccadic Eye Movements:
69
    //      Characteristics of Impulse Variability in the Oculomotor System"
70
    // http://www-personal.umich.edu/~kornblum/files/journal_exp_psych_HPP_15-3.pdf [table 2]
71 8c6c1163 Simon Schulz
    float max_accel = fmin(80000.0, 1526.53*distance_abs + 10245.4);
72
73 6d13138a sschulz
    // scale and limit max acceleration
74
    max_accel = max_accel * config->scale_acceleration_eye;
75
    max_accel = fmin(max_accel, config->limit_acceleration_eye);
76
77 0c8d22a5 sschulz
    // feed reflexxes api with data
78
    reflexxes_set_input(dof, target, current_position, current_velocity,
79 6d13138a sschulz
                        timestamp, max_velocity, max_accel);
80 8c6c1163 Simon Schulz
}
81
82
//! calculate joint targets
83 0c8d22a5 sschulz
void EyeMotionGenerator::calculate_targets() {
84
    // use the target values for a faster response
85 f7954788 sschulz
    float neck_pan_now  = 0.0;
86
    float neck_tilt_now = 0.0;
87
88 5cd4364c sschulz
    if (config->use_neck_target_instead_of_position_eye) {
89 f7954788 sschulz
        // position calc based on target
90
        neck_pan_now = joint_interface_->get_target_position(JointInterface::ID_NECK_PAN);
91
        neck_tilt_now = joint_interface_->get_target_position(JointInterface::ID_NECK_TILT);
92
    } else {
93
        // position calc based on real position
94
        neck_pan_now =
95
                joint_interface_->get_ts_position(JointInterface::ID_NECK_PAN).get_newest_value();
96
        neck_tilt_now =
97
                joint_interface_->get_ts_position(JointInterface::ID_NECK_TILT).get_newest_value();
98
    }
99 8c6c1163 Simon Schulz
100 0c8d22a5 sschulz
    // calculate target angles for the eyes
101
    // right eye is dominant -> direct output
102 ea068cf1 sschulz
    float eye_pan_r_target = (requested_gaze_state_.pan + requested_gaze_state_.vergence/2.0)
103 0c8d22a5 sschulz
            - (neck_pan_now);
104 8c6c1163 Simon Schulz
105 0c8d22a5 sschulz
    // left eye is non dominant -> filtered output: TODO: activate low pass filtered output
106
    // FIXME: USE LOWPASS FILTER HERE --> output_angle[angle_names::EYE_PAN_L]
107
    //        + 0.1 * (eye_pan_l_target - output_angle[angle_names::EYE_PAN_L]) etc;
108 ea068cf1 sschulz
    float eye_pan_l_target = (requested_gaze_state_.pan - requested_gaze_state_.vergence/2.0)
109 0c8d22a5 sschulz
            - (neck_pan_now);
110 8c6c1163 Simon Schulz
111 0c8d22a5 sschulz
    // tilt
112 ea068cf1 sschulz
    float eye_tilt_target = requested_gaze_state_.tilt - (neck_tilt_now);
113 8c6c1163 Simon Schulz
114 0c8d22a5 sschulz
    // check and take care of limits
115 10cae485 Sebastian Meyer zu Borgsen
    eye_pan_l_target = limit_target(JointInterface::ID_EYES_LEFT_LR, eye_pan_l_target);
116
    eye_pan_r_target = limit_target(JointInterface::ID_EYES_RIGHT_LR, eye_pan_r_target);
117
    eye_tilt_target = limit_target(JointInterface::ID_EYES_BOTH_UD, eye_tilt_target);
118
119 0c8d22a5 sschulz
    // fetch current dataset
120 730467d3 Simon Schulz
    float eye_pan_l_now, eye_pan_r_now, eye_tilt_now;
121
    float eye_pan_l_speed, eye_pan_r_speed, eye_tilt_speed;
122
123
    humotion::Timestamp eye_pan_l_ts = get_timestamped_state(
124
                JointInterface::ID_EYES_LEFT_LR,
125
                &eye_pan_l_now,
126
                &eye_pan_l_speed);
127
128
    humotion::Timestamp eye_pan_r_ts = get_timestamped_state(
129
                JointInterface::ID_EYES_RIGHT_LR,
130
                &eye_pan_r_now,
131
                &eye_pan_r_speed);
132
133
    humotion::Timestamp eye_tilt_ts = get_timestamped_state(
134
                JointInterface::ID_EYES_BOTH_UD,
135
                &eye_tilt_now,
136
                &eye_tilt_speed);
137 8c6c1163 Simon Schulz
138 0c8d22a5 sschulz
    // pass paramaters to reflexxes api
139 730467d3 Simon Schulz
    setup_eyemotion(0, eye_pan_l_target, eye_pan_l_now, eye_pan_l_speed, eye_pan_l_ts);
140
    setup_eyemotion(1, eye_pan_r_target, eye_pan_r_now, eye_pan_r_speed, eye_pan_r_ts);
141
    setup_eyemotion(2, eye_tilt_target, eye_tilt_now, eye_tilt_speed,    eye_tilt_ts);
142 0d0f5ca1 Simon Schulz
143
    // cout << "EYE MOTION 2 " << eye_tilt_target << " now=" << eye_tilt_now << "\n";
144 8c6c1163 Simon Schulz
145 0c8d22a5 sschulz
    // call reflexxes to handle profile calculation
146 8c6c1163 Simon Schulz
    reflexxes_calculate_profile();
147
148 0c8d22a5 sschulz
    // tell the joint about the new values
149 ea068cf1 sschulz
    joint_interface_->set_target(JointInterface::ID_EYES_LEFT_LR,
150 35b3ca25 Simon Schulz
                                reflexxes_position_output->NewPositionVector->VecData[0],
151
                                reflexxes_position_output->NewVelocityVector->VecData[0]);
152 ea068cf1 sschulz
    joint_interface_->set_target(JointInterface::ID_EYES_RIGHT_LR,
153 35b3ca25 Simon Schulz
                                reflexxes_position_output->NewPositionVector->VecData[1],
154
                                reflexxes_position_output->NewVelocityVector->VecData[1]);
155 ea068cf1 sschulz
    joint_interface_->set_target(JointInterface::ID_EYES_BOTH_UD,
156 35b3ca25 Simon Schulz
                                reflexxes_position_output->NewPositionVector->VecData[2],
157
                                reflexxes_position_output->NewVelocityVector->VecData[2]);
158 2aa96942 sschulz
159
    // store debug data:
160
    store_debug_data("eye/neck_pan_now", neck_pan_now);
161
    store_debug_data("eye/neck_tilt_now", neck_tilt_now);
162
    store_debug_data("eye/eye_pan_r_target", eye_pan_r_target);
163
    store_debug_data("eye/eye_pan_l_target", eye_pan_l_target);
164
    store_debug_data("eye/eye_tilt_target", eye_tilt_target);
165
166
    store_debug_data("eye/gaze_target_pan", requested_gaze_state_.pan);
167
    store_debug_data("eye/gaze_target_tilt", requested_gaze_state_.tilt);
168
169
    store_debug_data("eye/eye_pan_r_now", eye_pan_r_now);
170
    store_debug_data("eye/eye_pan_l_now", eye_pan_l_now);
171
    store_debug_data("eye/eye_tilt_now", eye_tilt_now);
172
173
    store_debug_data("eye/output_l_lr", reflexxes_position_output->NewPositionVector->VecData[0]);
174
    store_debug_data("eye/output_r_lr", reflexxes_position_output->NewPositionVector->VecData[1]);
175
    store_debug_data("eye/output_ud",   reflexxes_position_output->NewPositionVector->VecData[2]);
176 8c6c1163 Simon Schulz
}
177
178
179
//! pubish the calculated targets to the joint subsystem
180 0c8d22a5 sschulz
void EyeMotionGenerator::publish_targets() {
181
    // publish values if there is an active gaze input within the last timerange
182
    if (gaze_target_input_active()) {
183 ea068cf1 sschulz
        joint_interface_->publish_target(JointInterface::ID_EYES_LEFT_LR);
184
        joint_interface_->publish_target(JointInterface::ID_EYES_RIGHT_LR);
185
        joint_interface_->publish_target(JointInterface::ID_EYES_BOTH_UD);
186 8c6c1163 Simon Schulz
    }
187
}