Revision 0c8d22a5 src/server/neck_motion_generator.cpp

View differences:

src/server/neck_motion_generator.cpp
25 25
* Excellence Initiative.
26 26
*/
27 27

  
28
#include "server/neck_motion_generator.h"
29
#include "server/gaze_motion_generator.h"
30
#include "server/server.h"
31 28
#include <cmath>
32 29

  
33
using namespace std;
34
using namespace humotion;
35
using namespace humotion::server;
30
#include "humotion/server/gaze_motion_generator.h"
31
#include "humotion/server/neck_motion_generator.h"
32
#include "humotion/server/server.h"
36 33

  
37
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;
34
using humotion::server::NeckMotionGenerator;
39 35

  
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
36
const float NeckMotionGenerator::CONST_GUITTON87_A = 4.39/2.0;
37
const float NeckMotionGenerator::CONST_GUITTON87_B = 106.0/2.0;
38

  
39
// healthy adult human: 12-15 breaths/min (see "Ganong's review of medical physiology")
40
// total: 60/12-15 = 3-5s
41
// inhale 1.5-2s
42
// exhale 1.5-2s
43
// pause      2s
44
const float NeckMotionGenerator::CONST_BREATH_PERIOD = 1500.0+1500.0+1500.0;  // given in ms
45
const float NeckMotionGenerator::CONST_BREATH_AMPLITUDE = 1.0;  // degrees
47 46

  
48 47

  
49 48
//! constructor
50
NeckMotionGenerator::NeckMotionGenerator(JointInterface *j) : GazeMotionGenerator(j, 3, 1.0/Server::MOTION_UPDATERATE){
49
NeckMotionGenerator::NeckMotionGenerator(JointInterface *j) :
50
    GazeMotionGenerator(j, 3, 1.0/Server::MOTION_UPDATERATE) {
51 51
    breath_time = 0.0;
52 52
}
53 53

  
54 54

  
55 55
//! destructor
56
NeckMotionGenerator::~NeckMotionGenerator(){
56
NeckMotionGenerator::~NeckMotionGenerator() {
57 57
}
58 58

  
59 59
//! get a breath offset angle
60 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
61
float NeckMotionGenerator::get_breath_offset() {
62
    // we want to have a constant acceleration
63
    // -> triangular wave as speeds -> (x<0.5)? 2*x*x:  1- 2*(1-x)**2 = 4x - 2x**2 - 1
63 64
    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
    // 0...1 -> move up, 1..2 -> return, 2..3 -> still
66
    float breath_time_normalized = (breath_time * 3)/CONST_BREATH_PERIOD;
65 67

  
66
    if (breath_time_normalized <= 0.5){
67
        //accelerated motion:
68
    if (breath_time_normalized <= 0.5) {
69
        // accelerated motion
68 70
        breath_offset = CONST_BREATH_AMPLITUDE * (2.0 * pow(breath_time_normalized, 2));
69
    }else if (breath_time_normalized <= 1.0){
70
        //deaccelerate:
71
    } else if (breath_time_normalized <= 1.0) {
72
        // deaccelerate
71 73
        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
    } else if (breath_time_normalized <= 1.5) {
75
        // accelerate again
74 76
        breath_offset = CONST_BREATH_AMPLITUDE * (1.0 - (2.0 * pow(breath_time_normalized-1, 2)));
75
    }else if (breath_time_normalized <= 2.0){
77
    } else if (breath_time_normalized <= 2.0) {
76 78
        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
    } else if (breath_time_normalized <= 3.0) {
80
        // pause for some time
79 81
        breath_offset = 0;
80 82
    }
81 83

  
82
    //fetch next time
84
    // fetch next time
83 85
    breath_time += 1000.0/Server::MOTION_UPDATERATE;
84
    if (breath_time >= CONST_BREATH_PERIOD){
86
    if (breath_time >= CONST_BREATH_PERIOD) {
85 87
        breath_time -= CONST_BREATH_PERIOD;
86 88
    }
87 89

  
......
90 92

  
91 93

  
92 94
//! calculate joint targets
93
void NeckMotionGenerator::calculate_targets(){
94
    //fetch current dataset:
95
void NeckMotionGenerator::calculate_targets() {
96
    // fetch current dataset
95 97
    float neck_pan_now, neck_tilt_now, neck_roll_now;
96 98
    float neck_pan_speed, neck_tilt_speed, neck_roll_speed;
97 99

  
......
107 109
                                                            &neck_roll_now,
108 110
                                                            &neck_roll_speed);
109 111

  
110
    //reached target?
112
    // reached target?
111 113
    float goal_diff   = fabs(get_current_gaze().distance_pt_abs(requested_gaze_state));
112 114
    float target_diff = fabs(requested_gaze_state.distance_pt_abs(previous_neck_target));
113 115

  
114
    //printf("GOAL DIFF = %f TARGET DIFF = %f\n",goal_diff,target_diff);
115
    //get_current_gaze().dump();
116
    //requested_gaze_state.dump();
116
    // printf("GOAL DIFF = %f TARGET DIFF = %f\n",goal_diff,target_diff);
117
    // get_current_gaze().dump();
118
    // requested_gaze_state.dump();
117 119

  
118
    //check if new target
119
    //close to goal?
120
    if ( (neck_saccade_active) && (goal_diff < 1.0)){
120
    // check if new target
121
    // close to goal?
122
    if ( (neck_saccade_active) && (goal_diff < 1.0) ) {
121 123
        neck_saccade_reached_goal = true;
122 124
    }
123 125

  
124
    if (neck_saccade_active){
126
    if (neck_saccade_active) {
125 127
        previous_neck_target = requested_gaze_state;
126 128
    }
127 129

  
128
    //if we get a new target now, we can stop the neck saccade
129
    if (target_diff > .1){
130
        if (neck_saccade_reached_goal){
130
    // if we get a new target now, we can stop the neck saccade
131
    if (target_diff > .1) {
132
        if (neck_saccade_reached_goal) {
131 133
            // joint_interface->neck_saccade_done();
132 134
            neck_saccade_active = false;
133 135
            neck_saccade_reached_goal = false;
134 136
        }
135 137
    }
136 138

  
137
    if (neck_saccade_requested){
139
    if (neck_saccade_requested) {
138 140
        neck_saccade_active = true;
139 141
    }
140 142

  
141
    //check if this is a small or big saccade:
142
    if (neck_saccade_active || neck_saccade_omr){
143
        //full saccade with neck motion -> update neck target
143
    // check if this is a small or big saccade
144
    if (neck_saccade_active || neck_saccade_omr) {
145
        // full saccade with neck motion -> update neck target
144 146
        requested_neck_state = requested_gaze_state;
145 147
    }
146 148

  
147
    //get targets: this is the sum of stored neck target and up-to-date offset:
149
    // get targets: this is the sum of stored neck target and up-to-date offset:
148 150
    float neck_pan_target  = requested_neck_state.pan  + requested_gaze_state.pan_offset;
149 151
    float neck_tilt_target = requested_neck_state.tilt + requested_gaze_state.tilt_offset;
150
    //roll is always equal to requested gaze (not neck) state
152
    // roll is always equal to requested gaze (not neck) state
151 153
    float neck_roll_target = requested_gaze_state.roll + requested_gaze_state.roll_offset;
152 154

  
153
    //add breath wave to tilt:
155
    // add breath wave to tilt
154 156
    neck_tilt_target += get_breath_offset();
155 157

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

  
161
    //call reflexxes to handle profile calculation:
163
    // call reflexxes to handle profile calculation
162 164
    reflexxes_calculate_profile();
163 165

  
164
    //tell the joint if about the new values:
166
    // tell the joint if about the new values
165 167
    joint_interface->set_target(JointInterface::ID_NECK_PAN,
166 168
                                reflexxes_position_output->NewPositionVector->VecData[0],
167 169
                                reflexxes_position_output->NewVelocityVector->VecData[0]);
......
183 185
}
184 186

  
185 187
//! publish targets to motor boards:
186
void NeckMotionGenerator::publish_targets(){
187
    //publish values if there is an active gaze input within the last timerange
188
    if (gaze_target_input_active()){
188
void NeckMotionGenerator::publish_targets() {
189
    // publish values if there is an active gaze input within the last timerange
190
    if (gaze_target_input_active()) {
189 191
        joint_interface->publish_target(JointInterface::ID_NECK_PAN);
190 192
        joint_interface->publish_target(JointInterface::ID_NECK_TILT);
191 193
        joint_interface->publish_target(JointInterface::ID_NECK_ROLL);
......
199 201
//! \param target angle
200 202
//! \param current angle
201 203
void NeckMotionGenerator::setup_neckmotion(int dof, float target, float current_position,
202
                                           float current_speed, humotion::Timestamp timestamp){
203
    //get distance to target:
204
                                           float current_velocity, humotion::Timestamp timestamp) {
205
    // get distance to target
204 206
    float distance_abs = fabs(target - current_position);
205 207

  
206
    //get max speed: according to [guitton87] there is a relation between distance_abs and v_max_head:
207
    //v_max = 4.39 * d_total + 106.0 (in degrees)
208
    // get max speed: according to [guitton87] there is a relation
209
    // between distance_abs and v_max_head:
210
    // v_max = 4.39 * d_total + 106.0 (in degrees)
208 211
    float max_speed = (CONST_GUITTON87_A * distance_abs + CONST_GUITTON87_B);
209 212

  
210
    //max accel: 	assuming linear acceleration we have
211
    /* v ^
213
    // max accel: assuming linear acceleration we have:
214
    /* v ^  _
212 215
    *   |  / \
213 216
    *   | /   \
214 217
    *   |/_____\___> t
......
220 223
    // d_total = 1/4 * a * 4 * vmax^2 / a^2 = v_max^2 / a
221 224
    // d_total = a * 2 * d_total / (v_max^2)
222 225
    // and therefore
223
    //  a = v_max^2 / d_total
226
    // a = v_max^2 / d_total
224 227
    float max_accel = 0.0;
225
    if (distance_abs > 0.0){
228
    if (distance_abs > 0.0) {
226 229
        max_accel = pow(max_speed, 2) / distance_abs;
227 230
    }
228 231

  
229
    //smoother motion
230
    max_accel = max_accel * 0.7; //1.0; //0.7;
232
    // smoother motion
233
    max_accel = max_accel * 0.7;
231 234

  
232
    //limit maximum acceleration to reduce noise FIXME!
233
    if (max_accel>1000){
235
    // limit maximum acceleration to reduce noise FIXME!
236
    if (max_accel > 1000) {
234 237
        max_accel = 1000;
235 238
    }
236
    ///printf("MAX SPEED %4.2f / max accel %4.2f\n",max_speed, max_accel);
239
    // printf("MAX SPEED %4.2f / max accel %4.2f\n",max_speed, max_accel);
237 240

  
238
    //feed reflexxes api with data
239
    reflexxes_set_input(dof, target, current_position, current_speed, timestamp, max_speed, max_accel);
241
    // feed reflexxes api with data
242
    reflexxes_set_input(dof, target, current_position, current_velocity,
243
                        timestamp, max_speed, max_accel);
240 244
}

Also available in: Unified diff