[wpilib] Add a few unit overloads (#8231)

Co-authored-by: Sam Carlberg <sam@slfc.dev>
Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com>
Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
Murat65536
2025-10-29 03:18:55 +00:00
committed by GitHub
parent 58ba536351
commit 8b99ad82c3
11 changed files with 182 additions and 9 deletions

View File

@@ -182,6 +182,10 @@ void Notifier::StartPeriodic(units::second_t period) {
UpdateAlarm();
}
void Notifier::StartPeriodic(units::hertz_t frequency) {
StartPeriodic(1 / frequency);
}
void Notifier::Stop() {
std::scoped_lock lock(m_processMutex);
m_periodic = false;

View File

@@ -91,6 +91,8 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
HALUsageReporting::kFramework_Timed);
}
TimedRobot::TimedRobot(units::hertz_t frequency) : TimedRobot{1 / frequency} {}
TimedRobot::~TimedRobot() {
if (m_notifier != HAL_kInvalidHandle) {
int32_t status = 0;

View File

@@ -14,6 +14,7 @@
#include <utility>
#include <hal/Types.h>
#include <units/frequency.h>
#include <units/time.h>
#include <wpi/mutex.h>
@@ -107,6 +108,17 @@ class Notifier {
*/
void StartPeriodic(units::second_t period);
/**
* Run the callback periodically with the given frequency.
*
* The user-provided callback should be written so that it completes before
* the next time it's scheduled to run.
*
* @param frequency Frequency after which to call the callback starting one
* period after the call to this method.
*/
void StartPeriodic(units::hertz_t frequency);
/**
* Stop further callback invocations.
*

View File

@@ -11,6 +11,7 @@
#include <hal/Notifier.h>
#include <hal/Types.h>
#include <units/frequency.h>
#include <units/math.h>
#include <units/time.h>
#include <wpi/priority_queue.h>
@@ -47,10 +48,17 @@ class TimedRobot : public IterativeRobotBase {
/**
* Constructor for TimedRobot.
*
* @param period Period.
* @param period The period of the robot loop function.
*/
explicit TimedRobot(units::second_t period = kDefaultPeriod);
/**
* Constructor for TimedRobot.
*
* @param frequency The frequency of the robot loop function.
*/
explicit TimedRobot(units::hertz_t frequency);
TimedRobot(TimedRobot&&) = default;
TimedRobot& operator=(TimedRobot&&) = default;

View File

@@ -4,9 +4,12 @@
package edu.wpi.first.wpilibj;
import static edu.wpi.first.units.Units.Seconds;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.hal.NotifierJNI;
import edu.wpi.first.units.measure.Frequency;
import edu.wpi.first.units.measure.Time;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
@@ -186,6 +189,15 @@ public class Notifier implements AutoCloseable {
}
}
/**
* Run the callback once after the given delay.
*
* @param delay Time to wait before the callback is called.
*/
public void startSingle(Time delay) {
startSingle(delay.in(Seconds));
}
/**
* Run the callback periodically with the given period.
*
@@ -207,6 +219,32 @@ public class Notifier implements AutoCloseable {
}
}
/**
* Run the callback periodically with the given period.
*
* <p>The user-provided callback should be written so that it completes before the next time it's
* scheduled to run.
*
* @param period Period after which to call the callback starting one period after the call to
* this method.
*/
public void startPeriodic(Time period) {
startPeriodic(period.in(Seconds));
}
/**
* Run the callback periodically with the given frequency.
*
* <p>The user-provided callback should be written so that it completes before the next time it's
* scheduled to run.
*
* @param frequency Frequency at which to call the callback, starting one period after the call to
* this method.
*/
public void startPeriodic(Frequency frequency) {
startPeriodic(frequency.asPeriod());
}
/**
* Stop further callback invocations.
*

View File

@@ -11,6 +11,7 @@ import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.NotifierJNI;
import edu.wpi.first.units.measure.Frequency;
import edu.wpi.first.units.measure.Time;
import java.util.PriorityQueue;
@@ -84,7 +85,7 @@ public class TimedRobot extends IterativeRobotBase {
/**
* Constructor for TimedRobot.
*
* @param period Period in seconds.
* @param period The period of the robot loop function.
*/
protected TimedRobot(double period) {
super(period);
@@ -95,6 +96,24 @@ public class TimedRobot extends IterativeRobotBase {
HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed);
}
/**
* Constructor for TimedRobot.
*
* @param period The period of the robot loop function.
*/
protected TimedRobot(Time period) {
this(period.in(Seconds));
}
/**
* Constructor for TimedRobot.
*
* @param frequency The frequency of the robot loop function.
*/
protected TimedRobot(Frequency frequency) {
this(frequency.asPeriod());
}
@Override
public void close() {
NotifierJNI.stopNotifier(m_notifier);

View File

@@ -4,6 +4,10 @@
package edu.wpi.first.wpilibj;
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.units.measure.Time;
/**
* A timer class.
*
@@ -46,10 +50,20 @@ public class Timer {
}
/**
* Pause the thread for a specified time. Pause the execution of the thread for a specified period
* of time given in seconds. Motors will continue to run at their last assigned values, and
* sensors will continue to update. Only the task containing the wait will pause until the wait
* time is expired.
* Pause the execution of the thread for a specified period of time. Motors will continue to run
* at their last assigned values, and sensors will continue to update. Only the task containing
* the wait will pause until the wait time is expired.
*
* @param period Length of time to pause
*/
public static void delay(final Time period) {
delay(period.in(Seconds));
}
/**
* Pause the execution of the thread for a specified period of time given in seconds. Motors will
* continue to run at their last assigned values, and sensors will continue to update. Only the
* task containing the wait will pause until the wait time is expired.
*
* @param seconds Length of time to pause
*/
@@ -137,7 +151,17 @@ public class Timer {
/**
* Check if the period specified has passed.
*
* @param seconds The period to check.
* @param period The period to check.
* @return Whether the period has passed.
*/
public boolean hasElapsed(Time period) {
return hasElapsed(period.in(Seconds));
}
/**
* Check if the period specified has passed.
*
* @param seconds The period to check in seconds.
* @return Whether the period has passed.
*/
public boolean hasElapsed(double seconds) {

View File

@@ -4,7 +4,10 @@
package edu.wpi.first.wpilibj;
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.NotifierJNI;
import edu.wpi.first.units.measure.Time;
import java.io.Closeable;
import java.util.PriorityQueue;
import java.util.concurrent.locks.ReentrantLock;
@@ -55,6 +58,16 @@ public class Watchdog implements Closeable, Comparable<Watchdog> {
m_tracer = new Tracer();
}
/**
* Watchdog constructor.
*
* @param timeout The watchdog's timeout with microsecond resolution.
* @param callback This function is called when the timeout expires.
*/
public Watchdog(Time timeout, Runnable callback) {
this(timeout.in(Seconds), callback);
}
@Override
public void close() {
disable();

View File

@@ -4,9 +4,11 @@
package edu.wpi.first.wpilibj.event;
import static edu.wpi.first.units.Units.Seconds;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.math.filter.Debouncer;
import edu.wpi.first.units.measure.Time;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
@@ -167,7 +169,18 @@ public class BooleanEvent implements BooleanSupplier {
* Creates a new debounced event from this event - it will become active when this event has been
* active for longer than the specified period.
*
* @param seconds The debounce period.
* @param period The debounce period.
* @return The debounced event (rising edges debounced only).
*/
public BooleanEvent debounce(Time period) {
return debounce(period.in(Seconds));
}
/**
* Creates a new debounced event from this event - it will become active when this event has been
* active for longer than the specified period.
*
* @param seconds The debounce period in seconds.
* @return The debounced event (rising edges debounced only).
*/
public BooleanEvent debounce(double seconds) {
@@ -178,7 +191,19 @@ public class BooleanEvent implements BooleanSupplier {
* Creates a new debounced event from this event - it will become active when this event has been
* active for longer than the specified period.
*
* @param seconds The debounce period.
* @param period The debounce period.
* @param type The debounce type.
* @return The debounced event.
*/
public BooleanEvent debounce(Time period, Debouncer.DebounceType type) {
return debounce(period.in(Seconds), type);
}
/**
* Creates a new debounced event from this event - it will become active when this event has been
* active for longer than the specified period.
*
* @param seconds The debounce period in seconds.
* @param type The debounce type.
* @return The debounced event.
*/

View File

@@ -4,11 +4,14 @@
package edu.wpi.first.wpilibj.smartdashboard;
import static edu.wpi.first.units.Units.Meters;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.networktables.NTSendable;
import edu.wpi.first.networktables.NTSendableBuilder;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.units.measure.Distance;
import edu.wpi.first.util.sendable.SendableRegistry;
import java.util.ArrayList;
import java.util.List;
@@ -66,6 +69,17 @@ public class Field2d implements NTSendable, AutoCloseable {
m_objects.get(0).setPose(xMeters, yMeters, rotation);
}
/**
* Set the robot pose from x, y, and rotation.
*
* @param x X location
* @param y Y location
* @param rotation rotation
*/
public synchronized void setRobotPose(Distance x, Distance y, Rotation2d rotation) {
m_objects.get(0).setPose(x.in(Meters), y.in(Meters), rotation);
}
/**
* Get the robot pose.
*

View File

@@ -4,11 +4,14 @@
package edu.wpi.first.wpilibj.smartdashboard;
import static edu.wpi.first.units.Units.Meters;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.trajectory.Trajectory;
import edu.wpi.first.networktables.DoubleArrayEntry;
import edu.wpi.first.units.measure.Distance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -51,6 +54,17 @@ public class FieldObject2d implements AutoCloseable {
setPose(new Pose2d(xMeters, yMeters, rotation));
}
/**
* Set the pose from x, y, and rotation.
*
* @param x X location
* @param y Y location
* @param rotation rotation
*/
public synchronized void setPose(Distance x, Distance y, Rotation2d rotation) {
setPose(new Pose2d(x.in(Meters), y.in(Meters), rotation));
}
/**
* Get the pose.
*