Skip to content

Controllers

joelg236 edited this page Jun 21, 2013 · 2 revisions

Writing Controllers

A Controller is a looping virtual mechanism that runs a method at a specific rate. It is useful for things like PID controllers. Controllers should look like this:

public class ExampleController extends Controller {
    public ExampleController() {
        super(0.02, Controller.LoopType.FIXED_RATE);
    }

    public void run() {
        System.out.println(MathUtils.random());
    }
}

Although this is a completely useless controller, it is still valid. It will print a random number every 20ms. In the constructor, double values are considered seconds, and int values are considered hertz.

The difference between FIXED_RATE and FIXED_DELAY is that their running mechanisms on the CPU.

FIXED_RATE will attempt to run the method in as balanced a way as possible. Sometimes it will skip and run faster than the delay to catch up on cycles. If you need to have the method run as consistently quick as possible use FIXED_RATE.

FIXED_DELAY will run the method, and sleep for the delay. There is no compensation for slow or fast loop times. It is almost (although more elegantly) equivalent to running a loop like this

while(true) {
    run();
    Timer.delay(delayTime);
}

Be careful: the run() method is run in a completely different thread. Be sure to be thread-safe in your controllers, you have no choice!

Some default controllers have already been written in ATALibJ. It's most likely the case that you will use these and never need to write your own controller. They are found here.

Clone this wiki locally