机构电机:用按钮控制一个额外的电机(比如机械臂或进球机构)Mechanism motor: use buttons to control an extra motor (like an arm or intake)
没有框架,从头开始写No Template — Write from Scratch
这次不给框架代码。从声明电机开始,一步步搭建你自己的程序。下面有提示和参考答案,但请先自己试。This time there's no starter code. Start by declaring motors and build your program step by step. Hints and answers are below, but try on your own first.
在同一个循环里:读按钮 → 控制机构电机In the same loop: read buttons → control mechanism motor
循环末尾加 wait(20)Add wait(20) at the end of the loop
提示 2:机构电机的逻辑Hint 2: Mechanism Motor Logic
用 if / else if / else 判断:Use if / else if / else to decide:
C++ (伪代码)
if (R1 被按下) {
机构电机正转;
} else if (R2 被按下) {
机构电机反转;
} else {
机构电机停;
}
提示 3:机构电机用什么停止模式?Hint 3: What Stop Mode for the Mechanism Motor?
如果是机械臂(需要保持位置),用 hold。如果是进球轮(不需要保持),用 coast 或 brake。If it's an arm (needs to hold position), use hold. If it's an intake wheel (doesn't need to hold), use coast or brake.
5.5 参考答案5.5 Reference Answer
先自己写再看答案Write Your Own First
参考答案只是其中一种写法,你的写法可能不一样,只要功能对就行。The reference answer is just one way to write it. Your approach might be different — as long as it works, it's fine.
C++ (VEXcode)
// 声明电机
motor LeftMotor = motor(PORT1, ratio18_1, false);
motor RightMotor = motor(PORT2, ratio18_1, true);
motor ArmMotor = motor(PORT3, ratio36_1, false);
int main() {
// 机构电机停下时锁定位置
ArmMotor.setStopping(hold);
while (true) {
// === 底盘:Arcade 驾驶 ===
int fwd = Controller1.Axis3.value();
int turn = Controller1.Axis1.value();
// 死区
if (abs(fwd) < 15) fwd = 0;
if (abs(turn) < 15) turn = 0;
LeftMotor.spin(forward, fwd + turn, percent);
RightMotor.spin(forward, fwd - turn, percent);
// === 机构:按钮控制 ===
if (Controller1.ButtonR1.pressing()) {
ArmMotor.spin(forward, 80, percent);
} else if (Controller1.ButtonR2.pressing()) {
ArmMotor.spin(reverse, 80, percent);
} else {
ArmMotor.stop();
}
wait(20, msec);
}
}
C++ (VS Code)
// 声明电机
motor LeftMotor = motor(PORT1, ratio18_1, false);
motor RightMotor = motor(PORT2, ratio18_1, true);
motor ArmMotor = motor(PORT3, ratio36_1, false);
int main() {
ArmMotor.setStopping(hold);
while (true) {
// === 底盘:Arcade 驾驶 ===
int fwd = Controller1.Axis3.value();
int turn = Controller1.Axis1.value();
if (abs(fwd) < 15) fwd = 0;
if (abs(turn) < 15) turn = 0;
LeftMotor.spin(forward, fwd + turn, percent);
RightMotor.spin(forward, fwd - turn, percent);
// === 机构:按钮控制 ===
if (Controller1.ButtonR1.pressing()) {
ArmMotor.spin(forward, 80, percent);
} else if (Controller1.ButtonR2.pressing()) {
ArmMotor.spin(reverse, 80, percent);
} else {
ArmMotor.stop();
}
wait(20, msec);
}
}
在上面的程序中,如果把 while(true) 循环里的 wait(20, msec) 删掉,会发生什么?In the program above, what happens if you remove wait(20, msec) from the while(true) loop?
正确!没有 wait 的死循环会把 CPU 占满,导致其他任务(比如通信、屏幕更新)抢不到运行时间,机器人可能反应迟钝甚至卡住。Correct! An infinite loop without wait maxes out the CPU, preventing other tasks (like communication and screen updates) from running. The robot may become unresponsive or freeze.
通过Passed
检查点:调试思维Checkpoint: Debugging Mindset
你写好程序下载到机器人上,发现推摇杆向前时机器人在原地转圈。最可能的原因是什么?You download your program to the robot and find that when you push the joystick forward, the robot spins in place. What's the most likely cause?
正确!原地转圈说明左右轮在往相反方向转。最常见的原因就是忘记在声明电机时设置 true(反转)。这是新手最容易踩的坑之一。Correct! Spinning in place means the left and right wheels are going in opposite directions. The most common cause is forgetting to set true (reverse) when declaring the motor. This is one of the most common beginner mistakes.