2702 Recipes

Why Mechanisms Need a Zero

What a Kraken's encoder knows at power-up, what a wrong zero breaks, the ways to find the zero, when to set it, how it gets lost, and how to know a mechanism is homed.

A position-controlled mechanism needs to know where it is before it can go anywhere. The motor can't tell it. In short:

  1. The motor counts, but doesn't know where it started. A Kraken's encoder measures how far the mechanism has moved since the motor controller turned on. It doesn't know where the mechanism was at that moment.
  2. The zero is the reference. Setpoints, soft limits, gravity feedforward, and superstructure interlocks are all measured from it. If the zero is off, all of them are off by the same amount.
  3. There are three ways to find it. Put the mechanism somewhere known (a hard stop), detect a known position with a sensor, or measure the position directly with an absolute encoder.
  4. Know whether you're homed. Keep a flag for each mechanism. Show it to the drive team, and don't let the mechanism move on its own until the flag is set.

1.1 Words used in this section

TermMeaning
ZeroThe known position that the code measures every other position from. It doesn't have to read 0, or be at the end of travel. A stop at 90° can be the "zero" if the code sets the position to 90° there.
ZeroingSetting the position reading to a known value, for example with setPosition() in Phoenix 6.
HomingMoving a mechanism until it finds its reference (a hard stop or a sensor), and then zeroing it. Many teams use "homing" and "zeroing" for the same thing.
OffsetThe amount added to a sensor's raw reading so it reads the mechanism's true position. An absolute encoder's offset is measured once and stored.
SeedingSetting the motor's position from another sensor, such as an absolute encoder, so the motor's fast encoder starts from the right value.
Relative encoderAn encoder that counts motion from wherever it started.
Absolute encoderAn encoder that reports its angle within one turn, right from power-up.

1.2 What a wrong zero breaks

A Kraken's built-in encoder reads the rotor's angle within one rotor turn, and Phoenix 6 starts the motor's position from that angle when the motor controller boots. A gearbox sits between the rotor and the mechanism, though. On a 50:1 turret, one rotor turn moves the turret only 7.2°. The motor knows where the rotor is within its turn, but not which of the 50 turns per turret rotation it is on. So for the mechanism, the motor's position only counts motion since power-up (Relative Encoders). §4.4 shows a way to use that one-turn angle anyway.

Suppose the robot powers up with an arm at 30° above level, and the code assumes it is level. Every position the code sees is now 30° off:

What uses the zeroWhat goes wrong
SetpointsThe arm goes 30° away from where it was asked. Every scoring position misses.
Soft limitsThe limits sit 30° from where they belong. On one side they stop the arm early. On the other, they let it drive into the hard stop.
Gravity feedforwardPhoenix 6 computes kGcos⁡θk_G \cos\theta from the measured angle (Arm control output). With a wrong zero, the holding voltage is wrong at every angle, and the arm sags or lifts.
InterlocksThe superstructure decides what is safe from measured positions. A wrong zero can make it think the arm is clear of the intake when it isn't.
AimingA turret that is 2° off misses by about 17 cm at 5 m.

1.3 Ways to find the zero

Hard stopStart on it, or drive into itNo sensor neededLimit switchPressed at one positionUsually at an end of travelHall effect sensorSees a magnetAnywhere in the travelAbsolute encoderKnows its angle at power-upWithin one turnTwo encodersDifferent tooth countsMore than one turn
What a mechanism can zero against. Orange marks the reference, the part that tells the robot where the mechanism is. Blue arrows show the motion.

The strategies fall into three families:

FamilyStrategyHow it worksPage
Put it somewhere knownKnown starting positionThe mechanism rests on a hard stop when the match starts. The code sets the position to that value.Hard Stops
Home against a hard stopDrive slowly into a hard stop. When the mechanism stalls, set the position.Hard Stops
Detect a known positionIndex sensorA limit switch or a hall effect sensor triggers at one position. Set the position when it does.Limit Switches and Hall Effect Sensors
Measure the positionAbsolute encoderRead the angle directly at power-up. The travel has to fit in one turn of the encoder.Relative and Absolute Encoders
Two absolute encodersTwo encoders geared differently read an angle over more than one turn.Two Encoders and the CRT

Strategies combine. A turret can use a hall effect sensor to find a window and the motor's rotor angle to find the exact angle in it. An elevator can start on its hard stop and re-zero against it every time it comes down.

1.4 When to zero

WhenGood forWatch out for
At boot, in the subsystem's constructorReading an absolute encoder. Setting a known starting position.A mechanism that was off its stop when the robot turned on.
While disabled, every loopWatching an index sensor, so a mechanism moved onto it by hand is homed before the match.Mechanisms don't move by themselves while disabled. Someone has to move it, or it has to already be there.
At the start of autonomousSetting a known starting position. In a match, the robot is in its starting configuration, so each mechanism is on its stop.Practice without autonomous, and running autonomous again with mechanisms off their stops.
At the first enableRunning a homing routine against a hard stop.The mechanism moves as soon as the robot is enabled, and homing takes time.
During the matchRe-zeroing whenever an elevator rests on its bottom stop, or a sensor triggers. It removes slow drift and catches a skipped belt tooth.Only re-zero when the mechanism is really at the reference: stopped, and on the stop or the sensor.

The motor keeps counting while the robot is disabled, as long as it has power. A mechanism pushed by hand after the zero is set still reads correctly. What can't be fixed later is a zero set while the mechanism was somewhere else.

1.5 How a zero gets lost

  1. The mechanism wasn't where the code assumed. The robot was turned on with the arm resting off its stop, or the stop itself was bent.
  2. A motor controller reboots during a match. A brownout or a loose power or CAN wire can restart one motor controller. Its position starts over from the rotor's angle, so a mechanism that relied on the motor's encoder has lost its zero. Phoenix 6 reports a reboot through hasResetOccurred(), which returns true once after the device restarts, whether the robot was enabled or not. Call it every loop, and once in the constructor before the zero is set, so only later reboots count. The BootDuringEnable fault covers less: it is set only when the device booted while the robot was enabled. Its sticky version (getStickyFault_BootDuringEnable()) stays set until clearStickyFault_BootDuringEnable() clears it, which makes it useful in logs after a match. A mechanism that uses an absolute encoder as its feedback sensor recovers on its own (§3.5).
  3. Something slips between the motor and the mechanism. A belt skips a tooth, a chain jumps a sprocket, a hub slips on a shaft. The motor keeps counting, but the mechanism is no longer where the count says. Only a sensor on the mechanism's side of the slip can catch this.
  4. An absolute encoder's offset goes stale. The encoder or its magnet was moved during a repair. Redo the offset, as with a swerve module's encoder.

1.6 Know whether you're homed

  1. Keep a flag. Each mechanism that can lose its zero has an isHomed() flag. Set it when the zero is set. Clear it when its motor controller reports a reboot.
  2. Show it. Put the flag on the dashboard and in the logs. Flash the robot's LEDs while a mechanism isn't homed, so the drive team sees it before the match starts.
  3. Don't move on your own until homed. Until the flag is set, run only the homing routine, or nothing. A turret, for example, shouldn't aim at the target until it is homed.
  4. Give the drive team a manual re-zero. A button that runs the homing routine again, or sets the zero with the mechanism at a known position. Put it behind a two-button combination so it can't be pressed by accident.
  5. Check the zero against something else. Compare an absolute encoder with the motor's position, or check that an elevator reads close to zero whenever its bottom sensor is pressed. Log any disagreement.

On this page