2020-02-24 19:48:38 -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;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.wpilibj2.command.CommandBase;
|
|
|
|
|
import frc4388.robot.Constants.DriveConstants;
|
|
|
|
|
import frc4388.robot.subsystems.Drive;
|
|
|
|
|
|
|
|
|
|
public class DrivePositionMPAux extends CommandBase {
|
|
|
|
|
Drive m_drive;
|
|
|
|
|
double m_cruiseVel;
|
|
|
|
|
double m_rampDist;
|
|
|
|
|
double m_targetPos;
|
|
|
|
|
double m_currentVel;
|
|
|
|
|
double m_currentPos;
|
|
|
|
|
double m_targetGyro;
|
|
|
|
|
double m_targetVel;
|
|
|
|
|
double m_rampAcc;
|
|
|
|
|
long m_startTime;
|
|
|
|
|
long m_rampRate;
|
2020-02-26 16:46:22 -07:00
|
|
|
int m_counter;
|
2020-02-24 19:48:38 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new DrivePositionMPAux.
|
|
|
|
|
*
|
|
|
|
|
* @param subsystem The drive subsystem
|
2020-02-26 20:29:01 -07:00
|
|
|
* @param cruiseVel The target velocity for the motors in in/s
|
2020-02-24 19:48:38 -07:00
|
|
|
* @param rampDist The distance before cruise velocity is reached in inches
|
|
|
|
|
* @param rampRate The time to reach the cruise velocity in seconds
|
|
|
|
|
* @param targetPos The target position
|
|
|
|
|
*/
|
2020-02-26 20:29:01 -07:00
|
|
|
public DrivePositionMPAux(Drive subsystem, double cruiseVel, double rampDist, float rampRate, double targetPos) {
|
2020-02-24 19:48:38 -07:00
|
|
|
// Use addRequirements() here to declare subsystem dependencies.
|
|
|
|
|
m_drive = subsystem;
|
2020-02-25 21:33:12 -07:00
|
|
|
m_cruiseVel = cruiseVel * DriveConstants.TICKS_PER_INCH_LOW / 10;
|
|
|
|
|
m_rampDist = rampDist * DriveConstants.TICKS_PER_INCH_LOW;
|
2020-02-24 19:48:38 -07:00
|
|
|
m_rampRate = (long) rampRate * 1000;
|
2020-02-25 21:33:12 -07:00
|
|
|
m_targetPos = targetPos * DriveConstants.TICKS_PER_INCH_LOW;
|
2020-02-24 19:48:38 -07:00
|
|
|
//m_targetGyro = targetGyro * DriveConstants.TICKS_PER_GYRO_REV / 360;
|
|
|
|
|
m_targetGyro = m_drive.m_rightFrontMotor.getSelectedSensorPosition(DriveConstants.PID_TURN);
|
|
|
|
|
addRequirements(m_drive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
|
|
|
|
m_currentVel = m_drive.m_rightFrontMotorVel;
|
|
|
|
|
m_currentPos = m_drive.m_rightFrontMotorPos;
|
|
|
|
|
m_targetPos = m_targetPos + m_currentPos;
|
|
|
|
|
m_targetVel = m_currentVel;
|
|
|
|
|
m_startTime = System.currentTimeMillis();
|
|
|
|
|
m_rampAcc = (m_cruiseVel - m_currentVel) / m_rampRate;
|
2020-02-26 16:46:22 -07:00
|
|
|
m_counter = 0;
|
2020-02-24 19:48:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called every m_isRamptime the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
|
|
|
|
m_currentVel = m_drive.m_rightFrontMotorVel;
|
|
|
|
|
m_currentPos = m_drive.m_rightFrontMotorPos;
|
|
|
|
|
if (System.currentTimeMillis() - m_startTime < m_rampRate) {
|
|
|
|
|
// Ramping
|
2020-02-25 23:34:38 -07:00
|
|
|
m_targetVel += m_rampAcc * m_drive.m_deltaTimeMs;
|
2020-02-24 19:48:38 -07:00
|
|
|
m_drive.runDriveVelocityPID(-m_targetVel, m_targetGyro);
|
|
|
|
|
} else if (m_targetPos - m_currentPos > m_rampDist) {
|
|
|
|
|
// Cruising
|
|
|
|
|
m_drive.runDriveVelocityPID(-m_cruiseVel, m_targetGyro);
|
|
|
|
|
} else {
|
|
|
|
|
// Deramp PID
|
|
|
|
|
m_drive.runDrivePositionPID(-m_targetPos, m_targetGyro);
|
|
|
|
|
}
|
2020-02-26 16:46:22 -07:00
|
|
|
m_counter ++;
|
2020-02-24 19:48:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() {
|
2020-02-26 16:46:22 -07:00
|
|
|
if (Math.abs((int)m_drive.m_rightFrontMotor.getSelectedSensorVelocity(DriveConstants.PID_PRIMARY)) < 5 && (m_counter > 5)) {
|
2020-02-26 20:29:01 -07:00
|
|
|
//return true;
|
2020-02-24 19:48:38 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|