Skip to content
Matthew Fan edited this page Oct 3, 2017 · 1 revision

Using the Core Package

The core package strives to deliver structure and power to your team's robot code.

Structure

Robots are divided into three sections- a base, a component, and a command.

Base

The RobotBase class is designed as a template for your own base class. To create your own base:

public class MyBase extends RobotBase
{
  // Add more if needed
  MyComponent component = new MyComponent();  

  @Override
  public void init(HardwareMap HW)
  {
    hardware = HW;  // It is critical that you write this!

    // Initialize more if you have more
    component.init(this);
  }
}

The base serves as a single object that you can pass in OpModes. It also serves as a central object in hardware mapping.

Component

The RobotComponent class is designed to control the functionality of a single component of your robot(drivetrain, shooter, etc.). To create your own component:

public class MyComponent extends RobotComponent
{
  // Standard hardware declarations go here
  
  @Overrid
  public void init(final RobotBase BASE)
  {
    super.init(BASE);

    // Hardware map here
  }

  // Component functionality goes here
}

Command

The RobotCommand class serves as a wrapper for commands that wish to be executed on a separate thread or need the flexibility to be executed on either the main thread or a separate thread. To create a command:

class MyCommand extends RobotCommand
{
  private boolean _stop = false;

  // MUST be overwritten!
  @Override
  public void runSequentially()
  {
    // Command Actions 
  }

  // May be not overwritten- if not overwritten, will execute runSequentially() on a separate thread.
  @Override
  public void runParallel()
  {
    if(t == null)
    {
      _stop = false;
      t = new Thread(new Runnable()
      {
        @Override 
        public void run()
        {
          // Command Actions
        }
      }

      t.start();
    }
  }

  // MUST be overwritten!
  @Override public void stop()
  {
    _stop = true;  
  }
}

Hardware Mapping

The HardwareMapper class serves to make hardware mapping easy, organized, and readable. To use:

public class MyComponent extends RobotComponent
{
  DcMotor motor;

  @Override void init(final RobotBase BASE)
  {
    super.init(BASE);

    motor = mapper.mapMotor("motor" , DcMotorSimple.Direction.FORWARD);
  }
}

Using in an OpMode

Note: we use LinearOpMode as opposed to OpMode

@TeleOp(name = "My TeleOp" , group = "Tests")
public class OpMyTeleOp extends LinearOpMode
{
  private MyBase _base = new MyBase();

  @Override
  public void runOpMode() throws InterruptedException
  {
    _base.init(hardwareMap);

    waitForStart();

    while(opModeIsActive())
    {
      // Run components
    }
  }
}