Skip to content

Latest commit

 

History

History
230 lines (183 loc) · 12.7 KB

absolute-encoders.md

File metadata and controls

230 lines (183 loc) · 12.7 KB

Absolute Encoders

YAGSL supports most common FRC absolute encoders. Absolute encoders are required if you are using a brushed motor .

The absolute encoder value will show up in shuffleboard under swerve/modules/.../Raw Absolute Encoder and can be used to tune the absoluteEncoderOffset in module JSON configuration files under most circumstances.

Absolute Encoder Checklist

  • All Absolute Encoder's spin counterclockwise positive.
  • Magnetic Absolute encoders have a good read on the magnet while still and in operation (while the robot moves).
  • Absolute Encoders have unique CAN ID's or Analog Input Channel's.
  • Absolute Encoder are defined with the correct ID or Analog Input Channel.
  • Absolute Encoders have full range (0-360)

Swerve Absolute Encoder Wrapper

YAGSL created wrappers over all supported Absolute Encoders to uniformly fetch and set data that is needed for a Swerve Module to operate. This wrapper is called SwerveAbsoluteEncoder. All SwerveAbsoluteEncoder's can be fetched via the SwerveModule configuration object SwerveModuleConfiguration absolute encoder attribute absoluteEncoder. The SwerveModule is able to be fetched by SwerveDrive.getModules() easily.

YAGSL created wrappers over all supported Motor Controllers to uniformly fetch and set data that is needed for a Swerve Drive to operate. This wrapper is called SwerveMotor. All SwerveMotor's can be fetched via the SwerveModule configuration object SwerveModuleConfiguration motor definitions angleMotor and driveMotor. The SwerveModule is able to be fetched by SwerveDrive.getModules() easily.

import com.ctre.phoenix6.hardware.CANcoder;
 
   /**
   * Initialize {@link SwerveDrive} with the directory provided.
   *
   * @param directory Directory of swerve drive config files.
   */
  public SwerveSubsystem(File directory)
  {
    // Angle conversion factor is 360 / (GEAR RATIO * ENCODER RESOLUTION)
    //  In this case the gear ratio is 12.8 motor revolutions per wheel rotation.
    //  The encoder resolution per motor revolution is 1 per motor revolution.
    double angleConversionFactor = SwerveMath.calculateDegreesPerSteeringRotation(12.8, 1);
    // Motor conversion factor is (PI * WHEEL DIAMETER IN METERS) / (GEAR RATIO * ENCODER RESOLUTION).
    //  In this case the wheel diameter is 4 inches, which must be converted to meters to get meters/second.
    //  The gear ratio is 6.75 motor revolutions per wheel rotation.
    //  The encoder resolution per motor revolution is 1 per motor revolution.
    double driveConversionFactor = SwerveMath.calculateMetersPerRotation(Units.inchesToMeters(4), 6.75, 1);

    // Configure the Telemetry before creating the SwerveDrive to avoid unnecessary objects being created.
    SwerveDriveTelemetry.verbosity = TelemetryVerbosity.HIGH;
    try
    {
      swerveDrive = new SwerveParser(directory).createSwerveDrive(maximumSpeed, angleConversionFactor, driveConversionFactor);
    } catch (Exception e)
    {
      throw new RuntimeException(e);
    }
    swerveDrive.setHeadingCorrection(false); // Heading correction should only be used while controlling the robot via angle.

    for(SwerveModule m : swerveDrive.getModules())
    {
      System.out.println("Module Name: "+m.configuration.name);
      CANcoder absoluteEncoder = (CANcoder)m.configuration.absoluteEncoder.getAbsoluteEncoder();
    }
  }

Absolute Encoder Configuration

{% hint style="warning" %} Only CTRE devices currently support the canbus option, if your device is using the roboRIO canbus you must use the value of null or "rio" for supported CTRE devices. If you are using a CANivore, and the device is on the CANivore bus, the name must match the CANivore name. {% endhint %}

{% hint style="success" %} If your absolute encoder is attached to your SparkMAX, use the function SwerveDrive.pushOffsetsToEncoders() for the best performance. This sets the onboard PID sensor to the attached encoder! {% endhint %}

Inside any module JSON such as frontleft.json,frontright.json,backleft.json,backright.json this is what you would see to configure a absolute encoder.

{
  "drive": {
    "type": "sparkmax",
    "id": 5,
    "canbus": null
  },
  "angle": {
    "type": "sparkmax",
    "id": 6,
    "canbus": null
  },
  "encoder": {
    "type": "cancoder",
    "id": 11,
    "canbus": null
  },
  "inverted": {
    "drive": false,
    "angle": false
  },
  "absoluteEncoderOffset": -18.281,
  "absoluteEncoderInverted": false,
  "location": {
    "front": -12,
    "left": -12
  }
}

Possible Absolute Encoder Types

{% hint style="warning" %} Try inverting your steering/angle/azimuth motor if your module keeps spinning around. {% endhint %}

Devicetype
Nonenone
Integrated/Attached (via SparkMAX DutyCycle)attached
SparkMax Analog (via SparkMAX Analog Pin)sparkmax_analog
SparkMax Analog (via SparkMAX Analog Pin with 5V power)sparkmax_analog5v
Canandmag (via SparkMAX)canandmag
Canandmag (via CAN)canandmag_can
CANcodercancoder
Throughbore (via PWM)throughbore
Thrifty Absolute Magnetic Encoder (via Analog Input)thrifty
MA3 (via Analog Input)ma3
SRX Mag (via PWM)ctre_mag
AM Mag (via PWM)am_mag
PWM DutyCycledutycycle
Analog Encoderanalog