Advanced Hardware - addonovan/ftc-ext GitHub Wiki

The Problem

There are some things that are rather monotonous and ugly to implement with the barebones API that we have been provided. For example: toggle servos between two states isn't difficult, it just requires a lot of logic that just clutters up the OpMode and makes it difficult to read. Just think about everything that you'd need to have for one: a variable to hold it's current state, two constants for the positions it should go to, a variable to keep track of how long it's been since it last toggled (wouldn't want the servo to be able to toggle immediately after it was just toggled, especially if it's bound to a button, after all), and then the logic to tie all these together. Now imagine you have two servos that need to function as toggles. You now need to duplicate most of those just for the second servo.

Hardware Extensions

As you can see, this can be very painful; however, it doesn't have to be! You see, that's what the advanced hardware solves for you. There are already classes for all this behavior so that you don't need to create it yourself. And, best of all, they function like regular devices as well.

For example, we were just talking about toggle servos, which is one of the first pieces of advanced hardware this library contained. Setting up a toggle servo is almost frivolous with ftc-ext (although, it's uglier in java). Below, you'll see how to create, set-up, and toggle a ToggleServo when the 'a' button on controller 1 is pressed.

Examples

Java

private ToggleServo toggle = ( ToggleServo ) getDevice( "toggle_servo", ToggleServo.class );

@Override
public void init()
{
    toggle.setDisengagedPosition( ... );
    toggle.setEngagedPosition( ... );
}

@Override
public void loop()
{
    if ( getGamepad1().a ) toggle.toggle();
}

Kotlin

private val toggle: ToggleServo = getDevice( "toggle_servo" );

override fun init()
{
    toggle.DisengagedPosition = ...;
    toggle.EngagedPosition = ...;
}

override fun loop()
{
    if ( gamepad1.a ) toggle.toggle();
}

All the timing, states, movement, etc. is handled completely for you, all you have to do is toggle it!

Now, I realize the syntax for the java example is really rather ugly; however, it is possible for you to create your own hardware extension instances directly, as any valid hardware extension must abide by some rules (found [here](Custom Hardware Extensions)). There are two other possibilities for Java users to use to access hardware extensions:

private ToggleServo toggle = ( ToggleServo ) getDevice( "toggle_servo", ToggleServo.class );
// some devices only require the base device to create
private ToggleServo toggle = ToggleServo( servo( "toggle_servo" ) );
// some devices might require the base device and a name for it
private Motor motor = Motor( motor( "motor" ), "motor" ); // maybe don't name things like this :/

If the latter two options seem nicer to you (hint: they are), feel free to use them. They do the same thing, except maybe a few milliseconds faster than the getDevice method by how that method works underneath.

Standard Classes

Some standard hardware extensions (ones that are supplied with the library) are listed below.

  • ToggleServo
    • constructor Servo
    • used to toggle between two states effectively
  • ContinuousServo
    • constructor Servo
    • mostly used to differentiate between a regular servo and a continuous one
  • ContinuousToggleServo
    • constructor Servo
    • a toggle servo made from a continuous servo, for when you're out of regular servos
  • Motor
    • constructor DcMotor, String
    • A more useful motor class
    • resetEncoders()
      • registers a task which will reset the encoders on the motor
    • moveDistance( double, double )
      • registers a task that will move forward the distance
      • distance is completely ideal and won't work if the robot slips or anything