Add Hall Effect Sensor code to test sensor
authorshainachen <shaina.sierra@gmail.com>
Sun, 5 Feb 2017 00:05:32 +0000 (16:05 -0800)
committerCindy Zhang <cindyzyx9@gmail.com>
Tue, 7 Feb 2017 03:39:49 +0000 (19:39 -0800)
src/org/usfirst/frc/team3501/robot/Constants.java
src/org/usfirst/frc/team3501/robot/Robot.java
src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheel.java
src/org/usfirst/frc/team3501/robot/commands/shooter/RunFlyWheelContinuous.java
src/org/usfirst/frc/team3501/robot/subsystems/Shooter.java
src/org/usfirst/frc/team3501/robot/utils/HallEffectSensor.java [new file with mode: 0644]

index d34f79a5ce64caa239e25929ddf57744e1ebfd9e..3933fdf0fdfbf50c1dadcb267c3613db4ad3f741 100644 (file)
@@ -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 {
index 1cffcb01aeeeae61e0daaae4d795230bda22459f..6d1094c641ad98d7c42147e69f507e5bcb5bb64e 100644 (file)
@@ -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());
   }
 }
index bb3fbccf56de701b1b56691b876517bb2b92f562..83d558acfca15a4d069d1cc230b9227c011242fa 100644 (file)
@@ -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);
   }
index a73e7d065ad8555936378217e0d15d597d80a7f4..8c8248d51272f2ba93219ce366c4709e08eabbb8 100644 (file)
@@ -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);
   }
index 150ce5091a27713f73a63b970f06ee1274daa138..d786d22cf5120e6ff680d6c017256ba8c487bd44 100644 (file)
@@ -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 (file)
index 0000000..384dc02
--- /dev/null
@@ -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;
+  }
+
+}