Skip to content

Commit 3ed3c25

Browse files
committed
added comments
1 parent 63be63c commit 3ed3c25

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

examples/magic_wand/imu_provider.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

examples/magic_wand/rasterize_stroke.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ void RasterizeStroke(
6767
int width,
6868
int height,
6969
int8_t* out_buffer) {
70+
//Convert stroke (2d coordinates of the gesture) into a 2d color image
7071
constexpr int num_channels = 3;
7172
const int buffer_byte_count = height * width * num_channels;
7273

74+
//initialize all pixels to black
7375
for (int i = 0; i < buffer_byte_count; ++i) {
7476
out_buffer[i] = -128;
7577
}
@@ -86,6 +88,9 @@ void RasterizeStroke(
8688
const int one_half_fp = (kFixedPoint / 2);
8789

8890
for (int point_index = 0; point_index < (stroke_points_count - 1); ++point_index) {
91+
//Iterate through the stroke and select two sequential start and end coordinate pairs
92+
// to form the a line segment
93+
//example: Stroke points = [a,b,c...] then we would iterate start=a end=b, start=b end=c...
8994
const int8_t* start_point = &stroke_points[point_index * 2];
9095
const int32_t start_point_x_fp = (start_point[0] * kFixedPoint) / 128;
9196
const int32_t start_point_y_fp = (start_point[1] * kFixedPoint) / 128;
@@ -101,6 +106,8 @@ void RasterizeStroke(
101106
const int32_t delta_x_fp = end_x_fp - start_x_fp;
102107
const int32_t delta_y_fp = end_y_fp - start_y_fp;
103108

109+
//assign the color of the pixels of this line segment
110+
// the color shifts from red->green->blue as we get later in the gesture
104111
const int32_t t_fp = point_index * t_inc_fp;
105112
int32_t red_i32;
106113
int32_t green_i32;
@@ -145,6 +152,7 @@ void RasterizeStroke(
145152
}
146153
}
147154
for (int i = 0; i < (line_length + 1); ++i) {
155+
//iterate through the line segment and assign the pixel values
148156
const int32_t x_fp = start_x_fp + (i * x_inc_fp);
149157
const int32_t y_fp = start_y_fp + (i * y_inc_fp);
150158
const int x = RoundFPToInt(x_fp);

0 commit comments

Comments
 (0)