forked from wpilibsuite/allwpilib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add struct implementations for SwerveDriveKinematics
- Loading branch information
1 parent
42999b9
commit af09a74
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/SwerveDriveKinematicsStruct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.math.kinematics.struct; | ||
|
||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.math.kinematics.SwerveDriveKinematics; | ||
import edu.wpi.first.util.struct.Struct; | ||
import java.nio.ByteBuffer; | ||
|
||
public final class SwerveDriveKinematicsStruct implements Struct<SwerveDriveKinematics> { | ||
private final int m_numModules; | ||
|
||
/** | ||
* Constructs the {@link Struct} implementation. | ||
* | ||
* @param numModules the number of modules of the kinematics objects this serializer processes. | ||
*/ | ||
public SwerveDriveKinematicsStruct(int numModules) { | ||
m_numModules = numModules; | ||
} | ||
|
||
@Override | ||
public Class<SwerveDriveKinematics> getTypeClass() { | ||
return SwerveDriveKinematics.class; | ||
} | ||
|
||
@Override | ||
public String getTypeString() { | ||
return "struct:SwerveDriveKinematics__" + m_numModules; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return Translation2d.struct.getSize() * m_numModules; | ||
} | ||
|
||
@Override | ||
public String getSchema() { | ||
return "Translation2d modules[" + m_numModules + "]"; | ||
} | ||
|
||
@Override | ||
public Struct<?>[] getNested() { | ||
return new Struct<?>[] {Translation2d.struct}; | ||
} | ||
|
||
@Override | ||
public SwerveDriveKinematics unpack(ByteBuffer bb) { | ||
return new SwerveDriveKinematics(Struct.unpackArray(bb, m_numModules, Translation2d.struct)); | ||
} | ||
|
||
@Override | ||
public void pack(ByteBuffer bb, SwerveDriveKinematics value) { | ||
Struct.packArray(bb, value.getModules(), Translation2d.struct); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
wpimath/src/main/native/include/frc/kinematics/struct/SwerveDriveKinematicsStruct.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
#include <wpi/ct_string.h> | ||
#include <wpi/struct/Struct.h> | ||
|
||
#include "frc/kinematics/SwerveDriveKinematics.h" | ||
|
||
template <size_t NumModules> | ||
struct wpi::Struct<frc::SwerveDriveKinematics<NumModules>> { | ||
static constexpr ct_string kTypeString = | ||
wpi::Concat("struct:SwerveDriveKinematics__"_ct_string, | ||
wpi::NumToCtString<NumModules>()); | ||
static constexpr std::string_view GetTypeString() { return kTypeString; } | ||
static constexpr size_t GetSize() { | ||
return NumModules * wpi::Struct<frc::Translation2d>::GetSize(); | ||
} | ||
static constexpr ct_string kSchema = | ||
wpi::Concat("Translation2d modules["_ct_string, | ||
wpi::NumToCtString<NumModules>(), "]"_ct_string); | ||
static constexpr std::string_view GetSchema() { return kSchema; } | ||
|
||
static frc::SwerveDriveKinematics<NumModules> Unpack( | ||
std::span<const uint8_t> data); | ||
static void Pack(std::span<uint8_t> data, | ||
const frc::SwerveDriveKinematics<NumModules>& value); | ||
}; | ||
|
||
static_assert(wpi::StructSerializable<frc::SwerveDriveKinematics<4>>); | ||
static_assert(wpi::StructSerializable<frc::SwerveDriveKinematics<3>>); | ||
|
||
#include "frc/kinematics/struct/SwerveDriveKinematicsStruct.inc" |
27 changes: 27 additions & 0 deletions
27
wpimath/src/main/native/include/frc/kinematics/struct/SwerveDriveKinematicsStruct.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#pragma once | ||
|
||
#include "frc/kinematics/struct/SwerveDriveKinematicsStruct.h" | ||
|
||
namespace { | ||
constexpr size_t kModulesOff = 0; | ||
} // namespace | ||
|
||
template <size_t NumModules> | ||
frc::SwerveDriveKinematics<NumModules> | ||
wpi::Struct<frc::SwerveDriveKinematics<NumModules>>::Unpack( | ||
std::span<const uint8_t> data) { | ||
return frc::SwerveDriveKinematics<NumModules>{ | ||
wpi::UnpackStructArray<frc::Translation2d, kModulesOff, NumModules>( | ||
data)}; | ||
} | ||
|
||
template <size_t NumModules> | ||
void wpi::Struct<frc::SwerveDriveKinematics<NumModules>>::Pack( | ||
std::span<uint8_t> data, | ||
const frc::SwerveDriveKinematics<NumModules>& value) { | ||
wpi::PackStructArray<kModulesOff, NumModules>(data, value.GetModules()); | ||
} |
29 changes: 29 additions & 0 deletions
29
...h/src/test/java/edu/wpi/first/math/kinematics/struct/SwerveDriveKinematicsStructTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.math.kinematics.struct; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.math.kinematics.SwerveDriveKinematics; | ||
import edu.wpi.first.wpilibj.StructTestBase; | ||
|
||
@SuppressWarnings("PMD.TestClassWithoutTestCases") | ||
class SwerveDriveKinematicsStructTest extends StructTestBase<SwerveDriveKinematics> { | ||
SwerveDriveKinematicsStructTest() { | ||
super( | ||
new SwerveDriveKinematics( | ||
new Translation2d(1.0, 2.1), | ||
new Translation2d(1.5, -0.9), | ||
new Translation2d(-1.8, 1.2), | ||
new Translation2d(-1.7, -1.3)), | ||
SwerveDriveKinematics.getStruct(4)); | ||
} | ||
|
||
@Override | ||
public void checkEquals(SwerveDriveKinematics testData, SwerveDriveKinematics data) { | ||
assertArrayEquals(testData.getModules(), data.getModules()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
wpimath/src/test/native/cpp/kinematics/struct/SwerveDriveKinematicsStructTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "../../StructTestBase.h" | ||
#include "frc/kinematics/SwerveDriveKinematics.h" | ||
#include "frc/kinematics/struct/SwerveDriveKinematicsStruct.h" | ||
|
||
using namespace frc; | ||
|
||
struct SwerveDriveKinematicsStructTestData { | ||
using Type = SwerveDriveKinematics<4>; | ||
|
||
inline static const Type kTestData{ | ||
frc::Translation2d{1.0_m, 0.9_m}, frc::Translation2d{1.1_m, -0.8_m}, | ||
frc::Translation2d{-1.2_m, 0.7_m}, frc::Translation2d{-1.3_m, -0.6_m}}; | ||
|
||
static void CheckEq(const Type& testData, const Type& data) { | ||
EXPECT_EQ(testData.GetModules(), data.GetModules()); | ||
} | ||
}; | ||
|
||
INSTANTIATE_TYPED_TEST_SUITE_P(SwerveDriveKinematics, StructTest, | ||
SwerveDriveKinematicsStructTestData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters