Add key listener to runFlyWheel rohan/addKeyListener
authorRohan Rodrigues <rohanrodrigues19@gmail.com>
Sat, 25 Feb 2017 19:43:14 +0000 (11:43 -0800)
committerRohan Rodrigues <rohanrodrigues19@gmail.com>
Sat, 25 Feb 2017 19:43:14 +0000 (11:43 -0800)
src/org/usfirst/frc/team3501/robot/OI.java
src/org/usfirst/frc/team3501/robot/commands/climber/TestToggleWinch.java [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/commands/shooter/TestFlyWheelButton.java [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/utils/Lidar.java [new file with mode: 0644]

index c608b86b8d1767a8eeab2c2b268693ef6ce74d0e..3929af241277c3f207465503bdb5e4a51eedd922 100644 (file)
@@ -1,5 +1,10 @@
 package org.usfirst.frc.team3501.robot;
 
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+
+import javax.swing.JButton;
+
 import org.usfirst.frc.team3501.robot.commands.climber.ToggleWinch;
 import org.usfirst.frc.team3501.robot.commands.driving.ToggleGear;
 import org.usfirst.frc.team3501.robot.commands.intake.ReverseIntakeContinuous;
@@ -22,7 +27,8 @@ public class OI {
 
   public static Button runIndexWheel;
   public static Button reverseIndexWheel;
-  public static Button toggleFlyWheel;
+  // public static Button toggleFlyWheel;
+  public static JButton toggleFlyWheel;
 
   public static Button toggleGear;
 
@@ -32,6 +38,8 @@ public class OI {
   public static Button increaseShooterSpeed;
   public static Button decreaseShooterSpeed;
 
+  // private static Key a;
+
   public OI() {
     leftJoystick = new Joystick(Constants.OI.LEFT_STICK_PORT);
     rightJoystick = new Joystick(Constants.OI.RIGHT_STICK_PORT);
@@ -44,9 +52,37 @@ public class OI {
         Constants.OI.REVERSE_INDEXWHEEL_PORT);
     reverseIndexWheel.whileHeld(new ReverseIndexWheelContinuous());
 
-    toggleFlyWheel = new JoystickButton(rightJoystick,
-        Constants.OI.TOGGLE_FLYWHEEL_PORT);
-    toggleFlyWheel.toggleWhenPressed(new RunFlyWheelContinuous());
+    /*
+     * toggleFlyWheel = new JoystickButton(rightJoystick,
+     * Constants.OI.TOGGLE_FLYWHEEL_PORT); toggleFlyWheel.toggleWhenPressed(new
+     * RunFlyWheelContinuous());
+     */
+
+    toggleFlyWheel = new JButton();
+    toggleFlyWheel.addKeyListener(new KeyAdapter() {
+      RunFlyWheelContinuous runflywheel = new RunFlyWheelContinuous();
+
+      /***
+       * Starts the run index wheel continuous function
+       */
+      @Override
+      public void keyPressed(KeyEvent e) {
+        if (e.getKeyCode() == KeyEvent.VK_0) {
+          runflywheel.start();
+        }
+      }
+
+      /***
+       * Ends the run index wheel continuous function
+       */
+      @Override
+      public void keyReleased(KeyEvent e) {
+        if (e.getKeyCode() == KeyEvent.VK_0) {
+          runflywheel.cancel();
+        }
+      }
+
+    });
 
     toggleGear = new JoystickButton(leftJoystick,
         Constants.OI.TOGGLE_GEAR_PORT);
diff --git a/src/org/usfirst/frc/team3501/robot/commands/climber/TestToggleWinch.java b/src/org/usfirst/frc/team3501/robot/commands/climber/TestToggleWinch.java
new file mode 100644 (file)
index 0000000..aab8457
--- /dev/null
@@ -0,0 +1,50 @@
+package org.usfirst.frc.team3501.robot.commands.climber;
+
+import org.usfirst.frc.team3501.robot.Robot;
+import org.usfirst.frc.team3501.robot.subsystems.DriveTrain;
+
+import edu.wpi.first.wpilibj.command.Command;
+
+/**
+ *
+ */
+public class TestToggleWinch extends Command {
+  DriveTrain driveTrain = Robot.getDriveTrain();
+  boolean isRunning = driveTrain.shouldBeClimbing;
+
+  public TestToggleWinch() {
+    requires(driveTrain);
+  }
+
+  @Override
+  protected void initialize() {
+
+  }
+
+  @Override
+  protected void execute() {
+    if (driveTrain.shouldBeClimbing) {
+      driveTrain.shouldBeClimbing = false;
+      System.out.println("Ending Toggle Winch");
+    } else {
+      driveTrain.shouldBeClimbing = true;
+      System.out.println("Running Toggle Winch");
+    }
+
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return true;
+  }
+
+  @Override
+  protected void end() {
+
+  }
+
+  @Override
+  protected void interrupted() {
+    end();
+  }
+}
diff --git a/src/org/usfirst/frc/team3501/robot/commands/shooter/TestFlyWheelButton.java b/src/org/usfirst/frc/team3501/robot/commands/shooter/TestFlyWheelButton.java
new file mode 100644 (file)
index 0000000..7536afd
--- /dev/null
@@ -0,0 +1,53 @@
+package org.usfirst.frc.team3501.robot.commands.shooter;
+
+import org.usfirst.frc.team3501.robot.Robot;
+import org.usfirst.frc.team3501.robot.subsystems.Shooter;
+
+import edu.wpi.first.wpilibj.command.Command;
+
+/**
+
+ */
+public class TestFlyWheelButton extends Command {
+  private Shooter shooter = Robot.getShooter();
+
+  /**
+   * See JavaDoc comment in class for details
+   *
+   * @param motorVal
+   *          value range from -1 to 1
+   */
+  public TestFlyWheelButton() {
+    requires(shooter);
+  }
+
+  // Called just before this Command runs the first time
+  @Override
+  protected void initialize() {
+  }
+
+  // Called repeatedly when this Command is scheduled to run
+  @Override
+  protected void execute() {
+    System.out.println("Running Fly Wheel");
+  }
+
+  // Called once after isFinished returns true
+  @Override
+  protected void end() {
+  }
+
+  // Called when another command which requires one or more of the same
+  // subsystems is scheduled to run
+  @Override
+  protected void interrupted() {
+    end();
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return false;
+
+  }
+
+}
diff --git a/src/org/usfirst/frc/team3501/robot/utils/Lidar.java b/src/org/usfirst/frc/team3501/robot/utils/Lidar.java
new file mode 100644 (file)
index 0000000..49f76d4
--- /dev/null
@@ -0,0 +1,87 @@
+package org.usfirst.frc.team3501.robot.utils;
+
+import java.util.TimerTask;
+import edu.wpi.first.wpilibj.I2C;
+import edu.wpi.first.wpilibj.Timer;
+import edu.wpi.first.wpilibj.I2C.Port;
+import edu.wpi.first.wpilibj.PIDSource;
+import edu.wpi.first.wpilibj.PIDSourceType;
+
+public class Lidar implements PIDSource {
+  private I2C i2c;
+  private byte[] distance;
+  private java.util.Timer updater;
+
+  private final int LIDAR_ADDR = 0x62;
+  private final int LIDAR_CONFIG_REGISTER = 0x00;
+  private final int LIDAR_DISTANCE_REGISTER = 0x8f;
+
+  public Lidar(Port port) {
+    i2c = new I2C(port, LIDAR_ADDR);
+
+    distance = new byte[2];
+
+    updater = new java.util.Timer();
+  }
+
+  // Distance in cm
+  public int getDistance() {
+    return (int) Integer.toUnsignedLong(distance[0] << 8)
+        + Byte.toUnsignedInt(distance[1]);
+  }
+
+  @Override
+  public double pidGet() {
+    return getDistance();
+  }
+
+  // Start 10Hz polling
+  public void start() {
+    updater.scheduleAtFixedRate(new LIDARUpdater(), 0, 100);
+  }
+
+  // Start polling for period in milliseconds
+  public void start(int period) {
+    updater.scheduleAtFixedRate(new LIDARUpdater(), 0, period);
+  }
+
+  public void stop() {
+    updater.cancel();
+    updater = new java.util.Timer();
+  }
+
+  // Update distance variable
+  public void update() {
+    i2c.write(LIDAR_CONFIG_REGISTER, 0x04); // Initiate measurement
+    Timer.delay(0.04); // Delay for measurement to be taken
+    i2c.read(LIDAR_DISTANCE_REGISTER, 2, distance); // Read in measurement
+    Timer.delay(0.005); // Delay to prevent over polling
+  }
+
+  // Timer task to keep distance updated
+  private class LIDARUpdater extends TimerTask {
+    @Override
+    public void run() {
+      while (true) {
+        update();
+        try {
+          Thread.sleep(10);
+        } catch (InterruptedException e) {
+          e.printStackTrace();
+        }
+      }
+    }
+  }
+
+  @Override
+  public void setPIDSourceType(PIDSourceType pidSource) {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public PIDSourceType getPIDSourceType() {
+    // TODO Auto-generated method stub
+    return null;
+  }
+}