My arm makes random commands

When I start my code my arm simply goes forward and then returns to the desired position, which ends up damaging it a little and getting in our way.

here is a video

and here is the arm code

package frc.robot.subsystems;

import com.revrobotics.CANSparkBase.IdleMode;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DutyCycleEncoder;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants.MotorPorts;

public class IntakeSubsystem extends SubsystemBase {

  private static CANSparkMax pivotMotor, intakeMotor;
  private final PIDController pid;
  private final DutyCycleEncoder encoder;
  private final DigitalInput m_limitSwitch;
  public boolean limitSwitchState;

  public IntakeSubsystem() {
    pivotMotor =
      new CANSparkMax(MotorPorts.PIVOT_MOTOR_PORT, MotorType.kBrushless);

    intakeMotor =
      new CANSparkMax(MotorPorts.INTAKE_MOTOR_PORT, MotorType.kBrushless);

    pivotMotor.setIdleMode(IdleMode.kCoast);

    m_limitSwitch = new DigitalInput(2);

    encoder = new DutyCycleEncoder(0);

    pid = new PIDController(3.0, 0.0, 0);
  }

  @Override
  public void periodic() {
    limitSwitchState = m_limitSwitch.get();

    double output = pid.calculate(getMeasurement());
    output = MathUtil.clamp(output, -0.5, 0.5);
    pivotMotor.set(output);

    SmartDashboard.putNumber("Encoder", getMeasurement());
    SmartDashboard.putBoolean("Coleta", limitSwitchState);
  }

  public void setIntakeSpeed(double speed) {
    intakeMotor.set(speed);
  }

  public void setIntakePosition(double setpoint) {
    pid.setSetpoint(setpoint);
  }

  public double getMeasurement() {
    return encoder.getAbsolutePosition();
  }
}

Where is the encoder located mechanically? You are getting its absolute position, to be able to use it absolute, encoder should not rotate more than one round. 1:1 gearing is the most desirable situation. Also you need the read the offset of the encoder at the 0 position so that it does not wrap the angle to the next 360 during the motion.

Edit: Or you can simply get the relative position and reset your position on auto start but the absolute is a better solution

2 Likes

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