Class Turret

java.lang.Object
com.crossbotics.ftchelper.robot.Module<Turret>
com.crossbotics.ftchelper.robot.Turret
All Implemented Interfaces:
Subsystem

public final class Turret extends Module<Turret>
A turret that turns inside its limits (or round and round, with a slip ring), by hand or following the goal with a Camera or the odometry. Angles in degrees, counterclockwise +, 0 = straight forward.

 // Robot.java: what turns it (one of these), then the options
 turret = add(Turret.motor("turret", "gobilda-5203-117").gearRatio(5).follows(camera));   // a motor
 turret = add(Turret.servo("turret").follows(camera));                                    // a standard servo
 turret = add(Turret.crServo("turret").encoder("turret_enc", 8192).follows(camera));      // CR servo + Through Bore
 turret = add(Turret.crServo("turret").analogEncoder("turret_pos").follows(camera));      // CR servo + absolute encoder
     // options: .homeSwitch("turret_home")  .continuous()  .reversed()  .cameraOnBody()

 public static class Turret {                                // Tunables.java
     public static double min = -90, max = 90;              // its range: it never turns past these
     public static double startAngle = 0;                   // where it points at INIT (no switch)
     public static double kP = 0.02, kI = 0, kD = 0.001;
     public static double maxPower = 0.8, tolerance = 1.0, cameraOffset = 0;
 }
 

How it knows its angle (every way, with the Tunables numbers it needs, is in docs/guides/turret-setup.md):

  • Encoder + start position (no switch): it trusts startAngle (default 0 = straight forward) at INIT. Put the turret there by hand before INIT (a mark, a jig): if it is not, every angle is off by the difference. The INIT screen says so.
  • Home switch (homeSwitch(java.lang.String), a touch sensor or a magnetic switch): at START it turns slowly (homingPower, toward homeAngle from startAngle) until the switch closes; there it is homeAngle. Every later pass over the switch fixes any drift. Following and "aimed" wait until homed. Nothing moves in INIT (the game rules want a robot standing still after INIT, and the library never moves before START).
  • Absolute encoder (analogEncoder(java.lang.String)): it knows its angle at once (encoderOffset = the reading at 0).
  • Servo (servo(java.lang.String)): its angle is where it was sent (posA/angleA, posB/angleB); "arrived" is a time estimate (travelSec).

From autonomous to TeleOp: an encoder counts from where it was at INIT, and TeleOp's INIT starts it again. So the angle at the end of every program is remembered (until the robot app restarts, at most 5 minutes) and TeleOp starts from it instead of startAngle (and does not home again). Autonomous always starts from startAngle or the switch. keepAngle = false turns this off.

Range: min/max (e.g. -90/90, -180/180, -190/190: past 180 it picks the way round that never passes a limit), or continuous() for a slip ring (no limits, always the shortest way).

Follow mode (followMode(), on from START when follows(com.crossbotics.ftchelper.robot.Camera) or followsOdometry() is used): every loop it turns toward the goal. The camera first; when the camera does not see the goal and the robot has odometry and Tunables.Aim.goalX, goalY, toward the goal worked out from where the robot is (see Aim). The Driver Station says which. A goal just past the range: it waits at the limit and the Driver Station says "goal out of turret range". A goal far past it (more than outOfRangeHold, 10 degrees, past a limit: behind the robot) from the odometry: it holds where it points, so it does not swing from one limit to the other as the heading wobbles, until the goal is back within 5 degrees of the range. The stick takes over while it is moved, then following goes on.

Aimed (isAimed()) while following means aimed at the goal NOW: the goal is seen this moment (camera or odometry), the turret is homed and in range, not held by an interlock rule (Interlock), and it points within the tolerance. A launcher gated on it never fires after the goal is lost.

A following turret and whole-robot aim (robot.aimMode()) are separate modes: use one (see Aim).

Usual buttons (lines to write in Controls.java): operator right stick X turns it by hand; operator BACK switches following on and off. Docs: docs/reference/robot-java.md (section "Turret"), docs/guides/turret-setup.md

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    The part's telemetry line; each module adds its own.
    void
    aimAt(double deg)
    Turns to an angle (inside the limits) and stops following.
    double
    How many degrees the turret points away from the goal now (any sign), NaN if the goal is not known (no fresh picture and no odometry).
    Where the following got the goal last loop: CAMERA, ODOMETRY or NONE.
    analogEncoder(String analogPort)
    A CR servo turret's absolute encoder on an analog port (e.g.
    double
    The angle now, degrees (-180 to 180 for a continuous() turret).
    The camera is on the robot's body, not on the turret.
    No limits: a slip ring carries the wires, so it can turn round and round.
    static Turret
    crServo(String servo)
    A turret on a continuous-rotation (CR) servo.
    encoder(String encoderPort, double countsPerRev)
    A CR servo turret's encoder on a motor port (e.g.
    A CR servo turret's encoder counts the other way (down while the turret turns counterclockwise).
    Following the camera's goal: switch it with a button (g2.back.toggle(robot.turret.followMode())).
    Follows this camera's goal (follow mode on from START).
    Follows the goal with NO camera: its direction comes from the odometry (where the robot is) and Tunables.Aim.goalX, goalY (see Aim).
    static void
    Forgets every remembered turret angle (tests, or a tool that knows the turret was moved).
    A servo turret uses its servo's whole turn: positions 0 to 1 send the servo's full pulse range (the part's, else 500-2500 us) instead of the FTC SDK's normal 600-2400 us.
    gearRatio(double turnsPerTurretTurn)
    Turns per turret turn of what counts the angle: the MOTOR for motor(java.lang.String, java.lang.String) (its gears or belt), the ENCODER for crServo(java.lang.String) (1 = on the turret's axis).
    homeSwitch(String switchName)
    A home switch: a touch sensor (configured as "REV Touch Sensor") or a magnetic switch (a "Digital Device", e.g.
    boolean
    True when it points where it should, within the tolerance.
    boolean
    True while an interlock rule holds it back from where it should point (Interlock).
    boolean
    True once its angle is known (homed on its switch, or no switch).
    boolean
    True when the goal is past its range (it waits at the limit, or holds while the goal is behind).
    boolean
    True for a Turret.servo with reversed() in Robot.java.
    boolean
    True for Turret.servo (a positional servo; not a motor or CR servo turret).
    void
    manual(double power)
    By hand, -1 to 1 (a stick, every loop).
    static Turret
    motor(String motor, String motorId)
    A turret on this motor (its own encoder counts the angle).
    The launcher.Turret inside, for anything this module does not offer.
    Positive power turns the other way (if + turns it clockwise).
    static Turret
    servo(String servo)
    A turret on a standard (position) servo: it goes to an angle by itself, no encoder and no homing.
    void
    Stops all motion.
    void
    Called once every loop.

    Methods inherited from class com.crossbotics.ftchelper.robot.Module

    deviceName, isOptional, isPresent, looks, name, optional, part, partId, tunables, zone, zoneAbove, zoneBelow

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • motor

      public static Turret motor(String motor, String motorId)
      A turret on this motor (its own encoder counts the angle).
      Parameters:
      motor - the motor's configuration name
      motorId - the motor's parts-catalog id (see Motors)
    • servo

      public static Turret servo(String servo)
      A turret on a standard (position) servo: it goes to an angle by itself, no encoder and no homing. Tunables: posA, angleA, posB, angleB (two servo positions and the turret's angle at each, measured with the Servo Finder), travelSec (seconds from posA to posB). Its range is the angles between them (or min/max, if narrower).
      Parameters:
      servo - the servo's configuration name
    • fullRange

      public Turret fullRange()
      A servo turret uses its servo's whole turn: positions 0 to 1 send the servo's full pulse range (the part's, else 500-2500 us) instead of the FTC SDK's normal 600-2400 us. A goBILDA 2000 servo then turns 300 degrees instead of 270 (more turret angle). Turn it on BEFORE measuring posA/posB with the Servo Finder (Y = full range there too): the same position points somewhere else after the change.
      Returns:
      this turret
      Throws:
      IllegalStateException - on a motor or CR servo turret
    • crServo

      public static Turret crServo(String servo)
      A turret on a continuous-rotation (CR) servo. It needs an encoder for its angle: encoder(String, double) or analogEncoder(String).
      Parameters:
      servo - the CR servo's configuration name
    • encoder

      public Turret encoder(String encoderPort, double countsPerRev)
      A CR servo turret's encoder on a motor port (e.g. a REV Through Bore: 8192 counts per turn). The port may also drive another part's motor: only its count is read (and never reset).
      Parameters:
      encoderPort - the configuration name of the motor port the encoder is plugged into
      countsPerRev - counts per turn of the ENCODER (8192 for a REV Through Bore)
    • analogEncoder

      public Turret analogEncoder(String analogPort)
      A CR servo turret's absolute encoder on an analog port (e.g. an Axon servo's feedback wire, or an absolute encoder: 0 V to its top voltage = one turn). It knows the angle at once: no homing. Tunables: encoderOffset (its reading, degrees 0 to 360, with the turret at 0).
      Parameters:
      analogPort - the analog input's configuration name
    • gearRatio

      public Turret gearRatio(double turnsPerTurretTurn)
      Turns per turret turn of what counts the angle: the MOTOR for motor(java.lang.String, java.lang.String) (its gears or belt), the ENCODER for crServo(java.lang.String) (1 = on the turret's axis). Default 1. Not used by a servo turret.
    • reversed

      public Turret reversed()
      Positive power turns the other way (if + turns it clockwise). A servo turret: 0 and 1 swap.
    • encoderReversed

      public Turret encoderReversed()
      A CR servo turret's encoder counts the other way (down while the turret turns counterclockwise).
    • homeSwitch

      public Turret homeSwitch(String switchName)
      A home switch: a touch sensor (configured as "REV Touch Sensor") or a magnetic switch (a "Digital Device", e.g. the REV Magnetic Limit Switch: it reads LOW near the magnet). The library finds which. Tunables: homeAngle (the turret's angle where the switch closes: needed), optional homeDirection (+1 = turn counterclockwise to find it, -1 = clockwise; default: from startAngle toward homeAngle), homingPower (0.2), homingTimeout (5 s). Missing from the configuration: an INIT note, and the start angle is trusted.
      Parameters:
      switchName - the switch's configuration name
    • continuous

      public Turret continuous()
      No limits: a slip ring carries the wires, so it can turn round and round. It always takes the shortest way, and the goal is never "out of range" (min/max are not used).
    • follows

      public Turret follows(Camera c)
      Follows this camera's goal (follow mode on from START). The camera turns with the turret. With an odometry(...) line and Tunables.Aim.goalX, goalY, the odometry takes over while the camera does not see the goal.
    • followsOdometry

      public Turret followsOdometry()
      Follows the goal with NO camera: its direction comes from the odometry (where the robot is) and Tunables.Aim.goalX, goalY (see Aim). Needs an odometry(...) line. Follow mode on from START. Assumes the turret turns about the robot's center.
    • cameraOnBody

      public Turret cameraOnBody()
      The camera is on the robot's body, not on the turret.
    • forgetAngles

      public static void forgetAngles()
      Forgets every remembered turret angle (tests, or a tool that knows the turret was moved).
    • followMode

      public Mode followMode()
      Following the camera's goal: switch it with a button (g2.back.toggle(robot.turret.followMode())).
    • aimAt

      public void aimAt(double deg)
      Turns to an angle (inside the limits) and stops following.
      Parameters:
      deg - robot-relative, counterclockwise +, 0 = forward
    • manual

      public void manual(double power)
      By hand, -1 to 1 (a stick, every loop). While it is moved, following pauses.
      Parameters:
      power - + = counterclockwise
    • isReversedServo

      public boolean isReversedServo()
      True for a Turret.servo with reversed() in Robot.java.
    • isServoTurret

      public boolean isServoTurret()
      True for Turret.servo (a positional servo; not a motor or CR servo turret).
    • isAimed

      public boolean isAimed()
      True when it points where it should, within the tolerance. While FOLLOWING, that means at the goal now: the goal is seen this moment (a fresh camera picture, or the odometry), the turret is homed and in range, not held by an interlock rule, it reached its target, and the goal is within the tolerance (the larger of Tunables.Turret.tolerance and Tunables.Aim.toleranceDeg). False the moment the goal is lost, so a launcher gated on it (requireReady("aimed", turret::isAimed)) never fires blind.
    • isOutOfRange

      public boolean isOutOfRange()
      True when the goal is past its range (it waits at the limit, or holds while the goal is behind).
    • isHeld

      public boolean isHeld()
      True while an interlock rule holds it back from where it should point (Interlock).
    • isHomed

      public boolean isHomed()
      True once its angle is known (homed on its switch, or no switch).
    • angle

      public double angle()
      The angle now, degrees (-180 to 180 for a continuous() turret).
    • aimErrorDeg

      public double aimErrorDeg()
      How many degrees the turret points away from the goal now (any sign), NaN if the goal is not known (no fresh picture and no odometry). For the gamepad buzz (rumbleWhenAimed()).
    • aimSource

      public Aim.Source aimSource()
      Where the following got the goal last loop: CAMERA, ODOMETRY or NONE.
    • part

      public Turret part()
      The launcher.Turret inside, for anything this module does not offer.
    • update

      public void update()
      Description copied from interface: Subsystem
      Called once every loop. Do the hardware work here. Must be quick: no sleep, no waiting. While update() waits, the whole robot is frozen (no driving, no limits, no STOP).
    • stop

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

      public void addTelemetry(RobotTelemetry t)
      Description copied from class: Module
      The part's telemetry line; each module adds its own.
      Specified by:
      addTelemetry in interface Subsystem
      Overrides:
      addTelemetry in class Module<Turret>