Beargineers logo

#27628

Beargineers

Motors and servos

Motors with positions and speeds in real units, servos that turn to a position or at a power, and any other device.

Motors and servos are parts of the robot like any other: they belong to the part that uses them, and their settings start with that part’s path.

Motors

class Turret(parent: Component) : Component(parent) {
    // goBILDA 435 RPM: 384.5 ticks per turn of its shaft, from goBILDA's product page. The turret is centered by hand
    // once, and autonomous and TeleOp share that zero.
    val motor = Motor(this, "motor", device = "turret", ticksPerTurn = 384.5, gearRatio = 121.0 / 16, keepZero = true)
}

Motor(this, "motor", device = "turret") is the part turret.motor, and the motor turret in the robot configuration; without device, the name is the motor’s name in the robot configuration too.

Property Is
power from -1.0 to 1.0; set it to drive the motor
position how far the output turned since INIT or the last resetPosition(), as an Angle
velocity how fast the output turns, as an AngularVelocity: motor.velocity.inRpm
ticks encoder ticks since INIT or the last resetPosition()
device FIRST’s DcMotorEx, for anything the platform doesn’t cover

resetPosition() makes the current position the new zero.

Settings, all with the constructor arguments as defaults, so they can be fixed on the robot without rebuilding:

Setting Is
turret.motor.direction F or R: which way positive power turns
turret.motor.brake whether the motor holds its position at zero power, instead of coasting; on by default
turret.motor.ticksPerTurn encoder ticks per turn of the motor’s own output shaft
turret.motor.gearRatio turns of the motor’s shaft per turn of what it drives, for gears after the motor
turret.motor.keepZero keep the position’s zero from one opmode to the next
turret.motor.velocityControl let the hub hold the motor’s speed, with power as a share of top speed
turret.motor.hubVelocityGains the hub’s own speed control gains: p, i, d, f; unset, the hub keeps its own
turret.motor.powerTolerance how much the power must change before it is sent to the hub (0.005)
turret.motor.velocityTolerance how much a target velocity must change before it is sent, as a share of it (0.005)

Positions and speeds

position and velocity are measured at the output, after the gears, in real units. They need ticksPerTurn: the encoder resolution “at the output shaft” on the motor’s product page, like 384.5 for a goBILDA 435 RPM motor or 1425.1 for a 117 RPM one. Without it they fail, saying what to set; a motor whose encoder isn’t used doesn’t need it.

The motor type picked in the robot configuration isn’t used for this, because it is wrong for most current motors: the SDK’s goBILDA type always has the 60 RPM model’s numbers, and its REV UltraPlanetary type assumes a 20:1 gearbox.

With gearRatio, positions are of what the motor drives: a turret turned by a 16-tooth gear on the motor meshing with a 121-tooth gear has gearRatio = 121.0 / 16, and position is the turret’s angle.

Keeping the zero

A motor’s zero is where it was at INIT. With keepZero, it is where it was at the first INIT after the robot app started, and the opmodes after share it: for a turret or an arm set to a known position once, before the match, and used by autonomous and TeleOp alike. A hub that lost power counts from zero again, which the platform can’t tell.

Holding a speed

The hub can hold a motor’s speed itself, with the motor’s encoder: it measures the speed and adjusts the power many times faster than the robot’s loop, whatever the load, friction or battery. A flywheel asks for a speed in real units:

val wheel = Motor(this, "wheel", ticksPerTurn = 28.0, brake = false)   // goBILDA 6000 RPM

wheel.targetVelocity = 3800.rpm      // the hub holds it, at the output after gearRatio
wheel.velocity                       // what it measures
wheel.power = 0.0                    // back to plain power: the wheel coasts down

The robot only sends a new target when it differs from the one the hub has by at least velocityTolerance, half a percent: a speed that follows the distance to a goal doesn’t write to the hub every loop. Setting power ends the target, and so does the opmode ending. A target needs ticksPerTurn.

The hub’s gains are hubVelocityGains, as p, i, d, f, in the units the hub takes. Set them: unset, the hub keeps whatever gains were set last, by any app, as they last across opmodes and app restarts, and the first target logs which those are. Tune them live on the settings page while watching the speed on the graphs page:

  1. f gives power in proportion to the target: f = power × 32767 / speed, with the speed in encoder ticks per second. Measure it on the mechanism rather than from the motor’s top speed: run it at a power near where it will work, like power = 0.8, and use the speed it settles at. Friction makes that far lower than the motor alone would say.
  2. p corrects what f misses. Raise it as long as the speed stays steady: hundreds are normal.
  3. i takes out what remains, but it winds up during a long spin-up and overshoots afterwards. With a good f and a strong p, a flywheel holds its speed without it.

Beta’s flywheel, one 6000 RPM motor on a heavy wheel with a lot of friction, ended at p=300, i=0, d=0, f=31: no overshoot, within a percent of its target, and back at speed after a shot as fast as full power can get it there. Weaker gains, like p and i a tenth of f, couldn’t even start that wheel toward 1000 rpm.

A flywheel does all this itself in the Tune opmode: its find gains tuning measures f, tries several p, and suggests the gains.

With velocityControl, the hub holds a speed for power too, which then asks for a share of the motor’s top speed: a drivetrain’s sticks drive at a speed rather than a power. The top speed comes from the motor type in the robot configuration; for goBILDA and REV motors it is right whatever the gearbox.

Either way the encoder has to be plugged in. Without it the hub never sees the motor move and keeps raising the power, up to full: so when the encoder doesn’t move for half a second while the hub controls the speed, the motor is stopped and the Driver Station says why.

Flywheels

A Flywheel is a wheel spun at a speed by one motor with its encoder plugged in, like a shooter’s. The hub holds its target, it reads the speed every readInterval (50 ms), and it says when it is ready to shoot:

val nectar = Flywheel(this, "nectar", device = "nectarShooter", ticksPerTurn = 28.0)
val speeds by config(SpeedTable(100.cm to 3200.rpm, 300.cm to 4000.rpm))

nectar.target = speeds[distanceToGoal]   // speeds in between are interpolated
repeat(3) {
    nectar.awaitReady()                  // or awaitReady(timeout = 2.seconds), which says whether it got there
    feed()
    nectar.shotFired()
}
nectar.coast()                           // no target: the wheel coasts down

It is ready once its speed has stayed within readyWithin (3%) of the target for steadyFor (100 ms). Once ready, a dip that far below the target ends it, like a ball’s, while it may run up to twice as far above the target before it does, so readings that wobble above it don’t make it flicker. A new target it is far from ends it right away; a small change, like following the distance, doesn’t.

Readings show a ball’s dip only a while after it: the hub measures the speed over 50 ms, and the wheel reads it every readInterval. Two ways to keep that from fooling code that waits for the wheel:

  • shooting { } reads the speed every tick for as long as its block runs, so a dip ends readiness as soon as the hub’s measurement shows it. That suits a feeder that pushes balls in as long as the wheel is ready, without knowing when each one goes in:

    nectar.shooting {
        while (balls > 0) {
            feeder.power = if (nectar.isReady) 1.0 else 0.0
            nextTick()
        }
    }
    

    Reading every tick costs a read of the motor’s hub per loop: on Beta, half a millisecond more on average. A ball already on its way when the dip shows still goes in, so balls fed closer together than the wheel recovers can leave a little slow.

  • shotFired() says when a ball went in, where the code knows: right after a kicker pushes one, if it reaches the wheel within a tenth of a second, or when a sensor at the wheel sees one go by. Readiness ends at once and waits for readings from after the shot.

A wheel driven by more than one motor names them all, each with the way it turns to spin the wheel forward:

val shooter = Flywheel(this, "shooter", ticksPerTurn = 28.0,
    motors = mapOf("fly1" to Direction.FORWARD, "fly2" to Direction.REVERSE))

Every motor needs its encoder plugged in, into the port of its own motor: the hub holds each one at the target, so they share the work, also when a shot takes speed away. The wheel’s speed is their average, and shooter.hubVelocityGains are the gains of all of them; shooter.fly2.hubVelocityGains would be one motor’s own. Each motor turns the same wheel, so when one measures less than half of another, the flywheel says which, on the Driver Station and in the log: an encoder unplugged or in another motor’s port, or a slipping belt. The graphs page shows each motor’s speed too. On Beta’s flywheel, a second motor took spinning up to 1500 rpm from 2.4 s to 1.0 s.

In config, a SpeedTable is pairs of distance and speed: shooter.speeds = 1m: 3200rpm, 2m: 3600rpm, 3m: 4000rpm. The motor’s settings, like its gains, are under the flywheel’s: nectar.motor.hubVelocityGains.

On Beta’s flywheel, one 6000 RPM motor on a heavy wheel, it was ready 2.4 s after starting toward 1500 rpm. While shooting, a sharp 30 ms hit ended readiness 75 to 95 ms after it began, and the wheel was ready again 230 to 350 ms after it; with reads 50 ms apart, readiness ended after 120 to 140 ms, and one hit in three went unnoticed.

Writes to the hub

Every power sent to the hub takes time on the connection all devices share (see Performance). So a power is only sent when it differs from the last one sent by at least powerTolerance, and code like a PID that nudges the power every loop doesn’t write every loop. Stopping and full power are always sent exactly. sentPower is what the hub has.

Motors stop when the opmode ends.

Servos that turn to a position

class Shooter(parent: Component) : Component(parent) {
    val latch = PositionServo(this, "latch")
    val latchOpen by config(0.3)
    val latchClosed by config(0.65)

    fun open() { latch.position = latchOpen }
    fun close() { latch.position = latchClosed }
}

position goes from 0.0 to 1.0; values outside are clamped. The servo holds still until the first position is set, and a position is only sent to the hub when it changes. Keep the positions as settings of the part that uses the servo, like latchOpen above, so they are tuned live.

Settings: shooter.latch.direction (F or R), and pwmMin and pwmMax, the pulse widths in microseconds for positions 0 and 1, for servos that need other ones than the SDK’s 600 and 2400.

Servo axes

A ServoAxis turns something to angles with a servo, through gears, like a sorter table or a turret: angles of what it turns rather than servo positions, moves at a speed, and with the servo’s position feedback, like an Axon’s fourth wire, it knows when it got there and when it is stuck.

// An Axon MAX MK2 whose 48-tooth gear turns the table's 20-tooth one: the table turns 2.4 times per servo turn
val table = ServoAxis(this, "table", device = "tableServo", encoder = "tableAngle", gearRatio = 20.0 / 48)

table.moveTo(45.degrees)                                         // at the servo's full speed
val done = table.moveBy((-1).rotations, speed = 2.5.rotations.perSecond)

The servo holds the position itself, so nothing is sent while it stands still; a move at a speed sends a new target every loop. It reaches from minAngle, where the servo’s position is 0 and the angle is zero, to maxAngle: the servo’s range, as it is programmed, through the gears. An Axon MK2 reaches up to 360° and takes 500 to 2500 µs pulses, which are the defaults.

With the feedback, angle is where it really is, and a move returns true once it is within tolerance (2°) of the target. It returns false, standing still where it is instead of pushing on, when it falls more than maxLag (45°) behind a moving target, or doesn’t arrive within arriveWithin (300 ms) after the target stopped: jammed. Calibrate the feedback once: with the servo at position 0, the encoder’s angle is encoderAtStart. Its encoder’s own settings are under <name>.encoder (see Sensors).

Servos that turn at a power

A continuous rotation servo, like on an intake’s rollers, turns at a power from -1.0 to 1.0:

val roller = CRServo(this, "roller")
...
roller.power = 1.0

Like a motor’s, its power is only sent when it differs from what the hub has by at least powerTolerance (0.005), and it stops when the opmode ends. Settings: direction, powerTolerance, and pwmMin and pwmMax: many continuous rotation servos reach full speed only at 500 and 2500.

Any other device

For a device the platform has no class for, take FIRST’s own from the robot configuration:

class Vision(parent: Component) : Component(parent) {
    val limelight by hardware<Limelight3A>()                    // the device "limelight"
    val imu by hardware<IMU>("controlHubImu")                   // named differently
}

The device is named like the property unless a name is given. It is looked up on first use, which has to be in init() or later: hardware isn’t available in constructors. A device the robot configuration doesn’t have stops the opmode when it is first used, naming it; the platform’s own classes use theirs in init(), so they fail INIT.

Every platform class gives its SDK device as device too, like motor.device, for what the platform doesn’t cover.