让电机转起来Making Motors Spin
1.1 电机是什么?1.1 What Is a Motor?
电机(Motor)是机器人的"肌肉"。VEX V5 的智能电机不仅能转,还自带传感器,能告诉你它转了多少圈、现在速度是多少。A motor is the "muscle" of your robot. VEX V5 smart motors can not only spin, but also have built-in sensors that tell you how many rotations they've made and their current speed.
要让电机工作,你需要在代码里做两件事:To make a motor work, you need to do two things in your code:
- 声明电机对象 — 告诉程序"我在哪个端口接了一个电机"Declare a motor object — tell the program "I have a motor connected to this port"
- 发指令 — 告诉电机转、停、用多大力Send commands — tell the motor to spin, stop, or how much power to use
1.2 声明电机1.2 Declaring a Motor
先来看怎么声明一个电机对象:Let's see how to declare a motor object:
// 在端口 1 接一个电机,绿色齿轮比,不反转
motor LeftMotor = motor(PORT1, ratio18_1, false);
// 在端口 2 接一个电机,绿色齿轮比,反转
motor RightMotor = motor(PORT2, ratio18_1, true);
三个参数的意思:What the three parameters mean:
PORT1— 电机插在 Brain 的哪个端口(1~21)PORT1— which port on the Brain the motor is plugged into (1~21)ratio18_1— 齿轮比(后面会讲)ratio18_1— gear ratio (we'll cover this later)false/true— 是否反转方向false/true— whether to reverse the direction
// 在端口 1 接一个电机,绿色齿轮比,不反转
motor LeftMotor = motor(PORT1, ratio18_1, false);
// 在端口 2 接一个电机,绿色齿轮比,反转
motor RightMotor = motor(PORT2, ratio18_1, true);
三个参数的意思:What the three parameters mean:
PORT1— 电机插在 Brain 的哪个端口(1~21)PORT1— which port on the Brain the motor is plugged into (1~21)ratio18_1— 齿轮比(后面会讲)ratio18_1— gear ratio (we'll cover this later)false/true— 是否反转方向false/true— whether to reverse the direction
// 在端口 1 接一个电机,绿色齿轮比
pros::Motor left_motor(1, pros::MotorGearset::green);
// 在端口 2 接一个电机,绿色齿轮比,反转
pros::Motor right_motor(2, pros::MotorGearset::green, true);
参数的意思:What the parameters mean:
1— 端口号(1~21)1— port number (1~21)pros::MotorGearset::green— 齿轮比pros::MotorGearset::green— gear ratiotrue(可选)— 是否反转方向true(optional) — whether to reverse the direction
机器人左右两侧的电机方向是镜像的。同样是"往前转",左边电机顺时针,右边电机逆时针。设置反转后,你写 forward 两个电机就都往前走了。The motors on the left and right sides of the robot are mirrored. For "spinning forward," the left motor goes clockwise while the right motor goes counterclockwise. After setting one to reversed, writing forward makes both motors drive the robot forward.
1.3 让电机转起来1.3 Making the Motor Spin
声明好电机后,用 spin() 让它转:After declaring the motor, use spin() to make it spin:
// 正转,50% 速度
LeftMotor.spin(forward, 50, percent);
// 反转,50% 速度
LeftMotor.spin(reverse, 50, percent);
spin() 三个参数:方向、速度值、单位。spin() takes three parameters: direction, speed value, and unit.
// 正转,50% 速度
LeftMotor.spin(forward, 50, percent);
// 反转,50% 速度
LeftMotor.spin(reverse, 50, percent);
spin() 三个参数:方向、速度值、单位。spin() takes three parameters: direction, speed value, and unit.
// 正转,速度 50(范围 -127 到 127)
left_motor.move(50);
// 反转,速度 -50
left_motor.move(-50);
PROS 用 move(),正数正转,负数反转。范围 -127 到 127。PROS uses move() — positive values spin forward, negative values spin in reverse. Range is -127 to 127.
1.4 停下来1.4 Stopping
让电机停下来也很简单:Stopping a motor is also simple:
LeftMotor.stop();
LeftMotor.stop();
left_motor.brake();
但"停"有三种不同的方式:But there are three different ways to "stop":
| 停止模式Stop Mode | 效果Effect | 适用场景Use Case |
|---|---|---|
coast | 断电滑行,慢慢停下来Power off and coast to a stop | 底盘移动Drivetrain movement |
brake | 刹车,较快停住Brake, stops relatively quickly | 一般场景General use |
hold | 锁定当前位置,不让它动Lock in current position, prevent movement | 机械臂、升降Arms, lifts |
// 设置停止模式为 hold(锁定)
LeftMotor.setStopping(hold);
LeftMotor.stop();
// 设置停止模式为 hold(锁定)
LeftMotor.setStopping(hold);
LeftMotor.stop();
// 设置停止模式为 hold(锁定)
left_motor.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
left_motor.brake();
把停止模式分别改成 coast、brake、hold,观察电机停下来的方式有什么不同。用手转一下电机轴,感受区别。Change the stop mode to coast, brake, and hold respectively, and observe how the motor stops differently each time. Try turning the motor shaft by hand to feel the difference.
1.5 齿轮比1.5 Gear Ratios
VEX V5 电机可以换内部齿轮,不同齿轮对应不同的速度和力矩:VEX V5 motors have swappable internal gears. Different gears give different speeds and torque:
| 齿轮颜色Gear Color | 最高转速Max Speed | 力矩Torque | 代码写法Code |
|---|---|---|---|
| 蓝色Blue | 600 RPM | 小Low |
ratio6_1
ratio6_1
pros::MotorGearset::blue
|
| 绿色Green | 200 RPM | 中Medium |
ratio18_1
ratio18_1
pros::MotorGearset::green
|
| 红色Red | 100 RPM | 大High |
ratio36_1
ratio36_1
pros::MotorGearset::red
|
速度和力矩是跷跷板关系。底盘通常用蓝色(要速度),机械臂用红色(要力气),不确定就用绿色。Speed and torque are a trade-off. Drivetrains usually use blue (need speed), arms use red (need power), if unsure, use green.
motor ArmMotor = motor(PORT3, ratio36_1, false);
ArmMotor.spin(forward, 100, percent);
wait(2000, msec);
ArmMotor.setStopping(coast);
ArmMotor.stop();
motor ArmMotor = motor(PORT3, ratio36_1, false);
ArmMotor.spin(forward, 100, percent);
wait(2000, msec);
ArmMotor.setStopping(coast);
ArmMotor.stop();
pros::Motor arm_motor(3, pros::MotorGearset::red);
arm_motor.move(127);
pros::delay(2000);
arm_motor.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
arm_motor.brake();
1.6 写代码练习1.6 Coding Exercise
写一段代码,完成以下动作:Write code to perform these actions:
- 声明一个电机在端口 5,蓝色齿轮比,不反转Declare a motor on port 5, blue gear ratio, not reversed
- 让它以 80% 速度正转 3 秒Spin it forward at 80% speed for 3 seconds
- 以 hold 模式停下来Stop with hold mode