0 / 5 页已完成

让电机转起来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:

  1. 声明电机对象 — 告诉程序"我在哪个端口接了一个电机"Declare a motor object — tell the program "I have a motor connected to this port"
  2. 发指令 — 告诉电机转、停、用多大力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:

C++ (VEXcode)
// 在端口 1 接一个电机,绿色齿轮比,不反转
motor LeftMotor = motor(PORT1, ratio18_1, false);

// 在端口 2 接一个电机,绿色齿轮比,反转
motor RightMotor = motor(PORT2, ratio18_1, true);

三个参数的意思:What the three parameters mean:

C++ (VS Code)
// 在端口 1 接一个电机,绿色齿轮比,不反转
motor LeftMotor = motor(PORT1, ratio18_1, false);

// 在端口 2 接一个电机,绿色齿轮比,反转
motor RightMotor = motor(PORT2, ratio18_1, true);

三个参数的意思:What the three parameters mean:

C++ (PROS)
// 在端口 1 接一个电机,绿色齿轮比
pros::Motor left_motor(1, pros::MotorGearset::green);

// 在端口 2 接一个电机,绿色齿轮比,反转
pros::Motor right_motor(2, pros::MotorGearset::green, true);

参数的意思:What the parameters mean:

为什么要反转?Why Reverse?

机器人左右两侧的电机方向是镜像的。同样是"往前转",左边电机顺时针,右边电机逆时针。设置反转后,你写 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:

C++ (VEXcode)
// 正转,50% 速度
LeftMotor.spin(forward, 50, percent);

// 反转,50% 速度
LeftMotor.spin(reverse, 50, percent);

spin() 三个参数:方向、速度值、单位。spin() takes three parameters: direction, speed value, and unit.

C++ (VS Code)
// 正转,50% 速度
LeftMotor.spin(forward, 50, percent);

// 反转,50% 速度
LeftMotor.spin(reverse, 50, percent);

spin() 三个参数:方向、速度值、单位。spin() takes three parameters: direction, speed value, and unit.

C++ (PROS)
// 正转,速度 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:

C++ (VEXcode)
LeftMotor.stop();
C++ (VS Code)
LeftMotor.stop();
C++ (PROS)
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
C++ (VEXcode)
// 设置停止模式为 hold(锁定)
LeftMotor.setStopping(hold);
LeftMotor.stop();
C++ (VS Code)
// 设置停止模式为 hold(锁定)
LeftMotor.setStopping(hold);
LeftMotor.stop();
C++ (PROS)
// 设置停止模式为 hold(锁定)
left_motor.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
left_motor.brake();
动手试试Try It Out

把停止模式分别改成 coastbrakehold,观察电机停下来的方式有什么不同。用手转一下电机轴,感受区别。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
怎么选齿轮比?How to Choose a Gear Ratio?

速度和力矩是跷跷板关系。底盘通常用蓝色(要速度),机械臂用红色(要力气),不确定就用绿色Speed and torque are a trade-off. Drivetrains usually use blue (need speed), arms use red (need power), if unsure, use green.

检查点:预测结果Checkpoint: Predict the Result
下面这段代码执行后,电机会怎样?What will happen to the motor after running this code?
C++ (VEXcode)
motor ArmMotor = motor(PORT3, ratio36_1, false);
ArmMotor.spin(forward, 100, percent);
wait(2000, msec);
ArmMotor.setStopping(coast);
ArmMotor.stop();
C++ (VS Code)
motor ArmMotor = motor(PORT3, ratio36_1, false);
ArmMotor.spin(forward, 100, percent);
wait(2000, msec);
ArmMotor.setStopping(coast);
ArmMotor.stop();
C++ (PROS)
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

挑战Challenge

写一段代码,完成以下动作:Write code to perform these actions:

  1. 声明一个电机在端口 5,蓝色齿轮比,不反转Declare a motor on port 5, blue gear ratio, not reversed
  2. 让它以 80% 速度正转 3 秒Spin it forward at 80% speed for 3 seconds
  3. 以 hold 模式停下来Stop with hold mode