dual-controller-sync

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dual-Controller Synchronization Guide

双控制器同步指南

When to Use This Skill

何时使用该技能

Apply this skill when the user:
  • Has issues with I2C communication between main and camera controllers
  • Needs to debug the dual-ESP32 robocar architecture
  • Wants to monitor both controllers simultaneously
  • Experiences timing or synchronization issues
当用户遇到以下场景时可使用本技能:
  • 主控与摄像头控制器之间的I2C通信存在异常
  • 需要调试双ESP32无人车架构
  • 需要同时监控两个控制器的运行状态
  • 遇到时序或同步类问题

Architecture Overview

架构概述

┌─────────────────────┐     I2C      ┌─────────────────────┐
│   Main Controller   │◄────────────►│   Camera Module     │
│  Heltec WiFi LoRa   │              │     ESP32-CAM       │
│                     │              │                     │
│  - AI decisions     │              │  - Image capture    │
│  - Motor control    │              │  - Vision analysis  │
│  - LoRa comms       │              │  - I2C slave        │
└─────────────────────┘              └─────────────────────┘
┌─────────────────────┐     I2C      ┌─────────────────────┐
│   Main Controller   │◄────────────►│   Camera Module     │
│  Heltec WiFi LoRa   │              │     ESP32-CAM       │
│                     │              │                     │
│  - AI decisions     │              │  - Image capture    │
│  - Motor control    │              │  - Vision analysis  │
│  - LoRa comms       │              │  - I2C slave        │
└─────────────────────┘              └─────────────────────┘

Build Both Controllers

编译两个控制器固件

bash
undefined
bash
undefined

Build main controller

编译主控制器固件

make robocar-build-main
make robocar-build-main

Build camera module

编译摄像头模块固件

make robocar-build-cam
make robocar-build-cam

Or build both

同时编译两者固件

make robocar-build-all
undefined
make robocar-build-all
undefined

Monitor Both Controllers

监控两个控制器运行状态

Two Terminal Setup

双终端配置方案

Open two terminals and monitor both:
Terminal 1 - Main Controller:
bash
make robocar-monitor-main PORT=/dev/cu.usbserial-0001
Terminal 2 - Camera Module:
bash
make robocar-monitor-cam PORT=/dev/cu.usbserial-0002
打开两个终端分别监控两个设备:
终端1 - 主控制器:
bash
make robocar-monitor-main PORT=/dev/cu.usbserial-0001
终端2 - 摄像头模块:
bash
make robocar-monitor-cam PORT=/dev/cu.usbserial-0002

Identifying Ports

端口识别方法

If unsure which device is which:
bash
ls -la /dev/cu.usbserial-* /dev/ttyUSB*
Look for:
  • Device that appears first after connecting main controller
  • Device that appears after connecting camera module
如果不确定设备对应的端口:
bash
ls -la /dev/cu.usbserial-* /dev/ttyUSB*
判断规则:
  • 连接主控制器后最先出现的设备
  • 连接摄像头模块后新增的设备

I2C Communication Debugging

I2C通信调试

Common I2C Issues

常见I2C问题

SymptomPossible CauseSolution
No responseWrong addressVerify 7-bit address format
TimeoutMissing pull-upsAdd 4.7K resistors on SDA/SCL
Corrupted dataSpeed too fastReduce I2C clock frequency
IntermittentLoose connectionCheck wiring
现象可能原因解决方案
无响应地址配置错误确认使用7位地址格式
超时缺少上拉电阻在SDA/SCL线路上添加4.7K电阻
数据损坏传输速率过高降低I2C时钟频率
通信不稳定连接松动检查接线是否牢固

I2C Address Configuration

I2C地址配置

Ensure both controllers use the same address:
Main controller (master):
c
#define CAMERA_I2C_ADDR 0x55  // 7-bit address
Camera module (slave):
c
#define I2C_SLAVE_ADDR 0x55   // Must match master
确保两个控制器使用相同的通信地址:
主控制器(主设备):
c
#define CAMERA_I2C_ADDR 0x55  // 7-bit address
摄像头模块(从设备):
c
#define I2C_SLAVE_ADDR 0x55   // Must match master

Verify I2C Wiring

验证I2C接线

Main Controller          Camera Module
    SDA (21) ◄──────────► SDA (GPIO)
    SCL (22) ◄──────────► SCL (GPIO)
    GND      ◄──────────► GND
Pull-up resistors (4.7K) on SDA and SCL to 3.3V.
Main Controller          Camera Module
    SDA (21) ◄──────────► SDA (GPIO)
    SCL (22) ◄──────────► SCL (GPIO)
    GND      ◄──────────► GND
SDA和SCL线路需要接4.7K上拉电阻到3.3V。

Protocol Debugging

协议调试

Expected Communication Pattern

预期通信流程

  1. Main sends command byte
  2. Camera processes command
  3. Camera sends response
  4. Main reads response
  1. 主控制器发送命令字节
  2. 摄像头处理接收的命令
  3. 摄像头返回响应数据
  4. 主控制器读取响应数据

Debug Logging

调试日志配置

Enable verbose I2C logging in both projects:
c
esp_log_level_set("i2c", ESP_LOG_DEBUG);
Look for matching transactions:
  • Main: "Sent command 0x01 to 0x55"
  • Camera: "Received command 0x01"
在两个项目中开启I2C详细日志输出:
c
esp_log_level_set("i2c", ESP_LOG_DEBUG);
查找匹配的通信事务日志:
  • 主控制器:"Sent command 0x01 to 0x55"
  • 摄像头:"Received command 0x01"

Timing Issues

时序问题处理

If commands arrive too fast:
Add delay between commands:
c
i2c_master_cmd_begin(I2C_NUM, cmd, pdMS_TO_TICKS(100));
vTaskDelay(pdMS_TO_TICKS(10));  // Wait for slave to process
如果命令发送频率过高:
在命令之间添加延迟:
c
i2c_master_cmd_begin(I2C_NUM, cmd, pdMS_TO_TICKS(100));
vTaskDelay(pdMS_TO_TICKS(10));  // Wait for slave to process

Flash Workflow

烧录流程

Flash Both Controllers

烧录两个控制器固件

bash
undefined
bash
undefined

Flash main controller first

先烧录主控制器

make robocar-flash-main PORT=/dev/cu.usbserial-0001
make robocar-flash-main PORT=/dev/cu.usbserial-0001

Then flash camera (requires GPIO0 to GND)

再烧录摄像头(需要先将GPIO0接GND)

Connect GPIO0 to GND on ESP32-CAM

把ESP32-CAM上的GPIO0连接到GND

make robocar-flash-cam PORT=/dev/cu.usbserial-0002
make robocar-flash-cam PORT=/dev/cu.usbserial-0002

Disconnect GPIO0 from GND and reset

烧录完成后断开GPIO0与GND的连接并复位设备

undefined
undefined

Development Cycle

开发迭代流程

For rapid iteration:
  1. Make code changes
  2. Build:
    make robocar-build-all
  3. Flash main:
    make robocar-flash-main
  4. Flash camera (GPIO0→GND):
    make robocar-flash-cam
  5. Monitor both in separate terminals
适用于快速开发迭代场景:
  1. 修改代码
  2. 编译:
    make robocar-build-all
  3. 烧录主控制器:
    make robocar-flash-main
  4. 烧录摄像头(GPIO0接GND):
    make robocar-flash-cam
  5. 在独立终端中同时监控两个控制器

Common Synchronization Issues

常见同步问题

Issue: Camera Not Responding

问题:摄像头无响应

Symptoms:
  • Main reports I2C timeout
  • Camera not in monitor output
Debug Steps:
  1. Verify camera is powered and running
  2. Check I2C address matches
  3. Verify GPIO pins for I2C slave
  4. Check clock speed compatibility
现象:
  • 主控制器上报I2C超时
  • 监控输出中没有摄像头的运行日志
调试步骤:
  1. 确认摄像头已通电并正常运行
  2. 检查I2C地址是否匹配
  3. 确认I2C从设备的GPIO引脚配置正确
  4. 检查时钟速率兼容性

Issue: Corrupted Data

问题:数据损坏

Symptoms:
  • Data received but values wrong
  • Checksum failures
Debug Steps:
  1. Reduce I2C speed (try 100kHz)
  2. Add pull-up resistors
  3. Check for noise on long wires
  4. Verify endianness of multi-byte data
现象:
  • 能收到数据但数值错误
  • 校验和校验失败
调试步骤:
  1. 降低I2C速率(建议尝试100kHz)
  2. 添加上拉电阻
  3. 检查长线路是否存在噪声干扰
  4. 确认多字节数据的字节序配置正确

Issue: Timing Mismatch

问题:时序不匹配

Symptoms:
  • Works sometimes, fails other times
  • Camera not ready when main sends command
Debug Steps:
  1. Add handshake mechanism
  2. Increase timeout values
  3. Use interrupt-based I2C on camera
  4. Add ready signal GPIO
现象:
  • 通信时好时坏,成功率不稳定
  • 主控制器发送命令时摄像头未就绪
调试步骤:
  1. 添加通信握手机制
  2. 增大超时时间阈值
  3. 摄像头端使用基于中断的I2C实现
  4. 新增就绪信号GPIO引脚

Tools and Commands

工具与命令

bash
undefined
bash
undefined

Show system info

展示系统信息

make robocar-info
make robocar-info

Check environment

检查开发环境

make check-environment
make check-environment

Clean and rebuild

清理并重新编译

make robocar-clean make robocar-build-all
make robocar-clean make robocar-build-all

Full dev cycle for main

主控制器完整开发流程

make robocar-develop-main PORT=/dev/xxx
make robocar-develop-main PORT=/dev/xxx

Full dev cycle for camera

摄像头模块完整开发流程

make robocar-develop-cam PORT=/dev/xxx
undefined
make robocar-develop-cam PORT=/dev/xxx
undefined