// main/main.cc (ESP32 version)
// Gesture classifier using TFLite Micro on ESP32
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "gesture_model_data.h"
static const char *TAG = "gesture";
#define MPU6050_ADDR 0x68
#define SAMPLE_RATE_HZ 50
#define WINDOW_SAMPLES 50
#define NUM_FEATURES (WINDOW_SAMPLES * NUM_AXES) // 150
constexpr int kArenaSize = 16 * 1024;
alignas(16) static uint8_t tensor_arena[kArenaSize];
// I2C and MPU6050 functions (same as Lesson 2)
static void i2c_init(void) {
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = { .clk_speed = 400000 },
i2c_param_config(I2C_NUM_0, &conf);
i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0);
static void mpu6050_init(void) {
uint8_t buf[2] = {0x6B, 0x00};
i2c_master_write_to_device(I2C_NUM_0, MPU6050_ADDR, buf, 2, pdMS_TO_TICKS(100));
vTaskDelay(pdMS_TO_TICKS(100));
buf[0] = 0x1C; buf[1] = 0x00;
i2c_master_write_to_device(I2C_NUM_0, MPU6050_ADDR, buf, 2, pdMS_TO_TICKS(100));
static void read_accel(float *ax, float *ay, float *az) {
uint8_t reg = 0x3B, data[6];
i2c_master_write_read_device(I2C_NUM_0, MPU6050_ADDR,
®, 1, data, 6, pdMS_TO_TICKS(100));
int16_t rx = (int16_t)((data[0] << 8) | data[1]);
int16_t ry = (int16_t)((data[2] << 8) | data[3]);
int16_t rz = (int16_t)((data[4] << 8) | data[5]);
extern "C" void app_main(void) {
ESP_LOGI(TAG, "Loading gesture model (%u bytes)", gesture_model_int8_tflite_len);
const tflite::Model *model = tflite::GetModel(gesture_model_int8_tflite);
if (model->version() != TFLITE_SCHEMA_VERSION) {
ESP_LOGE(TAG, "Model schema mismatch");
static tflite::MicroMutableOpResolver<5> resolver;
resolver.AddFullyConnected();
resolver.AddDequantize();
static tflite::MicroInterpreter interpreter(model, resolver,
tensor_arena, kArenaSize);
if (interpreter.AllocateTensors() != kTfLiteOk) {
ESP_LOGE(TAG, "AllocateTensors failed");
TfLiteTensor *input = interpreter.input(0);
TfLiteTensor *output = interpreter.output(0);
float input_scale = input->params.scale;
int32_t input_zp = input->params.zero_point;
float output_scale = output->params.scale;
int32_t output_zp = output->params.zero_point;
ESP_LOGI(TAG, "Arena used: %zu bytes", interpreter.arena_used_bytes());
ESP_LOGI(TAG, "Input: scale=%.6f zp=%d", input_scale, input_zp);
ESP_LOGI(TAG, "Output: scale=%.6f zp=%d", output_scale, output_zp);
float raw_buffer[NUM_FEATURES];
int interval_ms = 1000 / SAMPLE_RATE_HZ;
ESP_LOGI(TAG, "Ready. Perform gestures...");
// Collect 1 second of accelerometer data
for (int i = 0; i < WINDOW_SAMPLES; i++) {
read_accel(&ax, &ay, &az);
raw_buffer[i * 3 + 0] = ax;
raw_buffer[i * 3 + 1] = ay;
raw_buffer[i * 3 + 2] = az;
vTaskDelay(pdMS_TO_TICKS(interval_ms));
// Normalize and quantize
for (int i = 0; i < NUM_FEATURES; i++) {
float normalized = (raw_buffer[i] - GESTURE_INPUT_MIN) /
(GESTURE_INPUT_MAX - GESTURE_INPUT_MIN);
if (normalized < 0.0f) normalized = 0.0f;
if (normalized > 1.0f) normalized = 1.0f;
input->data.int8[i] = (int8_t)roundf(normalized / input_scale + input_zp);
int64_t t_start = esp_timer_get_time();
TfLiteStatus status = interpreter.Invoke();
int64_t t_end = esp_timer_get_time();
if (status != kTfLiteOk) {
ESP_LOGE(TAG, "Invoke failed");
// Dequantize output and find best class
float scores[GESTURE_NUM_CLASSES];
float best_score = -1.0f;
for (int c = 0; c < GESTURE_NUM_CLASSES; c++) {
int8_t q_val = output->data.int8[c];
scores[c] = (q_val - output_zp) * output_scale;
if (scores[c] > best_score) {
int64_t inference_us = t_end - t_start;
ESP_LOGI(TAG, "Gesture: %s (%.1f%%) | wave=%.2f punch=%.2f flex=%.2f | %lld us",
gesture_labels[best_idx], best_score * 100.0f,
scores[0], scores[1], scores[2], inference_us);
Comments