Skip to content
This repository has been archived by the owner on Nov 6, 2017. It is now read-only.

Commit

Permalink
Merge pull request #43 from SouthEugeneRoboticsTeam/align-shooter
Browse files Browse the repository at this point in the history
Auto
  • Loading branch information
SUPERCILEX authored Feb 24, 2017
2 parents d673bd8 + 0adea5a commit f2fa75b
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 301 deletions.
39 changes: 26 additions & 13 deletions src/org/usfirst/frc/team2521/robot/OI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.usfirst.frc.team2521.robot.commands.automation.DriveToGear;
import org.usfirst.frc.team2521.robot.commands.base.RunAgitator;
import org.usfirst.frc.team2521.robot.commands.base.RunFeeder;
import org.usfirst.frc.team2521.robot.commands.groups.RunShooterAndAgitator;
import org.usfirst.frc.team2521.robot.commands.groups.AlignShooter;
import org.usfirst.frc.team2521.robot.commands.groups.RunShooterSubsystems;

import java.util.stream.IntStream;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
Expand All @@ -13,31 +15,35 @@
* driver to control the robot.
*/
public class OI {
private static final int[] CUSTOM_BUTTONS = {2, 3, 4, 5};

private final Joystick left;
private final Joystick right;
private final Joystick secondary;
private final Joystick custom;

private JoystickButton alignShooterButton;
private JoystickButton driveToGearLeftButton;
private JoystickButton driveToGearRightButton;
private JoystickButton runShooterAndAgitatorButton;
private JoystickButton runFeederButton;
private JoystickButton runShooterSubsystemsButton;
private JoystickButton runAgitatorBackwardButton;

private OI() {
left = new Joystick(RobotMap.LEFT_STICK_PORT);
right = new Joystick(RobotMap.RIGHT_STICK_PORT);
secondary = new Joystick(RobotMap.SECONDARY_STICK_PORT);
custom = new Joystick(RobotMap.CUSTOM_STICK_PORT);

// Right joystick buttons
driveToGearLeftButton = new JoystickButton(right, RobotMap.DRIVE_TO_GEAR_LEFT_PORT);
driveToGearRightButton = new JoystickButton(right, RobotMap.DRIVE_TO_GEAR_RIGHT_PORT);
alignShooterButton = new JoystickButton(left, RobotMap.ALIGN_SHOOTER_BUTTON_PORT);
driveToGearLeftButton = new JoystickButton(left, RobotMap.DRIVE_TO_GEAR_LEFT_PORT);
driveToGearRightButton = new JoystickButton(left, RobotMap.DRIVE_TO_GEAR_RIGHT_PORT);

// Secondary joystick buttons
runShooterAndAgitatorButton =
new JoystickButton(secondary, RobotMap.RUN_SHOOTER_AND_AGITATOR_BUTTON_PORT);
runFeederButton = new JoystickButton(secondary, RobotMap.RUN_FEEDER_BUTTON_PORT);
runAgitatorBackwardButton =
new JoystickButton(secondary, RobotMap.RUN_AGITATOR_BACKWARD_BUTTON_PORT);
runShooterSubsystemsButton = new JoystickButton(secondary,
RobotMap.RUN_SHOOTER_SUBSYSTEMS_BUTTON_PORT);
runAgitatorBackwardButton = new JoystickButton(secondary,
RobotMap.RUN_AGITATOR_BACKWARD_BUTTON_PORT);

setButtonListeners();
}
Expand All @@ -49,6 +55,13 @@ public static OI getInstance() {
return Holder.INSTANCE;
}

public int getAutoMode() {
return IntStream.range(0, CUSTOM_BUTTONS.length)
.filter(i -> custom.getRawButton(CUSTOM_BUTTONS[i]))
.map(i -> (int) Math.pow(2, i))
.sum();
}

/**
* @return the left joystick
*/
Expand All @@ -71,10 +84,10 @@ public Joystick getSecondaryStick() {
}

private void setButtonListeners() {
alignShooterButton.toggleWhenActive(new AlignShooter());
driveToGearLeftButton.toggleWhenActive(new DriveToGear(true));
driveToGearRightButton.toggleWhenActive(new DriveToGear(false));
runShooterAndAgitatorButton.toggleWhenActive(new RunShooterAndAgitator());
runFeederButton.whileActive(new RunFeeder());
runShooterSubsystemsButton.toggleWhenActive(new RunShooterSubsystems());
runAgitatorBackwardButton.toggleWhenActive(new RunAgitator(false));
}

Expand Down
3 changes: 3 additions & 0 deletions src/org/usfirst/frc/team2521/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void robotInit() {
agitator = new Agitator();

auto = new Auto();

sensors.setCVCamera(Sensors.Camera.REAR);
}

@Override
Expand All @@ -47,6 +49,7 @@ public void disabledPeriodic() {

@Override
public void autonomousInit() {
Robot.sensors.setCVCamera(Sensors.Camera.FRONT);
auto.start();
}

Expand Down
27 changes: 19 additions & 8 deletions src/org/usfirst/frc/team2521/robot/RobotMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ public class RobotMap {
public static final int LEFT_STICK_PORT = 0;
public static final int RIGHT_STICK_PORT = 1;
public static final int SECONDARY_STICK_PORT = 2;
public static final int CUSTOM_STICK_PORT = 3;

// Right joystick buttons
public static final int DRIVE_TO_GEAR_LEFT_PORT = 4; // On right stick
public static final int DRIVE_TO_GEAR_RIGHT_PORT = 5; // On right stick
public static final int ALIGN_SHOOTER_BUTTON_PORT = 7; // On Left stick
public static final int DRIVE_TO_GEAR_LEFT_PORT = 8; // On left stick
public static final int DRIVE_TO_GEAR_RIGHT_PORT = 9; // On left stick

// Secondary joystick buttons
public static final int RUN_SHOOTER_AND_AGITATOR_BUTTON_PORT = 1; // On secondary stick
public static final int RUN_FEEDER_BUTTON_PORT = 2; // On secondary stick
public static final int RUN_AGITATOR_BACKWARD_BUTTON_PORT = 4; // On secondary stick
public static final int RUN_SHOOTER_SUBSYSTEMS_BUTTON_PORT = 1; // On secondary stick
public static final int RUN_AGITATOR_BACKWARD_BUTTON_PORT = 2; // On secondary stick

public static final int FRONT_RIGHT_MOTOR = 10;
public static final int FRONT_LEFT_MOTOR = 14;
Expand All @@ -30,7 +31,17 @@ public class RobotMap {
public static final int SHOOTER_MOTOR = 13;
public static final int FEEDER_MOTOR = 12;

public static final int FRONT_ULTRA_PORT = 0;
public static final int REAR_ULTRA_PORT = 2;
public static final int SIDE_LIDAR_PORT = 1;
public static final int REAR_ULTRA_PORT = 1;
public static final int LEFT_LIDAR_PORT = 0;
public static final int RIGHT_LIDAR_PORT = 2;

public final static class AutoModes {
public final static int NOTHING = 0;
public final static int CROSS_BASE_LINE = 6;
public final static int BALL_THEN_GEAR = 15;
public final static int GEAR_LEFT = 8;
public final static int GEAR_MIDDLE = 4;
public final static int GEAR_RIGHT = 2;
public final static int BALLS_ONLY = 1;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.usfirst.frc.team2521.robot.commands.automation;

import org.usfirst.frc.team2521.robot.Robot;

import edu.wpi.first.wpilibj.command.PIDCommand;

/**
* This abstract class provides a template for driving to the gear or the boiler.
*/
public abstract class DriveToBlob extends PIDCommand {
private static final double CAMERA_PROJ_PLANE_DISTANCE = 216.226;

/** true if we're straight-on */
protected boolean oriented = false;
/** true if we're on the left side of the target */
private boolean onLeftSide;
private boolean hasFoundBlob = false;

/**
* @param onLeftSide whether we're on the left side of the target
*/
protected DriveToBlob(double p, double i, double d, boolean onLeftSide) {
super(p, i, d);
this.onLeftSide = onLeftSide;
requires(Robot.drivetrain);
}

protected abstract double getSlowSpeed();

@Override
protected abstract void initialize();

@Override
protected final void execute() {
// Angle between camera line of sight and target
double targetAngle = Math.atan(Robot.sensors.getCVOffsetX() / CAMERA_PROJ_PLANE_DISTANCE);

// Convert angle to degrees
targetAngle *= 180 / Math.PI;

// Consider ourselves oriented when our angle error is less than 20
oriented = Math.abs(targetAngle) < 20;

targetAngle += Robot.sensors.getNavxAngle();
setSetpoint(targetAngle);

if (Robot.sensors.getBlobFound()) {
hasFoundBlob = true;
}
}

@Override
protected final double returnPIDInput() {
return Robot.sensors.getNavxAngle();
}

protected void setCurrentBlobFoundMotorSpeed() {
if (hasFoundBlob) {
Robot.drivetrain.setLeft(getSlowSpeed());
Robot.drivetrain.setRight(getSlowSpeed());
} else {
// Turn clockwise if we're too far left, counter-clockwise if we're too far right
Robot.drivetrain.setLeft(onLeftSide ? -getSlowSpeed() : getSlowSpeed());
Robot.drivetrain.setRight(onLeftSide ? -getSlowSpeed() : getSlowSpeed());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.usfirst.frc.team2521.robot.commands.automation;

import org.usfirst.frc.team2521.robot.Robot;
import org.usfirst.frc.team2521.robot.subsystems.Sensors;

/**
* This command drives to the correct spot to shoot from automatically.
*/
public class DriveToBoiler extends DriveToBlob {
private static final double P = 0.008;
private static final double I = 0;
private static final double D = 0;

private static final double P2 = 0.0125;
private static final double DISTANCE_SETPOINT = 35;
private static final double DISTANCE_ERROR_THRESHOLD = 3;

public DriveToBoiler() {
super(P, I, D, false);
}

@Override
protected double getSlowSpeed() {
return P2 * (DISTANCE_SETPOINT - Robot.sensors.getRearUltraInches());
}

@Override
protected void initialize() {
Robot.sensors.setCVCamera(Sensors.Camera.REAR);
}

@Override
protected boolean isFinished() {
return Math.abs(DISTANCE_SETPOINT - Robot.sensors.getRearUltraInches()) < DISTANCE_ERROR_THRESHOLD;
}

@Override
protected final void usePIDOutput(double output) {
if (Robot.sensors.getBlobFound()) {
// If we are already oriented, drive straight
if (oriented) {
Robot.drivetrain.setLeft(getSlowSpeed());
Robot.drivetrain.setRight(getSlowSpeed());
} else if (-output > 0) {
Robot.drivetrain.setLeft(output);
} else {
Robot.drivetrain.setRight(-output);
}
} else {
setCurrentBlobFoundMotorSpeed();
}
}

}

This file was deleted.

Loading

0 comments on commit f2fa75b

Please sign in to comment.