Advertisement
Guest User

Team 4665

a guest
Mar 25th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.02 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*/
  2. /* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
  3. /* Open Source Software %- may be modified and shared by FRC teams. The code   */
  4. /* must be accompanied by the FIRST BSD license file in the root directory of */
  5. /* the project.                                                               */
  6. /*----------------------------------------------------------------------------*/
  7.  
  8. package frc.robot;
  9.  
  10. import edu.wpi.first.wpilibj.Joystick;
  11. import edu.wpi.first.wpilibj.TimedRobot;
  12. import edu.wpi.first.wpilibj.Timer;
  13. import edu.wpi.first.wpilibj.Spark;
  14. import edu.wpi.first.wpilibj.drive.DifferentialDrive;
  15. import edu.wpi.first.wpilibj.SpeedControllerGroup;
  16. import edu.wpi.first.wpilibj.command.Command;
  17. import edu.wpi.first.wpilibj.AnalogInput;
  18. import edu.wpi.first.wpilibj.Encoder;
  19. import edu.wpi.first.wpilibj.PWMTalonSRX;
  20. import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
  21.  
  22.  
  23. /**
  24.  * The VM is configured to automatically run this class, and to call the
  25.  * functions corresponding to each mode, as described in the TimedRobot
  26.  * documentation. If you change the name of this class or the package after
  27.  * creating this project, you must also update the manifest file in the resource
  28.  * directory.
  29.  */
  30. public class Robot extends TimedRobot {
  31.   private Spark m_frontLeft =  new Spark(1);
  32.   //private Spark m_rearLeft = new Spark(0);
  33.     private SpeedControllerGroup m_left = new SpeedControllerGroup(m_frontLeft);
  34.     private Spark m_frontRight = new Spark(3);
  35.     private Spark m_rearRight = new Spark(2);
  36.   private SpeedControllerGroup m_right = new SpeedControllerGroup(m_frontRight, m_rearRight);
  37.     private DifferentialDrive m_robotdrive = new DifferentialDrive(m_left, m_right);
  38.   private Joystick controller = new Joystick(0);
  39.   private Joystick controller2 = new Joystick(1);
  40.   private final Timer m_timer = new Timer();
  41.   private boolean isCarrying = false;
  42.   private boolean wannaGoSlower = false;
  43.   private boolean isAutoCenter = false;
  44.   private Encoder elevatorEncoder = new Encoder(0, 1, false, Encoder.EncodingType.k4X);
  45.   private Encoder dinoEncoder = new Encoder(2, 3, false, Encoder.EncodingType.k4X);
  46.   private PWMTalonSRX l_elevator = new PWMTalonSRX(5);
  47.   private PWMTalonSRX r_elevator = new PWMTalonSRX(4);
  48.   private Spark sideDrive = new Spark(0);
  49.   private PWMTalonSRX dinoArms = new PWMTalonSRX(7);
  50.   private double standardLevels = 1;
  51.   private double rotationValue = 1;
  52.   private double targetHeight = 1;
  53.   private AnalogInput ai = new AnalogInput(0);
  54.  
  55.  
  56.  
  57.    
  58.   /**
  59.    * This function is run when the robot is first started up and should be
  60.    * used for any initialization code.
  61.    */
  62.   @Override
  63.   public void robotInit() {
  64.   }
  65.  
  66.  
  67.    // This function is run once each time the robot enters autonomous mode.
  68.   @Override
  69.   public void autonomousInit() {
  70.     m_timer.reset();
  71.     m_timer.start();
  72.   }
  73.    // This function is called periodically during autonomous.
  74.   @Override
  75.   public void autonomousPeriodic() {
  76.     // Drive for 2 seconds
  77.     if (m_timer.get() < 2.0) {
  78.       m_robotdrive.arcadeDrive(0.5, 0.0); // drive forwards half speed
  79.     } else {
  80.       m_robotdrive.stopMotor(); // stop robot
  81.     }
  82.   }
  83.  
  84.  
  85.  
  86.   /**
  87.    * This function is called once each time the robot enters teleoperated mode.
  88.    */
  89.   @Override
  90.   public void teleopInit() {
  91.     elevatorEncoder.reset();
  92.     dinoEncoder.reset();
  93.     targetHeight = .5;
  94.  
  95.   }
  96.  
  97.  
  98.  
  99.   /**
  100.    * This function is called periodically during teleoperated mode.
  101.    */
  102.  
  103.  
  104.  
  105.  
  106.   @Override
  107.   public void teleopPeriodic() {
  108.  
  109.     double carryOffset;// isCarrying input
  110.     double elevatorSpeed;
  111.     double whenClose;
  112.     double distanceToTravel = elevatorEncoder.get() - targetHeight * 65.4;
  113.     double driveSpeed;
  114.     double dinoSpeed = 0;
  115.     int pressedButton = 0;
  116.     int pressedButton2 = 0;
  117.     int dinoPosition = 0;
  118.     int dinoP = 0;
  119.    
  120.     SmartDashboard.putBoolean("DB/LED 0", isCarrying);
  121.     SmartDashboard.putBoolean("DB/LED 1", wannaGoSlower);
  122.     SmartDashboard.putBoolean("DB/LED 2", isAutoCenter);
  123.  
  124.     //half Speed thing
  125.     if(controller.getRawButtonPressed(1)){ // 1 pressed, toggle driveSpeed
  126.       wannaGoSlower = !wannaGoSlower;
  127.     }
  128.     if(wannaGoSlower){
  129.       driveSpeed = .75;
  130.     }else{
  131.       driveSpeed = 1;
  132.     }
  133.  
  134.  
  135.  
  136.     //Basic drive
  137.     m_robotdrive.arcadeDrive(controller.getY()*-1 * driveSpeed, controller.getX() * driveSpeed);
  138.  
  139.     //Side drive
  140.     if(controller.getRawButtonPressed(3)){
  141.       isAutoCenter = !isAutoCenter;
  142.     }
  143.     if(isAutoCenter){
  144.       //auto side drive
  145.       if(ai.getValue() > 1.75 && ai.getValue() < 3.2){
  146.         sideDrive.set(.4);
  147.       }else if(ai.getValue() < 1.65 && ai.getValue() > .1){
  148.         sideDrive.set(-.4);
  149.       }else{
  150.         sideDrive.set(0);
  151.       }
  152.     }else{
  153.       if (controller.getRawButton(7)) { // Left trigger pressed, go left
  154.         sideDrive.set(.7);
  155.       }else if (controller.getRawButton(8)) { // Right trigger pressed, go right
  156.         sideDrive.set(-.7);
  157.       }else {
  158.         sideDrive.set(0);
  159.       }
  160.     }
  161.    
  162.     //Elevator Stuff
  163.     if(controller2.getRawButtonPressed(1)){ // X pressed, toggle isCarrying
  164.       isCarrying = !isCarrying;
  165.       pressedButton = 1;
  166.     }
  167.     if(isCarrying){
  168.       carryOffset = 4.75;//IN INCHES****
  169.     }else{
  170.       carryOffset = 0;
  171.     }
  172.  
  173.  
  174.     if (Math.abs(distanceToTravel) < 200 /*encoder units*/){
  175.       whenClose = .25;
  176.     }else{
  177.       whenClose = 1;
  178.     }
  179.  
  180.     if (controller.getRawButton(9) || controller.getRawButton(10)){
  181.       if(controller.getRawButton(9)){//UP
  182.         l_elevator.set(.8);
  183.         r_elevator.set(.8);
  184.       }else if(controller.getRawButton(10)){//DOWN
  185.         l_elevator.set(-.5);
  186.         r_elevator.set(-.5);
  187.       }else{
  188.         l_elevator.set(0);
  189.         r_elevator.set(0);
  190.       }
  191.     }else{
  192.  
  193.      
  194.  
  195.       if(controller2.getRawButtonPressed(4)){
  196.         standardLevels = .5; //IN INCHES****
  197.         pressedButton = 4;
  198.       }
  199.       if(controller.getRawButtonPressed(2)){//IN INCHES****
  200.         standardLevels = 11;
  201.         pressedButton = 21;
  202.       }
  203.       if(controller2.getRawButtonPressed(3)){//IN INCHES****
  204.         standardLevels = 28;
  205.         pressedButton = 3;
  206.       }
  207.       if(controller2.getRawButtonPressed(2)){//IN INCHES****
  208.         standardLevels = 55;
  209.         pressedButton = 22;
  210.       }
  211.      
  212.  
  213.       if(pressedButton != 0){
  214.         targetHeight = standardLevels + carryOffset;
  215.       }
  216.  
  217.       if(elevatorEncoder.get() > ((targetHeight + .15) * 65.4)){//Convert to counts
  218.         elevatorSpeed = (-.8 * whenClose);
  219.       }else if(elevatorEncoder.get() < ((targetHeight - .15) * 65.4)){//Convert to counts
  220.         elevatorSpeed = (1 * whenClose);
  221.       }else{
  222.         elevatorSpeed = 0.10;
  223.       }
  224.      
  225.       l_elevator.set(elevatorSpeed);
  226.       r_elevator.set(elevatorSpeed);
  227.     }
  228.  
  229.  // homing program
  230.  if(controller2.getRawButtonPressed(10)){
  231.   m_timer.reset();
  232.   m_timer.start();
  233.  
  234.   if(m_timer.get() < 5.0){
  235.     l_elevator.set(-.20);
  236.     r_elevator.set(-.20);  
  237.   }else if(m_timer.get() < 5.1){
  238.     elevatorEncoder.reset();
  239.   }
  240.  }
  241.  
  242.     //Dino Arms
  243.     if(controller2.getRawButton(5)){//UP
  244.       dinoSpeed = -.4;
  245.     }else if(controller2.getRawButton(7)){//DOWN
  246.       dinoSpeed = .3;
  247.     }else if(controller2.getRawButton(9)){
  248.    
  249.       if(controller2.getRawButtonPressed(9)){
  250.       pressedButton2 = 1;
  251.       dinoP = 1;
  252.       dinoPosition = 155;
  253.       }
  254.    
  255.       if(pressedButton2 != 0){
  256.         rotationValue = dinoPosition;
  257.       }
  258.      
  259.       if(dinoEncoder.get() > (rotationValue + 5)){
  260.         dinoSpeed = -.4;
  261.       }else if(dinoEncoder.get() < (rotationValue)){
  262.         dinoSpeed = .3;
  263.       }
  264.      
  265.     }else{
  266.       dinoSpeed = -.1;
  267.     }
  268.    
  269.   dinoArms.set(dinoSpeed);  
  270.   System.out.println(dinoEncoder.get());
  271.  
  272.  
  273.  
  274.   }
  275.  
  276.  
  277.  
  278.  
  279.   /**
  280.    * This function is
  281.    * called periodically during test mode.
  282.    */
  283.   @Override
  284.   public void testPeriodic() {
  285.   }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement