NextMotor

class NextMotor @JvmOverloads constructor(initializer: () -> DcMotorImplEx, var anglePerCount: Angle = 1.0.radians, cacheTolerance: Double = 0.01)

Comprehensive wrapper around a DcMotorImplEx supporting multiple control modes.

NextMotor provides a high-level interface for DC motor control including:

  • Throttle control: Direct power/PWM percentage.

  • Voltage control: Set power as a voltage relative to the input rail voltage.

  • Position control: PID-based regulation to reach and hold a target encoder position.

  • Velocity control: PID + feedforward to reach and hold a target velocity.

The motor wraps a DcMotorImplEx obtained lazily through an initializer, supporting construction from a Lynx module/port, hardware map name, or a raw initializer function.

Encoder readings are automatically converted to Angle and AngularVelocity using the configured anglePerCount conversion factor.

Example:

val motor = NextMotor("driveMotor", anglePerCount = 0.05.radians)
motor.setVelocitySetpoint(12.0.radiansPerSecond)

Parameters

initializer

A function that returns the backing DcMotorImplEx. Invoked lazily.

anglePerCount

Conversion factor from encoder counts to angle. Defaults to 1.0 radian.

cacheTolerance

Power caching tolerance to reduce redundant hardware writes. Defaults to 0.01.

Constructors

Link copied to clipboard
constructor(initializer: () -> DcMotorImplEx, anglePerCount: Angle = 1.0.radians, cacheTolerance: Double = 0.01)
constructor(module: NextLynxModule, port: Int, anglePerCount: Angle = 1.0.radians, cacheTolerance: Double = 0.01)

Constructs a motor from a Lynx hub module and port number.

constructor(name: String, anglePerCount: Angle = 1.0.radians, cacheTolerance: Double = 0.01)

Constructs a motor by name from the current hardware map.

Types

Link copied to clipboard
object Companion
Link copied to clipboard
sealed class ControlType

Motor control mode.

Link copied to clipboard

Motor rotation direction.

Link copied to clipboard

Motor zero power behavior.

Properties

Link copied to clipboard

Optional external absolute encoder via analog feedback.

Link copied to clipboard

Current absolute encoder position.

Link copied to clipboard
Link copied to clipboard

Current active control mode (THROTTLE, VOLTAGE, POSITION, or VELOCITY).

Link copied to clipboard

Motor rotation direction (FORWARD or REVERSE).

Link copied to clipboard

Current encoder position in physical angle units.

Link copied to clipboard

Current encoder velocity in physical angle per time units.

Link copied to clipboard

Position control constants (PID and feedforward gains).

Link copied to clipboard

PID controller used for position setpoint tracking.

Link copied to clipboard

Set throttle control mode and power.

Link copied to clipboard

Velocity control constants (PID and feedforward gains).

Link copied to clipboard

Feedforward calculator used in conjunction with velocity PID.

Link copied to clipboard

PID controller used for velocity setpoint tracking.

Link copied to clipboard

Returns the approximate motor voltage command.

Link copied to clipboard

Motor zero power behavior (FLOAT or BRAKE).

Functions

Link copied to clipboard
fun follow(motor: NextMotor, direction: NextMotor.Direction = Direction.FORWARD)

Configures this motor to follow another motor's behavior and align its rotation direction.

Link copied to clipboard

Set absolute position control mode and position setpoint.

Link copied to clipboard
fun setPositionSetpoint(setpoint: Angle)

Set position control mode and position setpoint.

Link copied to clipboard

Set velocity control mode and velocity setpoint.

Link copied to clipboard
fun update()

Updates the motor power based on the current active control mode.