MotorPositionController

class MotorPositionController(var goal: Double, var motor: Motor, var enc: RotaryEncoder, var useHardwareVelocity: Boolean = true)

Utility that uses a PID controller to control the position of a motor similarly to a servo.

The default PID controller can be overridden using the setPid function.

Using the scale variable motor positions can be set in units other than radians. For example, the position of a lift could be set in mm of movement rather than radians of motor rotation. Whenever the term "position" is used in this context it refers to the value with this scale applied. Raw angle with be referred to as "angle".

Positions are set relative to a "home" position. This home position can be set using the setHome function.

Parameters

goal

Initial goal

motor

Motor to be controller

enc

Motor encoder

useHardwareVelocity

Whether velocity should be obtained directly from the encoder

Constructors

Link copied to clipboard
constructor(goal: Double, motor: Motor, enc: RotaryEncoder, useHardwareVelocity: Boolean = true)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

If lowerLimitEnabled is set to true the position will not be set lower than this value. This is applied post scaling meaning that if position is in mm this will also be in mm. IMPORTANT: THIS WILL NOT BE APPLIED IF lowerLimitEnabled IS SET TO FALSE.

Link copied to clipboard

If set to true position will be clamped at minimum value of lowerLimit

Link copied to clipboard
Link copied to clipboard
var pid: PID
Link copied to clipboard

Amount that position will be scaled to convert it to radians. Useful for mechanisms like a lift where position should be set in mm instead of angle.

Link copied to clipboard

If upperLimitEnabled is set to true the position will not be set higher than this value. This is applied post scaling meaning that if position is in mm this will also be in mm. IMPORTANT: THIS WILL NOT BE APPLIED IF upperLimitEnabled IS SET TO FALSE.

Link copied to clipboard

If set to true position will be clamped at maximum value of upperLimit

Link copied to clipboard

Functions

Link copied to clipboard
fun disable()

Disable the position controller

Link copied to clipboard
fun enable()

Enable the position controller

Link copied to clipboard

Get position of the encoder relative to the home position.

Link copied to clipboard

Get the home position of the motor

Link copied to clipboard

Get the position of the arm (not angle)

Link copied to clipboard
fun isAtGoal(epsilon: Float): Boolean

Check if the motor is at its target.

Link copied to clipboard
fun loop()

Update motor power based on new encoder data

Link copied to clipboard
fun offsetHome(offset: Double)

Offset the home position of the motor

Link copied to clipboard
fun setAngle(angle: Double)

Set target angle. As this directly controller the angle of the motor the setPosition is preferred.

Link copied to clipboard
fun setHome()

Set the home position (position = 0) of the motor to its current position. Useful in conjunction with a homing touch sensor.

Link copied to clipboard
fun setPosition(position: Double)

Set the motor target position. The scale variable can be used to set the units of position.