Skip to content

03 Extending: Subsystems and Arduino `setup()` method

Björn Giesler edited this page Mar 25, 2024 · 2 revisions

Writing new subsystems

The initialize() method

This method should do everything you need to do to prepare your subsystem for working. Set pins, configure baud rates, etc. It is called exactly once at system startup, there is currently no way to call initialize() again programmatically except for reboot.

**Housekeeping: **In the initialize() method, the subsystem should add the subsytem to the subsystem manager. It should also set its own name, description, and help text, and it should set the started_ and operationStatus_ members. Here is an example:

Result MySubsystem::initialize() {
    name_ = "mysubsystem";
    description_ = "Example subsystem";
    help_ = "Example help text";
    bb::SubsystemManager::registerSubsystem(this);
    operationStatus_ = RES_SUBSYS_NOT_STARTED;
    started_ = false;

    // Your actual initialization code goes here.

    return RES_OK;
}

The start() method

This method should do everything you need to do to get your subsystem operational. Since subsystems can be stopped and restarted through the console, this should open connections, start servers, allocate memory, or similar.

Housekeeping: start() should set the started_ bool, and the operationStatus_ member. Here's an example:

Result MySubsystem::start() {
    // Your actual start code goes here.

    if(everythingWentOK) {
        started_ = true;
        operationStatus_ = RES_OK;
    } else {
        started_ = false; // for good measure
        operationStatus_ = RES_SUBSYS_RESOURCE_NOT_AVAILABLE; // or some other error code, please check BBError.h
    }
    return operationStatus_;
}

The stop() method

This method is symmetric to start(). It should undo everything start() does, like close connections, stop servers, free memory, etc., so that another start() can be called safely after a stop().

Housekeeping: stop() should set the started_ bool, and the operationStatus_ member. Here's an example:

Result MySubsystem::stop() {
    // Your actual stop code goes here.

    started_ = false;
    operationStatus_ = RES_SUBSYS_NOT_STARTED;
    return operationStatus_;
}

The step() method

This method is where your actual work gets done. It gets called cyclically, typically at ~100Hz, but your mileage may vary. Make sure this method does not take too long! You will get a console warning if you take too much time.

Housekeeping: Nothing special for step(), although it's a good idea to check whether your start() has been successful.

Result MySubsystem::stop() {
    if(operationStatus_ != RES_OK) {
        return operationStatus_;
    }

    // Your actual work code goes here.

    return operationStatus_;
}