@@ -14,7 +14,6 @@ namespace {
14
14
constexpr int moving_sample_count = 50 ;
15
15
16
16
static float current_velocity[3 ] = {0 .0f , 0 .0f , 0 .0f };
17
- static float current_position[3 ] = {0 .0f , 0 .0f , 0 .0f };
18
17
static float current_gravity[3 ] = {0 .0f , 0 .0f , 0 .0f };
19
18
static float current_gyroscope_drift[3 ] = {0 .0f , 0 .0f , 0 .0f };
20
19
@@ -185,15 +184,11 @@ namespace {
185
184
current_velocity[0 ] *= friction_fudge;
186
185
current_velocity[1 ] *= friction_fudge;
187
186
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 ];
193
187
}
194
188
}
195
189
196
190
void EstimateGyroscopeDrift (float * drift) {
191
+ // Estimate and update the drift of the gyroscope when the Ardiuno is not moving
197
192
const bool isMoving = VectorMagnitude (current_velocity) > 0 .1f ;
198
193
if (isMoving) {
199
194
return ;
@@ -227,6 +222,7 @@ namespace {
227
222
}
228
223
229
224
void UpdateOrientation (int new_samples, float * gravity, float * drift) {
225
+ // update the current orientation by integrating the angular velocity over time
230
226
const float drift_x = drift[0 ];
231
227
const float drift_y = drift[1 ];
232
228
const float drift_z = drift[2 ];
@@ -269,6 +265,9 @@ namespace {
269
265
}
270
266
271
267
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()
272
271
constexpr float moving_threshold = 10 .0f ;
273
272
274
273
if ((gyroscope_data_index - samples_before) < moving_sample_count) {
@@ -296,15 +295,21 @@ namespace {
296
295
}
297
296
298
297
void UpdateStroke (int new_samples, bool * done_just_triggered) {
298
+ // Take the angular values and project them into an XY plane
299
+
300
+
299
301
constexpr int minimum_stroke_length = moving_sample_count + 10 ;
300
302
constexpr float minimum_stroke_size = 0 .2f ;
301
303
302
304
*done_just_triggered = false ;
303
-
305
+
306
+ // iterate through the new samples
304
307
for (int i = 0 ; i < new_samples; ++i) {
305
308
const int current_head = (new_samples - (i + 1 ));
306
309
const bool is_moving = IsMoving (current_head);
307
310
const int32_t old_state = *stroke_state;
311
+
312
+ // determine if there is a break between gestures
308
313
if ((old_state == eWaiting) || (old_state == eDone)) {
309
314
if (is_moving) {
310
315
stroke_length = moving_sample_count;
@@ -320,7 +325,8 @@ namespace {
320
325
}
321
326
}
322
327
}
323
-
328
+
329
+ // if the stroke is too small we skip to the next iteration
324
330
const bool is_waiting = (*stroke_state == eWaiting);
325
331
if (is_waiting) {
326
332
continue ;
@@ -341,7 +347,8 @@ namespace {
341
347
const int start_index = ((gyroscope_data_index +
342
348
(gyroscope_data_length - (3 * (stroke_length + current_head)))) %
343
349
gyroscope_data_length);
344
-
350
+
351
+ // accumulate the x, y, and z orintation data
345
352
float x_total = 0 .0f ;
346
353
float y_total = 0 .0f ;
347
354
float z_total = 0 .0f ;
@@ -357,7 +364,8 @@ namespace {
357
364
const float y_mean = y_total / stroke_length;
358
365
const float z_mean = z_total / stroke_length;
359
366
constexpr float range = 90 .0f ;
360
-
367
+
368
+ // Account for the roll orientation of the Arduino
361
369
const float gy = current_gravity[1 ];
362
370
const float gz = current_gravity[2 ];
363
371
float gmag = sqrtf ((gy * gy) + (gz * gz));
@@ -374,7 +382,8 @@ namespace {
374
382
const float yaxisy = ngz;
375
383
376
384
*stroke_transmit_length = stroke_length / stroke_transmit_stride;
377
-
385
+
386
+ // project the angular orientation into the 2d X/Y plane
378
387
float x_min;
379
388
float y_min;
380
389
float x_max;
@@ -396,7 +405,8 @@ namespace {
396
405
397
406
const int stroke_index = j * 2 ;
398
407
int8_t * stroke_entry = &stroke_points[stroke_index];
399
-
408
+
409
+ // cap the x/y values at -128 and 127 (int8)
400
410
int32_t unchecked_x = static_cast <int32_t >(roundf (x_axis * 128 .0f ));
401
411
int8_t stored_x;
402
412
if (unchecked_x > 127 ) {
0 commit comments