Preseason Lesson1

From 1511Wookiee
Revision as of 22:41, 28 November 2021 by Heydowns (talk | contribs)
Jump to navigationJump to search

Goal

This lesson is to introduce you to the basic structure of FRC robot code. The overall goal is to turn the assigned motor at a specific and fixed speed when the robot is enabled.

Detailed Introduction

  • The motor you will use is connected to a Talon-SRX motor controller, which is controlled by messages from the roboRIO to the motor controller over the CAN Bus.
  • Because the CAN bus is wired to many different devices, you will need to specify which motor controller you are trying to "talk" to by specifying the CAN Identifier (ID) of the specific device you want to control. The ID is a numeric value.
  • Talon-SRX motor controllers use additional software libraries - the CTRE Phoenix software - to enable easier control of the devices. This software is separate from the basic WPILib library.
  • The documentation for both the base WPILib software libraries and the CTRE Phoenix library can be found on the main Programming page of this wiki.

Detailed Goals

  • The motor you will use is labelled Motor 5 on the test board.
  • The goal is to turn the motor at 1/4 of full speed in the forward direction whenever the robot is enabled

Guided Help

This section will give you guidance on how to achieve the goals in the section above. If you have some idea of how to proceed from past experience, you can skip this entire section.

Initial Setup

You should have a pre-season project setup as described on the pre-season test project creation page. If you followed those instructions, you should have this test project created and saved to your desktop as preseason-yourname. Make sure you have this project open in VSCode before proceeding.

Creating a Motor Controller Object

Right now, our project is pretty empty - it has only a main robot class with some basic methods (functions) defined to control our robot. Right now these are all empty, so our robot will do nothing! To begin, we will need to create an object/variable that represents the Talon SRX motor controller connected to the motor that we want to control.

  1. We want to add our new motor controller object to our main robot class. Our main robot class is defined in robot.h, which was created when you made the test project.
  2. Open robot.h by navigating to it in the left-side tree:
    1. Expand src - src is short for "source code"
    2. Expand main
    3. Expand include - include is the folder where all our header (.h) files go.
    4. Double click Robot.h
  3. Our new motor controller object will be controlling the Talon SRX. To use the class that controls these, we first need to tell our main robot class file where to find the Talon SRX's controller class. We do this by "including" the class's header file into our program's file that needs to use it.
    1. At the top of robot.h, you should already see the line:
      #include <frc/TimedRobot.h>
    2. To use Talon-SRX, we need to include ctre/Phoenix.h so below the above line, add this line:
      #include <ctre/Phoenix.h>
  4. The full class name for the Talon SRX motor controller is ctre::phoenix::motorcontrol::can::TalonSRX -- that's quite long! Instead of typing out the full name every time we want to use this class, we can tell our program that we want to use just the class name instead. We do this by adding a 'using' statement which will let us refer to any class inside of a namespace by just its simple/short name. Add this new line below the include line we added in the previous steps:
    using namespace ctre::phoenix::motorcontrol::can;
  5. Now we are ready to create our motor controller object
    1. Objects can be created by specifying the type of the object followed by a name. This creates an object with the specified name. The name should be something meaningful!!
    2. Inside the Robot class, above the public section, declare your new motor controller object. For this, we will use the name "motor":
      TalonSRX motor{5};
    3. The '{5}' indicates the constructor will be called and '5' will be passed in for the CAN address ID parameter.

Using our new Motor Controller Object

We want to tell the motor to run when the robot is enabled. To do this, we will use our new TalonSRX motor controller object inside the definition of our main robot class.

  1. To start, we need to open the file containing the definition of our main robot class -- the header, robot.h, that we have been using contained the declaration of the our main robot class. The declaration is a high-level interface description. But the definition of our class contains all the detailed code that will control how our robot will work.
    1. In the left tree, expand src, then main if it is not already
    2. Then expand 'cpp', which is the folder that will contain all of our .cpp files. The cpp files are where we put class definitions
    3. Double click Robot.cpp to open it.
  2. Just like the class declaration, this file is currently quite empty as a new project template. You will see all of the methods for controlling the robot in various control modes and phases of an FRC match, but they are all empty! We will be adding code to one of them for now.
  3. The method TeleopPeriodic() is the part of the main robot class that will be running whenever the robot is enabled. This is where we will add our code to turn on the motor.
    1. The braces {} after TeleopPeriodic() is what will enclose all of our code to control the motor - add a new line inside the braces:
      motor.Set(ControlMode::PercentOutput, 0.25);
    2. The above line "sets" the motor to run at a percentage of full power. Recall that we wanted to run at 1/4 of full power. This is what the 0.25 indicates.

Testing Your Program

After you have modified Robot.h and Robot.cpp to control the motor, build the project - refer to Preseason Build Project if you need help doing this.

Once project builds without errors, let instructor know you are ready to try to deploy and test. Do not deploy until told to do so as you may interfere with your classmates!

The move to the Driver Station laptop and enable the robot; the motor should run at 1/4 speed forward. Verify the direction by looking at the lights on the motor controller! They should be green and flashing.