Skip to main content

Simple Linear Axis

Pre-requisites

Create a new robot project

Open FRC VSCode, and create a new robot project.

note

if you don't have this year's version, you can download it from here.

Installing vendor libraries:

Install the CTRE vendor libraries so that we can import the TalonSRX class. The upstream docs are here.

In short:

  1. Ctrl+P > Install vendor libraries.
  2. Paste in the URL for Phoenix (v5): https://maven.ctr-electronics.com/release/com/ctre/phoenix/Phoenix5-frc2023-latest.json.
  3. Press "Enter" and ensure that the project builds.

Tasks

1. Getting started:

  1. Set up the robot code so that when you push the left stick on the Gamepad to the right (in the positive direction), the block moves away from the motor.
  2. Print out the "demand" (the value being assigned to the motor) and make sure that positive values demanded correspond with the block moving away from the motor. If it doesn't, you can use the setInverted method on the motor object to invert the motor direction
An english explanation for how to do this

Define a Joystick and a TalonSRX as field variables (defined at the class level, not inside a method). Initialize these two variables as part of robotInit().

In teleopPeriodic(), assign an output to the motor; use the set(ControlMode.PercentOutput, <value>) method on the TalonSRX variable you created in the previous step. I typically get values from Joystick using the getRaw methods, but feel free to use whatever works for you.

The answer (a complete Robot.java)

2. Define and output the limit switches

Our goal in step 3 is to use the forward and reverse limit switches to prevent the block from moving outside of this mechanism's physical limits. In this step, we want to make sure the limit switches are wired up correctly and that we can read whether they are pressed or not.

  1. In the robot code, publish forwardLimit and reverseLimit, the states of the two limit switches, using NetworkTables. Forward refers to the switch the block would hit when moving in the forward/positive direction.
  2. Use OutlineViewer (installed as part of FRC VSCode/WPILib) to view the values in NetworkTables and confirm that they change when the limit switches are pressed.
An english explanation for how to do this

Define DigitalInputs for both the forward and reverse limit - as we did with the Joystick and TalonSRX. Initialize these two variables as part of robotInit().

Define NetworkTableEntrys for both the forward and reverse limit. In robotInit(), the code block you'll need to initialize these would look something like this:

NetworkTableInstance instance = NetworkTableInstance.getDefault();
forwardLimitEntry = instance.getEntry("forwardLimit");
reverseLimitEntry = instance.getEntry("reverseLimit");

In teleopPeriodic(), use the setBoolean method on the NetworkTableEntry variables you created in the previous step and publish the values of each limit switch. Deploy your code to the robot, open up OutlineViewer, enable the robot, and ensure that the values change when you click each limit switch.

The answer (a complete Robot.java)

3. Implement the limit switches

  1. Configure the robot code so that you can't drive forward when the forward limit is pressed. Same with the reverse limit.
  2. Verify that your logic works, by driving the motor forward slowly and triggering the switch with your finger. The motor should stop. Repeat the same testing process with the reverse limit.

4. Add the encoder

  1. Just as we added the limit switch data to NetworkTables, add an Encoder and publish its position to NetworkTables.

5. Add a home sequence

In order for us to control position, we need to define a "starting point". This could be in relation to wherever the robot code starts up, but that's not typically helpful. Instead, we define the "origin" to be at the reverse limit. That way in the future, we can assign a position demand of 1 inch and the block will move to 1 inch in front of the reverse limit.

  1. Use the following skeleton https://github.com/Team176AcesHigh/2022RoboxLinearAxis/blob/step/10_state_machine_template/src/main/java/frc/robot/Robot.java
  2. When the robot starts up, the axis should home itself. It should:
    1. Drive backwards slowly
    2. Listen for the reverse limit switch to be pressed
    3. As soon as it's pressed, we should reset the encoder to 0, and
    4. Transition to OPEN_LOOP mode with a demand of 0 (0%/do nothing)

6. Add position setpoint

  1. When you press the a button, the block should move to 1 inch in front of the reverse limit.
  2. When you press the b button, the block should move to 2 inches in front of the reverse limit.

7. Add a re-home button

  1. When you press the x button, the axis should re-home by tapping the reverse limit switch, then switching back to open-loop mode