From 268b004857f2e73159cccfc18edeff7ad33dfe78 Mon Sep 17 00:00:00 2001 From: shainachen Date: Sat, 4 Feb 2017 16:05:32 -0800 Subject: [PATCH] Add Hall Effect Sensor code to test sensor --- .../usfirst/frc/team3501/robot/Constants.java | 2 + src/org/usfirst/frc/team3501/robot/Robot.java | 5 ++- .../robot/commands/shooter/RunFlyWheel.java | 2 +- .../shooter/RunFlyWheelContinuous.java | 2 +- .../team3501/robot/subsystems/Shooter.java | 11 ++++- .../robot/utils/HallEffectSensor.java | 40 +++++++++++++++++++ 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/org/usfirst/frc/team3501/robot/utils/HallEffectSensor.java diff --git a/src/org/usfirst/frc/team3501/robot/Constants.java b/src/org/usfirst/frc/team3501/robot/Constants.java index d34f79a..3933fdf 100644 --- a/src/org/usfirst/frc/team3501/robot/Constants.java +++ b/src/org/usfirst/frc/team3501/robot/Constants.java @@ -28,6 +28,8 @@ public class Constants { public final static int TOGGLE_FLYWHEEL_PORT = 0; public final static int TOGGLE_INDEXWHEEL_PORT = 0; + + public final static int HALL_EFFECT_PORT = 4; } public static class DriveTrain { diff --git a/src/org/usfirst/frc/team3501/robot/Robot.java b/src/org/usfirst/frc/team3501/robot/Robot.java index 1cffcb0..6d1094c 100644 --- a/src/org/usfirst/frc/team3501/robot/Robot.java +++ b/src/org/usfirst/frc/team3501/robot/Robot.java @@ -3,6 +3,7 @@ package org.usfirst.frc.team3501.robot; import org.usfirst.frc.team3501.robot.subsystems.DriveTrain; import org.usfirst.frc.team3501.robot.subsystems.Intake; import org.usfirst.frc.team3501.robot.subsystems.Shooter; +import org.usfirst.frc.team3501.robot.utils.HallEffectSensor; import edu.wpi.first.wpilibj.IterativeRobot; import edu.wpi.first.wpilibj.command.Scheduler; @@ -12,6 +13,7 @@ public class Robot extends IterativeRobot { private static Shooter shooter; private static OI oi; private static Intake intake; + private HallEffectSensor hallEffect; @Override public void robotInit() { @@ -20,6 +22,7 @@ public class Robot extends IterativeRobot { shooter = Shooter.getShooter(); intake = Intake.getIntake(); + hallEffect = Shooter.getHallEffectSensor(); } public static DriveTrain getDriveTrain() { @@ -59,6 +62,6 @@ public class Robot extends IterativeRobot { @Override public void teleopPeriodic() { Scheduler.getInstance().run(); - + System.out.println("Hall Effect Period: " + hallEffect.getCounterPeriod()); } } diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java index bb3fbcc..83d558a 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java @@ -46,7 +46,7 @@ public class RunFlyWheel extends Command { // Called repeatedly when this Command is scheduled to run protected void execute() { double shooterSpeed = this.wheelController - .calcPID(this.shooter.getShooterSpeed()); + .calcPID(this.shooter.getShooterRPM()); this.shooter.setFlyWheelMotorVal(shooterSpeed); } diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java index a73e7d0..8c8248d 100644 --- a/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java +++ b/src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java @@ -51,7 +51,7 @@ public class RunFlyWheelContinuous extends Command { // Called repeatedly when this Command is scheduled to run protected void execute() { double shooterSpeed = this.wheelController - .calcPID(this.shooter.getShooterSpeed()); + .calcPID(this.shooter.getShooterRPM()); this.shooter.setFlyWheelMotorVal(shooterSpeed); } diff --git a/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java b/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java index 150ce50..d786d22 100644 --- a/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java +++ b/src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java @@ -1,6 +1,7 @@ package org.usfirst.frc.team3501.robot.subsystems; import org.usfirst.frc.team3501.robot.Constants; +import org.usfirst.frc.team3501.robot.utils.HallEffectSensor; import com.ctre.CANTalon; @@ -9,6 +10,7 @@ import edu.wpi.first.wpilibj.command.Subsystem; public class Shooter extends Subsystem { public double wheelP = 0, wheelI = 0, wheelD = -0; private static Shooter shooter; + private static HallEffectSensor hallEffect; private final CANTalon flyWheel, indexWheel; public static final double DEFAULT_INDEXING_SPEED = 0; @@ -21,6 +23,11 @@ public class Shooter extends Subsystem { flyWheel = new CANTalon(Constants.Shooter.FLY_WHEEL); indexWheel = new CANTalon(Constants.Shooter.INDEX_WHEEL); + hallEffect = new HallEffectSensor(Constants.Shooter.HALL_EFFECT_PORT, 1); + } + + public static HallEffectSensor getHallEffectSensor() { + return hallEffect; } /** @@ -74,7 +81,7 @@ public class Shooter extends Subsystem { } - public double getShooterSpeed() { - return 0.0; + public double getShooterRPM() { + return hallEffect.getRPM(); } } diff --git a/src/org/usfirst/frc/team3501/robot/utils/HallEffectSensor.java b/src/org/usfirst/frc/team3501/robot/utils/HallEffectSensor.java new file mode 100644 index 0000000..384dc02 --- /dev/null +++ b/src/org/usfirst/frc/team3501/robot/utils/HallEffectSensor.java @@ -0,0 +1,40 @@ +package org.usfirst.frc.team3501.robot.utils; + +import edu.wpi.first.wpilibj.Counter; + +public class HallEffectSensor { + private Counter counter; + + public HallEffectSensor(int port, int bufferLength) { + counter = new Counter(port); + counter.setSamplesToAverage(bufferLength); + } + + /** + * Returns rotations per second(buffered) of hall effect sensor counter + * + * @return rotations per second of hall effect counter + */ + public double getRPS() { + return 1.0 / counter.getPeriod(); + } + + /** + * Get the period of the most recent count. + * + * @return period of latest count in seconds + */ + public double getCounterPeriod() { + return counter.getPeriod(); + } + + /** + * Returns rotations per minute(buffered) of hall effect sensor counter + * + * @return rotations per minute of hall effect sensor + */ + public double getRPM() { + return this.getRPS() * 60; + } + +} -- 2.30.2