为指令写代码。

子系统类让机器人的机械装置移动,但为了让它在正确的时间停止并通过更复杂的操作顺序,你编写命令。之前在:doc:编写子系统代码时,我们为机器人上的`Claw 子系统开发了代码,用于启动爪子张开、闭合或停止移动。现在我们将为一个命令编写代码,该指令将在正确的时间实际运行爪形电机,以使爪形打开和关闭。我们的爪形示例是一个非常简单的机制,我们运行电机 1 秒钟以将其打开或直到限位开关跳闸将其关闭。

在 RobotBuilder 中关闭 Claw 指令

../../../../../_images/writing-command-code-1.png

这是 RobotBuilder 中 CloseClaw 指令的定义。请注意,它需要 Claw 子系统。这将在下一步中解释。

生成的 CloseClaw 类

11// ROBOTBUILDER TYPE: Command.
12
13package frc.robot.commands;
14import edu.wpi.first.wpilibj2.command.Command;
15import java.util.function.DoubleSupplier;
16
17// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
18import frc.robot.subsystems.Claw;
19
20    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
21
22/**
23 *
24 */
25public class CloseClaw extends Command {
26
27    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
28        private final Claw m_claw;
29 
30    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
31
32    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
33
34
35    public CloseClaw(Claw subsystem) {
36
37
38    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
39        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
40
41    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
42        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
43
44        m_claw = subsystem;
45        addRequirements(m_claw);
46
47    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
48    }
49
50    // Called when the command is initially scheduled.
51    @Override
52    public void initialize() {
53        m_claw.close();
54    }
55
56    // Called every time the scheduler runs while the command is scheduled.
57    @Override
58    public void execute() {
59    }
60
61    // Called once the command ends or is interrupted.
62    @Override
63    public void end(boolean interrupted) {
64        m_claw.stop();
65    }
66
67    // Returns true when the command should end.
68    @Override
69    public boolean isFinished() {
70        return m_claw.isGripping();
71    }
72
73    @Override
74    public boolean runsWhenDisabled() {
75        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
76        return false;
77
78    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
79    }
80}
11// ROBOTBUILDER TYPE: Command.
12
13// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
14
15#include "commands/CloseClaw.h"
16
17CloseClaw::CloseClaw(Claw* m_claw)
18:m_claw(m_claw){
19
20    // Use AddRequirements() here to declare subsystem dependencies
21    // eg. AddRequirements(m_Subsystem);
22    SetName("CloseClaw");
23    AddRequirements({m_claw});
24
25    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
26
27}
28
29// Called just before this Command runs the first time
30void CloseClaw::Initialize() {
31    m_claw->Close();
32}
33
34// Called repeatedly when this Command is scheduled to run
35void CloseClaw::Execute() {
36
37}
38
39// Make this return true when this Command no longer needs to run execute()
40bool CloseClaw::IsFinished() {
41    return m_claw->IsGripping();
42}
43
44// Called once after isFinished returns true
45void CloseClaw::End(bool interrupted) {
46    m_claw->Stop();
47}
48
49bool CloseClaw::RunsWhenDisabled() const {
50    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
51    return false;
52
53    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
54}

RobotBuilder 将为`CloseClaw` 指令生成类文件。指令代表了爪子的行为,即随着时间的推移进行的操作。要操作这个非常简单的爪形机构,电机需要在关闭方向上运行。 Claw 子系统具有启动和停止电机向正确方向运行的方法。命令的职责是在正确的时间运行电机。添加框中显示的代码行以添加此行为。

  1. 通过调用在 CloseClaw Initialize 方法中添加到 Claw 子系统的 Close() 方法,启动爪形电机朝关闭方向移动。

  2. This command is finished when the limit switch in the Claw subsystem is tripped.

  3. End() 方法在命令完成时被调用,它是一个清理的地方。在这种情况下,电机停止,因为时间已用完。