mirror of
https://github.com/m1ngsama/robot_arm.git
synced 2026-03-25 19:53:49 +00:00
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:
parent
f2ad220cc8
commit
2d36633dcf
1 changed files with 34 additions and 37 deletions
57
main.ino
57
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,7 +47,6 @@ 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"));
|
||||
|
|
@ -56,33 +55,31 @@ void loop() {
|
|||
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 > 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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue