Changes between Initial Version and Version 1 of ControlSystems/IntroRobotJava/HazelnutSample1


Ignore:
Timestamp:
Oct 7, 2019, 9:51:23 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/IntroRobotJava/HazelnutSample1

    v1 v1  
     1
     2{{{
     3
     4package frc.robot;
     5
     6// Import the classes of objects you will use
     7import edu.wpi.first.wpilibj.TimedRobot;
     8import edu.wpi.first.wpilibj.XboxController;
     9import edu.wpi.first.wpilibj.GenericHID.Hand;
     10import edu.wpi.first.wpilibj.Timer;
     11import edu.wpi.first.wpilibj.drive.DifferentialDrive;
     12import com.ctre.phoenix.motorcontrol.can.*;
     13import edu.wpi.first.wpilibj.Ultrasonic;
     14import edu.wpi.first.wpilibj.CameraServer;
     15
     16import edu.wpi.first.wpilibj.livewindow.LiveWindow;
     17
     18import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
     19import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
     20
     21/**
     22 * The VM is configured to automatically run this class, and to call the
     23 * functions corresponding to each mode, as described in the TimedRobot
     24 * documentation. If you change the name of this class or the package after
     25 * creating this project, you must also update the build.gradle file in the
     26 * project.
     27 */
     28public class Robot extends TimedRobot {
     29  // Create instances of each object
     30  private static final String kDefaultAuto = "Default";
     31  private static final String kCustomAuto = "My Auto";
     32  private String m_autoSelected;
     33  private final SendableChooser<String> m_chooser = new SendableChooser<>();
     34  private XboxController xbox;
     35  private Timer timer;
     36  private WPI_TalonSRX m_rearLeft;
     37  private WPI_TalonSRX m_rearRight;
     38  private DifferentialDrive m_drive;
     39  private boolean    f_safetyStop;
     40  private Ultrasonic f_ultrasonic;
     41
     42  /**
     43   * This function is run when the robot is first started up and should be
     44   * used for any initialization code.
     45   */
     46  @Override
     47  public void robotInit() {
     48    // initialize the objects and connect them to their underlying hardware
     49    m_chooser.addDefault("Default Auto", kDefaultAuto);
     50    m_chooser.addObject("My Auto", kCustomAuto);
     51    SmartDashboard.putData("Auto choices", m_chooser);
     52    xbox = new XboxController(0);
     53    timer = new Timer();
     54    m_rearLeft = new WPI_TalonSRX(4);
     55    m_rearRight = new WPI_TalonSRX(3);
     56    m_drive = new DifferentialDrive(m_rearLeft, m_rearRight);
     57    f_ultrasonic = new Ultrasonic(1,0); // ping, echo
     58    f_ultrasonic.setAutomaticMode(true);
     59    f_ultrasonic.setEnabled(true);
     60    CameraServer.getInstance().startAutomaticCapture();
     61}
     62
     63  /**
     64   * This function is called every robot packet, no matter the mode. Use
     65   * this for items like diagnostics that you want ran during disabled,
     66   * autonomous, teleoperated and test.
     67   *
     68   * <p>This runs after the mode specific periodic functions, but before
     69   * LiveWindow and SmartDashboard integrated updating.
     70   */
     71  @Override
     72  public void robotPeriodic() {
     73  }
     74
     75  /**
     76   * This autonomous (along with the chooser code above) shows how to select
     77   * between different autonomous modes using the dashboard. The sendable
     78   * chooser code works with the Java SmartDashboard. If you prefer the
     79   * LabVIEW Dashboard, remove all of the chooser code and uncomment the
     80   * getString line to get the auto name from the text box below the Gyro
     81   *
     82   * <p>You can add additional auto modes by adding additional comparisons to
     83   * the switch structure below with additional strings. If using the
     84   * SendableChooser make sure to add them to the chooser code above as well.
     85   */
     86  @Override
     87  public void autonomousInit() {
     88    m_autoSelected = m_chooser.getSelected();
     89    // autoSelected = SmartDashboard.getString("Auto Selector",
     90    // defaultAuto);
     91    System.out.println("Auto selected: " + m_autoSelected);
     92    timer.reset();
     93    timer.start();
     94  }
     95
     96  /*public void teleopInit() {
     97
     98  }*/
     99
     100  /**
     101   * This function is called periodically during autonomous.
     102   */
     103  @Override
     104  public void autonomousPeriodic() {
     105    if (timer.get() < 2.0) {
     106      m_drive.curvatureDrive(0.1, 0.0, false);
     107    }
     108    else
     109    {
     110      m_drive.curvatureDrive(0.0, 0.0,false);
     111    }
     112    switch (m_autoSelected) {
     113      case kCustomAuto:
     114        // Put custom auto code here
     115        break;
     116      case kDefaultAuto:
     117      default:
     118        // Put default auto code here
     119        break;
     120    }
     121  }
     122
     123  /**
     124   * This function is called periodically during operator control.
     125   */
     126  @Override
     127  public void teleopPeriodic() {
     128
     129    // Use front-mounted ultrasonic sensor to stop robot
     130    // if it gets too close to an obstacle
     131    double f_range = f_ultrasonic.getRangeInches();
     132    if (f_ultrasonic.isRangeValid()) {
     133       if ((f_range < 15.0) && !f_safetyStop) {
     134          System.out.println("Safety stopped due to front obstacle");
     135          f_safetyStop = true;
     136       } else if (f_range >= 15.0 && f_safetyStop) {
     137          System.out.println("Resuming...");
     138          f_safetyStop = false;
     139       }
     140    }
     141
     142    // Use controller joysticks to set drive speed, but
     143    // safety stop if too close to an obstacle
     144    double leftSpeed  = -0.5*xbox.getY(Hand.kLeft);
     145    double rightSpeed = -0.5*xbox.getY(Hand.kRight);
     146    // If there's an obstacle in front of us, don't
     147    // allow any more forward motion
     148    if (f_safetyStop &&
     149        (leftSpeed > 0.0) && (rightSpeed > 0.0)) {
     150       m_drive.stopMotor();
     151    } else {
     152      // otherwise, set motors according to joysticks
     153       m_drive.tankDrive(leftSpeed, rightSpeed);
     154    }
     155    Timer.delay(0.01);
     156  }
     157
     158  /**
     159   * This function is called periodically during test mode.
     160   */
     161  @Override
     162  public void testPeriodic() {
     163    // LiveWindow.run();
     164  }
     165}
     166
     167}}}