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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 public class Claw extends SubsystemBase {
     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
 public static final double PlaceDistance = 0.1;
 public static final double BackAwayDistance = 0.6;

     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS

     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
 private PWMVictorSPX motor;
 private DigitalInput limitswitch;

     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS

     public Claw() {
         // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
 motor = new PWMVictorSPX(4);
  addChild("motor",motor);
  motor.setInverted(false);

 limitswitch = new DigitalInput(4);
  addChild("limit switch",limitswitch);



         // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
     }

     @Override
     public void periodic() {
         // This method will be called once per scheduler run

     }

     @Override
     public void simulationPeriodic() {
         // This method will be called once per scheduler run when in simulation

     }

     public void open() {
         motor.set(1.0);
     }

     public void close() {
         motor.set(-1.0);
     }

     public void stop() {
         motor.set(0.0);
     }

     public boolean isGripping() {
         return limitswitch.get();
     }

 }

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.

Note

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)

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Claw: public frc2::SubsystemBase {
private:
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
frc::PWMVictorSPX m_motor{4};
frc::DigitalInput m_limitswitch{4};

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
public:
Claw();

    void Periodic() override;
    void SimulationPeriodic() override;
    void Open();
    void Close();
    void Stop();
    bool IsGripping();
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
static constexpr const double PlaceDistance = 0.1;
static constexpr const double BackAwayDistance = 0.6;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS


};

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.