Programming The Commands
Commands are what gives actions or commands to different parts of the robot. In this example, we will be giving actions or commands to the intake subsystem. The actions or commands that we will be giving the intake are:
IntakeIn
IntakeOut
IntakeStop
You can find these files under the command folder.
There are three different files for each command because each command needs its own file.This will be helpful once we start programming the robot controller. Once you find the files, click on IntakeInCMD.java.
IntakeInCMD
Even though each command does something different, the step up for each command is very similar. Therefore, these will be broken into steps and each step will be referred to later on.
First, let's go to the file “IntakeInCMD.java”. Due to the fact that the methods that we made in the subsystem code are in a different file, we need to create a variable to let us access methods. In order to do this, we add the following code to line 12:
public final intake m_intake;
It should look like the photo below.
You will have a red line under “IntakeInCMD()” but we will fix that in the next step.
To break down the line of code:
“public” means that the variable can be used anywhere in the code.
“final” means the variable can only be defined once
“intake” is the name of the subsystem
“m_intake” is the variable that we will be using to access the methods in the intake subsystem
Next we need to fix the red line under “IntakeInCMD()”. To do this we will add
intake intake_subsystem
in between the two parentheses on line 13. This is allowing us to pass information about the subsystem to the code. Next we will write the following code inside the method on line 14
m_intake = intake_subsystem;
addRequirements(m_intake);
The first part is setting the information equal to the variable that we defined on line 12. The second part is telling the code that it needs the subsystem to run.
Your code should look like the photo below
Next we need to tell the robot what to do when the command runs. We do this by writing code in the execute() method. Inside the execute() method, we are going to run the intakeIn() method that we wrote in the intake subsystem. To do this we put the following code inside the excture() method on line 26
m_intake.intakeIn();
It should look like the photo below
What this code does is that it looks inside the intake.java subsystem, and it uses the intakeIn() method to run the code that is inside intakeIn()
Now, we need to make sure the robot’s intake stops when it is supposed to. To do this we will use the stopintake() method that we created in the subsystem, we will put the code
m_intake.intakestop();
on line 32 inside the end method.
It should look like the photo below
Now we will program the command to spin the motor in the other direction.First, open the intakeOutCMD.java file. It is in the “Commands” folder under IntakeInCMD.java. The setup is also the same as what you wrote for intakeInCMD. The only difference is what method you call in the execute() method. Therefore, You can go back to intakeInCMD and look at step 1 and follow it again. When you are done, it should look like the photo below
Next we need to tell the robot what to do when the command runs. Like what we did with intakeInCMD(), we will go to the execute method and run the intakeOut() method that we wrote in the intake subsystem. You can look back at step 2 and follow it again. When you are done, it should look like the photo below
After that we will put in the code so the motor stops spinning when the code is finished. To do this, you can go back and follow step 3 again. When you are done, it should look like the photo
Now we will program the command to stop spinning when the intake is not needed. Again, the setup is also the same as what you wrote for intakeInCMD and intakeOutCMD. The only difference is what method you call in the execute() method. Therefore, You can go back to intakeInCMD and look at step 1 and follow it again. When you are done, it should look like the photo below
Next we need to tell the robot what to do when the command runs.We will go to the execute method and run the intakeStop() method that we wrote in the intake subsystem. You can look back at step 2 and follow it again. When you are done, it should look like the photo below.
After that we will put in the code so the motor stops spinning when the code is finished. To do this, you can go back and follow step 3 again. When you are done, it should look like the photo below
Now you are all done! You have programmed the robot’s intake so now it can take in a game piece!