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