Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.usfirst.frc.team2199.robot;
- import edu.wpi.first.wpilibj.IterativeRobot;
- import edu.wpi.first.wpilibj.Joystick;
- import edu.wpi.first.wpilibj.Talon;
- import edu.wpi.first.wpilibj.Timer;
- import edu.wpi.first.wpilibj.RobotDrive;
- import edu.wpi.first.wpilibj.RobotDrive.MotorType;
- import edu.wpi.first.wpilibj.Gyro;
- import edu.wpi.first.wpilibj.AnalogInput;
- //import edu.wpi.first.wpilibj.DriverStationLCD;
- import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
- /**
- * The VM is configured to automatically run this class, and to call the
- * functions corresponding to each mode, as described in the IterativeRobot
- * documentation. If you change the name of this class or the package after
- * creating this project, you must also update the manifest file in the resource
- * directory.
- */
- public class Robot extends IterativeRobot {
- Joystick driveJoystick = new Joystick(0);
- Joystick manipulatorJoystick = new Joystick(1); //May change based on driver preference
- Talon elevatorTalon = new Talon(5);
- Talon labeledElevatorTalon = new Talon(4);
- RobotDrive robotDrive;
- Gyro gyro = new Gyro(0);
- // Channels for the wheels
- final int frontLeftChannel = 1;
- final int rearLeftChannel = 0;
- final int frontRightChannel = 2;
- final int rearRightChannel = 3;
- int autoMode=0;
- //int dStationLed=0;
- Timer timer = new Timer();
- //DriverStationLCD dstation;
- /**
- * This function is run when the robot is first started up and should be
- * used for any initialization code.
- */
- public void robotInit() {
- robotDrive = new RobotDrive(frontLeftChannel, rearLeftChannel, frontRightChannel, rearRightChannel);
- robotDrive.setInvertedMotor(MotorType.kFrontLeft, true); // invert the left side motors
- robotDrive.setInvertedMotor(MotorType.kRearLeft, true);
- // you may need to change or remove this to match your robot
- //robotDrive.setExpiration(0.1);
- gyro.reset();
- //dstation = DriverStationLCD.getInstance();
- }
- /**
- * This function is run when the robot is in a disabled state and is
- * used to initialize autonomous mode.
- */
- public void disabledPeriodic() {
- if (driveJoystick.getRawButton(1) && (autoMode>=-1 && autoMode<=2))
- {
- autoModeIncrease();
- Timer.delay(.25);
- System.out.println("Auto mode is: " + autoMode);
- //dstation.println(DriverStationLCD.Line.kUser1,1,"Auto mode is: " + autoMode);
- }
- else if(driveJoystick.getRawButton(1) &&(autoMode>=3)) {
- autoMode=-1;
- //dStationLed=-1;
- Timer.delay(.25);
- System.out.println("Auto mode is: " + autoMode);
- }
- dStationLed(autoMode);
- }
- /**
- * This function is called periodically during autonomous
- */
- public void autonomousPeriodic() {
- switch(autoMode){
- case 0: //Pick up tote;drive backward
- elevatorUp(1.0);
- Timer.delay(.7);
- elevatorUp(0);
- Timer.delay(0.5);
- timer.start();
- autoMode=7;
- break;
- case 1: //Pick up container;drive backward
- elevatorUp(1.0);
- Timer.delay(1.0);
- elevatorUp(0);
- timer.start();
- autoMode=7;
- break;
- case 2: //Just drive backward
- timer.start();
- autoMode=7;
- break;
- case 3: //Pick up container to place on tote
- elevatorUp(1.0);
- Timer.delay(2.0);
- elevatorUp(0);
- timer.start();
- autoMode=8;
- break;
- case 6: //Logic to drive forward to hook tote; case 6 should not be executed by itself
- while(!timer.hasPeriodPassed(.6)){
- //elevatorDown(.1);
- double angle=gyro.getAngle();
- System.out.println("Gyro angle is " + angle);
- robotDrive.mecanumDrive_Cartesian(0.0,-0.2,0.0,-angle);//.00215 seems to give us the straightest drive so far
- Timer.delay(.01);
- }
- elevatorDown(0);
- timer.stop();
- Timer.delay(.1);
- autoMode=0;
- //autoMode=-1;
- break;
- case 7: //Logic to drive backward; case 7 should not be executed by itself
- while(!timer.hasPeriodPassed(2.0)){
- double angle=gyro.getAngle();
- System.out.println("Gyro angle is " + angle);
- robotDrive.mecanumDrive_Cartesian(0.0,0.35,-0.01,-angle*.00215);//.00215 seems to give us the straightest drive so far
- Timer.delay(.01);
- }
- timer.stop();
- autoMode=-1;
- break;
- case 8: //Logic to drive sideways left; case 8 should not be executed by itself
- while(!timer.hasPeriodPassed(.655)){
- double angle=gyro.getAngle();
- System.out.println("Gyro angle is " + angle);
- robotDrive.mecanumDrive_Cartesian(-0.6,0.05,-0.15,-angle*.00215);//.00215 seems to give us the straightest drive so far
- //robotDrive.mecanumDrive_Polar(-0.4, -270.0, -.02);
- Timer.delay(.05);
- }
- timer.stop();
- autoMode=9;
- //autoMode=-1;
- break;
- case 9: //Put down container on tote and prepare to pick up tote and move backward; case 9 should not be executed by itself
- elevatorDown(.5);
- Timer.delay(1.0);
- elevatorDown(.8);
- Timer.delay(1.95);
- elevatorDown(0);
- Timer.delay(1.0);
- timer.start();
- autoMode=6;
- break;
- default: //Do nothing
- robotDrive.stopMotor();
- }
- }
- public void autonomousInit() {
- gyro.reset();
- }
- public void teleopInit() {
- gyro.reset();
- }
- /**
- * This function is called periodically during operator control
- */
- public void teleopPeriodic() {
- drive(driveJoystick);
- if (manipulatorJoystick.getRawAxis(2) >0){
- elevatorDown(manipulatorJoystick.getRawAxis(2));
- }
- else if (manipulatorJoystick.getRawAxis(3) >0){
- elevatorUp(manipulatorJoystick.getRawAxis(3));
- } else {
- elevatorStop();
- }
- }
- /**
- * This function is called periodically during test mode
- */
- public void testPeriodic() {
- }
- private void drive(Joystick driveJoystick) {
- // Use the joystick X axis for lateral movement, Y axis for forward movement, and Z axis for rotation.
- // This sample does not use field-oriented drive, so the gyro input is set to zero.
- if ((driveJoystick.getX() > 0.1 || driveJoystick.getX() <=-.1) || (driveJoystick.getY() >0.1 || driveJoystick.getY() <=-.1)||(driveJoystick.getZ() >.1 ||driveJoystick.getZ() <=-.1)){
- robotDrive.mecanumDrive_Cartesian(driveJoystick.getX(), driveJoystick.getY(), driveJoystick.getZ(), 0);
- } else{
- //robotDrive.mecanumDrive_Cartesian(0,0,0,0);
- robotDrive.stopMotor();
- }
- Timer.delay(0.005); // wait 5ms to avoid hogging CPU cycles
- }
- private void autoDrive(double speed) {
- robotDrive.mecanumDrive_Polar(speed*-1,0.0,0.0);
- //Timer.delay(time);
- //robotDrive.stopMotor();
- }
- private void elevatorUp(double speed) {
- double realSpeed=speed*-1;
- elevatorTalon.set(realSpeed);
- labeledElevatorTalon.set(realSpeed);
- System.out.println("Executing elevatorUp with " + realSpeed);
- }
- private void elevatorDown(double speed) {
- elevatorTalon.set(speed);
- labeledElevatorTalon.set(speed);
- System.out.println("Executing elevatorDown with " + speed);
- }
- private void elevatorStop(){
- elevatorTalon.set(0);
- labeledElevatorTalon.set(0);
- }
- private void autoModeIncrease() {
- autoMode++;
- //dStationLed++;
- }
- private void dStationLed(int dStationLed){
- if( dStationLed==0){
- SmartDashboard.putBoolean("DB/LED 0", true);
- SmartDashboard.putBoolean("DB/LED 1", false);
- SmartDashboard.putBoolean("DB/LED 2", false);
- SmartDashboard.putBoolean("DB/LED 3", false);
- }
- else if(dStationLed==1){
- SmartDashboard.putBoolean("DB/LED 0", false);
- SmartDashboard.putBoolean("DB/LED 1", true);
- SmartDashboard.putBoolean("DB/LED 2", false);
- SmartDashboard.putBoolean("DB/LED 3", false);
- } else if(dStationLed==2){
- SmartDashboard.putBoolean("DB/LED 0", false);
- SmartDashboard.putBoolean("DB/LED 1", false);
- SmartDashboard.putBoolean("DB/LED 2", true);
- SmartDashboard.putBoolean("DB/LED 3", false);
- } else if(dStationLed==3){
- SmartDashboard.putBoolean("DB/LED 0", false);
- SmartDashboard.putBoolean("DB/LED 1", false);
- SmartDashboard.putBoolean("DB/LED 2", false);
- SmartDashboard.putBoolean("DB/LED 3", true);
- } else if(dStationLed>3 ||dStationLed<0){
- SmartDashboard.putBoolean("DB/LED 0", false);
- SmartDashboard.putBoolean("DB/LED 1", false);
- SmartDashboard.putBoolean("DB/LED 2", false);
- SmartDashboard.putBoolean("DB/LED 3", false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement