Skip to content

Commit

Permalink
Merge pull request #12 from SciBorgs/simple-motor
Browse files Browse the repository at this point in the history
Simple motor
  • Loading branch information
kishan243 authored Jan 15, 2025
2 parents b5b15c1 + 421fa6b commit 8191040
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/sciborgs1155/lib/SimpleMotor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.sciborgs1155.lib;

import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.hardware.TalonFX;
import java.util.function.DoubleConsumer;

public class SimpleMotor {
private final DoubleConsumer set;
private final Runnable close;

public SimpleMotor(DoubleConsumer set, Runnable close) {
this.set = set;
this.close = close;
}

public static SimpleMotor talon(TalonFX talon, TalonFXConfiguration config) {
FaultLogger.register(talon);
TalonUtils.addMotor(talon);
talon.getConfigurator().apply(config);
return new SimpleMotor(talon::set, talon::close);
}

public static SimpleMotor none() {
return new SimpleMotor(v -> {}, () -> {});
}

public void set(double power) {
set.accept(power);
}

public void close() {
close.run();
}
}
38 changes: 38 additions & 0 deletions src/test/java/org/sciborgs1155/lib/SimpleMotorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.sciborgs1155.lib;

import static edu.wpi.first.units.Units.RadiansPerSecondPerSecond;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.sciborgs1155.lib.UnitTestingUtil.*;

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
import edu.wpi.first.wpilibj2.command.Commands;
import org.junit.jupiter.api.Test;

public class SimpleMotorTest {
@Test
public void test() throws Exception {
setupTests();
DCMotorSim motor =
new DCMotorSim(LinearSystemId.createDCMotorSystem(1, 0.3), DCMotor.getKrakenX60(1));

assert motor.getAngularAcceleration().in(RadiansPerSecondPerSecond) == 0;

SimpleMotor sm = new SimpleMotor(motor::setInput, () -> {});
run(Commands.run(() -> motor.update(0.02)));

sm.set(0.5);
assertEquals(motor.getInput().get(0, 0), 0.5);

fastForward();
assertEquals(motor.getAngularVelocityRadPerSec(), 0.5, 2e-3);

sm.set(-0.5);

fastForward();
assertEquals(motor.getAngularVelocityRadPerSec(), -0.5, 2e-3);

reset();
}
}

0 comments on commit 8191040

Please sign in to comment.