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
This commit is contained in:
m1ngsama 2026-02-20 21:45:36 +08:00
parent f2ad220cc8
commit 2d36633dcf

View file

@ -1,45 +1,45 @@
/* /*
* ESP32 PWM ( Core 3.x ) * ESP32 native hardware PWM servo controller (ESP32 Arduino Core 3.x)
* 'ledcSetup' was not declared * Uses the updated ledcAttach/ledcWrite API (replaces deprecated ledcSetup).
*/ */
// --- 1. 引脚定义 --- // --- 1. Pin assignments ---
const int PIN_X = 14; const int PIN_X = 14; // X-axis servo
const int PIN_Y = 4; const int PIN_Y = 4; // Y-axis servo
const int PIN_Z = 5; const int PIN_Z = 5; // Z-axis servo
const int PIN_B = 18; const int PIN_B = 18; // Base rotation servo
const int PIN_G = 23; const int PIN_G = 23; // Gripper servo
// --- 2. PWM 参数 (舵机标准) --- // --- 2. PWM parameters (standard servo) ---
const int freq = 50; // 频率 50Hz (周期 20ms) const int freq = 50; // 50 Hz (20 ms period)
const int resolution = 12; // 分辨率 12位 (数值范围 0-4095) const int resolution = 12; // 12-bit resolution (04095)
// 舵机角度对应的占空比数值 (12位分辨率) // Duty-cycle values for servo angles (12-bit, 50 Hz):
// 0.5ms (0度) -> 约 102 // 0.5 ms ( 0°) → ~102
// 1.5ms (90度) -> 约 307 // 1.5 ms ( 90°) → ~307
// 2.5ms (180度) -> 约 512 // 2.5 ms (180°) → ~512
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000); 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_X, freq, resolution);
ledcAttach(PIN_Y, freq, resolution); ledcAttach(PIN_Y, freq, resolution);
ledcAttach(PIN_Z, freq, resolution); ledcAttach(PIN_Z, freq, resolution);
ledcAttach(PIN_B, freq, resolution); ledcAttach(PIN_B, freq, resolution);
ledcAttach(PIN_G, freq, resolution); ledcAttach(PIN_G, freq, resolution);
// 初始归中 // Center all servos at startup
writeAngle(PIN_X, 90); delay(500); writeAngle(PIN_X, 90); delay(500);
writeAngle(PIN_Y, 90); delay(500); writeAngle(PIN_Y, 90); delay(500);
writeAngle(PIN_Z, 90); delay(500); writeAngle(PIN_Z, 90); delay(500);
writeAngle(PIN_B, 90); delay(500); writeAngle(PIN_B, 90); delay(500);
writeAngle(PIN_G, 90); delay(500); writeAngle(PIN_G, 90); delay(500);
Serial.println("--- 5轴硬件 PWM 初始化完成 ---"); Serial.println("--- 5-axis hardware PWM ready ---");
} }
void loop() { void loop() {
@ -47,42 +47,39 @@ void loop() {
String cmd = Serial.readStringUntil('\n'); String cmd = Serial.readStringUntil('\n');
cmd.trim(); cmd.trim();
// 根据指令解析并控制 if (cmd.startsWith("Servo_ArmX")) writeAngle(PIN_X, extractAngle(cmd, "Servo_ArmX"));
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_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_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_ArmB")) writeAngle(PIN_B, extractAngle(cmd, "Servo_ArmB"));
else if (cmd.startsWith("Servo_Gripper")) { else if (cmd.startsWith("Servo_Gripper")) {
int a = extractAngle(cmd, "Servo_Gripper"); int a = extractAngle(cmd, "Servo_Gripper");
if (a != -1) { if (a != -1) {
writeAngle(PIN_G, a); writeAngle(PIN_G, a);
Serial.print("夹爪已转动至: "); Serial.println(a); Serial.print("Gripper moved to: "); Serial.println(a);
} }
} }
} }
} }
/** /**
* * Write a target angle to a servo pin.
* pin: * pin : GPIO pin number
* angle: (0-180) * angle: target angle in degrees (0180, clamped)
*/ */
void writeAngle(int pin, int angle) { void writeAngle(int pin, int angle) {
if (angle < 0) angle = 0; if (angle < 0) angle = 0;
if (angle > 180) angle = 180; if (angle > 180) angle = 180;
// 线性映射计算占空比 // Linear map: 0° → duty 102, 180° → duty 512
// 0度 = 102, 180度 = 512
int duty = (angle * (512 - 102) / 180) + 102; int duty = (angle * (512 - 102) / 180) + 102;
// 在新版 Core 中ledcWrite 直接接收引脚号和数值 // Core 3.x API: ledcWrite takes pin number directly
ledcWrite(pin, duty); ledcWrite(pin, duty);
} }
// 提取命令中的数字 /** Extract the numeric argument after a known command prefix. Returns -1 on failure. */
int extractAngle(String cmd, String prefix) { int extractAngle(String cmd, String prefix) {
int prefixLen = prefix.length(); String valPart = cmd.substring(prefix.length());
String valPart = cmd.substring(prefixLen);
if (valPart.length() > 0) return valPart.toInt(); if (valPart.length() > 0) return valPart.toInt();
return -1; return -1;
} }