Statistics
| Branch: | Tag: | Revision:

humotion / src / server / neck_motion_generator.cpp @ 79d4e516

History | View | Annotate | Download (10.455 KB)

1
/*
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 <cmath>
29

    
30
#include "humotion/server/gaze_motion_generator.h"
31
#include "humotion/server/neck_motion_generator.h"
32
#include "humotion/server/server.h"
33

    
34
using humotion::server::NeckMotionGenerator;
35
using humotion::server::Config;
36

    
37
//! constructor
38
NeckMotionGenerator::NeckMotionGenerator(JointInterface *j, Config *cfg) :
39
    GazeMotionGenerator(j, cfg, 3, 1.0/Server::MOTION_UPDATERATE) {
40
    breath_time_ = 0.0;
41
}
42

    
43

    
44
//! destructor
45
NeckMotionGenerator::~NeckMotionGenerator() {
46
}
47

    
48
//! get a breath offset angle
49
//! @return float of breath offset value
50
float NeckMotionGenerator::get_breath_offset() {
51
    // we want to have a constant acceleration
52
    // -> triangular wave as speeds -> (x<0.5)? 2*x*x:  1- 2*(1-x)**2 = 4x - 2x**2 - 1
53
    float breath_offset = 0.0;
54

    
55
    // 0...1 -> move up, 1..2 -> return, 2..3 -> still
56
    float breath_time_normalized = (breath_time_ * 3)/config->breath_period;
57

    
58
    if (breath_time_normalized <= 0.5) {
59
        // accelerated motion
60
        breath_offset = config->breath_amplitude * (2.0 * pow(breath_time_normalized, 2));
61
    } else if (breath_time_normalized <= 1.0) {
62
        // deaccelerate
63
        breath_offset = config->breath_amplitude * (1.0 - 2.0
64
                                                    * pow(1.0 - breath_time_normalized, 2));
65
    } else if (breath_time_normalized <= 1.5) {
66
        // accelerate again
67
        breath_offset = config->breath_amplitude * (1.0 - (2.0 * pow(breath_time_normalized-1, 2)));
68
    } else if (breath_time_normalized <= 2.0) {
69
        breath_offset = config->breath_amplitude * (2.0 * pow(2.0 - breath_time_normalized, 2));
70
    } else if (breath_time_normalized <= 3.0) {
71
        // pause for some time
72
        breath_offset = 0;
73
    }
74

    
75
    // fetch next time
76
    breath_time_ += 1.0/Server::MOTION_UPDATERATE;
77

    
78
    if (breath_time_ >= config->breath_period) {
79
        breath_time_ -= config->breath_period;
80
    }
81

    
82
    return breath_offset;
83
}
84

    
85

    
86
//! calculate joint targets
87
void NeckMotionGenerator::calculate_targets() {
88
    // fetch current dataset
89
    float neck_pan_now, neck_tilt_now, neck_roll_now;
90
    float neck_pan_speed, neck_tilt_speed, neck_roll_speed;
91

    
92
    humotion::Timestamp neck_pan_ts = get_timestamped_state(JointInterface::ID_NECK_PAN,
93
                                                            &neck_pan_now,
94
                                                            &neck_pan_speed);
95

    
96
    humotion::Timestamp neck_tilt_ts = get_timestamped_state(JointInterface::ID_NECK_TILT,
97
                                                            &neck_tilt_now,
98
                                                            &neck_tilt_speed);
99

    
100
    humotion::Timestamp neck_roll_ts = get_timestamped_state(JointInterface::ID_NECK_ROLL,
101
                                                            &neck_roll_now,
102
                                                            &neck_roll_speed);
103

    
104
    // reached target?
105
    float goal_diff   = fabs(get_current_gaze().distance_pt_abs(requested_gaze_state_));
106
    float target_diff = fabs(requested_gaze_state_.distance_pt_abs(previous_neck_target_));
107

    
108
    // printf("GOAL DIFF = %f TARGET DIFF = %f\n",goal_diff,target_diff);
109
    // get_current_gaze().dump();
110
    // requested_gaze_state.dump();
111

    
112
    // check if new target
113
    // close to goal?
114
    if ( (neck_saccade_active_) && (goal_diff < 1.0) ) {
115
        neck_saccade_reached_goal_ = true;
116
    }
117

    
118
    if (neck_saccade_active_) {
119
        previous_neck_target_ = requested_gaze_state_;
120
    }
121

    
122
    // if we get a new target now, we can stop the neck saccade
123
    if (target_diff > .1) {
124
        if (neck_saccade_reached_goal_) {
125
            // joint_interface->neck_saccade_done();
126
            neck_saccade_active_ = false;
127
            neck_saccade_reached_goal_ = false;
128
        }
129
    }
130

    
131
    if (neck_saccade_requested) {
132
        neck_saccade_active_ = true;
133
    }
134

    
135
    // check if this is a small or big saccade
136
    if (neck_saccade_active_ || neck_saccade_omr) {
137
        // full saccade with neck motion -> update neck target
138
        requested_neck_state_ = requested_gaze_state_;
139
    }
140

    
141
    // get targets: this is the sum of stored neck target and up-to-date offset:
142
    float neck_pan_target  = requested_neck_state_.pan  + requested_gaze_state_.pan_offset;
143
    float neck_tilt_target = requested_neck_state_.tilt + requested_gaze_state_.tilt_offset;
144

    
145
    // roll is always equal to requested gaze (not neck) state
146
    float neck_roll_target = requested_gaze_state_.roll + requested_gaze_state_.roll_offset;
147

    
148
    // add breath wave to tilt
149
    neck_tilt_target += get_breath_offset();
150

    
151
    //neck_roll_target = 0.0;
152
    //printf("%f %f %f %f ROLL\n", neck_pan_target, neck_roll_target, neck_roll_now, neck_roll_speed);
153

    
154
    // pass parameters to reflexxes api
155
    setup_neckmotion(0, neck_pan_target,  neck_pan_now,  neck_pan_speed,  neck_pan_ts);
156
    setup_neckmotion(1, neck_tilt_target, neck_tilt_now, neck_tilt_speed, neck_tilt_ts);
157
    setup_neckmotion(2, neck_roll_target, neck_roll_now, neck_roll_speed, neck_roll_ts);
158

    
159

    
160
    // call reflexxes to handle profile calculation
161
    reflexxes_calculate_profile();
162

    
163
    // tell the joint if about the new values
164
    joint_interface_->set_target(JointInterface::ID_NECK_PAN,
165
                                reflexxes_position_output->NewPositionVector->VecData[0],
166
                                reflexxes_position_output->NewVelocityVector->VecData[0]);
167

    
168
    joint_interface_->set_target(JointInterface::ID_NECK_TILT,
169
                                reflexxes_position_output->NewPositionVector->VecData[1],
170
                                reflexxes_position_output->NewVelocityVector->VecData[1]);
171

    
172
    joint_interface_->set_target(JointInterface::ID_NECK_ROLL,
173
                                reflexxes_position_output->NewPositionVector->VecData[2],
174
                                reflexxes_position_output->NewVelocityVector->VecData[2]);
175

    
176
    //printf("%f %f %f RRR\n", );
177

    
178
    /*printf("\n%f %f %f %f %f DBG\n",
179
            neck_pan_now, neck_pan_target,
180
            reflexxes_position_output->NewPositionVector->VecData[0],
181
            joint_interface_->get_ts_speed(JointInterface::ID_NECK_PAN).get_newest_value(),
182
            reflexxes_position_output->NewVelocityVector->VecData[0]
183
            );*/
184

    
185
    /*printf("\n%f %f %f %f %f #GAZELOG\n",
186
            boost::get_system_time().time_of_day().total_milliseconds()/1000.0,
187
            requested_gaze_state_.pan + requested_gaze_state_.pan_offset,
188
            get_current_gaze().pan,
189
            (get_current_position(JointInterface::ID_EYES_LEFT_LR) + get_current_position(JointInterface::ID_EYES_RIGHT_LR))/2.0,
190
            get_current_position(JointInterface::ID_NECK_PAN));*/
191

    
192
    // store debug data:
193
    store_debug_data("neck/neck_pan_now", neck_pan_now);
194
    store_debug_data("neck/neck_tilt_now", neck_tilt_now);
195
    store_debug_data("neck/neck_pan_target", neck_pan_target);
196
    store_debug_data("neck/neck_tilt_target", neck_tilt_target);
197

    
198
    store_debug_data("neck/gaze_target_pan", requested_gaze_state_.pan);
199
    store_debug_data("neck/gaze_target_tilt", requested_gaze_state_.tilt);
200

    
201
    store_debug_data("neck/output_pan", reflexxes_position_output->NewPositionVector->VecData[0]);
202
    store_debug_data("neck/output_tilt", reflexxes_position_output->NewPositionVector->VecData[1]);
203
}
204

    
205
//! publish targets to motor boards:
206
void NeckMotionGenerator::publish_targets() {
207
    // publish values if there is an active gaze input within the last timerange
208
    if (gaze_target_input_active()) {
209
        joint_interface_->publish_target(JointInterface::ID_NECK_PAN);
210
        joint_interface_->publish_target(JointInterface::ID_NECK_TILT);
211
        joint_interface_->publish_target(JointInterface::ID_NECK_ROLL);
212
    }
213
}
214

    
215

    
216
//! set up neck motion profile
217
//! this will use speed and acceleration calc formulas from literature:
218
//! \param dof id of joint
219
//! \param target angle
220
//! \param current angle
221
void NeckMotionGenerator::setup_neckmotion(int dof, float target, float current_position,
222
                                           float current_velocity, humotion::Timestamp timestamp) {
223
    // get distance to target
224
    float distance_abs = fabs(target - current_position);
225

    
226

    
227
    // get max speed: according to the equation Hmax from [guitton87] there is a linear relation
228
    // between distance_abs and v_max_head:
229
    // v_max = 4.39 * d_total + 106.0 (in degrees)
230
    float max_velocity = 4.39 * distance_abs + 106.0;
231

    
232
    // scale and limit max speed:
233
    max_velocity = max_velocity * config->scale_velocity_neck;
234
    max_velocity = fmin(max_velocity, config->limit_velocity_neck);
235

    
236
    // max accel: assuming linear acceleration we have:
237

    
238
    /* v ^  _
239
    *   |  / \
240
    *   | /   \
241
    *   |/_____\___> t
242
    */
243
    // d_total = 2 * 1/2 * a * (t_total/2)^2 = 1/4 * a * t_total^2
244
    // as we use linear accel we have
245
    // v_max = a * t_total/2  --> t_total = 2*v_max / a
246
    // combine both
247
    // d_total = 1/4 * a * 4 * vmax^2 / a^2 = v_max^2 / a
248
    // d_total = a * 2 * d_total / (v_max^2)
249
    // and therefore
250
    // a = v_max^2 / d_total
251
    float max_accel = 1.0;
252

    
253
    if (distance_abs > 0.0) {
254
        max_accel = pow(max_velocity, 2) / distance_abs;
255
    }
256

    
257
    // scale and limit acceleration
258
    max_accel = max_accel * config->scale_acceleration_neck;
259
    max_accel = fmin(max_accel, config->limit_acceleration_neck);
260

    
261
    // printf("MAX SPEED %4.2f / max accel %4.2f\n",max_speed, max_accel);
262
    // printf("%f %f %f ", distance_abs, max_velocity, max_accel);
263

    
264
    // feed reflexxes api with data
265
    reflexxes_set_input(dof, target, current_position, current_velocity,
266
                        timestamp, max_velocity, max_accel);
267
}