Subsystem Toggling

Introduction

A subsystem toggle is an operator control feature that allows the user to turn a specific subsystem on the robot on or off. This can be useful for a variety of reasons, such as conserving energy, testing individual components, or disabling a subsystem that is not currently in use.

This is widely used on subsystems that may need to run for prolonged periods of time but may need to stop for various reasons such as a flywheel, lift position hold, or an intake.

Concept

Conceptually, we want to visualize our "toggle" feature as a state machine with 2 states. We start in state 1 where the subsystem is off by default, and turn on the subsystem by pressing the toggle button. In order to keep track of this state, we can use a Boolean variable to do so. If more states are needed, we simply expand the diagram above to have more states where the subsystem has different actions than off and on, and use an integer value or an enumeration to keep track of current state.

Example Code

In the example code below, we will use the button "b" on the VEX V5 controller to toggle a VEX V5 motor on and off.

#include "main.h"

pros::Motor motor(1);
pros::Controller master();

bool move = false; // by default, this will not move

void opcontrol() {
    while(true) {
        if(master.get_digital_new_press(pros::Digital_B)) {
            // If this is true, move will changed to false
            // and vice versa. 
            move = !move; 
        }
        
        // If move is true, move the motor.         
        if(move) {
            motor.move(100);
        }
        // Else, don't
        else {
            motor.move(0);
        }
        pros::delay(20);
    }
}

Conclusion

While simple in concept, the execution of such code might be challenging to grasp for beginners. Do note that if multiple threads are both writing and reading to the state variable, it is best practice to use some form of thread protection such as a mutex.

Contributing Teams to this Article:

  • BLRS (Purdue SIGBots)

Last updated

Logo

This work is licensed under a Attribution-ShareAlike 2.0 Generic License