From: Gajula, Anish Date: Tue, 25 Nov 2025 07:03:53 +0000 (-0500) Subject: pedometer + calories X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=refs%2Fheads%2Ftask_imu;p=smartwatch.git pedometer + calories --- diff --git a/.gitignore b/.gitignore index d0e8a42..5d362fb 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ dependencies.lock .vscode/ managed_components/ + diff --git a/README b/README index b6d0209..98ac93f 100644 --- a/README +++ b/README @@ -11,14 +11,15 @@ Task List =========== Anish -- Pedometer + - Workouts App -- Compass - RTC & AM/PM logic - Rings (Move, Exercise, and Standing) (extra) - Snake Game (extra) - Messaging (extra) - 2035le (extra) +- Pedometer +- Calories burned Advaith - Button Shortcuts diff --git a/main/task_imu.c b/main/task_imu.c index d9435c8..80f616b 100644 --- a/main/task_imu.c +++ b/main/task_imu.c @@ -25,12 +25,55 @@ i2c_config_t conf = { .clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL }; +static int step_count = 0; +static bool is_paused = false; +static uint32_t pause_start_ms = 0; + + const int16_t SWING_THRESHOLD = 15000; + const uint32_t PAUSE_DURATION_MS = 500; + /* ICM 20948 configuration */ icm0948_config_i2c_t icm_config = { .i2c_port = I2C_NUM_0, .i2c_addr = ICM_20948_I2C_ADDR_AD1 }; +void step_counter(icm20948_agmt_t agmt) { + + int16_t x = agmt.acc.axes.x; // x-axis + if (x < 0) x = -x; // absolute value + + uint32_t now_ms = xTaskGetTickCount() * portTICK_PERIOD_MS; + + // If currently paused, check if time is up + if (is_paused) { + if (now_ms - pause_start_ms >= PAUSE_DURATION_MS) { + is_paused = false; // pause finished + } + return; // idling + } + + // check for the arm movement + if (x >= SWING_THRESHOLD) { + step_count += 2; // add 2 steps + ESP_LOGI(TAG, "Swing detected. Total steps: %d", step_count); + + is_paused = true; // add a buffer to prevent over counting + pause_start_ms = now_ms; + } + +} + +float calculate_calories_from_steps(int steps, float height_cm, float weight_kg) { + + float stride_m = (height_cm / 100.0f) * 0.415f; + + float distance_m = steps * stride_m; + + float calories = (distance_m / 1000.0f) * weight_kg * 0.55f; + + return calories; +} void print_agmt(icm20948_agmt_t agmt) { @@ -110,6 +153,8 @@ void task_imu_all(void *) icm20948_agmt_t agmt; // = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0}}; if (icm20948_get_agmt(&icm, &agmt) == ICM_20948_STAT_OK) { print_agmt(agmt); + step_counter(agmt); + ESP_LOGI(TAG, "Calories burned: %.2f kcal\n Step Count: %d", calculate_calories_from_steps(step_count, 170.0f, 65.5f), step_count); } else { ESP_LOGE(TAG, "Uh oh"); }