Coding the Romi- Basic Movement

Challenge: Code the Romi

Now, you've just learned all the tools you'll use to code the Romi. This might be a difficult task, but splitting it up into smaller chunks will make it significantly easier to deal with.

Generally, your code should:

Come up with a thorough plan before you code, with backup plans. It will save you the hassle of rewriting everything mid-code. Also, bounce ideas off other programmers, and make sure to work with them. It's a good skill to work with others in a FIRST Robotics team, anyway.


RomiDrivetrain (Subsystem)

Write a GetInstance() Method

Your code should:

Sample Code + Explanation

Write a moveRobot() Method

Now that you learned about how to set motors, at the bottom of the RomiDrivetrain class, make a helper method called moveRobot().

Conditions:

For the last point, although you can use math.abs() or a bunch of if-statements, there's another way we can do this, but using one of the built-in libraries in WPILIB VSC.

WPILIB's MathUtil class has a clamp method, clamp(number, lowestBound, highestBound).

Whatever's in the number parameter will never be smaller than the lowestBound and greater than the highestBound.

For example, if I were to do int num = MathUtil.clamp(3, 4, 6), num will be some value between 4 and 6 inclusively.

Make sure to import it! (Put this in your class header: import edu.wpi.first.math.MathUtil)

Once you have what you believe to be your answer, compare your code with the answer code below.

Answer Code
Alternate Answer Code

Answer Breakdown

DO NOT OPEN UNTIL YOU FINISHED WRITING CODE!

[sorry WIP]


Command

Create a MoveRobot() command

All you'll need to do is make a class that:

Sample Code
Sample Code (alternate)

Answer Breakdown

DO NOT OPEN UNTIL YOU FINISHED WRITING CODE!

Let's break down these two sets of code.

The header looks pretty normal, but you might find this line.

VSC gives warnings in your console for a variety of reasons. PMD.UnusedPrivateField refers to having private fields that are declared, but are never actually used. PMD.SingularField is creating a variable you'll only use in one spot of code, and never again.

Generally, doing these is considered messier, and VSC gives these warnings to help unsuspecting programmers optimize and clean up their code. However, due to the nature of FRC Programming, these warnings tend to be a nuisance rather than helpful. For example, what if you created a Spark variable, but didn't know how often you'd use it in your code? You'll create a new Spark variable at the top, instead of having to create a brand new one every time you want to use it. It's just more convenient.

This line of code stops VSC from panicking whenever these errors happen.

The constructor is pretty standard, nothing interesting to say about it, aside from the addRequirements() method. The addRequirements() method simply takes subsystem(s) in its parameters, and as the name suggests, forces the command to require the subsystem. Without it, your code will not work properly.

There are two approaches for using commands that I like to use.


RobotContainer

Bind MoveRobot() Command to Trigger

This is the final step before you can move the robot! All you'll need to do is:

Answer Code

Last updated