Skip to content

Instantly share code, notes, and snippets.

@zavallabots
Last active March 16, 2021 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zavallabots/ff850defe4c7bcc70b3b05cca32ae4c7 to your computer and use it in GitHub Desktop.
Save zavallabots/ff850defe4c7bcc70b3b05cca32ae4c7 to your computer and use it in GitHub Desktop.
NEWTANK
// 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;
/**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
* constants. This class should not be used for any other purpose. All constants should be declared
* globally (i.e. public static). Do not put anything functional in this class.
*
* <p>It is advised to statically import this class (or one of its inner classes) wherever the
* constants are needed, to reduce verbosity.
*/
public final class Constants {
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 kXboxPort = 0;
public static final int kLeft = 1;
public static final int kRight = 5;
}
package frc.robot.subsystems;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.ctre.phoenix.motorcontrol.ControlMode;
import static frc.robot.Constants.*;
/**
* Basic tank drive with WPI-wrapped motor controllers
* see https://docs.ctre-phoenix.com/en/stable/ch15_WPIDrive.html
*/
public class Drive extends SubsystemBase {
WPI_TalonSRX leftTalon1, leftTalon2, rightTalon1, rightTalon2;
SpeedControllerGroup leftGroup, rightGroup;
DifferentialDrive drivetrain;
/** Creates a new Drive. */
public Drive() {
leftTalon1 = new WPI_TalonSRX(Constants.Left_Motor_1);
leftTalon2 = new WPI_TalonSRX(Constants.Left_Motor_2);
rightTalon1 = new WPI_TalonSRX(Constants.Right_Motor_1);
rightTalon2 = new WPI_TalonSRX(Constants.Right_Motor_2);
SpeedControllerGroup leftGroup = new SpeedControllerGroup(leftTalon1,leftTalon2);
SpeedControllerGroup rightGroup = new SpeedControllerGroup(rightTalon1,rightTalon2);
drivetrain = new DifferentialDrive(leftGroup, rightGroup);
drivetrain.setRightSideInverted(true);
}
public void tankDrive(double leftGroup, double rightGroup){
drivetrain.tankDrive(leftGroup, rightGroup);
}
//public void curvatureDrive(double power, double turn) {
// drivetrain.curvatureDrive(power, turn, true);
//}
@Override
public void periodic() {
// This method will be called once per scheduler run
}
}
// 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.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
/**
* 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 {
private Command m_autonomousCommand;
private RobotContainer m_robotContainer;
/**
* 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.
m_robotContainer = new RobotContainer();
}
/**
* 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() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
}
/** 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.
if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {}
@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() {}
}
// 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.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import frc.robot.subsystems.*;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.RunCommand;
import static frc.robot.Constants.*;
/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
* "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
* periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
* subsystems, commands, and button mappings) should be declared here.
*/
public class RobotContainer {
XboxController xbox = new XboxController(kXboxPort);
//XboxController leftStickYController = leftJoystick(kLeft);
//XboxController rightStickYController = rightJoystick(kRight);
//Joystick leftJoystick = new Joystick(kLeft);
//Joystick rightJoystick = new Joystick(kRight);
// The robot's subsystems and commands are defined here...
private final Drive drive = new Drive();
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
// Configure the button bindings
configureButtonBindings();
drive.setDefaultCommand(
new RunCommand(
() -> drive.tankDrive(xbox.getY(GenericHID.Hand.kLeft), xbox.getY(GenericHID.Hand.kRight))
)
)
;
}
/**
* Use this method to define your button->command mappings. Buttons can be
* created by instantiating a {@link GenericHID} or one of its subclasses
* ({@link edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then
* passing it to a {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/
private void configureButtonBindings() {}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
*
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment