Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic outline for PID controlled arm. due to design constraints, buil… #5

Open
wants to merge 1 commit into
base: arm-pid
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 91 additions & 48 deletions src/main/java/frc/robot/commands/elevator/PidRotate.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,97 @@
import frc.robot.constants.ArmConstants;
import frc.robot.subsystems.Arm;

public class PidRotate extends CommandBase {
private final Arm arm;
public class PIDRotate extends CommandBase {
private final Arm arm;

private double setpointAngle;

/**
* Autonomously rotates the arm to a set position for scoring.
*
* @param arm
* @param angle in radians.
*/
public PIDRotate(Arm arm, double setpointAngle) {
this.arm = arm;
this.setpointAngle = setpointAngle;

addRequirements(arm);
}

@Override
public void initialize() {
this.arm.resetEncoder();
}

@Override
public void execute(
double armAngle,
double unixEpochTime
) {
double armAngleNew = this.arm.getAngle();
double unixEpochTimeNew = System.currentTimeMillis() / 1000;
double armAngularVelocity = (
chargeStationAngleNew - chargeStationAngle
) / (
unixEpochTimeNew - unixEpochTime
);

double armDistanceFromSetpoint = this.setPointAngle - armAngleNew;

// Built in calculate() method does not support variability
// in torque to voltage ratio; too much effort to reengineer.
// bootleg solution proposed:

this.arm.setMotor(
(
(
ArmConstants.ARM_SPEED_FACTOR
) - (
armAngularVelocity
)
) * (
armDistanceFromSetpoint
) + Math.cos(
armAngleNew
) * (
ArmConstants.ARM_MASS_FACTOR
)
);

private double setpointAngle;

private boolean setpointReached;

/**
* Autonomously rotates the arm to a set position for scoring.
*
* @param arm
* @param angle in radians.
*/
public PidRotate(Arm arm, double setpointAngle) {
this.arm = arm;
this.setpointAngle = setpointAngle;
this.setpointReached = false;

addRequirements(arm);
}

@Override
public void initialize() {
this.arm.resetEncoder();
}

@Override
public void execute() {
if (!setpointReached) {
double pid = arm.getPIDController().calculate(
this.arm.getAngle(), setpointAngle);
this.arm.setMotor(pid);
DriverStation.reportWarning(Double.toString(pid), false);

if (Math.abs(this.arm.getEncoder().getDistance() - setpointAngle) < ArmConstants.ARM_TOLERANCE) {
this.setpointReached = true;
}
}
}

@Override
public boolean isFinished() {
return this.setpointReached;
}

@Override
public void end(boolean interrupted) {
this.arm.resetEncoder();
}
DriverStation.reportWarning(Double.toString(pid), false);
}

public void raise(double raiseDeltaTime) {
this.setPointAngle += raiseDeltaTime * ArmConstants.ARM_SPEED_FACTOR;

if (this.setPointAngle > ArmConstants.ARM_UPPER_BOUND)
this.setPointAngle = ArmConstants.ARM_UPPER_BOUND;
}

public void lower(double lowerDeltaTime) {
this.setPointAngle -= lowerDeltaTime * ArmConstants.ARM_SENSITIVITY;

if (this.setpointAngle < ArmConstants.ARM_LOWER_BOUND)
this.setPointAngle = ArmConstants.ARM_LOWER_BOUND;
}

@Override
public boolean isFinished() {
return Math.pow(
(
this.arm.getAngle
) / (
57
) - (
this.setPointAngle
), - 2
) > ArmConstants.ARM_TOLERANCE;
}

@Override
public void end(boolean interrupted) {
this.arm.resetEncoder();
}

}
13 changes: 9 additions & 4 deletions src/main/java/frc/robot/constants/ArmConstants.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package frc.robot.constants;

public class ArmConstants {
public static final double ARM_SPEED_FACTOR = 0.1;
public static final double ARM_TOLERANCE = 5 * Math.PI / 180;
public static final int LEFT_MOTOR_PORT = 8;
public static final int RIGHT_MOTOR_PORT = 5;
public static final double ARM_SPEED_FACTOR = 0.3;
public static final double ARM_TOLERANCE = 120;
public static final int LEFT_MOTOR_PORT = 8;
public static final int RIGHT_MOTOR_PORT = 5;
// todo: calibrate
public static final double ARM_UPPER_BOUND = 1.5;
public static final double ARM_LOWER_BOUND = -.5;
public static final double ARM_MASS_FACTOR = 0.5;
public static final double ARM_SENSITIVITY = 0.1;
}