为指令写代码。
子系统类让机器人的机械装置移动,但为了让它在正确的时间停止并通过更复杂的操作顺序,你编写命令。之前在:doc:编写子系统代码时,我们为机器人上的`Claw 子系统开发了代码,用于启动爪子张开、闭合或停止移动。现在我们将为一个命令编写代码,该指令将在正确的时间实际运行爪形电机,以使爪形打开和关闭。我们的爪形示例是一个非常简单的机制,我们运行电机 1 秒钟以将其打开或直到限位开关跳闸将其关闭。
在 RobotBuilder 中关闭 Claw 指令
这是 RobotBuilder 中 CloseClaw 指令的定义。请注意,它需要 Claw 子系统。这将在下一步中解释。
生成的 CloseClaw 类
11 // ROBOTBUILDER TYPE: Command.
12
13 package frc.robot.commands;
14 import edu.wpi.first.wpilibj2.command.CommandBase;
15
16 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
17 import frc.robot.subsystems.Claw;
18
19 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
20
21 /**
22 *
23 */
24 public class CloseClaw extends CommandBase {
25
26 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
27 private final Claw m_claw;
28
29 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
30
31 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
32
33
34 public CloseClaw(Claw subsystem) {
35
36
37 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
38 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
39
40 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
41 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
42
43 m_claw = subsystem;
44 addRequirements(m_claw);
45
46 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
47 }
48
49 // Called when the command is initially scheduled.
50 @Override
51 public void initialize() {
52 m_claw.close(); // (1)
53 }
54
55 // Called every time the scheduler runs while the command is scheduled.
56 @Override
57 public void execute() {
58 }
59
60 // Called once the command ends or is interrupted.
61 @Override
62 public void end(boolean interrupted) {
63 m_claw.stop(); // (3)
64 }
65
66 // Returns true when the command should end.
67 @Override
68 public boolean isFinished() {
69 return m_claw.isGripping(); // (2)
70 }
71
72 @Override
73 public boolean runsWhenDisabled() {
74 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
75 return false;
76
77 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
78 }
79 }
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(); // (1)
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(); // (2)
42}
43
44// Called once after isFinished returns true
45void CloseClaw::End(bool interrupted) {
46 m_claw->Stop(); // (3)
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 子系统具有启动和停止电机向正确方向运行的方法。命令的职责是在正确的时间运行电机。添加框中显示的代码行以添加此行为。
通过调用在 CloseClaw Initialize 方法中添加到 Claw 子系统的 Close() 方法,启动爪形电机朝关闭方向移动。
This command is finished when the limit switch in the Claw subsystem is tripped.
End()
方法在命令完成时被调用,它是一个清理的地方。在这种情况下,电机停止,因为时间已用完。