0 / 5 页已完成

遥控器操控Controller Operation

3.1 遥控器摇杆3.1 Controller Joysticks

VEX 遥控器有两个摇杆,每个摇杆有两个轴(上下、左右),返回值范围是 -127 到 127The VEX controller has two joysticks, each with two axes (up/down, left/right), returning values from -127 to 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
C++ (VEXcode)
// 读取摇杆值
int forward_val = Controller1.Axis3.value(); // -127 到 127
int turn_val = Controller1.Axis1.value();    // -127 到 127
C++ (VS Code)
// 读取摇杆值
int forward_val = Controller1.Axis3.value(); // -127 到 127
int turn_val = Controller1.Axis1.value();    // -127 到 127
C++ (PROS)
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:

Arcade 公式Arcade Formula

左轮速度 = 前后值 + 转弯值Left wheel speed = forward value + turn value

右轮速度 = 前后值 - 转弯值Right wheel speed = forward value - turn value

想想为什么这样能转弯?Think about why this makes the robot turn:

来写代码:Let's write the code:

C++ (VEXcode)
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); // 别让循环跑太快
    }
}
C++ (VS 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); // 别让循环跑太快
    }
}
C++ (PROS)
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); // 别让循环跑太快
    }
}
为什么要 wait(20)?Why wait(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.

先体验再修Experience Before Fixing

建议你先不加死区,下载到机器人上跑一跑,感受一下"手没碰摇杆但机器人在轻微移动"的现象。体验过后再加死区代码,对比效果。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.

C++ (VEXcode)
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);
    }
}
C++ (VS Code)
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);
    }
}
C++ (PROS)
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);
    }
}
死区值怎么选?How to Choose a Deadzone Value?

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.

检查点:Arcade 公式Checkpoint: Arcade Formula
如果 Axis3(前后)= 80,Axis1(转弯)= -40,那么左轮和右轮的速度分别是多少?If Axis3 (forward) = 80, Axis1 (turn) = -40, what are the left and right wheel speeds?

3.4 写代码练习3.4 Coding Exercise

挑战Challenge

写一段完整的 Arcade 驾驶代码(含死区处理),死区设为 10Write a complete Arcade drive program (with deadzone handling), deadzone set to 10.