From 2d36633dcf1e6a825669ccbaacfa902c52feacc3 Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Fri, 20 Feb 2026 21:45:36 +0800 Subject: [PATCH] chore(ino): translate main.ino comments and Serial output to English All Chinese comments, Serial.println strings, and inline notes translated to English per the project's English-first codebase rule. No logic changes. Closes #11 --- main.ino | 71 +++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/main.ino b/main.ino index f71085c..532eba4 100644 --- a/main.ino +++ b/main.ino @@ -1,45 +1,45 @@ /* - * ESP32 原生硬件 PWM 控制程序 (适配 Core 3.x 版本) - * 解决 'ledcSetup' was not declared 报错问题 + * ESP32 native hardware PWM servo controller (ESP32 Arduino Core 3.x) + * Uses the updated ledcAttach/ledcWrite API (replaces deprecated ledcSetup). */ -// --- 1. 引脚定义 --- -const int PIN_X = 14; -const int PIN_Y = 4; -const int PIN_Z = 5; -const int PIN_B = 18; -const int PIN_G = 23; +// --- 1. Pin assignments --- +const int PIN_X = 14; // X-axis servo +const int PIN_Y = 4; // Y-axis servo +const int PIN_Z = 5; // Z-axis servo +const int PIN_B = 18; // Base rotation servo +const int PIN_G = 23; // Gripper servo -// --- 2. PWM 参数 (舵机标准) --- -const int freq = 50; // 频率 50Hz (周期 20ms) -const int resolution = 12; // 分辨率 12位 (数值范围 0-4095) +// --- 2. PWM parameters (standard servo) --- +const int freq = 50; // 50 Hz (20 ms period) +const int resolution = 12; // 12-bit resolution (0–4095) -// 舵机角度对应的占空比数值 (12位分辨率) -// 0.5ms (0度) -> 约 102 -// 1.5ms (90度) -> 约 307 -// 2.5ms (180度) -> 约 512 +// Duty-cycle values for servo angles (12-bit, 50 Hz): +// 0.5 ms ( 0°) → ~102 +// 1.5 ms ( 90°) → ~307 +// 2.5 ms (180°) → ~512 void setup() { Serial.begin(115200); delay(1000); - Serial.println("\n--- 使用 ESP32 Core 3.x LEDC 驱动初始化 ---"); + Serial.println("\n--- ESP32 Core 3.x LEDC driver initializing ---"); - // 在新版 Core 中,直接使用 ledcAttach(引脚, 频率, 分辨率) + // Core 3.x API: ledcAttach(pin, frequency, resolution) ledcAttach(PIN_X, freq, resolution); ledcAttach(PIN_Y, freq, resolution); ledcAttach(PIN_Z, freq, resolution); ledcAttach(PIN_B, freq, resolution); ledcAttach(PIN_G, freq, resolution); - // 初始归中 + // Center all servos at startup writeAngle(PIN_X, 90); delay(500); writeAngle(PIN_Y, 90); delay(500); writeAngle(PIN_Z, 90); delay(500); writeAngle(PIN_B, 90); delay(500); writeAngle(PIN_G, 90); delay(500); - Serial.println("--- 5轴硬件 PWM 初始化完成 ---"); + Serial.println("--- 5-axis hardware PWM ready ---"); } void loop() { @@ -47,42 +47,39 @@ void loop() { String cmd = Serial.readStringUntil('\n'); cmd.trim(); - // 根据指令解析并控制 - if (cmd.startsWith("Servo_ArmX")) writeAngle(PIN_X, extractAngle(cmd, "Servo_ArmX")); - else if (cmd.startsWith("Servo_ArmY")) writeAngle(PIN_Y, extractAngle(cmd, "Servo_ArmY")); - else if (cmd.startsWith("Servo_ArmZ")) writeAngle(PIN_Z, extractAngle(cmd, "Servo_ArmZ")); - else if (cmd.startsWith("Servo_ArmB")) writeAngle(PIN_B, extractAngle(cmd, "Servo_ArmB")); + if (cmd.startsWith("Servo_ArmX")) writeAngle(PIN_X, extractAngle(cmd, "Servo_ArmX")); + else if (cmd.startsWith("Servo_ArmY")) writeAngle(PIN_Y, extractAngle(cmd, "Servo_ArmY")); + else if (cmd.startsWith("Servo_ArmZ")) writeAngle(PIN_Z, extractAngle(cmd, "Servo_ArmZ")); + else if (cmd.startsWith("Servo_ArmB")) writeAngle(PIN_B, extractAngle(cmd, "Servo_ArmB")); else if (cmd.startsWith("Servo_Gripper")) { int a = extractAngle(cmd, "Servo_Gripper"); if (a != -1) { writeAngle(PIN_G, a); - Serial.print("夹爪已转动至: "); Serial.println(a); + Serial.print("Gripper moved to: "); Serial.println(a); } } } } /** - * 核心控制函数 - * pin: 控制的引脚号 - * angle: 目标角度 (0-180) + * Write a target angle to a servo pin. + * pin : GPIO pin number + * angle: target angle in degrees (0–180, clamped) */ void writeAngle(int pin, int angle) { - if (angle < 0) angle = 0; + if (angle < 0) angle = 0; if (angle > 180) angle = 180; - - // 线性映射计算占空比 - // 0度 = 102, 180度 = 512 + + // Linear map: 0° → duty 102, 180° → duty 512 int duty = (angle * (512 - 102) / 180) + 102; - - // 在新版 Core 中,ledcWrite 直接接收引脚号和数值 + + // Core 3.x API: ledcWrite takes pin number directly ledcWrite(pin, duty); } -// 提取命令中的数字 +/** Extract the numeric argument after a known command prefix. Returns -1 on failure. */ int extractAngle(String cmd, String prefix) { - int prefixLen = prefix.length(); - String valPart = cmd.substring(prefixLen); + String valPart = cmd.substring(prefix.length()); if (valPart.length() > 0) return valPart.toInt(); return -1; }