2020-02-19 08:46:55 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2020-03-02 21:45:38 -07:00
|
|
|
package frc4388.robot.commands.drive;
|
2020-03-02 16:57:16 -07:00
|
|
|
|
2020-02-19 08:46:55 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.CommandBase;
|
|
|
|
|
import frc4388.robot.Constants.DriveConstants;
|
|
|
|
|
import frc4388.robot.subsystems.Drive;
|
2020-03-02 16:57:16 -07:00
|
|
|
import frc4388.robot.subsystems.Pneumatics;
|
2020-02-19 08:46:55 -07:00
|
|
|
import frc4388.utility.controller.IHandController;
|
|
|
|
|
|
|
|
|
|
public class DriveWithJoystickDriveStraight extends CommandBase {
|
|
|
|
|
Drive m_drive;
|
2020-03-02 16:57:16 -07:00
|
|
|
Pneumatics m_pneumatics;
|
2020-02-19 08:46:55 -07:00
|
|
|
double m_targetGyro, m_currentGyro;
|
|
|
|
|
double m_stopPos;
|
|
|
|
|
long m_currTime, m_deltaTime;
|
|
|
|
|
long m_deadTimeSteer, m_deadTimeMove;
|
|
|
|
|
long m_deadTimeout = 100;
|
|
|
|
|
IHandController m_controller;
|
2020-03-01 15:34:40 -07:00
|
|
|
boolean m_isInterrupted;
|
2020-03-02 16:57:16 -07:00
|
|
|
double highGearMultiplier = 1;
|
|
|
|
|
double lowGearMultiplier = 1;
|
2020-02-19 08:46:55 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new DriveWithJoystickDriveStraight to control the drivetrain with an Xbox controller.
|
|
|
|
|
* Applies a curved ramp to the input from the controllers to make the robot less "touchy".
|
|
|
|
|
* Also uses PIDs to keep the robot on course when given a "dead" or 0 input.
|
|
|
|
|
* @param subsystem pass the Drive subsystem from {@link frc4388.robot.RobotContainer#RobotContainer() RobotContainer}
|
|
|
|
|
* @param controller pass the Driver {@link frc4388.utility.controller.IHandController#getClass() IHandController} using the
|
|
|
|
|
* {@link frc4388.robot.RobotContainer#getDriverJoystick() getDriverJoystick()} method in
|
|
|
|
|
* {@link frc4388.robot.RobotContainer#RobotContainer() RobotContainer}
|
|
|
|
|
*/
|
|
|
|
|
public DriveWithJoystickDriveStraight(Drive subsystem, IHandController controller) {
|
|
|
|
|
// Use addRequirements() here to declare subsystem dependencies.
|
|
|
|
|
m_drive = subsystem;
|
2020-03-02 16:57:16 -07:00
|
|
|
m_pneumatics = m_drive.m_pneumaticsSubsystem;
|
2020-02-19 08:46:55 -07:00
|
|
|
m_controller = controller;
|
|
|
|
|
addRequirements(m_drive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
|
|
|
|
m_currTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called every time the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
2020-02-19 19:15:45 -07:00
|
|
|
m_currentGyro = m_drive.m_rightFrontMotor.getSelectedSensorPosition(1);
|
2020-02-19 08:46:55 -07:00
|
|
|
double moveInput = -m_controller.getLeftYAxis();
|
|
|
|
|
double steerInput = m_controller.getRightXAxis();
|
|
|
|
|
double moveOutput = 0;
|
|
|
|
|
m_deltaTime = System.currentTimeMillis() - m_currTime;
|
|
|
|
|
m_currTime = System.currentTimeMillis();
|
|
|
|
|
|
2020-03-01 15:34:40 -07:00
|
|
|
if (m_isInterrupted) {
|
|
|
|
|
resetGyroTarget();
|
|
|
|
|
m_isInterrupted = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 08:46:55 -07:00
|
|
|
/* If steer stick is being used */
|
|
|
|
|
if (steerInput != 0) {
|
|
|
|
|
m_deadTimeSteer = m_currTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Curves the moveInput to be slightly more gradual at first */
|
|
|
|
|
if (moveInput >= 0) {
|
|
|
|
|
moveOutput = -Math.cos(1.571*moveInput)+1;
|
|
|
|
|
} else {
|
|
|
|
|
moveOutput = Math.cos(1.571*moveInput)-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If steer stick has not been used for less than 1 sec */
|
|
|
|
|
if (m_currTime - m_deadTimeSteer < m_deadTimeout) {
|
|
|
|
|
runDriveWithInput(moveOutput, steerInput);
|
|
|
|
|
resetGyroTarget();
|
|
|
|
|
}
|
|
|
|
|
/* If steer stick has not been used for 1 sec */
|
|
|
|
|
else {
|
|
|
|
|
runDriveStraight(moveOutput);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void runDriveWithInput(double move, double steer) {
|
2020-03-02 16:57:16 -07:00
|
|
|
|
2020-03-02 17:12:20 -07:00
|
|
|
double cosMultiplier;
|
2020-02-19 08:46:55 -07:00
|
|
|
double steerOutput = 0;
|
2020-03-01 15:34:40 -07:00
|
|
|
double deadzone = .1;
|
2020-03-02 16:57:16 -07:00
|
|
|
|
|
|
|
|
if (m_pneumatics.m_isSpeedShiftHigh) {
|
2020-03-02 17:34:10 -07:00
|
|
|
cosMultiplier = DriveConstants.COS_MULTIPLIER_HIGH;
|
2020-03-02 16:57:16 -07:00
|
|
|
} else {
|
2020-03-02 17:34:10 -07:00
|
|
|
cosMultiplier = DriveConstants.COS_MULTIPLIER_LOW;
|
2020-03-02 16:57:16 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 08:46:55 -07:00
|
|
|
/* Curves the steer output to be similarily gradual */
|
2020-03-01 15:34:40 -07:00
|
|
|
if (steer > 0) {
|
|
|
|
|
steerOutput = -(cosMultiplier - deadzone) * Math.cos(1.571*steer) + cosMultiplier;
|
|
|
|
|
} else if (steer < 0) {
|
|
|
|
|
steerOutput = (cosMultiplier - deadzone) * Math.cos(1.571*steer) - cosMultiplier;
|
2020-02-19 08:46:55 -07:00
|
|
|
}
|
|
|
|
|
m_drive.driveWithInput(move, steerOutput);
|
|
|
|
|
System.out.println("Driving With Input");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void runDriveStraight(double move) {
|
|
|
|
|
m_drive.driveWithInputAux(move * 3/4, m_targetGyro);
|
|
|
|
|
System.out.println("Driving Straight with Target: " + m_targetGyro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set target angle to current angle (prevents buildup of gyro error).
|
|
|
|
|
*/
|
|
|
|
|
private void resetGyroTarget() {
|
|
|
|
|
//m_targetGyro = m_currentGyro;
|
2020-03-02 16:57:16 -07:00
|
|
|
if (m_pneumatics.m_isSpeedShiftHigh) {
|
|
|
|
|
m_targetGyro = m_currentGyro
|
|
|
|
|
+ highGearMultiplier * m_drive.getTurnRate();
|
|
|
|
|
} else {
|
|
|
|
|
m_targetGyro = m_currentGyro
|
|
|
|
|
+ lowGearMultiplier * m_drive.getTurnRate();
|
|
|
|
|
}
|
2020-02-19 08:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called once the command ends or is interrupted.
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interrupted) {
|
2020-03-01 15:34:40 -07:00
|
|
|
m_isInterrupted = interrupted;
|
2020-02-19 08:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true when the command should end.
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|