WCP Swerve X, non flipped, can coder: Need help with code

Looking for some help with the WCP swerve x, non flipped programming. We changed drive gearts to 10t, all can coders and pigeon. We can not get our program to turn motors correctly in turn and drive is pending

Whats your drive code like? Also is this the motor for turning the angle?

I can share tomorrow. I left the laptop at our school. I am the sponsor for team 8827. This is technically our second year but we are considered 3yr. I can get you more information tomorrow on the type of code.

Sounds good.

1 Like

Our team programming mentor created an account. He is joining in this chat for programming help.

1 Like

Did you guys get it working?

Drive is at our school, we have to wait til next meet afterschool Monday

still can’t get it going

Can you send the code for the drivetrain? Also what do you mean by turn motors correctly? Are motors turning wrong way or are not turning?

We’ll send you code tomorrow morning around 9:30 central time

HI! I am a programmer for the team 8827 this is our code for SwerveDriveX so far SwerveDrive/src/main at main · Blue0901/SwerveDrive · GitHub. The problem we are having is that the wheels jitter and turn abruptly when we enable the code and we don’t know what is causing it. Currently we don’t think anything is wrong with the actual code so we are adjusting the PID values as we think that is what is causing the issue but we haven’t had much luck. Sorry I updated this a bit late. Also I have a question. How do we know that the PID values we assigned are correct? How is are the wheels supposed to behave once we assign the correct values.

1 Like

The first thing I see is that you have several unit errors:

  • The Spark Max uses rotations.
  • The CANCoder will return values in degrees.
  • SwerveModuleState provides speed in meter per second.

Look at this code, there are several places where the units are wrong:

  /**
   * Sets the desired state for the module.
   *
   * @param desiredState Desired state with speed and angle.
   */
  public void setDesiredState(SwerveModuleState desiredState) {
    // Optimize the reference state to avoid spinning further than 90 degrees
    SwerveModuleState state =
        SwerveModuleState.optimize(desiredState, new Rotation2d(Math.toDegrees(m_turningEncoder.getAbsolutePosition())));

    // Calculate the drive output from the drive PID controller.
    final double driveOutput =
        m_drivePIDController.calculate(m_driveEncoder.getVelocity(), state.speedMetersPerSecond);

    final double driveFeedforward = m_driveFeedforward.calculate(state.speedMetersPerSecond);

    // Calculate the turning motor output from the turning PID controller.
    //double wantedAngle = ExtraMath.mod(state.angle.getDegrees() + 180.0, 360.0) - 180.0;
    double wantedAngle = ExtraMath.mod(state.angle.getDegrees()+180.0, 360.0) - 180.0;
    final double turnOutput =
        m_turningPIDController.calculate(m_turningEncoder.getAbsolutePosition(), wantedAngle);
        
    m_driveMotor.set(driveOutput);
    m_turningMotor.set(turnOutput);
  }
  1. new Rotation2d(Math.toDegrees(m_turningEncoder.getAbsolutePosition())
    The encoder returns a value in rotations, Math.toDegrees() converts from radians. You want to do this:
    Rotation2d.fromRotations(m_turningEncoder.getAbsolutePosition());
  2. m_drivePIDController.calculate(m_driveEncoder.getVelocity(), state.speedMetersPerSecond);
    The encoder returns velocity in rotations per second, but state is in meters per second. You can handle this conversion in a few different ways, but I recommend setting the encoder conversion factor with setVelocityConversionFactor​.
  3. It’s not clear what this is doing. I don’t think you need it:
    double wantedAngle = ExtraMath.mod(state.angle.getDegrees()+180.0, 360.0) - 180.0;
  4. m_turningPIDController.calculate(m_turningEncoder.getAbsolutePosition(), wantedAngle);
    The turning encoder’s value is in rotations, but wantedAngle is in degrees. I think you want this:
    m_turningPIDController.calculate(m_turningEncoder.getAbsolutePosition(), state.angle.getRotations());

You have the same unit errors in your getState() and getPosition() methods:

  /**
   * Returns the current state of the module.
   *
   * @return The current state of the module.
   */
  public SwerveModuleState getState() {
    return new SwerveModuleState(
        m_driveEncoder.getVelocity(), new Rotation2d(Math.toDegrees(m_turningEncoder.getAbsolutePosition())));
  }

  /**
   * Returns the current position of the module.
   *
   * @return The current position of the module.
   */
  public SwerveModulePosition getPosition() {
    return new SwerveModulePosition(
        m_driveEncoder.getPosition(), new Rotation2d(m_turningEncoder.getAbsolutePosition()));
  }

Have you looked at using YAGSL instead of writing your own code?

1 Like

Okay thank you so much this helps a lot I will definitely look at the unit errors and look into YAGSL. Will keep updated if after we fix those problems there is still issues.

Did you get the link to drive train program?