Class Turret
- All Implemented Interfaces:
Subsystem
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, towardhomeAnglefromstartAngle) until the switch closes; there it ishomeAngle. 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 TypeMethodDescriptionvoidThe part's telemetry line; each module adds its own.voidaimAt(double deg) Turns to an angle (inside the limits) and stops following.doubleHow 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.doubleangle()The angle now, degrees (-180 to 180 for acontinuous()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 TurretA turret on a continuous-rotation (CR) servo.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) andTunables.Aim.goalX, goalY(seeAim).static voidForgets 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 formotor(java.lang.String, java.lang.String)(its gears or belt), the ENCODER forcrServo(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.booleanisAimed()True when it points where it should, within the tolerance.booleanisHeld()True while an interlock rule holds it back from where it should point (Interlock).booleanisHomed()True once its angle is known (homed on its switch, or no switch).booleanTrue when the goal is past its range (it waits at the limit, or holds while the goal is behind).booleanTrue for aTurret.servowithreversed()in Robot.java.booleanTrue forTurret.servo(a positional servo; not a motor or CR servo turret).voidmanual(double power) By hand, -1 to 1 (a stick, every loop).static TurretA turret on this motor (its own encoder counts the angle).part()The launcher.Turret inside, for anything this module does not offer.reversed()Positive power turns the other way (if + turns it clockwise).static TurretA turret on a standard (position) servo: it goes to an angle by itself, no encoder and no homing.voidstop()Stops all motion.voidupdate()Called once every loop.
-
Method Details
-
motor
A turret on this motor (its own encoder counts the angle).- Parameters:
motor- the motor's configuration namemotorId- the motor's parts-catalog id (seeMotors)
-
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 (ormin/max, if narrower).- Parameters:
servo- the servo's configuration name
-
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
A turret on a continuous-rotation (CR) servo. It needs an encoder for its angle:encoder(String, double)oranalogEncoder(String).- Parameters:
servo- the CR servo's configuration name
-
encoder
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 intocountsPerRev- counts per turn of the ENCODER (8192 for a REV Through Bore)
-
analogEncoder
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
Turns per turret turn of what counts the angle: the MOTOR formotor(java.lang.String, java.lang.String)(its gears or belt), the ENCODER forcrServo(java.lang.String)(1 = on the turret's axis). Default 1. Not used by a servo turret. -
reversed
Positive power turns the other way (if + turns it clockwise). A servo turret: 0 and 1 swap. -
encoderReversed
A CR servo turret's encoder counts the other way (down while the turret turns counterclockwise). -
homeSwitch
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), optionalhomeDirection(+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
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/maxare not used). -
follows
Follows this camera's goal (follow mode on from START). The camera turns with the turret. With anodometry(...)line andTunables.Aim.goalX, goalY, the odometry takes over while the camera does not see the goal. -
followsOdometry
Follows the goal with NO camera: its direction comes from the odometry (where the robot is) andTunables.Aim.goalX, goalY(seeAim). Needs anodometry(...)line. Follow mode on from START. Assumes the turret turns about the robot's center. -
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
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 aTurret.servowithreversed()in Robot.java. -
isServoTurret
public boolean isServoTurret()True forTurret.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 acontinuous()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
Where the following got the goal last loop: CAMERA, ODOMETRY or NONE. -
part
The launcher.Turret inside, for anything this module does not offer. -
update
public void update()Description copied from interface:SubsystemCalled 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:SubsystemStops all motion. Called at the end of every OpMode. -
addTelemetry
Description copied from class:ModuleThe part's telemetry line; each module adds its own.- Specified by:
addTelemetryin interfaceSubsystem- Overrides:
addTelemetryin classModule<Turret>
-