Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/main/java/org/carlmontrobotics/lib199/sim/MockedCANCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

import org.carlmontrobotics.lib199.Lib199Subsystem;

import edu.wpi.first.hal.HALValue;
import edu.wpi.first.hal.SimDevice;
import edu.wpi.first.hal.SimDevice.Direction;
import edu.wpi.first.hal.simulation.SimValueCallback;
import edu.wpi.first.hal.SimDouble;
import edu.wpi.first.wpilibj.simulation.SimDeviceSim;

public class MockedCANCoder {

Expand All @@ -19,6 +22,7 @@ public class MockedCANCoder {

private int port;
private SimDevice device;
private SimDeviceSim deviceSim;
private SimDouble position; // Rotations - Continuous
private SimDouble gearing;
private CANcoderSimState sim;
Expand All @@ -29,14 +33,16 @@ public MockedCANCoder(CANcoder canCoder) {
position = device.createDouble("count", Direction.kInput, 0);
gearing = device.createDouble("gearing", Direction.kOutput, 1);
sim = canCoder.getSimState();
Lib199Subsystem.registerAsyncSimulationPeriodic(this::update);
deviceSim = new SimDeviceSim("CANCoder", port);
deviceSim.registerValueChangedCallback(position, new SimValueCallback() {
@Override
public void callback(String name, int handle, int direction, HALValue value) {
sim.setRawPosition(value.getDouble() / kCANCoderCPR);
}
}, true);
sims.put(port, this);
}

public void update() {
sim.setRawPosition(position.get() / kCANCoderCPR);
}

public void setGearing(double gearing) {
this.gearing.set(gearing);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.carlmontrobotics.lib199.sim;

import org.carlmontrobotics.lib199.testUtils.TestRules;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.Assert.assertEquals;

import com.ctre.phoenix6.hardware.CANcoder;
import com.ctre.phoenix6.sim.CANcoderSimState;

import edu.wpi.first.wpilibj.simulation.SimDeviceSim;

public class MockedCANCoderTest {
@ClassRule
public static TestRules.InitializeHAL simClassRule = new TestRules.InitializeHAL();
@Rule
public TestRules.ResetSimDeviceSimData simTestRule = new TestRules.ResetSimDeviceSimData();

@Test
public void testCountUpdatesPosition() throws InterruptedException {
CANcoder canCoder = new CANcoder(0);

// Set up a SimDevice for the CANcoder
new MockedCANCoder(canCoder);

// Consider the current position to be 0 rotations.
canCoder.setPosition(0.0);

// Set the position to 0.42 rotations via the SimDevice interface
SimDeviceSim canCoderSim = new SimDeviceSim("CANCoder", 0);
canCoderSim.getDouble("count").set(0.42 * MockedCANCoder.kCANCoderCPR);

// Wait for the CANCoder to update the position. This appears to happen on a separate thread.
canCoder.getPosition().waitForUpdate(1.0);

assertEquals(0.42, canCoder.getPosition().getValueAsDouble(), 0.001);
}

}