Changes between Initial Version and Version 1 of ControlSystems/SampleCode/Ultrasonic


Ignore:
Timestamp:
Oct 1, 2019, 5:45:02 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SampleCode/Ultrasonic

    v1 v1  
     1{{{
     2/*----------------------------------------------------------------------------*/
     3/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
     4/* Open Source Software - may be modified and shared by FRC teams. The code   */
     5/* must be accompanied by the FIRST BSD license file in the root directory of */
     6/* the project.                                                               */
     7/*----------------------------------------------------------------------------*/
     8
     9package frc.robot;
     10
     11import edu.wpi.first.wpilibj.TimedRobot;
     12import edu.wpi.first.wpilibj.Ultrasonic;
     13import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
     14import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
     15import edu.wpi.first.wpilibj.Timer;
     16import java.lang.Thread;
     17
     18/**
     19 * The VM is configured to automatically run this class, and to call the
     20 * functions corresponding to each mode, as described in the TimedRobot
     21 * documentation. If you change the name of this class or the package after
     22 * creating this project, you must also update the build.gradle file in the
     23 * project.
     24 */
     25public class Robot extends TimedRobot {
     26  private static final String kDefaultAuto = "Default";
     27  private static final String kCustomAuto = "My Auto";
     28  private String m_autoSelected;
     29  private final SendableChooser<String> m_chooser = new SendableChooser<>();
     30  private Ultrasonic front;
     31  private Ultrasonic rear;
     32
     33  /**
     34   * This function is run when the robot is first started up and should be used
     35   * for any initialization code.
     36   */
     37  @Override
     38  public void robotInit() {
     39    m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
     40    m_chooser.addOption("My Auto", kCustomAuto);
     41    SmartDashboard.putData("Auto choices", m_chooser);
     42
     43    front = new Ultrasonic(8, 9);
     44    rear = new Ultrasonic(0, 1);
     45
     46    // front.setEnabled(true);
     47    // rear.setEnabled(true);
     48    // front.setAutomaticMode(true);
     49    front.setAutomaticMode(true);
     50
     51  }
     52
     53  /**
     54   * This function is called every robot packet, no matter the mode. Use this for
     55   * items like diagnostics that you want ran during disabled, autonomous,
     56   * teleoperated and test.
     57   *
     58   * <p>
     59   * This runs after the mode specific periodic functions, but before LiveWindow
     60   * and SmartDashboard integrated updating.
     61   */
     62  @Override
     63  public void robotPeriodic() {
     64  }
     65
     66  /**
     67   * This autonomous (along with the chooser code above) shows how to select
     68   * between different autonomous modes using the dashboard. The sendable chooser
     69   * code works with the Java SmartDashboard. If you prefer the LabVIEW Dashboard,
     70   * remove all of the chooser code and uncomment the getString line to get the
     71   * auto name from the text box below the Gyro
     72   *
     73   * <p>
     74   * You can add additional auto modes by adding additional comparisons to the
     75   * switch structure below with additional strings. If using the SendableChooser
     76   * make sure to add them to the chooser code above as well.
     77   */
     78  @Override
     79  public void autonomousInit() {
     80    m_autoSelected = m_chooser.getSelected();
     81    // m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
     82    System.out.println("Auto selected: " + m_autoSelected);
     83  }
     84
     85  /**
     86   * This function is called periodically during autonomous.
     87   */
     88  @Override
     89  public void autonomousPeriodic() {
     90    switch (m_autoSelected) {
     91    case kCustomAuto:
     92      // Put custom auto code here
     93      break;
     94    case kDefaultAuto:
     95    default:
     96      // Put default auto code here
     97      break;
     98    }
     99  }
     100
     101  /**
     102   * This function is called periodically during operator control.
     103   */
     104  @Override
     105  public void teleopPeriodic() {
     106    double distance;
     107
     108    distance = front.getRangeInches();
     109    System.out.println(distance);
     110    System.out.println("front");
     111 
     112      distance = rear.getRangeInches();
     113      System.out.println(distance);
     114      System.out.println("rear");
     115  }
     116
     117  /**
     118   * This function is called periodically during test mode.
     119   */
     120  @Override
     121  public void testPeriodic() {
     122  }
     123}
     124
     125}}}