Skip to content

Instantly share code, notes, and snippets.

@zavallabots

zavallabots/Main.java

Last active Mar 4, 2021
Embed
What would you like to do?
So close.... I think?
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot.commands;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.Robot;
import frc.robot.RobotMap;
import frc.robot.subsystems.DriveTrain;
/** Add your docs here. */
public class TankDrive extends CommandBase {
public TankDrive() {
addRequirements(Robot.driveTrain);
}
private void addRequirements(DriveTrain driveTrain) {
}
@Override
public void initialize() {
}
@Override
public void execute() {
double leftStickY = Robot.oi.GetDriverRawAxis(RobotMap.LEFT_STICK_Y);
double rightStickY = Robot.oi.GetDriverRawAxis(RobotMap.RIGHT_STICK_Y);
Robot.driveTrain.setLeftMotors(leftStickY);
Robot.driveTrain.setRightMotors(rightStickY);
}
@Override
public boolean isFinished() {
return false;
}
}
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.RobotBase;
/**
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
* you are doing, do not modify this file except to change the parameter class to the startRobot
* call.
*/
public final class Main {
private Main() {}
/**
* Main initialization function. Do not perform any initialization here.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.Joystick;
/** Add your docs here. */
public class OI {
private Joystick driverController = new Joystick(RobotMap.DRIVER_CONTROLLER);
public double GetDriverRawAxis(int axis){
return driverController.getRawAxis(axis);
}}
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import frc.robot.subsystems.DriveTrain;
/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
* each mode, as described in the TimedRobot documentation. If you change the name of this class or
* the package after creating this project, you must also update the build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
public static DriveTrain driveTrain = new DriveTrain();
public static OI oi;
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
@Override
public void robotInit() {
// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
oi = new OI();
}
/**
* This function is called every robot packet, no matter the mode. Use this for items like
* diagnostics that you want ran during disabled, autonomous, teleoperated and test.
*
* <p>This runs after the mode specific periodic functions, but before LiveWindow and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
// Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
// commands, running already-scheduled commands, removing finished or interrupted commands,
// and running subsystem periodic() methods. This must be called from the robot's periodic
// block in order for anything in the Command-based framework to work.
CommandScheduler.getInstance().run();
}
/** This function is called once each time the robot enters Disabled mode. */
@Override
public void disabledInit() {}
@Override
public void disabledPeriodic() {}
/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
@Override
public void autonomousInit() {
// schedule the autonomous command (example)
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {}
@Override
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {
Scheduler.getInstance().run();
}
@Override
public void testInit() {
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {}
public static void tankDrive(double rawAxis) {
}
}
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
public class RobotMap {
public static final int Left_Motor_1 = 0;
public static final int Left_Motor_2 = 1;
public static final int Right_Motor_1 = 2;
public static final int Right_Motor_2 = 3;
public static final int DRIVER_CONTROLLER = 0;
public static final int LEFT_STICK_Y = 1;
public static final int RIGHT_STICK_Y = 5;}
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot.subsystems;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Robot;
import frc.robot.RobotMap;
import frc.robot.commands.TankDrive;
public class DriveTrain extends SubsystemBase {
private static final int LEFT_STICK_Y = 1;
private static final int RIGHT_STICK_Y = 5;
private TalonSRX motorLeft1 = new TalonSRX(RobotMap.Left_Motor_1);
private TalonSRX motorLeft2 = new TalonSRX(RobotMap.Left_Motor_2);
private TalonSRX motorRight1 = new TalonSRX(RobotMap.Right_Motor_1);
private TalonSRX motorRight2 = new TalonSRX(RobotMap.Right_Motor_2);
public void initDefaultCommand() {
setDefaultCommand(new TankDrive());
}
public void setLeftMotors(double speed) {
motorLeft1.set(ControlMode.PercentOutput, -speed);
motorLeft2.set(ControlMode.PercentOutput, -speed);
}
public void setRightMotors(double speed) {
motorRight1.set(ControlMode.PercentOutput, speed);
motorRight2.set(ControlMode.PercentOutput, speed);
}
public void driveWithJoysticks(Joystick controller, double speed) {
Robot.tankDrive(controller.getRawAxis(LEFT_STICK_Y));
Robot.tankDrive(controller.getRawAxis(RIGHT_STICK_Y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment