Writing the Code for a Subsystem

Adding code to create an actual working subsystem is very straightforward. For simple subsystems that don’t use feedback it turns out to be extremely simple. In this section we will look at an example of a Claw subsystem. The Claw subsystem also has a limit switch to determine if an object is in the grip.

RobotBuilder Representation of the Claw Subsystem

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

The claw at the end of a robot arm is a subsystem operated by a single VictorSPX Motor Controller. There are three things we want the motor to do, start opening, start closing, and stop moving. This is the responsibility of the subsystem. The timing for opening and closing will be handled by a command later in this tutorial. We will also define a method to get if the claw is gripping an object.

Adding Subsystem Capabilities

12 public class Claw extends SubsystemBase {
13     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
14 public static final double PlaceDistance = 0.1;
15 public static final double BackAwayDistance = 0.6;
16
17     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
18
19     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
20 private PWMVictorSPX motor;
21 private DigitalInput limitswitch;
22
23     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
24
25     public Claw() {
26         // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
27 motor = new PWMVictorSPX(4);
28  addChild("motor",motor);
29  motor.setInverted(false);
30
31 limitswitch = new DigitalInput(4);
32  addChild("limit switch",limitswitch);
33
34
35
36         // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
37     }
38
39     @Override
40     public void periodic() {
41         // This method will be called once per scheduler run
42
43     }
44
45     @Override
46     public void simulationPeriodic() {
47         // This method will be called once per scheduler run when in simulation
48
49     }
50
51     public void open() {
52         motor.set(1.0);
53     }
54
55     public void close() {
56         motor.set(-1.0);
57     }
58
59     public void stop() {
60         motor.set(0.0);
61     }
62
63     public boolean isGripping() {
64         return limitswitch.get();
65     }
66
67 }

Add methods to the claw.java or claw.cpp that will open, close, and stop the claw from moving and get the claw limit switch. Those will be used by commands that actually operate the claw.

Nota

The comments have been removed from this file to make it easier to see the changes for this document.

Notice that member variable called motor and limitswitch are created by RobotBuilder so it can be used throughout the subsystem. Each of your dragged-in palette items will have a member variable with the name given in RobotBuilder.

Adding the Method Declarations to the Header File (C++ Only)

12class Claw: public frc2::SubsystemBase {
13private:
14    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
15frc::PWMVictorSPX m_motor{4};
16frc::DigitalInput m_limitswitch{4};
17
18    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
19public:
20Claw();
21
22    void Periodic() override;
23    void SimulationPeriodic() override;
24    void Open();
25    void Close();
26    void Stop();
27    bool IsGripping();
28    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS
29
30    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS
31    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
32static constexpr const double PlaceDistance = 0.1;
33static constexpr const double BackAwayDistance = 0.6;
34
35    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
36
37
38};

In addition to adding the methods to the class implementation file, Claw.cpp, the declarations for the methods need to be added to the header file, Claw.h. Those declarations that must be added are shown here.

To add the behavior to the claw subsystem to handle opening and closing you need to define commands.