[wpilib, examples] Cleanup PotentiometerPID, Ultrasonic, UltrasonicPID examples (#4893)

Fix C++ Ultrasonic to return correct units.
This commit is contained in:
Starlight220
2023-01-09 02:33:07 +02:00
committed by GitHub
parent babb0c1fcf
commit 2cd9be413f
24 changed files with 1052 additions and 301 deletions

View File

@@ -90,9 +90,6 @@ public class Ultrasonic implements Sendable, AutoCloseable {
m_pingChannel.setSimDevice(m_simDevice);
m_echoChannel.setSimDevice(m_simDevice);
}
if (m_task == null) {
m_task = new UltrasonicChecker();
}
final boolean originalMode = m_automaticEnabled;
setAutomaticMode(false); // kill task when adding a new sensor
m_sensors.add(this);
@@ -202,7 +199,7 @@ public class Ultrasonic implements Sendable, AutoCloseable {
* sensors fire at the same time. If another scheduling algorithm is preferred, it can be
* implemented by pinging the sensors manually and waiting for the results to come back.
*/
public static void setAutomaticMode(boolean enabling) {
public static synchronized void setAutomaticMode(boolean enabling) {
if (enabling == m_automaticEnabled) {
return; // ignore the case of no change
}
@@ -217,14 +214,18 @@ public class Ultrasonic implements Sendable, AutoCloseable {
}
// Start round robin task
m_task = new UltrasonicChecker();
m_task.start();
} else {
// Wait for background task to stop running
try {
m_task.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
if (m_task != null) {
// Wait for background task to stop running
try {
m_task.join();
m_task = null;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
}
}
/* Clear all the counters (data now invalid) since automatic mode is

View File

@@ -9,6 +9,7 @@ import edu.wpi.first.hal.SimDouble;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.Ultrasonic;
/** Class to control a simulated {@link edu.wpi.first.wpilibj.Ultrasonic}. */
public class UltrasonicSim {
private final SimBoolean m_simRangeValid;
private final SimDouble m_simRange;
@@ -19,7 +20,18 @@ public class UltrasonicSim {
* @param ultrasonic The real ultrasonic to simulate
*/
public UltrasonicSim(Ultrasonic ultrasonic) {
SimDeviceSim simDevice = new SimDeviceSim("Ultrasonic", ultrasonic.getEchoChannel());
// ping parameter is unused
this(-1, ultrasonic.getEchoChannel());
}
/**
* Constructor.
*
* @param ping unused.
* @param echo the ultrasonic's echo channel.
*/
public UltrasonicSim(@SuppressWarnings("unused") int ping, int echo) {
SimDeviceSim simDevice = new SimDeviceSim("Ultrasonic", echo);
m_simRangeValid = simDevice.getBoolean("Range Valid");
m_simRange = simDevice.getDouble("Range (in)");
}

View File

@@ -4,12 +4,15 @@
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.wpi.first.wpilibj.simulation.UltrasonicSim;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class UltrasonicTest {
@Test
@@ -28,4 +31,23 @@ class UltrasonicTest {
assertEquals(0, ultrasonic.getRangeInches());
}
}
@Test
void automaticModeToggle() {
try (@SuppressWarnings("unused")
Ultrasonic ultrasonic = new Ultrasonic(0, 1)) {
assertDoesNotThrow(
() -> {
Ultrasonic.setAutomaticMode(true);
Ultrasonic.setAutomaticMode(false);
Ultrasonic.setAutomaticMode(true);
});
}
}
@ValueSource(booleans = {true, false})
@ParameterizedTest
void automaticModeWithZeroInstances(boolean enabling) {
assertDoesNotThrow(() -> Ultrasonic.setAutomaticMode(enabling));
}
}