Class PositionServo

java.lang.Object
com.crossbotics.ftchelper.mechanism.PositionServo
All Implemented Interfaces:
Subsystem, AngleDevice

public class PositionServo extends Object implements AngleDevice
A normal (positional) servo with named positions: a claw, a wrist, a hood, a gate. Can also work in degrees, move slowly, and tell (by estimate) when it has arrived.

 PositionServo claw = PositionServo.create(hardwareMap, "claw")
         .position("OPEN", 0.20)
         .position("CLOSED", 0.75)
         .travelTime(0.3);                       // seconds for a full 0..1 move (measure it)

 controls.bindPress(Pad.G2, Btn.RB, () -> claw.toggle("OPEN", "CLOSED"));
 routine.step("Grab", claw.goToStep("CLOSED"));  // waits until it should be closed

 PositionServo hood = PositionServo.create(hardwareMap, "hood")
         .degrees(0.10, 0, 0.90, 25);            // servo 0.10 = 0 deg, 0.90 = 25 deg (measured)
 hood.setDegrees(18);
 

Positions are raw servo positions (0 to 1). A servo cannot report where it really is, so "arrived" is an estimate from travelTime(double). Docs: docs/modules/mechanism.md (docs/images/servo-range.svg shows the 0 to 1 range).

  • Constructor Details

    • PositionServo

      public PositionServo(String name, com.qualcomm.robotcore.hardware.Servo servo)
      Wraps a servo you already have.
  • Method Details

    • create

      public static PositionServo create(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, String name)
      Gets the servo. Does not move it.
    • reversed

      public PositionServo reversed()
      Position 0 and 1 swap ends.
    • fullRange

      public PositionServo fullRange()
      Uses the servo's whole turn: positions 0 to 1 send 500 to 2500 microseconds instead of the SDK's normal 600 to 2400 (see ServoSetup.fullRange()). A goBILDA 2000 servo then turns 300 degrees over 0 to 1 instead of 270. Set it BEFORE measuring positions and degrees: the same number points somewhere else after the change. Does not move the servo.
      Returns:
      this servo, for the next setting
    • pulseRange

      public PositionServo pulseRange(double lowUs, double highUs)
      Sets the pulse for positions 0 and 1 (microseconds), e.g. pulseRange(500, 2500). A servo that cannot change its pulse keeps the normal range and shows a warning on the Driver Station.
      Parameters:
      lowUs - microseconds for position 0 (100 to 3000)
      highUs - microseconds for position 1 (more than lowUs, at most 3000)
      Returns:
      this servo, for the next setting
    • isFullRange

      public boolean isFullRange()
      True if fullRange() or pulseRange(double, double) changed this servo's pulse range.
    • limits

      public PositionServo limits(double min, double max)
      Never goes below min or above max (raw positions). Keeps it off the frame.
    • position

      public PositionServo position(String positionName, double value)
      Adds a named raw position, 0 to 1.
    • degrees

      public PositionServo degrees(double posA, double degA, double posB, double degB)
      Lets you use degrees. Measure two points: raw position posA is degA degrees, raw posB is degB degrees. In between is a straight line.
    • travelTime

      public PositionServo travelTime(double seconds)
      Seconds the servo needs for a full 0..1 move. Used to guess when it has arrived. Default 0.5.
    • maxSpeed

      public PositionServo maxSpeed(double positionsPerSecond)
      Moves slowly: at most this many raw positions per second (e.g. 0.5). 0 = full speed.
    • tunable

      public PositionServo tunable(String prefix)
      Puts every named position in the tuning menu as "prefix/NAME" (steps of 0.01, kept 0 to 1). Call it LAST. See docs/modules/tuning.md.
    • withServo

      public PositionServo withServo(com.qualcomm.robotcore.hardware.Servo other, DoubleSupplier trim)
      A second servo that moves with this one, to the same positions plus trim: the other side of a two-servo part (a claw's other finger, an arm's other side). Set its direction yourself (a servo mounted facing the other way: REVERSE). The trim is read at each move.
      Parameters:
      other - the other servo
      trim - raw position added for the other servo (0 = none), so two horns that do not line up still meet; the result is kept between 0 and 1
      Returns:
      this
    • goTo

      public void goTo(String positionName)
      Goes to a named position. An unknown name (a typo) does nothing and shows a warning.
    • getWarning

      public String getWarning()
      "" or the last command problem (unknown position name).
    • set

      public void set(double position)
      Goes to a raw position (0 to 1, kept inside the limits).
    • setDegrees

      public void setDegrees(double deg)
      Goes to an angle in degrees (needs degrees(double, double, double, double)).
      Specified by:
      setDegrees in interface AngleDevice
    • toggle

      public void toggle(String a, String b)
      Goes to b if at (or going to) a, else to a.
    • next

      public void next()
      The next named position in the order you added them (wraps around).
    • previous

      public void previous()
      The previous named position (wraps around).
    • update

      public void update()
      Once per loop. At full speed there is nothing to do (the servo was already told where to go). For a slow move (maxSpeed), it sends a position a little closer to the target each loop.
      Specified by:
      update in interface Subsystem
    • stop

      public void stop()
      Description copied from interface: Subsystem
      Stops all motion. Called at the end of every OpMode.
      Specified by:
      stop in interface Subsystem
    • isDone

      public boolean isDone()
      True when the servo should have arrived (an estimate: a servo cannot tell us where it is). We guess from the time since the move started and travelTime(double).
      Specified by:
      isDone in interface AngleDevice
    • goToStep

      public AutoStep goToStep(String positionName)
      A step (autonomous): go to a named position and wait until it should have arrived.
    • fence

      public PositionServo fence(double lo, double hi)
      A second, temporary range (raw positions) set every loop by an interlock rule: the servo is not sent past it. A target outside it waits at its edge (isHeld()) and goes on when the fence moves. fence(-inf, +inf) removes it.
    • isHeld

      public boolean isHeld()
      True while the fence keeps it from its target.
    • getEstimatedPosition

      public double getEstimatedPosition()
      Where the servo probably is now (raw), from the last position sent and the travel time; NaN before the first command.
    • getSweep

      public double[] getSweep()
      The raw positions the servo may be at now, {lowest, highest}: everything between where its last move started and where it was sent, until it should have arrived; then just that position. Its whole range (the limits) before the first command, and during the first move (it could have been anywhere). Used by interlock rules.
    • getSweepDegrees

      public double[] getSweepDegrees()
      getSweep() in degrees ({lowest, highest}; needs degrees(double, double, double, double)), or null before the first command.
    • getEstimatedDegrees

      public double getEstimatedDegrees()
      getEstimatedPosition() in degrees (needs degrees(double, double, double, double)); NaN before the first command.
    • toDegrees

      public double toDegrees(double position)
      A raw position in degrees (needs degrees(double, double, double, double)).
    • toPosition

      public double toPosition(double deg)
      An angle in degrees as a raw position (needs degrees(double, double, double, double)).
    • getMin

      public double getMin()
      The lowest raw position it is ever sent (limits(double, double)).
    • getMax

      public double getMax()
      The highest raw position it is ever sent (limits(double, double)).
    • getTravelTime

      public double getTravelTime()
      Seconds for a full 0..1 move (travelTime(double)).
    • getTarget

      public double getTarget()
      Where it was told to go, raw (NaN before the first command): it may still be on its way, or held by a fence.
    • getPosition

      public double getPosition()
      The raw position sent last (NaN before the first move).
    • getDegrees

      public double getDegrees()
      The angle sent last, in degrees (needs degrees(double, double, double, double); without it the number means nothing).
    • getTargetDegrees

      public double getTargetDegrees()
      The angle it is GOING to, in degrees (after the limits), or NaN before the first command. Differs from getDegrees() only during a slow move (maxSpeed).
      Specified by:
      getTargetDegrees in interface AngleDevice
    • getTargetName

      public String getTargetName()
      The named position it is at or going to, or "-" .
    • getServo

      public com.qualcomm.robotcore.hardware.Servo getServo()
      The FTC servo inside, for anything this helper does not do.
    • name

      public String name()
      Description copied from interface: Subsystem
      The name shown in telemetry and in the hardware check.
      Specified by:
      name in interface Subsystem
    • addTelemetry

      public void addTelemetry(RobotTelemetry t)
      Shows the target name; in debug mode also the raw position (and degrees, if set up).
      Specified by:
      addTelemetry in interface Subsystem