Statistics
| Branch: | Tag: | Revision:

humotion / src / server / eye_motion_generator.cpp @ b6e7118f

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