Commit | Line | Data |
---|---|---|
79ba119a | 1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
04227dc0 | 3 | import org.usfirst.frc.team3501.robot.Constants; |
ac77a7b8 | 4 | import org.usfirst.frc.team3501.robot.MathLib; |
268b0048 | 5 | import org.usfirst.frc.team3501.robot.utils.HallEffectSensor; |
7ba6bc91 | 6 | import org.usfirst.frc.team3501.robot.utils.PIDController; |
04227dc0 | 7 | |
8 | import com.ctre.CANTalon; | |
9 | ||
185e0c8d RR |
10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; | |
04227dc0 | 12 | import edu.wpi.first.wpilibj.command.Subsystem; |
13 | ||
14 | public class Shooter extends Subsystem { | |
7ba6bc91 | 15 | public double wheelP = 0.00007, wheelI = 0, wheelD = 0.0008; |
079a8cb6 | 16 | private static Shooter shooter; |
268b0048 | 17 | private static HallEffectSensor hallEffect; |
3a86b1a5 | 18 | private final CANTalon flyWheel1, flyWheel2, indexWheel; |
79ba119a | 19 | |
7ba6bc91 CZ |
20 | private PIDController wheelController; |
21 | ||
22 | private static final double RPM_THRESHOLD = 10; | |
fc01fb0f CZ |
23 | private static final double DEFAULT_INDEXING_MOTOR_VALUE = 0.75; |
24 | private static final double REVERSE_FLYWHEEL_MOTOR_VALUE = -0.75; | |
25 | private static final double DEFAULT_SHOOTING_SPEED = 2700; // rpm | |
f56e6ebf | 26 | private static final double SHOOTING_SPEED_INCREMENT = 50; |
ad7e6b1e | 27 | |
7ba6bc91 CZ |
28 | private double targetShootingSpeed = DEFAULT_SHOOTING_SPEED; |
29 | private double currentShooterMotorValue = 0; | |
ad7e6b1e | 30 | |
185e0c8d RR |
31 | private final DoubleSolenoid piston; |
32 | ||
079a8cb6 | 33 | private Shooter() { |
3a86b1a5 CZ |
34 | flyWheel1 = new CANTalon(Constants.Shooter.FLY_WHEEL1); |
35 | flyWheel2 = new CANTalon(Constants.Shooter.FLY_WHEEL2); | |
04227dc0 | 36 | indexWheel = new CANTalon(Constants.Shooter.INDEX_WHEEL); |
ad7e6b1e | 37 | |
268b0048 | 38 | hallEffect = new HallEffectSensor(Constants.Shooter.HALL_EFFECT_PORT, 1); |
185e0c8d | 39 | |
82fa994f | 40 | piston = new DoubleSolenoid(Constants.Shooter.MODULE_NUMBER, |
185e0c8d | 41 | Constants.Shooter.PISTON_FORWARD, Constants.Shooter.PISTON_REVERSE); |
268b0048 | 42 | } |
43 | ||
04227dc0 | 44 | /** |
45 | * Returns shooter object | |
46 | * | |
47 | * @return Shooter object | |
48 | */ | |
079a8cb6 CZ |
49 | public static Shooter getShooter() { |
50 | if (shooter == null) { | |
51 | shooter = new Shooter(); | |
52 | } | |
53 | return shooter; | |
54 | } | |
41dfad94 | 55 | |
079a8cb6 | 56 | /** |
04227dc0 | 57 | * Sets fly wheel motor value to input. |
58 | * | |
59 | * @param val | |
60 | * motor value from -1 to 1(fastest forward) | |
079a8cb6 | 61 | */ |
ac77a7b8 CZ |
62 | public void setFlyWheelMotorVal(double val) { |
63 | val = MathLib.restrictToRange(val, 0.0, 1.0); | |
3a86b1a5 CZ |
64 | flyWheel1.set(val); |
65 | flyWheel2.set(val); | |
079a8cb6 | 66 | } |
41dfad94 | 67 | |
04227dc0 | 68 | /** |
69 | * Stops fly wheel motor. | |
70 | */ | |
71 | public void stopFlyWheel() { | |
3a86b1a5 CZ |
72 | flyWheel1.set(0); |
73 | flyWheel2.set(0); | |
04227dc0 | 74 | } |
41dfad94 | 75 | |
04227dc0 | 76 | /** |
77 | * Sets index wheel motor value to input. | |
78 | * | |
79 | * @param val | |
80 | * motor value from -1 to 1(fastest forward) | |
81 | */ | |
ac77a7b8 CZ |
82 | public void setIndexWheelMotorVal(double val) { |
83 | val = MathLib.restrictToRange(val, -1.0, 1.0); | |
04227dc0 | 84 | indexWheel.set(val); |
079a8cb6 | 85 | } |
41dfad94 | 86 | |
079a8cb6 | 87 | /** |
04227dc0 | 88 | * Stops index wheel motor. |
079a8cb6 CZ |
89 | */ |
90 | public void stopIndexWheel() { | |
04227dc0 | 91 | indexWheel.set(0); |
079a8cb6 | 92 | } |
41dfad94 | 93 | |
f625e57a CZ |
94 | @Override |
95 | protected void initDefaultCommand() { | |
96 | ||
97 | } | |
98 | ||
7ba6bc91 CZ |
99 | public double getRPMThreshold() { |
100 | return RPM_THRESHOLD; | |
101 | } | |
102 | ||
f625e57a CZ |
103 | public double getShooterRPM() { |
104 | return hallEffect.getRPM(); | |
09e509d3 NA |
105 | } |
106 | ||
7ba6bc91 CZ |
107 | public void setTargetShootingSpeed(double Value) { |
108 | targetShootingSpeed = Value; | |
09e509d3 NA |
109 | } |
110 | ||
7ba6bc91 CZ |
111 | public void decrementTargetShootingSpeed() { |
112 | this.targetShootingSpeed -= this.SHOOTING_SPEED_INCREMENT; | |
f625e57a | 113 | } |
079a8cb6 | 114 | |
7ba6bc91 CZ |
115 | public void incrementTargetShootingSpeed() { |
116 | this.targetShootingSpeed += this.SHOOTING_SPEED_INCREMENT; | |
079a8cb6 | 117 | } |
381dad77 | 118 | |
7ba6bc91 CZ |
119 | public void resetTargetShootingSpeed() { |
120 | this.targetShootingSpeed = this.DEFAULT_SHOOTING_SPEED; | |
f625e57a CZ |
121 | } |
122 | ||
7ba6bc91 CZ |
123 | public double getTargetShootingSpeed() { |
124 | return targetShootingSpeed; | |
f625e57a CZ |
125 | } |
126 | ||
127 | public void reverseIndexWheel() { | |
7ba6bc91 | 128 | this.setIndexWheelMotorVal(-DEFAULT_INDEXING_MOTOR_VALUE); |
f625e57a CZ |
129 | } |
130 | ||
131 | public void runIndexWheel() { | |
7ba6bc91 CZ |
132 | this.setIndexWheelMotorVal(DEFAULT_INDEXING_MOTOR_VALUE); |
133 | } | |
134 | ||
135 | public double calculateShooterSpeed() { | |
136 | this.wheelController.setSetPoint(targetShootingSpeed); | |
137 | double calculatedShooterIncrement = this.wheelController | |
138 | .calcPID(this.getShooterRPM()); | |
139 | currentShooterMotorValue += calculatedShooterIncrement; | |
140 | return currentShooterMotorValue; | |
141 | } | |
142 | ||
143 | public void initializePIDController() { | |
144 | this.wheelController = new PIDController(wheelP, wheelI, wheelD); | |
145 | this.wheelController.setDoneRange(10); | |
146 | this.wheelController.setMaxOutput(1.0); | |
147 | this.wheelController.setMinDoneCycles(3); | |
148 | this.wheelController.setSetPoint(this.targetShootingSpeed); | |
149 | this.currentShooterMotorValue = 0; | |
381dad77 | 150 | } |
185e0c8d RR |
151 | |
152 | public Value getPistonValue() { | |
153 | return piston.get(); | |
154 | } | |
155 | ||
156 | public void setHighGear() { | |
157 | changeGear(Constants.Shooter.HIGH_GEAR); | |
158 | } | |
159 | ||
160 | public void setLowGear() { | |
161 | changeGear(Constants.Shooter.LOW_GEAR); | |
162 | } | |
163 | ||
164 | private void changeGear(DoubleSolenoid.Value gear) { | |
165 | piston.set(gear); | |
166 | } | |
fc01fb0f CZ |
167 | |
168 | public void reverseFlyWheel() { | |
169 | this.setFlyWheelMotorVal(shooter.REVERSE_FLYWHEEL_MOTOR_VALUE); | |
170 | } | |
79ba119a | 171 | } |