2020-02-12 20:30:44 -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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
package frc4388.robot.commands;
|
|
|
|
|
|
2020-02-21 19:44:51 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2020-02-12 20:30:44 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.CommandBase;
|
|
|
|
|
import edu.wpi.first.wpiutil.math.MathUtil;
|
|
|
|
|
import frc4388.robot.Constants.DriveConstants;
|
|
|
|
|
import frc4388.robot.subsystems.Drive;
|
|
|
|
|
import frc4388.utility.controller.IHandController;
|
|
|
|
|
|
|
|
|
|
public class DriveWithJoystickUsingDeadAssistPID extends CommandBase {
|
|
|
|
|
Drive m_drive;
|
2020-02-17 13:10:10 -07:00
|
|
|
double m_targetGyro, m_currentGyro;
|
2020-02-18 17:29:26 -07:00
|
|
|
double m_stopPos;
|
2020-02-17 18:25:48 -07:00
|
|
|
long m_currTime, m_deltaTime;
|
|
|
|
|
long m_deadTimeSteer, m_deadTimeMove;
|
2020-02-18 17:29:26 -07:00
|
|
|
long m_deadTimeout = 100;
|
2020-02-12 20:30:44 -07:00
|
|
|
IHandController m_controller;
|
2020-02-21 19:44:51 -07:00
|
|
|
boolean m_isInterrupted;
|
2020-02-12 20:30:44 -07:00
|
|
|
|
|
|
|
|
/**
|
2020-02-12 20:33:16 -07:00
|
|
|
* Creates a new DriveWithJoystickUsingDeadAssistPID 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}
|
2020-02-12 20:30:44 -07:00
|
|
|
*/
|
|
|
|
|
public DriveWithJoystickUsingDeadAssistPID(Drive subsystem, IHandController controller) {
|
2020-02-17 13:55:29 -07:00
|
|
|
// Use addRequirements() here to declare subsystem dependencies.
|
2020-02-12 20:30:44 -07:00
|
|
|
m_drive = subsystem;
|
|
|
|
|
m_controller = controller;
|
|
|
|
|
addRequirements(m_drive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2020-02-17 18:25:48 -07:00
|
|
|
m_currTime = System.currentTimeMillis();
|
2020-02-21 19:44:51 -07:00
|
|
|
resetGyroTarget();
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called every time the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
2020-02-17 13:10:10 -07:00
|
|
|
m_currentGyro = m_drive.m_rightFrontMotor.getSelectedSensorPosition(DriveConstants.PID_TURN);
|
2020-02-13 17:11:54 -07:00
|
|
|
double moveInput = -m_controller.getLeftYAxis();
|
2020-02-12 20:30:44 -07:00
|
|
|
double steerInput = m_controller.getRightXAxis();
|
|
|
|
|
double moveOutput = 0;
|
2020-02-17 18:25:48 -07:00
|
|
|
m_deltaTime = System.currentTimeMillis() - m_currTime;
|
|
|
|
|
m_currTime = System.currentTimeMillis();
|
2020-02-12 20:30:44 -07:00
|
|
|
|
2020-02-21 19:44:51 -07:00
|
|
|
if (m_isInterrupted) {
|
|
|
|
|
resetGyroTarget();
|
|
|
|
|
m_isInterrupted = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:30:44 -07:00
|
|
|
/* If move stick is being used */
|
|
|
|
|
if (moveInput != 0) {
|
2020-02-17 18:25:48 -07:00
|
|
|
m_deadTimeMove = m_currTime;
|
2020-02-18 17:29:26 -07:00
|
|
|
m_stopPos = m_drive.m_rightFrontMotor.getSelectedSensorPosition()
|
|
|
|
|
+ (m_drive.m_rightFrontMotor.getSelectedSensorVelocity());
|
2020-02-17 18:25:48 -07:00
|
|
|
}
|
|
|
|
|
/* If steer stick is being used */
|
|
|
|
|
if (steerInput != 0) {
|
|
|
|
|
m_deadTimeSteer = m_currTime;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:29:26 -07:00
|
|
|
/* If move stick has been pressed within 1 sec */
|
2020-02-17 18:25:48 -07:00
|
|
|
if (m_currTime - m_deadTimeMove < m_deadTimeout) {
|
2020-02-12 20:30:44 -07:00
|
|
|
/* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 18:25:48 -07:00
|
|
|
/* If steer stick has not been used for less than 1 sec */
|
|
|
|
|
if (m_currTime - m_deadTimeSteer < m_deadTimeout) {
|
|
|
|
|
runDriveWithInput(moveOutput, steerInput);
|
2020-02-18 19:44:11 -07:00
|
|
|
resetGyroTarget();
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
2020-02-17 18:25:48 -07:00
|
|
|
/* If steer stick has not been used for 1 sec */
|
2020-02-12 20:30:44 -07:00
|
|
|
else {
|
2020-02-17 18:25:48 -07:00
|
|
|
runDriveStraight(moveOutput);
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-17 18:25:48 -07:00
|
|
|
/* If the move stick has not been used for 1 sec */
|
2020-02-12 20:30:44 -07:00
|
|
|
else {
|
2020-02-17 18:25:48 -07:00
|
|
|
runStoppedTurn(steerInput);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 19:44:51 -07:00
|
|
|
private void runDriveWithInput(double move, double steerInput) {
|
2020-02-21 20:46:52 -07:00
|
|
|
double cosMultiplier = .55;
|
2020-02-17 18:25:48 -07:00
|
|
|
double steerOutput = 0;
|
|
|
|
|
double deadzone = .2;
|
|
|
|
|
/* Curves the steer output to be similarily gradual */
|
2020-02-21 19:44:51 -07:00
|
|
|
if (steerInput > 0){
|
|
|
|
|
steerOutput = -cosMultiplier*Math.cos(1.571*steerInput)+(cosMultiplier+deadzone);
|
|
|
|
|
} else if (steerInput < 0) {
|
|
|
|
|
steerOutput = cosMultiplier*Math.cos(1.571*steerInput)-(cosMultiplier+deadzone);
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
2020-02-21 19:44:51 -07:00
|
|
|
|
2020-02-17 18:25:48 -07:00
|
|
|
m_drive.driveWithInput(move, steerOutput);
|
|
|
|
|
System.out.println("Driving With Input");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void runDriveStraight(double move) {
|
2020-02-18 17:29:26 -07:00
|
|
|
m_drive.driveWithInputAux(move * 3/4, m_targetGyro);
|
|
|
|
|
System.out.println("Driving Straight with Target: " + m_targetGyro);
|
2020-02-17 18:25:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void runStoppedTurn(double steer) {
|
2020-02-21 20:46:52 -07:00
|
|
|
double cosMultiplier = 0.55;
|
2020-02-21 19:44:51 -07:00
|
|
|
double steerOutput = 0;
|
|
|
|
|
double deadzone = .2;
|
|
|
|
|
/* Curves the steer output to be similarily gradual */
|
|
|
|
|
if (steer > 0) {
|
|
|
|
|
steerOutput = -cosMultiplier*Math.cos(1.571*steer)+(cosMultiplier+deadzone);
|
|
|
|
|
} else if (steer < 0) {
|
|
|
|
|
steerOutput = cosMultiplier*Math.cos(1.571*steer)-(cosMultiplier+deadzone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateGyroTarget(steerOutput);
|
|
|
|
|
double currentPos = m_drive.m_rightFrontMotorPos;
|
|
|
|
|
if (Math.abs(currentPos - m_stopPos) > 100) {
|
|
|
|
|
m_drive.runDrivePositionPID(m_stopPos, m_targetGyro);
|
|
|
|
|
} else {
|
|
|
|
|
m_drive.driveWithInputAux(0, m_targetGyro);
|
|
|
|
|
}
|
2020-02-18 17:29:26 -07:00
|
|
|
System.out.println("Turning with Target: " + m_targetGyro);
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-17 13:10:10 -07:00
|
|
|
/**
|
|
|
|
|
* If AuxPID is enabled, then update using the steer input
|
|
|
|
|
*/
|
|
|
|
|
private void updateGyroTarget(double steerInput) {
|
2020-02-18 17:29:26 -07:00
|
|
|
m_targetGyro -= 5 * steerInput * m_deltaTime;
|
2020-02-17 13:55:29 -07:00
|
|
|
m_targetGyro = MathUtil.clamp( m_targetGyro,
|
2020-02-21 20:46:52 -07:00
|
|
|
m_currentGyro-(DriveConstants.TICKS_PER_GYRO_REV/2),
|
|
|
|
|
m_currentGyro+(DriveConstants.TICKS_PER_GYRO_REV/2));
|
2020-02-13 17:08:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-17 13:10:10 -07:00
|
|
|
/**
|
|
|
|
|
* set target angle to current angle (prevents buildup of gyro error).
|
|
|
|
|
*/
|
2020-02-13 17:08:41 -07:00
|
|
|
private void resetGyroTarget() {
|
2020-02-18 19:44:11 -07:00
|
|
|
m_targetGyro = m_currentGyro;
|
|
|
|
|
m_targetGyro = m_currentGyro
|
2020-02-19 19:15:45 -07:00
|
|
|
+ m_drive.getTurnRate();
|
2020-02-13 17:08:41 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:30:44 -07:00
|
|
|
// Called once the command ends or is interrupted.
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interrupted) {
|
2020-02-21 19:44:51 -07:00
|
|
|
m_isInterrupted = interrupted;
|
2020-02-12 20:30:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true when the command should end.
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|