NextMotor

class NextMotor(initializer: () -> DcMotorImplEx, var distancePerCount: Distance = 1.0.inches, 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 Distance and LinearVelocity using the configured distancePerCount conversion factor.

Example:

val motor = NextMotor("driveMotor", distancePerCount = 0.05.inches)
motor.setVelocitySetpoint(12.0.inchesPerSecond)

Parameters

initializer

A function that returns the backing DcMotorImplEx. Invoked lazily.

distancePerCount

Conversion factor from encoder counts to distance. Defaults to 1.0 inch.

cacheTolerance

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

Constructors

Link copied to clipboard
constructor(initializer: () -> DcMotorImplEx, distancePerCount: Distance = 1.0.inches, cacheTolerance: Double = 0.01)
constructor(module: LynxModule, port: Int, distancePerCount: Distance = 1.0.inches, cacheTolerance: Double = 0.01)

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

constructor(name: String, distancePerCount: Distance = 1.0.inches, cacheTolerance: Double = 0.01)

Constructs a motor by name from the current hardware map.

Types

Link copied to clipboard

Motor control mode.

Link copied to clipboard

Motor rotation direction.

Properties

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
Link copied to clipboard

Current encoder position in physical distance units.

Link copied to clipboard

Current encoder velocity in physical distance 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

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.

Functions

Link copied to clipboard

Set position control mode and position setpoint.

Link copied to clipboard
fun setThrottle(throttle: Double)

Set throttle control mode and power.

Link copied to clipboard

Set velocity control mode and velocity setpoint.

Link copied to clipboard
fun setVoltage(voltage: Voltage)

Set voltage control mode and voltage setpoint.