遥控器操控Controller Operation
3.1 遥控器摇杆3.1 Controller Joysticks
VEX 遥控器有两个摇杆,每个摇杆有两个轴(上下、左右),返回值范围是 -127 到 127:The VEX controller has two joysticks, each with two axes (up/down, left/right), returning values from -127 to 127:
- 摇杆推到最上面 → 127Push joystick all the way up → 127
- 摇杆在中间 → 0Joystick at center → 0
- 摇杆推到最下面 → -127Push joystick all the way down → -127
最常用的两个轴:The two most commonly used axes:
| 轴Axis | 位置Position | 用途Purpose |
|---|---|---|
| Axis3(左摇杆 Y)Axis3 (Left Stick Y) | 左手上下Left hand up/down | 控制前后Forward/backward |
| Axis1(右摇杆 X)Axis1 (Right Stick X) | 右手左右Right hand left/right | 控制转弯Turning |
// 读取摇杆值
int forward_val = Controller1.Axis3.value(); // -127 到 127
int turn_val = Controller1.Axis1.value(); // -127 到 127
// 读取摇杆值
int forward_val = Controller1.Axis3.value(); // -127 到 127
int turn_val = Controller1.Axis1.value(); // -127 到 127
pros::Controller controller(pros::E_CONTROLLER_MASTER);
// 读取摇杆值
int forward_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
int turn_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_X);
3.2 Arcade 驾驶模式3.2 Arcade Drive Mode
Arcade 模式用左摇杆控制前后,右摇杆控制转弯,就像玩赛车游戏一样直觉。Arcade mode uses the left stick to go forward/backward and the right stick to turn, just like playing a racing game.
核心公式:The core formula:
左轮速度 = 前后值 + 转弯值Left wheel speed = forward value + turn value
右轮速度 = 前后值 - 转弯值Right wheel speed = forward value - turn value
想想为什么这样能转弯?Think about why this makes the robot turn:
- 只推左摇杆(前后 = 50,转弯 = 0)→ 左轮 50,右轮 50 → 直走Push only left stick (forward = 50, turn = 0) → left 50, right 50 → straight
- 只推右摇杆向右(前后 = 0,转弯 = 50)→ 左轮 50,右轮 -50 → 原地右转Push only right stick right (forward = 0, turn = 50) → left 50, right -50 → spin right
- 两个一起推(前后 = 50,转弯 = 30)→ 左轮 80,右轮 20 → 弧线右转Push both (forward = 50, turn = 30) → left 80, right 20 → arc right
来写代码:Let's write the code:
int main() {
while (true) {
int forward_val = Controller1.Axis3.value();
int turn_val = Controller1.Axis1.value();
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
LeftMotor.spin(forward, left_speed, percent);
RightMotor.spin(forward, right_speed, percent);
wait(20, msec); // 别让循环跑太快
}
}
int main() {
while (true) {
int forward_val = Controller1.Axis3.value();
int turn_val = Controller1.Axis1.value();
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
LeftMotor.spin(forward, left_speed, percent);
RightMotor.spin(forward, right_speed, percent);
wait(20, msec); // 别让循环跑太快
}
}
void opcontrol() {
pros::Controller controller(pros::E_CONTROLLER_MASTER);
while (true) {
int forward_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
int turn_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_X);
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
left_motor.move(left_speed);
right_motor.move(right_speed);
pros::delay(20); // 别让循环跑太快
}
}
while(true) 是死循环,没有等待的话电脑会以最快速度反复执行,浪费资源还可能卡住。加 20 毫秒的等待,每秒执行 50 次,人完全感觉不到延迟。while(true) is an infinite loop. Without a wait, the CPU runs it as fast as possible, wasting resources and possibly freezing. Adding a 20ms wait means it runs 50 times per second — you won't notice any delay.
3.3 死区处理3.3 Deadzone Handling
把上面的代码下载到机器人里,你会发现一个问题:明明手没有碰摇杆,机器人还在抖动。Download the code above to your robot, and you'll notice a problem: the robot keeps jittering even when you're not touching the joystick.
这是因为摇杆回中的时候不一定精确回到 0,可能是 3 或 -2 这样的小值。解决方法:设一个"死区",小于某个值就当作 0。This is because the joystick doesn't always return exactly to 0 when released — it might read 3 or -2. The fix: set a "deadzone" where values below a threshold are treated as 0.
建议你先不加死区,下载到机器人上跑一跑,感受一下"手没碰摇杆但机器人在轻微移动"的现象。体验过后再加死区代码,对比效果。Try without the deadzone first. Download to the robot and observe the "robot slightly moving even when you're not touching the joystick" effect. Then add the deadzone code and compare.
int main() {
while (true) {
int forward_val = Controller1.Axis3.value();
int turn_val = Controller1.Axis1.value();
// 死区处理:绝对值小于 15 就当作 0
if (abs(forward_val) < 15) forward_val = 0;
if (abs(turn_val) < 15) turn_val = 0;
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
LeftMotor.spin(forward, left_speed, percent);
RightMotor.spin(forward, right_speed, percent);
wait(20, msec);
}
}
int main() {
while (true) {
int forward_val = Controller1.Axis3.value();
int turn_val = Controller1.Axis1.value();
// 死区处理:绝对值小于 15 就当作 0
if (abs(forward_val) < 15) forward_val = 0;
if (abs(turn_val) < 15) turn_val = 0;
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
LeftMotor.spin(forward, left_speed, percent);
RightMotor.spin(forward, right_speed, percent);
wait(20, msec);
}
}
void opcontrol() {
pros::Controller controller(pros::E_CONTROLLER_MASTER);
while (true) {
int forward_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
int turn_val = controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_X);
// 死区处理:绝对值小于 15 就当作 0
if (abs(forward_val) < 15) forward_val = 0;
if (abs(turn_val) < 15) turn_val = 0;
int left_speed = forward_val + turn_val;
int right_speed = forward_val - turn_val;
left_motor.move(left_speed);
right_motor.move(right_speed);
pros::delay(20);
}
}
5~15 都可以。太小了抖动消不干净,太大了手感会变迟钝。15 是个不错的起点,实际调试时再微调。Anywhere from 5 to 15 works. Too small and the jitter won't fully go away; too large and the controls feel sluggish. 15 is a good starting point — fine-tune during testing.
3.4 写代码练习3.4 Coding Exercise
写一段完整的 Arcade 驾驶代码(含死区处理),死区设为 10。Write a complete Arcade drive program (with deadzone handling), deadzone set to 10.