Lesson 33: DriveMux
DriveMux is short for drive multiplexer. It is a pattern for deciding which part of the robot code is allowed to control the drive train at a given moment.
The Problem
Many parts of the robot may want to drive: joystick code, autonomous code, vision alignment, heading hold, or a safety stop. If they all set motor speeds directly, the last command wins and the behavior becomes confusing.
The DriveMux Idea
Instead of letting everything talk to the motors, everything sends drive requests to one place. The DriveMux chooses the active request and sends one final command to the drive train.
public class DriveRequest {
public double left;
public double right;
public int priority;
}Priorities
Some requests should override others. A safety stop should beat driver input. Vision alignment might temporarily beat normal arcade drive. Normal joystick drive might be the default when nothing else is active.
if (safetyStopActive) {
drive.tankDrive(0.0, 0.0);
} else if (visionAlignActive) {
drive.tankDrive(visionLeft, visionRight);
} else {
drive.tankDrive(driverLeft, driverRight);
}Why This Helps
A DriveMux makes ownership clear. The drive train receives one command per cycle, and you can debug why that command was chosen. That is much easier than searching the project for every place that sets the same motors.
Practice
List every feature on your robot that wants to control the drive train. Sort them from highest priority to lowest priority.