mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[hal, wpilib] PWM Rewrite (#7845)
The HAL will only contain the output period and the raw microseconds. Higher level things such as SimDevice can handle everything else.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// 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.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.Spark;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PWMMotorControllerSimTest {
|
||||
@Test
|
||||
void testMotor() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Spark spark = new Spark(0)) {
|
||||
PWMMotorControllerSim sim = new PWMMotorControllerSim(spark);
|
||||
|
||||
spark.set(0);
|
||||
assertEquals(0, sim.getSpeed());
|
||||
|
||||
spark.set(0.354);
|
||||
assertEquals(0.354, sim.getSpeed());
|
||||
|
||||
spark.set(-0.785);
|
||||
assertEquals(-0.785, sim.getSpeed());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.PWM;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PWMSimTest {
|
||||
private static final double PWM_STEP_SIZE = 1.0 / 2000.0;
|
||||
|
||||
@Test
|
||||
void testInitialize() {
|
||||
HAL.initialize(500, 0);
|
||||
@@ -55,119 +52,7 @@ class PWMSimTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetSpeed() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerSpeedCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
double kTestValue = 0.3504;
|
||||
pwm.setSpeed(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(kTestValue, callback.getSetValue(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue / 2 + 0.5, sim.getPosition(), PWM_STEP_SIZE * 2);
|
||||
assertEquals(kTestValue / 2 + 0.5, pwm.getPosition(), PWM_STEP_SIZE * 2);
|
||||
|
||||
kTestValue = -1.0;
|
||||
pwm.setSpeed(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(0.0, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(0.0, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 0.0;
|
||||
pwm.setSpeed(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(0.5, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(0.5, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 1.0;
|
||||
pwm.setSpeed(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 1.1;
|
||||
pwm.setSpeed(kTestValue);
|
||||
|
||||
assertEquals(1.0, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetPosition() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerPositionCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
double kTestValue = 0.3504;
|
||||
pwm.setPosition(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(kTestValue, callback.getSetValue(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue * 2 - 1.0, sim.getSpeed(), PWM_STEP_SIZE * 2);
|
||||
assertEquals(kTestValue * 2 - 1.0, pwm.getSpeed(), PWM_STEP_SIZE * 2);
|
||||
|
||||
kTestValue = -1.0;
|
||||
pwm.setPosition(kTestValue);
|
||||
|
||||
assertEquals(0.0, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(0.0, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 0.0;
|
||||
pwm.setPosition(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(-1.0, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(-1.0, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 1.0;
|
||||
pwm.setPosition(kTestValue);
|
||||
|
||||
assertEquals(kTestValue, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(kTestValue, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
|
||||
kTestValue = 1.1;
|
||||
pwm.setPosition(kTestValue);
|
||||
|
||||
assertEquals(1.0, sim.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, pwm.getPosition(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, sim.getSpeed(), PWM_STEP_SIZE);
|
||||
assertEquals(1.0, pwm.getSpeed(), PWM_STEP_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetPeriodScale() {
|
||||
void testSetOutputPeriod() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
@@ -176,30 +61,12 @@ class PWMSimTest {
|
||||
|
||||
IntCallback callback = new IntCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerPeriodScaleCallback(callback, false);
|
||||
try (CallbackStore cb = sim.registerOutputPeriodCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
sim.setPeriodScale(3504);
|
||||
assertEquals(3504, sim.getPeriodScale());
|
||||
sim.setOutputPeriod(3504);
|
||||
assertEquals(3504, sim.getOutputPeriod());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(3504, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetZeroLatch() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerZeroLatchCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
pwm.setZeroLatch();
|
||||
|
||||
assertTrue(callback.wasTriggered());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.Servo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ServoSimTest {
|
||||
@Test
|
||||
void testServo() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Servo servo = new Servo(0)) {
|
||||
ServoSim sim = new ServoSim(servo);
|
||||
|
||||
servo.set(0);
|
||||
assertEquals(0, sim.getPosition());
|
||||
|
||||
servo.set(0.354);
|
||||
assertEquals(0.354, sim.getPosition());
|
||||
|
||||
servo.setAngle(10);
|
||||
assertEquals(10, sim.getAngle());
|
||||
|
||||
servo.setAngle(90);
|
||||
assertEquals(90, sim.getAngle());
|
||||
|
||||
servo.setAngle(170);
|
||||
assertEquals(170, sim.getAngle());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user