Command based ArmBot example Constants and Math

Could someone please explain the significance and effect of each constant in the armbot example project? Maybe how each constant changes the behavior of the arm. Also for an arm using a CANSparkMax how would I approach finding my goal and other constants knowing the gear ratio and target position in radians that we have?

Constants
public static final int kMotorPort = 4;

public static final double kP = 1;

// These are fake gains; in actuality these must be determined individually for each robot
public static final double kSVolts = 1;
public static final double kGVolts = 1;
public static final double kVVoltSecondPerRad = 0.5;
public static final double kAVoltSecondSquaredPerRad = 0.1;

public static final double kMaxVelocityRadPerSecond = 3;
public static final double kMaxAccelerationRadPerSecSquared = 10;

public static final int[] kEncoderPorts = new int[] {4, 5};
public static final int kEncoderPPR = 256;
public static final double kEncoderDistancePerPulse = 2.0 * Math.PI / kEncoderPPR;

// The offset of the arm from the horizontal in its neutral position,
// measured from the horizontal
public static final double kArmOffsetRads = 0.5;

Arm Subsystem
public class ArmSubsystem extends ProfiledPIDSubsystem {
private final PWMSparkMax m_motor = new PWMSparkMax(ArmConstants.kMotorPort);
private final Encoder m_encoder =
new Encoder(ArmConstants.kEncoderPorts[0], ArmConstants.kEncoderPorts[1]);
private final ArmFeedforward m_feedforward =
new ArmFeedforward(
ArmConstants.kSVolts, ArmConstants.kGVolts,
ArmConstants.kVVoltSecondPerRad, ArmConstants.kAVoltSecondSquaredPerRad);

/** Create a new ArmSubsystem. */
public ArmSubsystem() {
super(
new ProfiledPIDController(
ArmConstants.kP,
0,
0,
new TrapezoidProfile.Constraints(
ArmConstants.kMaxVelocityRadPerSecond,
ArmConstants.kMaxAccelerationRadPerSecSquared)),
0);
m_encoder.setDistancePerPulse(ArmConstants.kEncoderDistancePerPulse);
// Start arm at rest in neutral position
setGoal(ArmConstants.kArmOffsetRads);
}

@Override
public void useOutput(double output, TrapezoidProfile.State setpoint) {
// Calculate the feedforward from the sepoint
double feedforward = m_feedforward.calculate(setpoint.position, setpoint.velocity);
// Add the feedforward to the PID output to get the motor output
m_motor.setVoltage(output + feedforward);
}

@Override
public double getMeasurement() {
return m_encoder.getDistance() + ArmConstants.kArmOffsetRads;
}
}

Robot Container
new JoystickButton(m_driverController, Button.kA.value)
.whenPressed(
() → {
m_robotArm.setGoal(2);
m_robotArm.enable();
},
m_robotArm);

// Move the arm to neutral position when the 'B' button is pressed.
new JoystickButton(m_driverController, Button.kB.value)
    .whenPressed(
        () -> {
          m_robotArm.setGoal(Constants.ArmConstants.kArmOffsetRads);
          m_robotArm.enable();
        },
        m_robotArm);

// Disable the arm controller when Y is pressed.
new JoystickButton(m_driverController, Button.kY.value).whenPressed(m_robotArm::disable);

Please wrap your code blocks in triple backticks (```) for proper formatting.

One set of constants is for the WPILib ArmFeedforward class. Another is a set of gains (well, a single proportional gain) is for the WPILib PIDController. The maximum velocity and acceleration are constraints for the motion profile generation used to generate setpoints for the controllers, and should be chosen to be simultaneously-achievable by the arm system (the feedforward classes have methods that help with this).

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.