Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Commit b4a0a99

Browse files
committed
Updated Spin
1 parent 7c6b2f3 commit b4a0a99

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

RobotCode2020/src/main/java/frc/robot/Robot.java

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Robot extends TimedRobot {
3131
public static Drivetrain drivetrain = new Drivetrain();
3232
public static Arm armMotor = new Arm();
3333
public static Climb climbDevice = new Climb();
34+
public static ColorSensor sensor = new ColorSensor();
3435

3536
Command m_autonomousCommand;
3637
SendableChooser<Command> m_chooser = new SendableChooser<>();

RobotCode2020/src/main/java/frc/robot/commands/Spin.java

+33-12
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
import edu.wpi.first.wpilibj.command.Command;
1111
import frc.robot.Robot;
12-
import frc.robot.RobotMap;
12+
/*import frc.robot.RobotMap;
1313
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1414
import com.ctre.phoenix.motorcontrol.ControlMode;
1515
import com.ctre.phoenix.motorcontrol.can.VictorSPX;
1616
import com.revrobotics.ColorSensorV3;
1717
import edu.wpi.first.wpilibj.I2C;
1818
import com.revrobotics.ColorMatch;
1919
import com.revrobotics.ColorMatchResult;
20+
import frc.robot.OI;*/
2021
import edu.wpi.first.wpilibj.util.Color;
2122

2223
public class Spin extends Command {
@@ -27,33 +28,51 @@ public Spin() {
2728
}
2829

2930
public Color initialColor;
30-
public Color testedColor;
31-
public Color prevColor;
32-
public int colorCount = 0;
31+
public Color currentColor;
32+
public Color previousColor;
33+
public int halfSpins = 0;
34+
public int fullSpins = 0;
3335

3436
// Called just before this Command runs the first time
3537
@Override
3638
protected void initialize() {
37-
initialColor = Robot.sensor.getColor();
38-
Robot.sensor.turn(0.5);
39+
initialColor = Robot.sensor.getColorMatch();
40+
previousColor = Robot.sensor.getColorMatch();
41+
halfSpins = 0;
42+
fullSpins = 0;
3943
}
4044

4145
// Called repeatedly when this Command is scheduled to run
4246
@Override
4347
protected void execute() {
44-
testedColor = Robot.sensor.getColor();
45-
if (testedColor.equals(initialColor) && !(testedColor.equals(prevColor))) {
46-
colorCount++;
48+
// System.out.println("running Spin execute");
49+
50+
currentColor = Robot.sensor.getColorMatch();
51+
52+
// if sensor sees initial color and the wheel has moved, then the wheel has turned halfway
53+
54+
if (currentColor.equals(initialColor) && !(currentColor.equals(previousColor))) {
55+
halfSpins++;
56+
// update number of full spins
57+
fullSpins = (int) (halfSpins / 2);
4758
}
48-
prevColor = Robot.sensor.getColor();
59+
60+
previousColor = Robot.sensor.getColorMatch();
61+
Robot.sensor.turn(0.25);
62+
63+
Robot.sensor.commandHalfSpins = halfSpins;
64+
Robot.sensor.commandFullSpins = fullSpins;
4965
}
5066

5167
// Make this return true when this Command no longer needs to run execute()
5268
@Override
5369
protected boolean isFinished() {
54-
if (colorCount > 6) {
70+
// stop spinning when done enough spins
71+
if (fullSpins >= 3) {
72+
Robot.sensor.turn(0);
5573
return true;
5674
}
75+
5776
return false;
5877
}
5978

@@ -66,5 +85,7 @@ protected void end() {
6685
// Called when another command which requires one or more of the same
6786
// subsystems is scheduled to run
6887
@Override
69-
protected void interrupted() {}
88+
protected void interrupted() {
89+
Robot.sensor.turn(0);
90+
}
7091
}

RobotCode2020/src/main/java/frc/robot/subsystems/ColorSensor.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import edu.wpi.first.wpilibj.command.Subsystem;
1111

1212
import frc.robot.RobotMap;
13+
import frc.robot.commands.Spin;
1314
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1415
import com.ctre.phoenix.motorcontrol.ControlMode;
1516
import com.ctre.phoenix.motorcontrol.can.VictorSPX;
@@ -18,6 +19,7 @@
1819
import com.revrobotics.ColorMatch;
1920
import com.revrobotics.ColorMatchResult;
2021
import edu.wpi.first.wpilibj.util.Color;
22+
import com.ctre.phoenix.motorcontrol.NeutralMode;
2123

2224
/** Add your docs here. */
2325
public class ColorSensor extends Subsystem {
@@ -34,11 +36,17 @@ public class ColorSensor extends Subsystem {
3436
private final Color kRedTarget = ColorMatch.makeColor(0.561, 0.232, 0.114);
3537
private final Color kYellowTarget = ColorMatch.makeColor(0.361, 0.524, 0.113);
3638

39+
// variables from Spin
40+
public int commandHalfSpins = 0;
41+
public int commandFullSpins = 0;
42+
3743
public ColorSensor() {
3844
m_colorMatcher.addColorMatch(kBlueTarget);
3945
m_colorMatcher.addColorMatch(kGreenTarget);
4046
m_colorMatcher.addColorMatch(kRedTarget);
4147
m_colorMatcher.addColorMatch(kYellowTarget);
48+
49+
sensorMotor.setNeutralMode(NeutralMode.Brake);
4250
}
4351

4452
Color detectedColor = m_colorSensor.getColor();
@@ -66,23 +74,32 @@ public void reportColorToDashboard() {
6674
// putting the values onto Shuffleboard
6775
SmartDashboard.putNumber("Red", detectedColor.red);
6876
SmartDashboard.putNumber("Green", detectedColor.green);
69-
SmartDashboard.putNumber("Confidence", match.confidence);
7077
SmartDashboard.putNumber("Blue", detectedColor.blue);
78+
SmartDashboard.putNumber("Confidence", match.confidence);
79+
7180
SmartDashboard.putString("DetectedColor", colorString);
7281
}
7382

83+
public void reportSpinsToDashboard() {
84+
SmartDashboard.putNumber("Half Spins", commandHalfSpins);
85+
SmartDashboard.putNumber("Full Spins", commandFullSpins);
86+
}
87+
7488
// Wrapper class
7589
public void turn(double voltagePercent) {
7690
sensorMotor.set(ControlMode.PercentOutput, voltagePercent);
7791
}
7892

7993
@Override
8094
public void initDefaultCommand() {
81-
// Set the default command for a subsystem here.
82-
// setDefaultCommand(new MySpecialCommand());
95+
// setDefaultCommand(new Spin());
8396
}
8497

8598
public Color getColor() {
8699
return m_colorSensor.getColor();
87100
}
101+
102+
public Color getColorMatch() {
103+
return match.color;
104+
}
88105
}

RobotCode2020/src/main/java/frc/robot/subsystems/Drivetrain.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import edu.wpi.first.wpilibj.DriverStation;
2626
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2727

28-
import frc.robot.commands.drivetrain.Drive;
28+
import frc.robot.commands.drivetrainDrive;
2929

3030
/** Drivetrain class w/ limelight vision tracking */
3131
public class Drivetrain extends Subsystem {

0 commit comments

Comments
 (0)