mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
SCRIPT Move java files
This commit is contained in:
committed by
Peter Johnson
parent
7ca1be9bae
commit
c350c5f112
@@ -0,0 +1,15 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference;
|
||||
|
||||
/**
|
||||
* 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 {}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference;
|
||||
|
||||
import edu.wpi.first.wpilibj.TimedRobot;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
import edu.wpi.first.wpilibj2.command.CommandScheduler;
|
||||
|
||||
/**
|
||||
* The methods in this class are called automatically 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 Main.java file in the project.
|
||||
*/
|
||||
public class Robot extends TimedRobot {
|
||||
private Command m_autonomousCommand;
|
||||
private final RobotContainer m_robotContainer;
|
||||
|
||||
/**
|
||||
* This function is run when the robot is first started up and should be used for any
|
||||
* initialization code.
|
||||
*/
|
||||
public Robot() {
|
||||
// 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 20 ms, 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() {
|
||||
// Get selected routine from the SmartDashboard
|
||||
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
|
||||
|
||||
// schedule the autonomous command (example)
|
||||
if (m_autonomousCommand != null) {
|
||||
CommandScheduler.getInstance().schedule(m_autonomousCommand);
|
||||
}
|
||||
}
|
||||
|
||||
/** This function is called periodically during autonomous. */
|
||||
@Override
|
||||
public void autonomousPeriodic() {}
|
||||
|
||||
@Override
|
||||
public void teleopInit() {
|
||||
// This makes sure that the autonomous stops running which will
|
||||
// use the default command which is ArcadeDrive. 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() {}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference;
|
||||
|
||||
import edu.wpi.first.wpilibj.GenericHID;
|
||||
import edu.wpi.first.wpilibj.Joystick;
|
||||
import edu.wpi.first.wpilibj.XboxController;
|
||||
import edu.wpi.first.wpilibj.examples.romireference.commands.ArcadeDrive;
|
||||
import edu.wpi.first.wpilibj.examples.romireference.commands.AutonomousDistance;
|
||||
import edu.wpi.first.wpilibj.examples.romireference.commands.AutonomousTime;
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj.romi.OnBoardIO;
|
||||
import edu.wpi.first.wpilibj.romi.OnBoardIO.ChannelMode;
|
||||
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
|
||||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
import edu.wpi.first.wpilibj2.command.PrintCommand;
|
||||
import edu.wpi.first.wpilibj2.command.button.Trigger;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
// The robot's subsystems and commands are defined here...
|
||||
private final Drivetrain m_drivetrain = new Drivetrain();
|
||||
private final OnBoardIO m_onboardIO = new OnBoardIO(ChannelMode.INPUT, ChannelMode.INPUT);
|
||||
|
||||
// Assumes a gamepad plugged into channel 0
|
||||
private final Joystick m_controller = new Joystick(0);
|
||||
|
||||
// Create SmartDashboard chooser for autonomous routines
|
||||
private final SendableChooser<Command> m_chooser = new SendableChooser<>();
|
||||
|
||||
// NOTE: The I/O pin functionality of the 5 exposed I/O pins depends on the hardware "overlay"
|
||||
// that is specified when launching the wpilib-ws server on the Romi raspberry pi.
|
||||
// By default, the following are available (listed in order from inside of the board to outside):
|
||||
// - DIO 8 (mapped to Arduino pin 11, closest to the inside of the board)
|
||||
// - Analog In 0 (mapped to Analog Channel 6 / Arduino Pin 4)
|
||||
// - Analog In 1 (mapped to Analog Channel 2 / Arduino Pin 20)
|
||||
// - PWM 2 (mapped to Arduino Pin 21)
|
||||
// - PWM 3 (mapped to Arduino Pin 22)
|
||||
//
|
||||
// Your subsystem configuration should take the overlays into account
|
||||
|
||||
/** The container for the robot. Contains subsystems, OI devices, and commands. */
|
||||
public RobotContainer() {
|
||||
// Configure the button bindings
|
||||
configureButtonBindings();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
// Default command is arcade drive. This will run unless another command
|
||||
// is scheduled over it.
|
||||
m_drivetrain.setDefaultCommand(getArcadeDriveCommand());
|
||||
|
||||
// Example of how to use the onboard IO
|
||||
Trigger onboardButtonA = new Trigger(m_onboardIO::getButtonAPressed);
|
||||
onboardButtonA
|
||||
.onTrue(new PrintCommand("Button A Pressed"))
|
||||
.onFalse(new PrintCommand("Button A Released"));
|
||||
|
||||
// Setup SmartDashboard options
|
||||
m_chooser.setDefaultOption("Auto Routine Distance", new AutonomousDistance(m_drivetrain));
|
||||
m_chooser.addOption("Auto Routine Time", new AutonomousTime(m_drivetrain));
|
||||
SmartDashboard.putData(m_chooser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to pass the autonomous command to the main {@link Robot} class.
|
||||
*
|
||||
* @return the command to run in autonomous
|
||||
*/
|
||||
public Command getAutonomousCommand() {
|
||||
return m_chooser.getSelected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to pass the teleop command to the main {@link Robot} class.
|
||||
*
|
||||
* @return the command to run in teleop
|
||||
*/
|
||||
public Command getArcadeDriveCommand() {
|
||||
return new ArcadeDrive(
|
||||
m_drivetrain, () -> -m_controller.getRawAxis(1), () -> -m_controller.getRawAxis(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ArcadeDrive extends Command {
|
||||
private final Drivetrain m_drivetrain;
|
||||
private final Supplier<Double> m_xaxisSpeedSupplier;
|
||||
private final Supplier<Double> m_zaxisRotateSupplier;
|
||||
|
||||
/**
|
||||
* Creates a new ArcadeDrive. This command will drive your robot according to the speed supplier
|
||||
* lambdas. This command does not terminate.
|
||||
*
|
||||
* @param drivetrain The drivetrain subsystem on which this command will run
|
||||
* @param xaxisSpeedSupplier Lambda supplier of forward/backward speed
|
||||
* @param zaxisRotateSupplier Lambda supplier of rotational speed
|
||||
*/
|
||||
public ArcadeDrive(
|
||||
Drivetrain drivetrain,
|
||||
Supplier<Double> xaxisSpeedSupplier,
|
||||
Supplier<Double> zaxisRotateSupplier) {
|
||||
m_drivetrain = drivetrain;
|
||||
m_xaxisSpeedSupplier = xaxisSpeedSupplier;
|
||||
m_zaxisRotateSupplier = zaxisRotateSupplier;
|
||||
addRequirements(drivetrain);
|
||||
}
|
||||
|
||||
// Called when the command is initially scheduled.
|
||||
@Override
|
||||
public void initialize() {}
|
||||
|
||||
// Called every time the scheduler runs while the command is scheduled.
|
||||
@Override
|
||||
public void execute() {
|
||||
m_drivetrain.arcadeDrive(m_xaxisSpeedSupplier.get(), m_zaxisRotateSupplier.get());
|
||||
}
|
||||
|
||||
// Called once the command ends or is interrupted.
|
||||
@Override
|
||||
public void end(boolean interrupted) {}
|
||||
|
||||
// Returns true when the command should end.
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
|
||||
|
||||
public class AutonomousDistance extends SequentialCommandGroup {
|
||||
/**
|
||||
* Creates a new Autonomous Drive based on distance. This will drive out for a specified distance,
|
||||
* turn around and drive back.
|
||||
*
|
||||
* @param drivetrain The drivetrain subsystem on which this command will run
|
||||
*/
|
||||
public AutonomousDistance(Drivetrain drivetrain) {
|
||||
addCommands(
|
||||
new DriveDistance(-0.5, 10, drivetrain),
|
||||
new TurnDegrees(-0.5, 180, drivetrain),
|
||||
new DriveDistance(-0.5, 10, drivetrain),
|
||||
new TurnDegrees(0.5, 180, drivetrain));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
|
||||
|
||||
public class AutonomousTime extends SequentialCommandGroup {
|
||||
/**
|
||||
* Creates a new Autonomous Drive based on time. This will drive out for a period of time, turn
|
||||
* around for time (equivalent to time to turn around) and drive forward again. This should mimic
|
||||
* driving out, turning around and driving back.
|
||||
*
|
||||
* @param drivetrain The drive subsystem on which this command will run
|
||||
*/
|
||||
public AutonomousTime(Drivetrain drivetrain) {
|
||||
addCommands(
|
||||
new DriveTime(-0.6, 2.0, drivetrain),
|
||||
new TurnTime(-0.5, 1.3, drivetrain),
|
||||
new DriveTime(-0.6, 2.0, drivetrain),
|
||||
new TurnTime(0.5, 1.3, drivetrain));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
|
||||
public class DriveDistance extends Command {
|
||||
private final Drivetrain m_drive;
|
||||
private final double m_distance;
|
||||
private final double m_speed;
|
||||
|
||||
/**
|
||||
* Creates a new DriveDistance. This command will drive your your robot for a desired distance at
|
||||
* a desired speed.
|
||||
*
|
||||
* @param speed The speed at which the robot will drive
|
||||
* @param inches The number of inches the robot will drive
|
||||
* @param drive The drivetrain subsystem on which this command will run
|
||||
*/
|
||||
public DriveDistance(double speed, double inches, Drivetrain drive) {
|
||||
m_distance = inches;
|
||||
m_speed = speed;
|
||||
m_drive = drive;
|
||||
addRequirements(drive);
|
||||
}
|
||||
|
||||
// Called when the command is initially scheduled.
|
||||
@Override
|
||||
public void initialize() {
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
m_drive.resetEncoders();
|
||||
}
|
||||
|
||||
// Called every time the scheduler runs while the command is scheduled.
|
||||
@Override
|
||||
public void execute() {
|
||||
m_drive.arcadeDrive(m_speed, 0);
|
||||
}
|
||||
|
||||
// Called once the command ends or is interrupted.
|
||||
@Override
|
||||
public void end(boolean interrupted) {
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Returns true when the command should end.
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
// Compare distance travelled from start to desired distance
|
||||
return Math.abs(m_drive.getAverageDistanceInch()) >= m_distance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
|
||||
public class DriveTime extends Command {
|
||||
private final double m_duration;
|
||||
private final double m_speed;
|
||||
private final Drivetrain m_drive;
|
||||
private long m_startTime;
|
||||
|
||||
/**
|
||||
* Creates a new DriveTime. This command will drive your robot for a desired speed and time.
|
||||
*
|
||||
* @param speed The speed which the robot will drive. Negative is in reverse.
|
||||
* @param time How much time to drive in seconds
|
||||
* @param drive The drivetrain subsystem on which this command will run
|
||||
*/
|
||||
public DriveTime(double speed, double time, Drivetrain drive) {
|
||||
m_speed = speed;
|
||||
m_duration = time * 1000;
|
||||
m_drive = drive;
|
||||
addRequirements(drive);
|
||||
}
|
||||
|
||||
// Called when the command is initially scheduled.
|
||||
@Override
|
||||
public void initialize() {
|
||||
m_startTime = System.currentTimeMillis();
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Called every time the scheduler runs while the command is scheduled.
|
||||
@Override
|
||||
public void execute() {
|
||||
m_drive.arcadeDrive(m_speed, 0);
|
||||
}
|
||||
|
||||
// Called once the command ends or is interrupted.
|
||||
@Override
|
||||
public void end(boolean interrupted) {
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Returns true when the command should end.
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return (System.currentTimeMillis() - m_startTime) >= m_duration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
|
||||
public class TurnDegrees extends Command {
|
||||
private final Drivetrain m_drive;
|
||||
private final double m_degrees;
|
||||
private final double m_speed;
|
||||
|
||||
/**
|
||||
* Creates a new TurnDegrees. This command will turn your robot for a desired rotation (in
|
||||
* degrees) and rotational speed.
|
||||
*
|
||||
* @param speed The speed which the robot will drive. Negative is in reverse.
|
||||
* @param degrees Degrees to turn. Leverages encoders to compare distance.
|
||||
* @param drive The drive subsystem on which this command will run
|
||||
*/
|
||||
public TurnDegrees(double speed, double degrees, Drivetrain drive) {
|
||||
m_degrees = degrees;
|
||||
m_speed = speed;
|
||||
m_drive = drive;
|
||||
addRequirements(drive);
|
||||
}
|
||||
|
||||
// Called when the command is initially scheduled.
|
||||
@Override
|
||||
public void initialize() {
|
||||
// Set motors to stop, read encoder values for starting point
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
m_drive.resetEncoders();
|
||||
}
|
||||
|
||||
// Called every time the scheduler runs while the command is scheduled.
|
||||
@Override
|
||||
public void execute() {
|
||||
m_drive.arcadeDrive(0, m_speed);
|
||||
}
|
||||
|
||||
// Called once the command ends or is interrupted.
|
||||
@Override
|
||||
public void end(boolean interrupted) {
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Returns true when the command should end.
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
/* Need to convert distance travelled to degrees. The Standard
|
||||
Romi Chassis found here, https://www.pololu.com/category/203/romi-chassis-kits,
|
||||
has a wheel placement diameter (149 mm) - width of the wheel (8 mm) = 141 mm
|
||||
or 5.551 inches. We then take into consideration the width of the tires.
|
||||
*/
|
||||
double inchPerDegree = Math.PI * 5.551 / 360;
|
||||
// Compare distance travelled from start to distance based on degree turn
|
||||
return getAverageTurningDistance() >= (inchPerDegree * m_degrees);
|
||||
}
|
||||
|
||||
private double getAverageTurningDistance() {
|
||||
double leftDistance = Math.abs(m_drive.getLeftDistanceInch());
|
||||
double rightDistance = Math.abs(m_drive.getRightDistanceInch());
|
||||
return (leftDistance + rightDistance) / 2.0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj.examples.romireference.subsystems.Drivetrain;
|
||||
import edu.wpi.first.wpilibj2.command.Command;
|
||||
|
||||
/*
|
||||
* Creates a new TurnTime command. This command will turn your robot for a
|
||||
* desired rotational speed and time.
|
||||
*/
|
||||
public class TurnTime extends Command {
|
||||
private final double m_duration;
|
||||
private final double m_rotationalSpeed;
|
||||
private final Drivetrain m_drive;
|
||||
private long m_startTime;
|
||||
|
||||
/**
|
||||
* Creates a new TurnTime.
|
||||
*
|
||||
* @param speed The speed which the robot will turn. Negative is in reverse.
|
||||
* @param time How much time to turn in seconds
|
||||
* @param drive The drive subsystem on which this command will run
|
||||
*/
|
||||
public TurnTime(double speed, double time, Drivetrain drive) {
|
||||
m_rotationalSpeed = speed;
|
||||
m_duration = time * 1000;
|
||||
m_drive = drive;
|
||||
addRequirements(drive);
|
||||
}
|
||||
|
||||
// Called when the command is initially scheduled.
|
||||
@Override
|
||||
public void initialize() {
|
||||
m_startTime = System.currentTimeMillis();
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Called every time the scheduler runs while the command is scheduled.
|
||||
@Override
|
||||
public void execute() {
|
||||
m_drive.arcadeDrive(0, m_rotationalSpeed);
|
||||
}
|
||||
|
||||
// Called once the command ends or is interrupted.
|
||||
@Override
|
||||
public void end(boolean interrupted) {
|
||||
m_drive.arcadeDrive(0, 0);
|
||||
}
|
||||
|
||||
// Returns true when the command should end.
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return (System.currentTimeMillis() - m_startTime) >= m_duration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.romireference.subsystems;
|
||||
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
import edu.wpi.first.wpilibj.Encoder;
|
||||
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.Spark;
|
||||
import edu.wpi.first.wpilibj.romi.RomiGyro;
|
||||
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
||||
|
||||
public class Drivetrain extends SubsystemBase {
|
||||
private static final double kCountsPerRevolution = 1440.0;
|
||||
private static final double kWheelDiameterInch = 2.75591; // 70 mm
|
||||
|
||||
// The Romi has the left and right motors set to
|
||||
// PWM channels 0 and 1 respectively
|
||||
private final Spark m_leftMotor = new Spark(0);
|
||||
private final Spark m_rightMotor = new Spark(1);
|
||||
|
||||
// The Romi has onboard encoders that are hardcoded
|
||||
// to use DIO pins 4/5 and 6/7 for the left and right
|
||||
private final Encoder m_leftEncoder = new Encoder(4, 5);
|
||||
private final Encoder m_rightEncoder = new Encoder(6, 7);
|
||||
|
||||
// Set up the differential drive controller
|
||||
private final DifferentialDrive m_diffDrive =
|
||||
new DifferentialDrive(m_leftMotor::set, m_rightMotor::set);
|
||||
|
||||
// Set up the RomiGyro
|
||||
private final RomiGyro m_gyro = new RomiGyro();
|
||||
|
||||
/** Creates a new Drivetrain. */
|
||||
public Drivetrain() {
|
||||
SendableRegistry.addChild(m_diffDrive, m_leftMotor);
|
||||
SendableRegistry.addChild(m_diffDrive, m_rightMotor);
|
||||
|
||||
// We need to invert one side of the drivetrain so that positive voltages
|
||||
// result in both sides moving forward. Depending on how your robot's
|
||||
// gearbox is constructed, you might have to invert the left side instead.
|
||||
m_rightMotor.setInverted(true);
|
||||
|
||||
// Use inches as unit for encoder distances
|
||||
m_leftEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);
|
||||
m_rightEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);
|
||||
resetEncoders();
|
||||
}
|
||||
|
||||
public void arcadeDrive(double xaxisSpeed, double zaxisRotate) {
|
||||
m_diffDrive.arcadeDrive(xaxisSpeed, zaxisRotate);
|
||||
}
|
||||
|
||||
public void resetEncoders() {
|
||||
m_leftEncoder.reset();
|
||||
m_rightEncoder.reset();
|
||||
}
|
||||
|
||||
public int getLeftEncoderCount() {
|
||||
return m_leftEncoder.get();
|
||||
}
|
||||
|
||||
public int getRightEncoderCount() {
|
||||
return m_rightEncoder.get();
|
||||
}
|
||||
|
||||
public double getLeftDistanceInch() {
|
||||
return m_leftEncoder.getDistance();
|
||||
}
|
||||
|
||||
public double getRightDistanceInch() {
|
||||
return m_rightEncoder.getDistance();
|
||||
}
|
||||
|
||||
public double getAverageDistanceInch() {
|
||||
return (getLeftDistanceInch() + getRightDistanceInch()) / 2.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current angle of the Romi around the X-axis.
|
||||
*
|
||||
* @return The current angle of the Romi in degrees
|
||||
*/
|
||||
public double getGyroAngleX() {
|
||||
return m_gyro.getAngleX();
|
||||
}
|
||||
|
||||
/**
|
||||
* Current angle of the Romi around the Y-axis.
|
||||
*
|
||||
* @return The current angle of the Romi in degrees
|
||||
*/
|
||||
public double getGyroAngleY() {
|
||||
return m_gyro.getAngleY();
|
||||
}
|
||||
|
||||
/**
|
||||
* Current angle of the Romi around the Z-axis.
|
||||
*
|
||||
* @return The current angle of the Romi in degrees
|
||||
*/
|
||||
public double getGyroAngleZ() {
|
||||
return m_gyro.getAngleZ();
|
||||
}
|
||||
|
||||
/** Reset the gyro. */
|
||||
public void resetGyro() {
|
||||
m_gyro.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void periodic() {
|
||||
// This method will be called once per scheduler run
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user