Programming A Robot!
We are going to learn how to program a robot by programming Bonk’s intake! Bonk’s intake is a part of the robot that we use to get game pieces in and out of the robot. In the photo below, there is a photo of Bonk’s intake (what the yellow arrows are pointing at) intaking a red ball.
Now that you know what Bonk’s intake is and what it looks like, let's get started on programming it!
Note: If you haven’t read up on Command Based Programming and you aren’t too familiar with the basics of Java, you should go back and learn those two before moving on. Once you are comfortable with the basics of Java and have an understanding of Command Based Programming, then you can adventure on and learn how to program Bonk’s intake.
Getting the Code
The code can be found here. To get the code into your VSCode, you can download it, open VS Code, go to select folder and open the folder that you downloaded like shown in the photo.
Programming The Subsystem
Now let’s program Bonk’s Intake.The intake has a motor that spins clockwise to take in the game piece and spins counterclockwise to take out the game piece if needed.
In the subsystem, we need to do the following:
Define our motor controllers
Create methods for each action the mechanism needs to do.
To start programming the subsystem, click on the subsystem folder and click on the file called “intake.java”.
The robot uses a motor controller which is called a TalonSRX. Right now the robot doesn’t know this. So we need to tell the robot that it has a TalonSRX motor controller. We do this by adding the following line of code on line 14:
public WPI_TalonSRX intakearm = new WPI_TalonSRX(Constants.INTAKEARM);
Your code should look similar to the photo below.
To break down what this line means:
public means that it can be used anywhere in the code
WPI_TalonSRX is the name of the library, you can see that we import the library on line 7
Intakearm is the name we are giving the variable
= means it’s being set equal to something
new is just part of the set up
WPI_TalonSRX is the name of the library
(Constants.INTAKEARM); means that the motor controller ID can be found in the Constants.java file under the variable INTAKEARM.
You will have a red line under the word INTAKEARM because we have not made that variable yet. We will be going back to that later so you can leave it be for now.
Note: Adding libraries
When you write code for your robot, you have to import the libraries and other files that you are using. You don’t do this here because we already imported these for you. But for future reference, there are two ways to import your libraries and files
#1: As you typing out the name, you can click (or press enter) when the library name is highlighted in blue
#2: Once you have typed out the line of code, you can go back and import the library. You do this by putting your cursor over the line of code, select Quick Fix… (Ctrl+.) and then click on Import ‘WPI_TalonSRX’ (com.ctre.phoenix.motorcontrol.can).
Adding the subsystem: Continued
Next we need to create methods for each action that the intake needs to do.
The intake needs to spin counterclockwise to take in the game piece. So we will create a method that sets the motor controller to spin clockwise.
We will do this by writing the following code on line 18
public void intakeIn(){
intakearm.set(-1);
}
It should look like the photo below
To break down what the code means:
public means that the method can be used anywhere in the code.
void means it doesn’t use the keyword “return” so it’s not returning anything
intakeIn is the name we are giving the method.
intakearm.set(-1) means that we are taking the intakearm motor controller and setting the motor speed to -1. This makes it spin counterclockwise at full speed.
Lastly, we need to write code to make the intake stop. Without this code, the motor will keep spinning without stopping. We will name this method intakeStop. We will also set the speed to 0 because 0 means that the motor is not spinning.
The following code will go under your intakeOut() method on line 26.
public void intakeStop(){
intakearm.set(0);
}
It should look like the photo below
Now we need to add the motor controller ID in Constants.java. To do this we go to the Constant.java file.
Once you open up the file. We can add the motor controller ID. We will be adding the ID using the following line of code:
public static final int INTAKE= 12;
The code should look like the image below
To explain what the code does, it creates a variable that can’t be changed and it’s set equal to 12. You also can’t go back and try to define INTAKE to a different value in a different file as well because it’s final.
We are setting it equal to 12 because we set the motor controller to be 12 when we set it up. Now we can move on to programming the commands!