Usage - captainsoccer/BasicMotor GitHub Wiki
Usage
The BasicMotor library simplifies working with brushless motor controllers in FRC by providing a unified interface and built-in logging. This page explains how to create and use motors using either a quick-start constructor or a configuration-based approach.
Each motor type supported by the library โ such as TalonFX, Spark MAX, and Spark Flex โ has its own specific constructor and configuration class. These are designed to expose relevant features while keeping your code clean and consistent.
For detailed usage and configuration instructions, see the motor-specific guides below:
Configuration
The BasicMotor class supports constructing a motor using the BasicMotorConfig
class, this class holds any parameters necessary to configure a motor. For more information on how to use the configuration class check out the wiki page
Controlling the Motor
The main way to control a motor is by calling the setControl()
method, which takes two parameters: a reference value (such as a target velocity or position) and a control mode. The control mode determines how the reference is interpreted โ for example, as a voltage, velocity, or position command.
public void setTargetPosition(double position){
motor.setControl(position, Controller.ControlMode.POSITION);
}
When using open loop control modes (such as voltage or percent output) you can use a direct method for less boiler-plate code.
public void setVoltage(double voltage){
motor.setVoltage(voltage);
}
When you need to stop the motor you can call on the stop()
method.
public void stop(){
motor.stop();
}
You can also configure a motor to follow another motor using the follow(BasicMotor leader)
method, allowing synchronized control for multi-motor mechanisms like drivetrains or elevators.
โ ๏ธ Only motors of the same type (e.g., TalonFX โ TalonFX) can follow each other. Mixing different controller types is not supported.
followerMotor.follow(masterMotor);
Reading Motor Measurements
You can monitor the motor's state using the built-in measurement functions. getMeasurement()
returns a Measurements
object containing all available data, while getPosition()
and getVelocity()
provide direct access to the motorโs current position and velocity in the configured units. These methods make it easy to perform feedback calculations, display data on dashboards, or trigger logic based on motor motion.
public double getPosition(){
return motor.getPosition();
}
Or getting the whole measurement object:
public Measurement getMeasurement(){
return motor.getMeasurement();
}
Reading Closed-Loop Status
When using closed-loop control modes such as velocity or position, you can check whether the motor has reached its target using the atSetpoint()
function. This method returns true
when the current measurement is within the defined tolerance
range of the target reference. The tolerance is set in the pidConfig
during configuration and can be adjusted based on how precise you want the control to be.
public boolean atSetpoint(){
return motor.atSetpoint();
}
Resetting the Motor State
The resetEncoder(double newPosition)
function allows you to manually set the motor's current position to a specific value. This is useful when aligning software state with the physical position of a mechanism โ for example, after homing or syncing with an external sensor. The new position is applied in the same units defined by your unitConversion
setting. It's recommended to call this function only when the mechanism is not moving to ensure accurate positioning.
public void reset(){
motor.resetEncoder(0);
}
Reading Sensor Data
To access detailed electrical and performance data from the motor controller, use the getSensorData()
function. This returns a SensorData
object containing real-time information such as current draw, output current, input and output voltage, power draw, power output, motor temperature, and duty cycle. These values are useful for monitoring motor health, detecting faults, and analyzing performance during operation or logging.
public double getCurrentDraw(){
return motor.getSensorData().currentDraw();
}