001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.math.kinematics.struct;
006
007import edu.wpi.first.math.geometry.Translation2d;
008import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
009import edu.wpi.first.util.struct.Struct;
010import java.nio.ByteBuffer;
011
012public final class SwerveDriveKinematicsStruct implements Struct<SwerveDriveKinematics> {
013  private final int m_numModules;
014
015  /**
016   * Constructs the {@link Struct} implementation.
017   *
018   * @param numModules the number of modules of the kinematics objects this serializer processes.
019   */
020  public SwerveDriveKinematicsStruct(int numModules) {
021    m_numModules = numModules;
022  }
023
024  @Override
025  public Class<SwerveDriveKinematics> getTypeClass() {
026    return SwerveDriveKinematics.class;
027  }
028
029  @Override
030  public String getTypeName() {
031    return "SwerveDriveKinematics__" + m_numModules;
032  }
033
034  @Override
035  public int getSize() {
036    return Translation2d.struct.getSize() * m_numModules;
037  }
038
039  @Override
040  public String getSchema() {
041    return "Translation2d modules[" + m_numModules + "]";
042  }
043
044  @Override
045  public Struct<?>[] getNested() {
046    return new Struct<?>[] {Translation2d.struct};
047  }
048
049  @Override
050  public SwerveDriveKinematics unpack(ByteBuffer bb) {
051    return new SwerveDriveKinematics(Struct.unpackArray(bb, m_numModules, Translation2d.struct));
052  }
053
054  @Override
055  public void pack(ByteBuffer bb, SwerveDriveKinematics value) {
056    Struct.packArray(bb, value.getModules(), Translation2d.struct);
057  }
058}