humotion / src / server / neck_motion_generator.cpp @ ea068cf1
History | View | Annotate | Download (9.369 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 <cmath> |
||
29 | |||
30 | 0c8d22a5 | sschulz | #include "humotion/server/gaze_motion_generator.h" |
31 | #include "humotion/server/neck_motion_generator.h" |
||
32 | #include "humotion/server/server.h" |
||
33 | 8c6c1163 | Simon Schulz | |
34 | 0c8d22a5 | sschulz | using humotion::server::NeckMotionGenerator;
|
35 | 8c6c1163 | Simon Schulz | |
36 | 0c8d22a5 | sschulz | 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 |
||
46 | 8c6c1163 | Simon Schulz | |
47 | |||
48 | //! constructor
|
||
49 | 0c8d22a5 | sschulz | NeckMotionGenerator::NeckMotionGenerator(JointInterface *j) : |
50 | GazeMotionGenerator(j, 3, 1.0/Server::MOTION_UPDATERATE) { |
||
51 | ea068cf1 | sschulz | breath_time_ = 0.0; |
52 | 8c6c1163 | Simon Schulz | } |
53 | |||
54 | |||
55 | //! destructor
|
||
56 | 0c8d22a5 | sschulz | NeckMotionGenerator::~NeckMotionGenerator() { |
57 | 8c6c1163 | Simon Schulz | } |
58 | |||
59 | //! get a breath offset angle
|
||
60 | //! @return float of breath offset value
|
||
61 | 0c8d22a5 | sschulz | 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
|
||
64 | 8c6c1163 | Simon Schulz | float breath_offset = 0.0; |
65 | 0c8d22a5 | sschulz | // 0...1 -> move up, 1..2 -> return, 2..3 -> still
|
66 | ea068cf1 | sschulz | float breath_time_normalized = (breath_time_ * 3)/CONST_BREATH_PERIOD; |
67 | 8c6c1163 | Simon Schulz | |
68 | 0c8d22a5 | sschulz | if (breath_time_normalized <= 0.5) { |
69 | // accelerated motion
|
||
70 | 8c6c1163 | Simon Schulz | breath_offset = CONST_BREATH_AMPLITUDE * (2.0 * pow(breath_time_normalized, 2)); |
71 | 0c8d22a5 | sschulz | } else if (breath_time_normalized <= 1.0) { |
72 | // deaccelerate
|
||
73 | 8c6c1163 | Simon Schulz | breath_offset = CONST_BREATH_AMPLITUDE * (1.0 - 2.0 * pow(1.0 - breath_time_normalized, 2)); |
74 | 0c8d22a5 | sschulz | } else if (breath_time_normalized <= 1.5) { |
75 | // accelerate again
|
||
76 | 8c6c1163 | Simon Schulz | breath_offset = CONST_BREATH_AMPLITUDE * (1.0 - (2.0 * pow(breath_time_normalized-1, 2))); |
77 | 0c8d22a5 | sschulz | } else if (breath_time_normalized <= 2.0) { |
78 | 8c6c1163 | Simon Schulz | breath_offset = CONST_BREATH_AMPLITUDE * (2.0 * pow(2.0 - breath_time_normalized, 2)); |
79 | 0c8d22a5 | sschulz | } else if (breath_time_normalized <= 3.0) { |
80 | // pause for some time
|
||
81 | 8c6c1163 | Simon Schulz | breath_offset = 0;
|
82 | } |
||
83 | |||
84 | 0c8d22a5 | sschulz | // fetch next time
|
85 | ea068cf1 | sschulz | breath_time_ += 1000.0/Server::MOTION_UPDATERATE; |
86 | if (breath_time_ >= CONST_BREATH_PERIOD) {
|
||
87 | breath_time_ -= CONST_BREATH_PERIOD; |
||
88 | 8c6c1163 | Simon Schulz | } |
89 | |||
90 | return breath_offset;
|
||
91 | } |
||
92 | |||
93 | |||
94 | //! calculate joint targets
|
||
95 | 0c8d22a5 | sschulz | void NeckMotionGenerator::calculate_targets() {
|
96 | // fetch current dataset
|
||
97 | 730467d3 | Simon Schulz | float neck_pan_now, neck_tilt_now, neck_roll_now;
|
98 | float neck_pan_speed, neck_tilt_speed, neck_roll_speed;
|
||
99 | |||
100 | humotion::Timestamp neck_pan_ts = get_timestamped_state(JointInterface::ID_NECK_PAN, |
||
101 | &neck_pan_now, |
||
102 | &neck_pan_speed); |
||
103 | |||
104 | humotion::Timestamp neck_tilt_ts = get_timestamped_state(JointInterface::ID_NECK_TILT, |
||
105 | &neck_tilt_now, |
||
106 | &neck_tilt_speed); |
||
107 | |||
108 | humotion::Timestamp neck_roll_ts = get_timestamped_state(JointInterface::ID_NECK_ROLL, |
||
109 | &neck_roll_now, |
||
110 | &neck_roll_speed); |
||
111 | 8c6c1163 | Simon Schulz | |
112 | 0c8d22a5 | sschulz | // reached target?
|
113 | ea068cf1 | sschulz | float goal_diff = fabs(get_current_gaze().distance_pt_abs(requested_gaze_state_));
|
114 | float target_diff = fabs(requested_gaze_state_.distance_pt_abs(previous_neck_target_));
|
||
115 | 8c6c1163 | Simon Schulz | |
116 | 0c8d22a5 | sschulz | // printf("GOAL DIFF = %f TARGET DIFF = %f\n",goal_diff,target_diff);
|
117 | // get_current_gaze().dump();
|
||
118 | // requested_gaze_state.dump();
|
||
119 | 8c6c1163 | Simon Schulz | |
120 | 0c8d22a5 | sschulz | // check if new target
|
121 | // close to goal?
|
||
122 | ea068cf1 | sschulz | if ( (neck_saccade_active_) && (goal_diff < 1.0) ) { |
123 | neck_saccade_reached_goal_ = true;
|
||
124 | 8c6c1163 | Simon Schulz | } |
125 | |||
126 | ea068cf1 | sschulz | if (neck_saccade_active_) {
|
127 | previous_neck_target_ = requested_gaze_state_; |
||
128 | 8c6c1163 | Simon Schulz | } |
129 | |||
130 | 0c8d22a5 | sschulz | // if we get a new target now, we can stop the neck saccade
|
131 | if (target_diff > .1) { |
||
132 | ea068cf1 | sschulz | if (neck_saccade_reached_goal_) {
|
133 | 8c6c1163 | Simon Schulz | // joint_interface->neck_saccade_done();
|
134 | ea068cf1 | sschulz | neck_saccade_active_ = false;
|
135 | neck_saccade_reached_goal_ = false;
|
||
136 | 8c6c1163 | Simon Schulz | } |
137 | } |
||
138 | |||
139 | 0c8d22a5 | sschulz | if (neck_saccade_requested) {
|
140 | ea068cf1 | sschulz | neck_saccade_active_ = true;
|
141 | 8c6c1163 | Simon Schulz | } |
142 | |||
143 | 0c8d22a5 | sschulz | // check if this is a small or big saccade
|
144 | ea068cf1 | sschulz | if (neck_saccade_active_ || neck_saccade_omr) {
|
145 | 0c8d22a5 | sschulz | // full saccade with neck motion -> update neck target
|
146 | ea068cf1 | sschulz | requested_neck_state_ = requested_gaze_state_; |
147 | 8c6c1163 | Simon Schulz | } |
148 | |||
149 | 0c8d22a5 | sschulz | // get targets: this is the sum of stored neck target and up-to-date offset:
|
150 | ea068cf1 | sschulz | float neck_pan_target = requested_neck_state_.pan + requested_gaze_state_.pan_offset;
|
151 | float neck_tilt_target = requested_neck_state_.tilt + requested_gaze_state_.tilt_offset;
|
||
152 | 0c8d22a5 | sschulz | // roll is always equal to requested gaze (not neck) state
|
153 | ea068cf1 | sschulz | float neck_roll_target = requested_gaze_state_.roll + requested_gaze_state_.roll_offset;
|
154 | 8c6c1163 | Simon Schulz | |
155 | 0c8d22a5 | sschulz | // add breath wave to tilt
|
156 | 8c6c1163 | Simon Schulz | neck_tilt_target += get_breath_offset(); |
157 | |||
158 | 0c8d22a5 | sschulz | // pass parameters to reflexxes api
|
159 | 730467d3 | Simon Schulz | setup_neckmotion(0, neck_pan_target, neck_pan_now, neck_pan_speed, neck_pan_ts);
|
160 | setup_neckmotion(1, neck_tilt_target, neck_tilt_now, neck_tilt_speed, neck_tilt_ts);
|
||
161 | setup_neckmotion(2, neck_roll_target, neck_roll_now, neck_roll_speed, neck_roll_ts);
|
||
162 | 8c6c1163 | Simon Schulz | |
163 | 0c8d22a5 | sschulz | // call reflexxes to handle profile calculation
|
164 | 8c6c1163 | Simon Schulz | reflexxes_calculate_profile(); |
165 | |||
166 | 0c8d22a5 | sschulz | // tell the joint if about the new values
|
167 | ea068cf1 | sschulz | joint_interface_->set_target(JointInterface::ID_NECK_PAN, |
168 | 35b3ca25 | Simon Schulz | reflexxes_position_output->NewPositionVector->VecData[0],
|
169 | reflexxes_position_output->NewVelocityVector->VecData[0]);
|
||
170 | 21444915 | Simon Schulz | |
171 | ea068cf1 | sschulz | joint_interface_->set_target(JointInterface::ID_NECK_TILT, |
172 | 35b3ca25 | Simon Schulz | reflexxes_position_output->NewPositionVector->VecData[1],
|
173 | reflexxes_position_output->NewVelocityVector->VecData[1]);
|
||
174 | 0d0f5ca1 | Simon Schulz | |
175 | ea068cf1 | sschulz | joint_interface_->set_target(JointInterface::ID_NECK_ROLL, |
176 | 35b3ca25 | Simon Schulz | reflexxes_position_output->NewPositionVector->VecData[2],
|
177 | reflexxes_position_output->NewVelocityVector->VecData[2]);
|
||
178 | 0d0f5ca1 | Simon Schulz | |
179 | 708960ff | Simon Schulz | /*printf("\n%f %f %f %f %f DBG\n",
|
180 | 0d0f5ca1 | Simon Schulz | neck_pan_now, neck_pan_target,
|
181 | reflexxes_position_output->NewPositionVector->VecData[0],
|
||
182 | joint_interface->get_ts_speed(JointInterface::ID_NECK_PAN).get_newest_value(),
|
||
183 | reflexxes_position_output->NewVelocityVector->VecData[0]
|
||
184 | 708960ff | Simon Schulz | );*/
|
185 | 8c6c1163 | Simon Schulz | } |
186 | |||
187 | //! publish targets to motor boards:
|
||
188 | 0c8d22a5 | sschulz | void NeckMotionGenerator::publish_targets() {
|
189 | // publish values if there is an active gaze input within the last timerange
|
||
190 | if (gaze_target_input_active()) {
|
||
191 | ea068cf1 | sschulz | joint_interface_->publish_target(JointInterface::ID_NECK_PAN); |
192 | joint_interface_->publish_target(JointInterface::ID_NECK_TILT); |
||
193 | joint_interface_->publish_target(JointInterface::ID_NECK_ROLL); |
||
194 | 8c6c1163 | Simon Schulz | } |
195 | } |
||
196 | |||
197 | |||
198 | //! set up neck motion profile
|
||
199 | //! this will use speed and acceleration calc formulas from literature:
|
||
200 | //! \param dof id of joint
|
||
201 | //! \param target angle
|
||
202 | //! \param current angle
|
||
203 | 730467d3 | Simon Schulz | void NeckMotionGenerator::setup_neckmotion(int dof, float target, float current_position, |
204 | 0c8d22a5 | sschulz | float current_velocity, humotion::Timestamp timestamp) {
|
205 | // get distance to target
|
||
206 | 0d0f5ca1 | Simon Schulz | float distance_abs = fabs(target - current_position);
|
207 | 8c6c1163 | Simon Schulz | |
208 | 0c8d22a5 | sschulz | // 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)
|
||
211 | 8c6c1163 | Simon Schulz | float max_speed = (CONST_GUITTON87_A * distance_abs + CONST_GUITTON87_B);
|
212 | |||
213 | 0c8d22a5 | sschulz | // max accel: assuming linear acceleration we have:
|
214 | /* v ^ _
|
||
215 | 1c758459 | Simon Schulz | * | / \
|
216 | * | / \
|
||
217 | * |/_____\___> t
|
||
218 | */
|
||
219 | 8c6c1163 | Simon Schulz | // d_total = 2 * 1/2 * a * (t_total/2)^2 = 1/4 * a * t_total^2
|
220 | // as we use linear accel we have
|
||
221 | // v_max = a * t_total/2 --> t_total = 2*v_max / a
|
||
222 | // combine both
|
||
223 | // d_total = 1/4 * a * 4 * vmax^2 / a^2 = v_max^2 / a
|
||
224 | // d_total = a * 2 * d_total / (v_max^2)
|
||
225 | // and therefore
|
||
226 | 0c8d22a5 | sschulz | // a = v_max^2 / d_total
|
227 | 8c6c1163 | Simon Schulz | float max_accel = 0.0; |
228 | 0c8d22a5 | sschulz | if (distance_abs > 0.0) { |
229 | 8c6c1163 | Simon Schulz | max_accel = pow(max_speed, 2) / distance_abs;
|
230 | } |
||
231 | |||
232 | 0c8d22a5 | sschulz | // smoother motion
|
233 | max_accel = max_accel * 0.7; |
||
234 | 473a6a6c | Simon Schulz | |
235 | 0c8d22a5 | sschulz | // limit maximum acceleration to reduce noise FIXME!
|
236 | if (max_accel > 1000) { |
||
237 | 8c6c1163 | Simon Schulz | max_accel = 1000;
|
238 | } |
||
239 | 0c8d22a5 | sschulz | // printf("MAX SPEED %4.2f / max accel %4.2f\n",max_speed, max_accel);
|
240 | 8c6c1163 | Simon Schulz | |
241 | 0c8d22a5 | sschulz | // feed reflexxes api with data
|
242 | reflexxes_set_input(dof, target, current_position, current_velocity, |
||
243 | timestamp, max_speed, max_accel); |
||
244 | 8c6c1163 | Simon Schulz | } |