@@ -14,7 +14,6 @@ namespace {
1414 constexpr int moving_sample_count = 50 ;
1515
1616 static float current_velocity[3 ] = {0 .0f , 0 .0f , 0 .0f };
17- static float current_position[3 ] = {0 .0f , 0 .0f , 0 .0f };
1817 static float current_gravity[3 ] = {0 .0f , 0 .0f , 0 .0f };
1918 static float current_gyroscope_drift[3 ] = {0 .0f , 0 .0f , 0 .0f };
2019
@@ -185,15 +184,11 @@ namespace {
185184 current_velocity[0 ] *= friction_fudge;
186185 current_velocity[1 ] *= friction_fudge;
187186 current_velocity[2 ] *= friction_fudge;
188-
189- // Update the position estimate based on the velocity.
190- current_position[0 ] += current_velocity[0 ];
191- current_position[1 ] += current_velocity[1 ];
192- current_position[2 ] += current_velocity[2 ];
193187 }
194188 }
195189
196190 void EstimateGyroscopeDrift (float * drift) {
191+ // Estimate and update the drift of the gyroscope when the Ardiuno is not moving
197192 const bool isMoving = VectorMagnitude (current_velocity) > 0 .1f ;
198193 if (isMoving) {
199194 return ;
@@ -227,6 +222,7 @@ namespace {
227222 }
228223
229224 void UpdateOrientation (int new_samples, float * gravity, float * drift) {
225+ // update the current orientation by integrating the angular velocity over time
230226 const float drift_x = drift[0 ];
231227 const float drift_y = drift[1 ];
232228 const float drift_z = drift[2 ];
@@ -269,6 +265,9 @@ namespace {
269265 }
270266
271267 bool IsMoving (int samples_before) {
268+ // calculate if the Arduino is move using the mean squared difference
269+ // of the current and previous gyroscope data
270+ // Note: this is different from how we calulate isMoving in EstimateGyroscopeDrift()
272271 constexpr float moving_threshold = 10 .0f ;
273272
274273 if ((gyroscope_data_index - samples_before) < moving_sample_count) {
@@ -296,15 +295,21 @@ namespace {
296295 }
297296
298297 void UpdateStroke (int new_samples, bool * done_just_triggered) {
298+ // Take the angular values and project them into an XY plane
299+
300+
299301 constexpr int minimum_stroke_length = moving_sample_count + 10 ;
300302 constexpr float minimum_stroke_size = 0 .2f ;
301303
302304 *done_just_triggered = false ;
303-
305+
306+ // iterate through the new samples
304307 for (int i = 0 ; i < new_samples; ++i) {
305308 const int current_head = (new_samples - (i + 1 ));
306309 const bool is_moving = IsMoving (current_head);
307310 const int32_t old_state = *stroke_state;
311+
312+ // determine if there is a break between gestures
308313 if ((old_state == eWaiting) || (old_state == eDone)) {
309314 if (is_moving) {
310315 stroke_length = moving_sample_count;
@@ -320,7 +325,8 @@ namespace {
320325 }
321326 }
322327 }
323-
328+
329+ // if the stroke is too small we skip to the next iteration
324330 const bool is_waiting = (*stroke_state == eWaiting);
325331 if (is_waiting) {
326332 continue ;
@@ -341,7 +347,8 @@ namespace {
341347 const int start_index = ((gyroscope_data_index +
342348 (gyroscope_data_length - (3 * (stroke_length + current_head)))) %
343349 gyroscope_data_length);
344-
350+
351+ // accumulate the x, y, and z orintation data
345352 float x_total = 0 .0f ;
346353 float y_total = 0 .0f ;
347354 float z_total = 0 .0f ;
@@ -357,7 +364,8 @@ namespace {
357364 const float y_mean = y_total / stroke_length;
358365 const float z_mean = z_total / stroke_length;
359366 constexpr float range = 90 .0f ;
360-
367+
368+ // Account for the roll orientation of the Arduino
361369 const float gy = current_gravity[1 ];
362370 const float gz = current_gravity[2 ];
363371 float gmag = sqrtf ((gy * gy) + (gz * gz));
@@ -374,7 +382,8 @@ namespace {
374382 const float yaxisy = ngz;
375383
376384 *stroke_transmit_length = stroke_length / stroke_transmit_stride;
377-
385+
386+ // project the angular orientation into the 2d X/Y plane
378387 float x_min;
379388 float y_min;
380389 float x_max;
@@ -396,7 +405,8 @@ namespace {
396405
397406 const int stroke_index = j * 2 ;
398407 int8_t * stroke_entry = &stroke_points[stroke_index];
399-
408+
409+ // cap the x/y values at -128 and 127 (int8)
400410 int32_t unchecked_x = static_cast <int32_t >(roundf (x_axis * 128 .0f ));
401411 int8_t stored_x;
402412 if (unchecked_x > 127 ) {
0 commit comments