Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.399 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/gaze_motion_generator.h"
29
#include "humotion/server/server.h"
30 8c6c1163 Simon Schulz
31 0c8d22a5 sschulz
using humotion::server::GazeMotionGenerator;
32 8c6c1163 Simon Schulz
33 0c8d22a5 sschulz
// saccade detection threshold in deg/s
34
// const float GazeMotionGenerator::SACCADE_SPEED_THRESHOLD = 15.0;
35
// some constants to decide if the requested move will trigger a neck or eye saccade
36
const float GazeMotionGenerator::NECK_SACCADE_THRESHOLD      = 15.0;  // deg
37
const float GazeMotionGenerator::EYE_SACCADE_SPEED_THRESHOLD = 15.0;  // deg/s
38
39
// close to ocolumotor-range?
40
// 0.95 = reaching 95% of OMR will trigger a correction
41
const float GazeMotionGenerator::OMR_LIMIT_TRIGGERS_NECK_SACCADE = 0.95;
42 8c6c1163 Simon Schulz
43
44
//! constructor
45 32327f15 Simon Schulz
GazeMotionGenerator::GazeMotionGenerator(JointInterface *j, int dof, float t)
46 0c8d22a5 sschulz
    : ReflexxesMotionGenerator(j, dof, t) {
47 8c6c1163 Simon Schulz
}
48
49
//! destructor
50 0c8d22a5 sschulz
GazeMotionGenerator::~GazeMotionGenerator() {
51 8c6c1163 Simon Schulz
}
52
53
//! update gaze target:
54
//! \param GazeState with target values for the overall gaze
55 0c8d22a5 sschulz
void GazeMotionGenerator::set_gaze_target(GazeState new_gaze_target) {
56
    if (requested_gaze_state.gaze_type == GazeState::GAZETYPE_RELATIVE) {
57 1c758459 Simon Schulz
        printf("> ERROR: gaze targets should be converted to absolute before calling this\n");
58
        exit(EXIT_FAILURE);
59 8c6c1163 Simon Schulz
    }
60
61 0c8d22a5 sschulz
    // check magnitude of gaze change to detect eye-neck saccades
62 8c6c1163 Simon Schulz
    float dist = fabs(requested_gaze_state.distance_pt_abs(new_gaze_target));
63
64 0c8d22a5 sschulz
    // check requested speed
65
    float speed = fabs(requested_gaze_state.distance_pt_abs(new_gaze_target))
66
            / (new_gaze_target.timestamp.to_seconds()-requested_gaze_state.timestamp.to_seconds());
67 8c6c1163 Simon Schulz
68 0c8d22a5 sschulz
    // check magnitude and speed of gaze change to detect eye-neck saccades
69
    if (dist > NECK_SACCADE_THRESHOLD) {
70
        // the next saccade has to use neck motion as well
71
        if (speed > EYE_SACCADE_SPEED_THRESHOLD) {
72 8c6c1163 Simon Schulz
            neck_saccade_requested = true;
73
        }
74 0c8d22a5 sschulz
    } else {
75 8c6c1163 Simon Schulz
        neck_saccade_requested = false;
76
    }
77
78 0c8d22a5 sschulz
    // check for eye getting close to ocolomotor range
79
    // actually it makes more sense to trigger the neck saccade based on
80
    // the target getting out of the OMR
81 fdb6f148 Sebastian Meyer zu Borgsen
    float eye_target_l  = joint_interface->get_target_position(JointInterface::ID_EYES_LEFT_LR);
82
    float eye_target_r  = joint_interface->get_target_position(JointInterface::ID_EYES_RIGHT_LR);
83
    float eye_target_ud = joint_interface->get_target_position(JointInterface::ID_EYES_BOTH_UD);
84 8c6c1163 Simon Schulz
85 0c8d22a5 sschulz
    // min/max bounds
86
    float left_min  = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
87
            joint_interface->get_joint_min(JointInterface::ID_EYES_LEFT_LR);
88
    float left_max  = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
89
            joint_interface->get_joint_max(JointInterface::ID_EYES_LEFT_LR);
90
    float right_min = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
91
            joint_interface->get_joint_min(JointInterface::ID_EYES_RIGHT_LR);
92
    float right_max = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
93
            joint_interface->get_joint_max(JointInterface::ID_EYES_RIGHT_LR);
94
    float ud_min    = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
95
            joint_interface->get_joint_min(JointInterface::ID_EYES_BOTH_UD);
96
    float ud_max    = OMR_LIMIT_TRIGGERS_NECK_SACCADE *
97
            joint_interface->get_joint_max(JointInterface::ID_EYES_BOTH_UD);
98 8c6c1163 Simon Schulz
99
    if (
100 7ed40bef Simon Schulz
            (eye_target_l < left_min) || (eye_target_l > left_max) ||
101
            (eye_target_r < right_min) || (eye_target_r > right_max) ||
102 0c8d22a5 sschulz
            (eye_target_ud < ud_min) || (eye_target_ud > ud_max)) {
103
        // the eyeball gets close to OMR, activate a neck compensation motion
104 8c6c1163 Simon Schulz
        neck_saccade_omr = true;
105 0c8d22a5 sschulz
    } else {
106 8c6c1163 Simon Schulz
        neck_saccade_omr = false;
107
    }
108
109 0c8d22a5 sschulz
    // use base class set method
110 8c6c1163 Simon Schulz
    MotionGenerator::set_gaze_target(new_gaze_target);
111
}
112
113
//! check if an eye saccade is active:
114 0c8d22a5 sschulz
bool GazeMotionGenerator::get_eye_saccade_active() {
115 8c6c1163 Simon Schulz
    bool saccade_active;
116
117 0c8d22a5 sschulz
    float speed_left  = joint_interface->get_ts_speed(
118
                JointInterface::ID_EYES_LEFT_LR).get_newest_value();
119
    float speed_right = joint_interface->get_ts_speed(
120
                JointInterface::ID_EYES_RIGHT_LR).get_newest_value();
121
    float speed_tilt  = joint_interface->get_ts_speed(
122
                JointInterface::ID_EYES_BOTH_UD).get_newest_value();
123 8c6c1163 Simon Schulz
124
    float speed_total_l = sqrt(speed_left*speed_left + speed_tilt*speed_tilt);
125
    float speed_total_r = sqrt(speed_right*speed_right + speed_tilt*speed_tilt);
126
127 0c8d22a5 sschulz
    // thresholding
128
    if ((speed_total_l > EYE_SACCADE_SPEED_THRESHOLD) ||
129
            (speed_total_r > EYE_SACCADE_SPEED_THRESHOLD)) {
130
        // this is a saccade
131 8c6c1163 Simon Schulz
        saccade_active = true;
132 0c8d22a5 sschulz
    } else {
133 8c6c1163 Simon Schulz
        saccade_active = false;
134
    }
135
136
    return saccade_active;
137
}
138
139
//! get overall gaze
140 0c8d22a5 sschulz
humotion::GazeState GazeMotionGenerator::get_current_gaze() {
141
    // shortcut, makes the following easier to read
142 8c6c1163 Simon Schulz
    JointInterface *j = joint_interface;
143
144
    GazeState gaze = requested_gaze_state;
145
146
    gaze.pan = j->get_ts_position(JointInterface::ID_NECK_PAN).get_newest_value() +
147 0c8d22a5 sschulz
            (j->get_ts_position(JointInterface::ID_EYES_LEFT_LR).get_newest_value()
148
             + j->get_ts_position(JointInterface::ID_EYES_RIGHT_LR).get_newest_value())/2.0;
149
    gaze.tilt = j->get_ts_position(JointInterface::ID_NECK_TILT).get_newest_value()
150
            + j->get_ts_position(JointInterface::ID_EYES_BOTH_UD).get_newest_value();
151 8c6c1163 Simon Schulz
    gaze.roll = j->get_ts_position(JointInterface::ID_NECK_ROLL).get_newest_value();
152
153 0c8d22a5 sschulz
    gaze.vergence = j->get_ts_position(JointInterface::ID_EYES_LEFT_LR).get_newest_value()
154
            - j->get_ts_position(JointInterface::ID_EYES_RIGHT_LR).get_newest_value();
155 8c6c1163 Simon Schulz
156
    return gaze;
157
}