Tech Toolbox
  • Please Visit https://ftc-tech-toolbox.vercel.app/ for the new tech toolbox!!
    • Introduction
    • Choosing an IDE
    • Creating an OpMode
    • Motors and Encoders
    • Servos
    • Gamepad Controls
    • Drive Systems
    • Lynx Modules
    • Telemetry
    • Wireless Download
    • The Sleep Command
  • Please Visit the New Link
    • Tank Drive / Skid Steer (Part 1)
    • Mecanum Drive (Part 1)
    • Turrets
    • Linear Slides
    • Kicker
    • Active Intake / Sweepers
    • Flywheels / Shooters
  • Please Visit the new Link
    • Base Class (Step 1)
    • Module Classes (Step 2)
    • OpMode Classes (Step 3)
  • This domain is now depreciated and is no longer updated!
  • This domain is now depreciated and is no longer updated!
    • What is Localization?
    • General Odometry Logic
    • Tank (No Deadwheels)
    • Mecanum (No Deadwheels)
    • Deadwheel Odometry (Mecanum and Tank)
    • VSLAM
  • This domain is now depreciated and is no longer updated!
    • What is Control Theory?
    • Custom PID Loops
    • Essential Control Theory Concepts
    • Resources for Learning Advanced Control Theory
  • This domain is now depreciated and is no longer updated! Please visit this domain for the new TT!
    • Introduction
    • Mecanum Drive (Part 2)
    • Tank Drive (Part 2)
    • Introduction to Pure Pursuit
    • Pure Pursuit: Mecanum
    • Pure Pursuit: Tank
    • Advanced Pure Pursuit
    • Guided Vector Fields
    • Autonomous Movement Libraries
  • Sensors
    • IMU
    • Color Sensors
      • Advanced Sensing Code
    • Distance Sensors
    • Touch Sensor
  • Computer Vision
    • Setting up Camera/Intro to Computer Vision Tools
      • Intro to OpenCV
      • Vuforia
    • Streaming Frames
    • Color Thresholding
    • April Tags
    • Linear Regression
    • Machine Learning Toolchain
    • Object Distance Estimation
    • Object Tracking / Driving to an Object
    • Computer Vision Simulators
  • Simulators
    • Beta8397 Simulator
    • VRS
  • Cool Codebases
Powered by GitBook
On this page
  • Implementation
  • Base Class
  • Module Class
  1. Please Visit the new Link

Module Classes (Step 2)

Programming Robot Modules

PreviousBase Class (Step 1)NextOpMode Classes (Step 3)

Last updated 1 year ago

Prerequisites

Consider the base class to be the empty skeleton of your robot, a base class with no module classes is an empty robot. Each of your module classes will represent a hardware module on your robot such as your drive system, linear slide system, claws, etc. And each of your modules will contain some combination of sensors, motors, and servos. The purpose of your module classes will contain functions that control these hardware mechanisms to perform your functions.

By organizing your modules with classes, executing the purpose of the module can be as simple as calling the function in your opMode, allowing you to have an organized codebase.

Implementation

Suppose that you have a robot with a singular arm, controlled by a motor connected to an encoder. Following the code structure outlined, you create an Arm class containing the functions needed to control the arm motor and a base class that contains the module.

Base Class

Here we initialize the arm module as a global variable and fully initialize the class within our initailizeHardware function. Note that we also pass the motor needed for the class to function as a parameter within the class constructor.

public abstract class Base extends LinearOpMode {

  // Sensors
  public IMU gyro;

  // Module Classes
  public Arm armModule = null; // This is an actual class with various methods

  // Initialize Hardware Function
  public void initHardware() throws InterruptedException {
    // Hubs
    List<LynxModule> allHubs;
    allHubs = hardwareMap.getAll(LynxModule.class);
    for (LynxModule hub : allHubs) {
      hub.setBulkCachingMode(LynxModule.BulkCachingMode.AUTO);
    }

    // Motors
    DcMotor armMotor = hardwareMap.get(DcMotor.class, "Drive Motor");  
    armMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    armMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    armMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  
   // Init Module class
    armModule = new Arm(armMotor); 
  }
  
  //Utility Functions
  public String formatDegrees(double degrees) {
    return String.format(Locale.getDefault(), "%.1f", AngleUnit.DEGREES.normalize(degrees));
  }
  
  // Allows you to connect opModes to the base class 
  @Override
  public abstract void runOpMode() throws InterruptedException;
}

Module Class

Here we will create the functions required for the arm to execute its purpose. Take for example making the arm motor move some amount of encoder ticks.

public class Arm{
    
    DcMotor motor; 
    // class constructor
    public void Arm(DcMotor armMotor){
        this.motor = armMotor; // reinit motors for use within class functions. 
    }
    
    public void moveArmMotorTicks(int ticksToBeMoved){ 
        motor.setTargetPosition(ticksToBeMoved);  //Sets Target Tick Position
        motor.setMode(DcMotor.RunMode.RUN_TO_POSITION); 
        motor.setPower(1);  //Sets Motor to go to position at 1 power.
    }

}

In the next module, you will learn how to integrate opModes with this type of code structure!

Base Class
Commonly Programmed Modules