Skip to content
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

Converts the teeth amount of a gear to the pitch diameter. #72

Open
wants to merge 1 commit 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
45 changes: 45 additions & 0 deletions math/src/main/java/coppercore/math/GearConversionFunctions.java
Copy link
Member

Choose a reason for hiding this comment

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

We need to consistently name these functions. I propose renaming the functions to follow the format pitchDiameterFromMechanismName since that seems intuitive

For example:
pitchDiameterFrom3mmPulley or pitchDiameterFrom25ChainSprocket

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package coppercore.math;

public class GearConversionFunctions {
public static double pitchPulley3mm(int teeth) {
Copy link
Member

Choose a reason for hiding this comment

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

Per #52 all these functions should return a Distance, which is a unit from wpilib. So that needs to be added to all functions.

Also you'll need to include the following in your build.gradle in the dependencies block

def wpilibVersion = "2025.1.1-beta-1"
implementation "edu.wpi.first.wpiunits:wpiunits-java:$wpilibVersion"

teeth = Math.abs(teeth);
double pitchDiameter = 3 * teeth / ((double) Math.PI);
return pitchDiameter;
}

public static double pitchPulley5mm(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = 5 * teeth / ((double) Math.PI);
return pitchDiameter;
}

public static double pulleyRT25(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = 0.25 * teeth / ((double) Math.PI);
return pitchDiameter;
}

public static double chainSprocket25(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = 0.25 / (double) Math.sin(Math.PI / teeth);
return pitchDiameter;
}

public static double chainSprocket35(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = 0.375 / (double) Math.sin(Math.PI / teeth);
return pitchDiameter;
}

public static double gear20DP(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = (double) teeth / 20;
return pitchDiameter;
}

public static double gear32DP(int teeth) {
teeth = Math.abs(teeth);
double pitchDiameter = (double) teeth / 32;
return pitchDiameter;
}
}
62 changes: 62 additions & 0 deletions math/src/test/java/coppercore/math/GearConversionTest.java
Copy link
Member

Choose a reason for hiding this comment

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

Comparing floating point numbers with equality is generally considered bad practice. assertEquals should have an option to specify an epsilon, and I recommend setting that to 1e-10.

Copy link
Contributor

Choose a reason for hiding this comment

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

To use it just provide a third parameter which it the delta

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package coppercore.math;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class GearConversionTest {
@Test
public void pitchPulley3mmTest() {
Assertions.assertEquals(12 / (Math.PI), GearConversionFunctions.pitchPulley3mm(4));
Assertions.assertEquals(150 / (Math.PI), GearConversionFunctions.pitchPulley3mm(50));
Assertions.assertEquals(0 / (Math.PI), GearConversionFunctions.pitchPulley3mm(0));
Assertions.assertEquals(21 / (Math.PI), GearConversionFunctions.pitchPulley3mm(-7));
}

@Test
public void pitchPulley5mmTest() {
Assertions.assertEquals(20 / (Math.PI), GearConversionFunctions.pitchPulley5mm(4));
Assertions.assertEquals(250 / (Math.PI), GearConversionFunctions.pitchPulley5mm(50));
Assertions.assertEquals(0 / (Math.PI), GearConversionFunctions.pitchPulley5mm(0));
Assertions.assertEquals(35 / (Math.PI), GearConversionFunctions.pitchPulley5mm(-7));
}

@Test
public void pulleyRT25Test() {
Assertions.assertEquals(1 / (Math.PI), GearConversionFunctions.pulleyRT25(4));
Assertions.assertEquals(13 / (Math.PI), GearConversionFunctions.pulleyRT25(52));
Assertions.assertEquals(0 / (Math.PI), GearConversionFunctions.pulleyRT25(0));
Assertions.assertEquals(2 / (Math.PI), GearConversionFunctions.pulleyRT25(-8));
}

@Test
public void chainSprocket25Test() {
Assertions.assertEquals(
0.25 / (1 / Math.sqrt(2)), GearConversionFunctions.chainSprocket25(4));
Assertions.assertEquals(
0.25 / (Math.sqrt(3) / 2), GearConversionFunctions.chainSprocket25(3));
}

@Test
public void chainSprocket35Test() {
Assertions.assertEquals(
0.375 / (1 / Math.sqrt(2)), GearConversionFunctions.chainSprocket35(4));
Assertions.assertEquals(
0.375 / (Math.sqrt(3) / 2), GearConversionFunctions.chainSprocket35(3));
}

@Test
public void gear20DPTest() {
Assertions.assertEquals(0.2, GearConversionFunctions.gear20DP(4));
Assertions.assertEquals(1, GearConversionFunctions.gear20DP(20));
Assertions.assertEquals(0, GearConversionFunctions.gear20DP(0));
Assertions.assertEquals(2.0, GearConversionFunctions.gear20DP(-40));
}

@Test
public void gear32DPTest() {
Assertions.assertEquals(0.125, GearConversionFunctions.gear32DP(4));
Assertions.assertEquals(1.0, GearConversionFunctions.gear32DP(32));
Assertions.assertEquals(0, GearConversionFunctions.gear32DP(0));
Assertions.assertEquals(2.0, GearConversionFunctions.gear32DP(-64));
}
}
Loading