直走和转弯Driving Straight and Turning
2.1 双轮差速原理2.1 Differential Drive Basics
VEX 机器人最常见的底盘是左右各一组电机驱动。机器人怎么转弯?答案很简单:左右轮速度不一样就会转弯。The most common VEX robot drivetrain has a set of motors on each side. How does the robot turn? Simple: when the left and right wheels spin at different speeds, the robot turns.
这就像划船——左边划快、右边划慢,船就往右转。It's like rowing a boat — paddle faster on the left and slower on the right, and the boat turns right.
左右轮同速 = 直走,不同速 = 转弯,反向 = 原地转。Left and right wheels at same speed = go straight, different speeds = turn, opposite directions = spin in place.
2.2 直走2.2 Driving Straight
让左右电机以相同速度朝同一方向转:Make the left and right motors spin in the same direction at the same speed:
motor LeftMotor = motor(PORT1, ratio18_1, false);
motor RightMotor = motor(PORT2, ratio18_1, true);
// 直走:左右同速
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(2000, msec);
LeftMotor.stop();
RightMotor.stop();
motor LeftMotor = motor(PORT1, ratio18_1, false);
motor RightMotor = motor(PORT2, ratio18_1, true);
// 直走:左右同速
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(2000, msec);
LeftMotor.stop();
RightMotor.stop();
pros::Motor left_motor(1, pros::MotorGearset::green);
pros::Motor right_motor(2, pros::MotorGearset::green, true);
// 直走:左右同速
left_motor.move(50);
right_motor.move(50);
pros::delay(2000);
left_motor.brake();
right_motor.brake();
2.3 原地转弯2.3 Turning in Place
让左右电机反向转,机器人就会原地旋转:Make the left and right motors spin in opposite directions, and the robot will spin in place:
// 原地右转:左轮向前,右轮向后
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(reverse, 50, percent);
wait(500, msec);
LeftMotor.stop();
RightMotor.stop();
// 原地右转:左轮向前,右轮向后
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(reverse, 50, percent);
wait(500, msec);
LeftMotor.stop();
RightMotor.stop();
// 原地右转:左轮向前,右轮向后
left_motor.move(50);
right_motor.move(-50);
pros::delay(500);
left_motor.brake();
right_motor.brake();
原地左转就是反过来:左轮向后,右轮向前。搞混了方向很正常,多试几次就记住了。To turn left in place, just reverse it: left wheel backward, right wheel forward. Mixing up directions is totally normal — you'll get it after a few tries.
2.4 弧线转弯2.4 Arc Turns
给左右轮不同的速度(但同方向),机器人就会走弧线:Give the left and right wheels different speeds (but same direction), and the robot will drive in an arc:
// 弧线右转:左轮快,右轮慢
LeftMotor.spin(forward, 60, percent);
RightMotor.spin(forward, 20, percent);
wait(2000, msec);
LeftMotor.stop();
RightMotor.stop();
// 弧线右转:左轮快,右轮慢
LeftMotor.spin(forward, 60, percent);
RightMotor.spin(forward, 20, percent);
wait(2000, msec);
LeftMotor.stop();
RightMotor.stop();
// 弧线右转:左轮快,右轮慢
left_motor.move(60);
right_motor.move(20);
pros::delay(2000);
left_motor.brake();
right_motor.brake();
速度差越大,弯越急;速度差越小,弯越缓。The bigger the speed difference, the sharper the turn; the smaller the difference, the gentler the curve.
2.5 封装成函数2.5 Wrapping into Functions
每次写 4 行来直走太麻烦了,我们可以把常用动作封装成函数:Writing 4 lines every time just to drive straight is tedious. We can wrap common actions into functions:
void moveForward(int speed, int ms) {
LeftMotor.spin(forward, speed, percent);
RightMotor.spin(forward, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void turnRight(int speed, int ms) {
LeftMotor.spin(forward, speed, percent);
RightMotor.spin(reverse, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void turnLeft(int speed, int ms) {
LeftMotor.spin(reverse, speed, percent);
RightMotor.spin(forward, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void moveForward(int speed, int ms) {
LeftMotor.spin(forward, speed, percent);
RightMotor.spin(forward, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void turnRight(int speed, int ms) {
LeftMotor.spin(forward, speed, percent);
RightMotor.spin(reverse, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void turnLeft(int speed, int ms) {
LeftMotor.spin(reverse, speed, percent);
RightMotor.spin(forward, speed, percent);
wait(ms, msec);
LeftMotor.stop();
RightMotor.stop();
}
void moveForward(int speed, int ms) {
left_motor.move(speed);
right_motor.move(speed);
pros::delay(ms);
left_motor.brake();
right_motor.brake();
}
void turnRight(int speed, int ms) {
left_motor.move(speed);
right_motor.move(-speed);
pros::delay(ms);
left_motor.brake();
right_motor.brake();
}
void turnLeft(int speed, int ms) {
left_motor.move(-speed);
right_motor.move(speed);
pros::delay(ms);
left_motor.brake();
right_motor.brake();
}
有了这些函数,走一个正方形路线变得很简单:With these functions, driving in a square becomes really simple:
// 走正方形:前进 → 右转 → 前进 → 右转 ...
for (int i = 0; i < 4; i++) {
moveForward(50, 1000);
turnRight(30, 500);
}
// 走正方形:前进 → 右转 → 前进 → 右转 ...
for (int i = 0; i < 4; i++) {
moveForward(50, 1000);
turnRight(30, 500);
}
// 走正方形:前进 → 右转 → 前进 → 右转 ...
for (int i = 0; i < 4; i++) {
moveForward(50, 1000);
turnRight(30, 500);
}
for (int i = 0; i < 3; i++) {
moveForward(50, 1000);
turnLeft(30, 600);
}
for (int i = 0; i < 3; i++) {
moveForward(50, 1000);
turnLeft(30, 600);
}
for (int i = 0; i < 3; i++) {
moveForward(50, 1000);
turnLeft(30, 600);
}
2.6 写代码练习2.6 Coding Exercise
用 moveForward() 和 turnRight() 写一段代码,让机器人走一个L 形路线:先前进,右转 90 度,再前进。Write code using moveForward() and turnRight() to make the robot drive an L-shaped path: drive forward, turn right 90 degrees, then drive forward again.