Statistics
| Branch: | Tag: | Revision:

humotion / src / server / neck_motion_generator.cpp @ 7ed40bef

History | View | Annotate | Download (7.785 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
#include "server/neck_motion_generator.h"
29
#include "server/gaze_motion_generator.h"
30
#include "server/server.h"
31
#include <cmath>
32
33
using namespace std;
34
using namespace humotion;
35
using namespace humotion::server;
36
37 5f29f640 Simon Schulz
const float NeckMotionGenerator::CONST_GUITTON87_A = 4.39/2.0; // / 2.0;
38
const float NeckMotionGenerator::CONST_GUITTON87_B = 106.0/2.0; // /2.0;
39 8c6c1163 Simon Schulz
40
//healthy adult human: 12-15 breaths/min (see "Ganong's review of medical physiology")
41
//total: 60/12-15 = 3-5s
42
//inhale 1.5-2s
43
//exhale 1.5-2s
44
//pause      2s
45
const float NeckMotionGenerator::CONST_BREATH_PERIOD = 1500.0+1500.0+1500.0; //given in ms
46
const float NeckMotionGenerator::CONST_BREATH_AMPLITUDE = 1.0; //degrees
47
48
49
//! constructor
50
NeckMotionGenerator::NeckMotionGenerator(JointInterface *j) : GazeMotionGenerator(j, 3, 1.0/Server::MOTION_UPDATERATE){
51
    breath_time = 0.0;
52
}
53
54
55
//! destructor
56
NeckMotionGenerator::~NeckMotionGenerator(){
57
}
58
59
//! get a breath offset angle
60
//! @return float of breath offset value
61
float NeckMotionGenerator::get_breath_offset(){
62
    //we want to have a constant acceleration -> triangular wave as speeds -> (x<0.5)? 2*x*x:  1- 2*(1-x)**2 = 4x - 2x**2 - 1
63
    float breath_offset = 0.0;
64
    float breath_time_normalized = (breath_time * 3)/CONST_BREATH_PERIOD; //0...1 -> move up, 1..2 -> return, 2..3 -> still
65
66
    if (breath_time_normalized <= 0.5){
67
        //accelerated motion:
68
        breath_offset = CONST_BREATH_AMPLITUDE * (2.0 * pow(breath_time_normalized, 2));
69
    }else if (breath_time_normalized <= 1.0){
70
        //deaccelerate:
71
        breath_offset = CONST_BREATH_AMPLITUDE * (1.0 - 2.0 * pow(1.0 - breath_time_normalized, 2));
72
    }else if (breath_time_normalized <= 1.5){
73
        //accelerate again:
74
        breath_offset = CONST_BREATH_AMPLITUDE * (1.0 - (2.0 * pow(breath_time_normalized-1, 2)));
75
    }else if (breath_time_normalized <= 2.0){
76
        breath_offset = CONST_BREATH_AMPLITUDE * (2.0 * pow(2.0 - breath_time_normalized, 2));
77
    }else if (breath_time_normalized <= 3.0){
78
        //pause for some time
79
        breath_offset = 0;
80
    }
81
82
    //fetch next time
83
    breath_time += 1000.0/Server::MOTION_UPDATERATE;
84
    if (breath_time >= CONST_BREATH_PERIOD){
85
        breath_time -= CONST_BREATH_PERIOD;
86
    }
87
88
    return breath_offset;
89
}
90
91
92
//! calculate joint targets
93
void NeckMotionGenerator::calculate_targets(){
94
    //fetch current angles:
95
    float neck_pan_now  = get_current_position(JointInterface::ID_NECK_PAN);
96
    float neck_tilt_now = get_current_position(JointInterface::ID_NECK_TILT);
97
    float neck_roll_now = get_current_position(JointInterface::ID_NECK_ROLL);
98
99
    //reached target?
100
    float goal_diff   = fabs(get_current_gaze().distance_pt_abs(requested_gaze_state));
101
    float target_diff = fabs(requested_gaze_state.distance_pt_abs(previous_neck_target));
102
103
    //printf("GOAL DIFF = %f TARGET DIFF = %f\n",goal_diff,target_diff);
104
    //get_current_gaze().dump();
105
    //requested_gaze_state.dump();
106
107
    //check if new target
108
    //close to goal?
109
    if ( (neck_saccade_active) && (goal_diff < 1.0)){
110
        neck_saccade_reached_goal = true;
111
    }
112
113
    if (neck_saccade_active){
114
        previous_neck_target = requested_gaze_state;
115
    }
116
117
    //if we get a new target now, we can stop the neck saccade
118
    if (target_diff > .1){
119
        if (neck_saccade_reached_goal){
120
            // joint_interface->neck_saccade_done();
121
            neck_saccade_active = false;
122
            neck_saccade_reached_goal = false;
123
        }
124
    }
125
126
    if (neck_saccade_requested){
127
        neck_saccade_active = true;
128
    }
129
130
    //check if this is a small or big saccade:
131
    if (neck_saccade_active || neck_saccade_omr){
132
        //full saccade with neck motion -> update neck target
133
        requested_neck_state = requested_gaze_state;
134
    }
135
136
    //get targets: this is the sum of stored neck target and up-to-date offset:
137
    float neck_pan_target  = requested_neck_state.pan  + requested_gaze_state.pan_offset;
138
    float neck_tilt_target = requested_neck_state.tilt + requested_gaze_state.tilt_offset;
139
    //roll is always equal to requested gaze (not neck) state
140
    float neck_roll_target = requested_gaze_state.roll + requested_gaze_state.roll_offset;
141
142
    //add breath wave to tilt:
143
    neck_tilt_target += get_breath_offset();
144
145
    //pass parameters to reflexxes api:
146
    setup_neckmotion(0, neck_pan_target,  neck_pan_now);
147
    setup_neckmotion(1, neck_tilt_target, neck_tilt_now);
148
    setup_neckmotion(2, neck_roll_target, neck_roll_now);
149
150
    //call reflexxes to handle profile calculation:
151
    reflexxes_calculate_profile();
152
153
    //tell the joint if about the new values:
154
    joint_interface->set_target_position(JointInterface::ID_NECK_PAN,  reflexxes_position_output->NewPositionVector->VecData[0]);
155
    joint_interface->set_target_position(JointInterface::ID_NECK_TILT, reflexxes_position_output->NewPositionVector->VecData[1]);
156
    joint_interface->set_target_position(JointInterface::ID_NECK_ROLL, reflexxes_position_output->NewPositionVector->VecData[2]);
157
}
158
159
//! publish targets to motor boards:
160
void NeckMotionGenerator::publish_targets(){
161
    //publish values if there is an active gaze input within the last timerange
162
    if (gaze_target_input_active()){
163
        joint_interface->publish_target_position(JointInterface::ID_NECK_PAN);
164
        joint_interface->publish_target_position(JointInterface::ID_NECK_TILT);
165
        joint_interface->publish_target_position(JointInterface::ID_NECK_ROLL);
166
    }
167
}
168
169
170
//! set up neck motion profile
171
//! this will use speed and acceleration calc formulas from literature:
172
//! \param dof id of joint
173
//! \param target angle
174
//! \param current angle
175
void NeckMotionGenerator::setup_neckmotion(int dof, float target, float now){
176
    //get distance to target:
177
    float distance_abs = fabs(target - now);
178
179
    //get max speed: according to [guitton87] there is a relation between distance_abs and v_max_head:
180
    //v_max = 4.39 * d_total + 106.0 (in degrees)
181
    float max_speed = (CONST_GUITTON87_A * distance_abs + CONST_GUITTON87_B);
182
183
    //max accel:         assuming linear acceleration we have
184 1c758459 Simon Schulz
    /* v ^
185
    *   |  / \
186
    *   | /   \
187
    *   |/_____\___> t
188
    */
189 8c6c1163 Simon Schulz
    // d_total = 2 * 1/2 * a * (t_total/2)^2 = 1/4 * a * t_total^2
190
    // as we use linear accel we have
191
    // v_max = a * t_total/2  --> t_total = 2*v_max / a
192
    // combine both
193
    // d_total = 1/4 * a * 4 * vmax^2 / a^2 = v_max^2 / a
194
    // d_total = a * 2 * d_total / (v_max^2)
195
    // and therefore
196
    //  a = v_max^2 / d_total
197
    float max_accel = 0.0;
198
    if (distance_abs > 0.0){
199
        max_accel = pow(max_speed, 2) / distance_abs;
200
    }
201
202 473a6a6c Simon Schulz
    //smoother motion
203
    max_accel = max_accel * 0.7; //1.0; //0.7;
204
205 8c6c1163 Simon Schulz
    //limit maximum acceleration to reduce noise FIXME!
206
    if (max_accel>1000){
207
        max_accel = 1000;
208
    }
209
210
    ///printf("MAX SPEED %4.2f / max accel %4.2f\n",max_speed, max_accel);
211
212
    //feed reflexxes api with data
213
    reflexxes_set_input(dof, target, max_speed, max_accel);
214
}