Skip to content

Navigation Menu

Sign up

Limelight and Drivetrain

Ivan Wei edited this page Apr 21, 2019 · 5 revisions

Background

Team 694 uses the limelight as a way to assist the driver in getting to their target as fast as possible. In order to do this optimally, certain constants must be tuned. Consequently, in order to understand these constants, we must first understand how the structure of the program works.

This code uses Limelight.java to interface with the limelight.

Implementing AutomaticTurnCommand

No driver is ever perfect, and all will definitely appreciate assistance with aiming the game pieces. With the limelight, this is easy to implement. Limelight.getTargetXAngle(), can determine the amount needed to turn for alignment to the target.

Here is the class that would be used to have the robot turn towards the target:

public class AutomaticTurnCommand extends DrivetrainDriveCommand {
 @Override
 protected void initialize() {
 setInterruptible(false);
 }
 @Override
 protected void setTurn() {
 // Get Gamepad Input
 super.setTurn();
 // If Using CV
 if(Limelight.hasValidTarget()) {
 // Get Turn Div from Smart Dash Board
 double turnDiv = SmartDashboard.getNumber("TURN_DIV", RobotMap.CV.TURN_DIV);
 double moveTurnMult = SmartDashboard.getNumber("MOVE_TURN_MUL", RobotMap.CV.MOVE_TURN_MUL);
 // Take The Square Root of the X Angle
 double turnDelta = Limelight.getTargetXAngle();
 turnDelta = Math.signum(turnDelta) * Math.sqrt(Math.abs(turnDelta));
 // Increase Turning if robot is moving faster
 turnDelta *= Math.max(moveTurnMult * speed, 1);
 // Scale the Turn Delta
 turnDelta /= turnDiv;
 // Add Turn Delta to Turn
 turn += turnDelta;
 }
 }
}

This extends the DrivetrainDriveCommand class, which allows it to drive the robot by itself. The inclusion of super.setTurn() still allows the usage of the controller inputs to add to the limelight's turn value.

Limelight.hasValidTarget() determines if there is a target to aim at, or if the Limelight is working at all.

First, the constants used for turning the robot must be obtained.

double turnDiv = SmartDashboard.getNumber("TURN_DIV", RobotMap.CV.TURN_DIV);
double moveTurnMult = SmartDashboard.getNumber("MOVE_TURN_MUL", RobotMap.CV.MOVE_TURN_MUL);

SmartDashboard can be used to quickly change these values during testing, but it is good practice to update these constants in RobotMap or any similar constants file.

Current Constants and Their Purposes:

  1. TURN_DIV

    • When the limelight returns the angle that the target is in its camera, it returns a value between -27 and 27 degrees. However, the drivetrain only accepts turn values from -1 to 1. In order to convert the angle to an accepted value, it must be divided by this constant.

    • How To Tune:

      • If the robot turns to the target too slowly, or does not reach it at all, decrease this value.

      • If the robot overshoots or oscillates, increase this value.

    • Recommended Value: 16 to 24 (When used after MOVE_TURN_MUL and finding the square root)

  2. MOVE_TURN_MUL

    • If the robot is moving faster, turning has less of an effect. This constant increases the amount to turn.

    • How To Tune:

      • Increase this constant if the robot crashes into the side of the target when moving. Decrease either based on personal preference or if oscillation is present.
    • Recommended Value: 3 to 8; Alfred uses 6.3. Edwin is from 7-9.

  3. Limelight.X_ANGLE_SHIFT

    • In newer versions of Limelight.java, there is a variable called Limelight.X_ANGLE_SHIFT which works as an offset. Make sure this updated before tuning.

    • How To Tune: Drive up to the target using Autodrive. If it turns too far left, increase this value; if it turns too far right, decrease this value. THIS IS ONLY USED WHEN THE INITIAL CROSSHAIRS ARE OFF, you should probably tune the crosshairs, but this is a good last-minute fix.

How Automatic Turn Works - Implementation of these Constants

First, get the x offset from the Limelight:

double turnDelta = Limelight.getTargetXAngle();
turnDelta = Math.signum(turnDelta) * Math.sqrt(Math.abs(turnDelta));

The offset is stored in the variable turnDelta so that the AutomaticTurn value can be modified without changing the turn value of the entire robot.

Then, take the square root of turnDelta. If the offset happens to be negative, the square root will produce an imaginary number. Remedy this by finding the square root of the absolute value of turnDelta and reverting the result to the original sign using Math.signum(turnDelta). The square root of the X offset is used because the motors are less likely to stall at smaller values.

After that, multiply turnDelta by MOVE_TURN_MUL if the robot is moving too fast.

turnDelta *= Math.max(moveTurnMult * speed, 1);

Math.max is used to ensure that if MOVE_TURN_MUL * speed is below 1, the robot is still able to turn.

Lastly, scale turnDelta and add it to the drivetrain's turn value:

turnDelta /= turnDiv;
turn += turnDelta;

Implementing AutomaticDriveCommand

AutomaticDrive is a command that drives up to the target and turns at the same time.

public class AutomaticDriveCommand extends AutomaticTurnCommand {
 @Override
 protected void setSpeed() {
 super.setSpeed();
 if (Limelight.hasValidTarget()) {
 double targetXValue = Math.abs(Limelight.getTargetXAngle());
 targetXValue /= SmartDashboard.getNumber("SPEED_WHILE_TURNING", RobotMap.SPEED_WHILE_TURNING);
 double accel = Limelight.MAX_X_ANGLE - targetXValue;
 accel /= Limelight.MAX_X_ANGLE;
 accel *= SmartDashboard.getNumber("AUTOMATIC_DRIVE_SPEED", RobotMap.AUTOMATIC_DRIVE_SPEED);
 speed += accel;
 }
 }
}

This code contains 2 constants, targetXValue and accel. Because this command extends AutomaticTurnCommand, only setSpeed() needs to be changed in order to drive up to the target.

The robot moves at the constant speed AUTOMATIC_DRIVE_SPEED, but slows down if it is at a steep angle to the target.

A proportion accel is created that decreases as the X offset increases. accel is multiplied by AUTOMATIC_DRIVE_SPEED to slow down the drivetrain to whatever is most accurate. It is then sent to the robot.

Current Constants and What They Do:

  1. SPEED_WHILE_TURNING

    • This constant determines the speed that the robot will turn while it is at a steep angle. At the steepest angle, 1, the robot will not move. At the constant of 2, the robot will move at 1/2 speed. At 3, it will move at 2/3 speed, and so on.

    • How To Tune: Approach the target at a slanted angle. If the robot drives so that the target is out of view or the robot does not turn enough, decrease this. CAUTION: Do not set this value to lower than 1.

    • Recommended Value: 1 to 3

  2. AUTOMATIC_DRIVE_SPEED

    • This is the maximum speed at which the robot can move during AutomaticDrive.

    • How To Tune Determine if the robot moves too fast or too slow during AutomaticDrive. It may be moving too fast if it can not reach the target.

    • Recommended Value: 0.25 to 0.4 (Depends on Drivetrain)

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /