Skip to content

Added missing equality operations for DCMotor class #7888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.wpi.first.math.util.Units;
import edu.wpi.first.util.protobuf.ProtobufSerializable;
import edu.wpi.first.util.struct.StructSerializable;
import java.util.Objects;

/** Holds the constants for a DC motor. */
public class DCMotor implements ProtobufSerializable, StructSerializable {
Expand Down Expand Up @@ -321,4 +322,30 @@ public static DCMotor getNeoVortex(int numMotors) {
return new DCMotor(
12, 3.60, 211, 3.6, Units.rotationsPerMinuteToRadiansPerSecond(6784), numMotors);
}

@Override
public boolean equals(Object obj) {
return obj instanceof DCMotor other
&& Math.abs(other.nominalVoltageVolts - nominalVoltageVolts) < 1E-9
&& Math.abs(other.stallTorqueNewtonMeters - stallTorqueNewtonMeters) < 1E-9
&& Math.abs(other.stallCurrentAmps - stallCurrentAmps) < 1E-9
&& Math.abs(other.freeCurrentAmps - freeCurrentAmps) < 1E-9
&& Math.abs(other.freeSpeedRadPerSec - freeSpeedRadPerSec) < 1E-9
&& Math.abs(other.rOhms - rOhms) < 1E-9
&& Math.abs(other.KvRadPerSecPerVolt - KvRadPerSecPerVolt) < 1E-9
&& Math.abs(other.KtNMPerAmp - KtNMPerAmp) < 1E-9;
}

@Override
public int hashCode() {
return Objects.hash(
nominalVoltageVolts,
stallTorqueNewtonMeters,
stallCurrentAmps,
freeCurrentAmps,
freeSpeedRadPerSec,
rOhms,
KvRadPerSecPerVolt,
KtNMPerAmp);
}
}
5 changes: 5 additions & 0 deletions wpimath/src/main/native/include/frc/system/plant/DCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class WPILIB_DLLEXPORT DCMotor {
// From https://www.revrobotics.com/next-generation-spark-neo/
return DCMotor(12_V, 3.60_Nm, 211_A, 3.615_A, 6784_rpm, numMotors);
}

/**
* Checks equality between this DCMotor and another object.
*/
constexpr bool operator==(const DCMotor&) const = default;
};

} // namespace frc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.system.plant;

import static org.junit.jupiter.api.Assertions.assertEquals;

import edu.wpi.first.math.util.Units;
import org.junit.jupiter.api.Test;

class DCMotorTest {
@Test
void testDcMotorEquality() {
var gearboxA = DCMotor.getKrakenX60(2);
var gearboxB =
new DCMotor(12, 14.18, 732, 4, Units.rotationsPerMinuteToRadiansPerSecond(6000), 1);

assertEquals(gearboxA, gearboxB);
}
}
17 changes: 17 additions & 0 deletions wpimath/src/test/native/cpp/system/DCMotorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 <functional>

#include <gtest/gtest.h>

#include "frc/system/plant/DCMotor.h"

using namespace frc;

TEST(DCMotorTest, Equality) {
Copy link
Member

@calcmogul calcmogul Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't really necessary. It's a compiler-generated equality operator, and we don't write equality tests for other objects like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't Pose2dTest.cpp implement this? Or is this just auto generated? I was looking at other cpp tests for reference of what to add (I'm not a heavy cpp user).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only test the ones with non-default implementations.

[tav@myriad cpp]$ rg Equality
kinematics/SwerveModuleStateTest.cpp
130:TEST(SwerveModuleStateTest, Equality) {

kinematics/SwerveModulePositionTest.cpp
10:TEST(SwerveModulePositionTest, Equality) {

geometry/Rotation2dTest.cpp
65:TEST(Rotation2dTest, Equality) {

geometry/Pose2dTest.cpp
65:TEST(Pose2dTest, Equality) {

geometry/Translation3dTest.cpp
132:TEST(Translation3dTest, Equality) {

geometry/QuaternionTest.cpp
216:TEST(QuaternionTest, DotProductAsEquality) {

geometry/Twist3dTest.cpp
58:TEST(Twist3dTest, Equality) {

geometry/Translation2dTest.cpp
85:TEST(Translation2dTest, Equality) {

geometry/Rotation3dTest.cpp
290:TEST(Rotation3dTest, Equality) {

geometry/Twist2dTest.cpp
42:TEST(Twist2dTest, Equality) {

geometry/Pose3dTest.cpp
99:TEST(Pose3dTest, Equality) {

const DCMotor a = DCMotor::KrakenX60(2);
const DCMotor b{12_V, 14.18_Nm, 732_A, 4_A, 6000_rpm, 1};
EXPECT_TRUE(a == b);
}
Loading