mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpimath] Add timestamp getter to MathShared (#5091)
This makes it possible to mock the timestamp for wpimath without affecting the rest of the library. Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <wpi/timestamp.h>
|
||||
#include <wpimath/MathShared.h>
|
||||
|
||||
#include "WPILibVersion.h"
|
||||
@@ -138,6 +139,10 @@ class WPILibMathShared : public wpi::math::MathShared {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
units::second_t GetTimestamp() override {
|
||||
return units::second_t{wpi::Now() * 1.0e-6};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import edu.wpi.first.math.MathSharedStore;
|
||||
import edu.wpi.first.math.MathUsageId;
|
||||
import edu.wpi.first.networktables.MultiSubscriber;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
|
||||
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
|
||||
import edu.wpi.first.wpilibj.util.WPILibVersion;
|
||||
@@ -128,6 +129,11 @@ public abstract class RobotBase implements AutoCloseable {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTimestamp() {
|
||||
return WPIUtilJNI.now() * 1.0e-6;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,4 +20,11 @@ public interface MathShared {
|
||||
* @param count the usage count
|
||||
*/
|
||||
void reportUsage(MathUsageId id, int count);
|
||||
|
||||
/**
|
||||
* Get the current time.
|
||||
*
|
||||
* @return Time in seconds
|
||||
*/
|
||||
double getTimestamp();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
package edu.wpi.first.math;
|
||||
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
|
||||
public final class MathSharedStore {
|
||||
private static MathShared mathShared;
|
||||
|
||||
@@ -23,6 +25,11 @@ public final class MathSharedStore {
|
||||
|
||||
@Override
|
||||
public void reportUsage(MathUsageId id, int count) {}
|
||||
|
||||
@Override
|
||||
public double getTimestamp() {
|
||||
return WPIUtilJNI.now() * 1.0e-6;
|
||||
}
|
||||
};
|
||||
}
|
||||
return mathShared;
|
||||
@@ -56,4 +63,13 @@ public final class MathSharedStore {
|
||||
public static void reportUsage(MathUsageId id, int count) {
|
||||
getMathShared().reportUsage(id, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the time.
|
||||
*
|
||||
* @return The time in seconds.
|
||||
*/
|
||||
public static double getTimestamp() {
|
||||
return getMathShared().getTimestamp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.estimator;
|
||||
|
||||
import edu.wpi.first.math.MathSharedStore;
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
@@ -17,7 +18,6 @@ import edu.wpi.first.math.kinematics.DifferentialDriveKinematics;
|
||||
import edu.wpi.first.math.kinematics.DifferentialDriveOdometry;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N3;
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
@@ -292,7 +292,7 @@ public class DifferentialDrivePoseEstimator {
|
||||
public Pose2d update(
|
||||
Rotation2d gyroAngle, double distanceLeftMeters, double distanceRightMeters) {
|
||||
return updateWithTime(
|
||||
WPIUtilJNI.now() * 1.0e-6, gyroAngle, distanceLeftMeters, distanceRightMeters);
|
||||
MathSharedStore.getTimestamp(), gyroAngle, distanceLeftMeters, distanceRightMeters);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.estimator;
|
||||
|
||||
import edu.wpi.first.math.MathSharedStore;
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
@@ -18,7 +19,6 @@ import edu.wpi.first.math.kinematics.MecanumDriveOdometry;
|
||||
import edu.wpi.first.math.kinematics.MecanumDriveWheelPositions;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N3;
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
@@ -269,7 +269,7 @@ public class MecanumDrivePoseEstimator {
|
||||
* @return The estimated pose of the robot in meters.
|
||||
*/
|
||||
public Pose2d update(Rotation2d gyroAngle, MecanumDriveWheelPositions wheelPositions) {
|
||||
return updateWithTime(WPIUtilJNI.now() * 1.0e-6, gyroAngle, wheelPositions);
|
||||
return updateWithTime(MathSharedStore.getTimestamp(), gyroAngle, wheelPositions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.estimator;
|
||||
|
||||
import edu.wpi.first.math.MathSharedStore;
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
@@ -18,7 +19,6 @@ import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
|
||||
import edu.wpi.first.math.kinematics.SwerveModulePosition;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N3;
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
@@ -271,7 +271,7 @@ public class SwerveDrivePoseEstimator {
|
||||
* @return The estimated pose of the robot in meters.
|
||||
*/
|
||||
public Pose2d update(Rotation2d gyroAngle, SwerveModulePosition[] modulePositions) {
|
||||
return updateWithTime(WPIUtilJNI.now() * 1.0e-6, gyroAngle, modulePositions);
|
||||
return updateWithTime(MathSharedStore.getTimestamp(), gyroAngle, modulePositions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.filter;
|
||||
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import edu.wpi.first.math.MathSharedStore;
|
||||
|
||||
/**
|
||||
* A simple debounce filter for boolean streams. Requires that the boolean change value from
|
||||
@@ -60,11 +60,11 @@ public class Debouncer {
|
||||
}
|
||||
|
||||
private void resetTimer() {
|
||||
m_prevTimeSeconds = WPIUtilJNI.now() * 1e-6;
|
||||
m_prevTimeSeconds = MathSharedStore.getTimestamp();
|
||||
}
|
||||
|
||||
private boolean hasElapsed() {
|
||||
return (WPIUtilJNI.now() * 1e-6) - m_prevTimeSeconds >= m_debounceTimeSeconds;
|
||||
return MathSharedStore.getTimestamp() - m_prevTimeSeconds >= m_debounceTimeSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
package edu.wpi.first.math.filter;
|
||||
|
||||
import edu.wpi.first.math.MathSharedStore;
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
|
||||
/**
|
||||
* A class that limits the rate of change of an input value. Useful for implementing voltage,
|
||||
@@ -33,7 +33,7 @@ public class SlewRateLimiter {
|
||||
m_positiveRateLimit = positiveRateLimit;
|
||||
m_negativeRateLimit = negativeRateLimit;
|
||||
m_prevVal = initialValue;
|
||||
m_prevTime = WPIUtilJNI.now() * 1e-6;
|
||||
m_prevTime = MathSharedStore.getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ public class SlewRateLimiter {
|
||||
* @return The filtered value, which will not change faster than the slew rate.
|
||||
*/
|
||||
public double calculate(double input) {
|
||||
double currentTime = WPIUtilJNI.now() * 1e-6;
|
||||
double currentTime = MathSharedStore.getTimestamp();
|
||||
double elapsedTime = currentTime - m_prevTime;
|
||||
m_prevVal +=
|
||||
MathUtil.clamp(
|
||||
@@ -85,6 +85,6 @@ public class SlewRateLimiter {
|
||||
*/
|
||||
public void reset(double value) {
|
||||
m_prevVal = value;
|
||||
m_prevTime = WPIUtilJNI.now() * 1e-6;
|
||||
m_prevTime = MathSharedStore.getTimestamp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "units/time.h"
|
||||
|
||||
using namespace wpi::math;
|
||||
|
||||
@@ -15,6 +18,9 @@ class DefaultMathShared : public MathShared {
|
||||
void ReportWarningV(fmt::string_view format, fmt::format_args args) override {
|
||||
}
|
||||
void ReportUsage(MathUsageId id, int count) override {}
|
||||
units::second_t GetTimestamp() override {
|
||||
return units::second_t{wpi::Now() * 1.0e-6};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "frc/StateSpaceUtil.h"
|
||||
#include "frc/estimator/AngleStatistics.h"
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
@@ -153,7 +154,7 @@ void DifferentialDrivePoseEstimator::AddVisionMeasurement(
|
||||
Pose2d DifferentialDrivePoseEstimator::Update(const Rotation2d& gyroAngle,
|
||||
units::meter_t leftDistance,
|
||||
units::meter_t rightDistance) {
|
||||
return UpdateWithTime(units::microsecond_t(wpi::Now()), gyroAngle,
|
||||
return UpdateWithTime(wpi::math::MathSharedStore::GetTimestamp(), gyroAngle,
|
||||
leftDistance, rightDistance);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "frc/StateSpaceUtil.h"
|
||||
#include "frc/estimator/AngleStatistics.h"
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
@@ -163,7 +164,7 @@ void frc::MecanumDrivePoseEstimator::AddVisionMeasurement(
|
||||
Pose2d frc::MecanumDrivePoseEstimator::Update(
|
||||
const Rotation2d& gyroAngle,
|
||||
const MecanumDriveWheelPositions& wheelPositions) {
|
||||
return UpdateWithTime(units::microsecond_t(wpi::Now()), gyroAngle,
|
||||
return UpdateWithTime(wpi::math::MathSharedStore::GetTimestamp(), gyroAngle,
|
||||
wheelPositions);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "frc/filter/Debouncer.h"
|
||||
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
|
||||
@@ -21,11 +23,12 @@ Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
|
||||
}
|
||||
|
||||
void Debouncer::ResetTimer() {
|
||||
m_prevTime = units::microsecond_t(wpi::Now());
|
||||
m_prevTime = wpi::math::MathSharedStore::GetTimestamp();
|
||||
}
|
||||
|
||||
bool Debouncer::HasElapsed() const {
|
||||
return units::microsecond_t(wpi::Now()) - m_prevTime >= m_debounceTime;
|
||||
return wpi::math::MathSharedStore::GetTimestamp() - m_prevTime >=
|
||||
m_debounceTime;
|
||||
}
|
||||
|
||||
bool Debouncer::Calculate(bool input) {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "frc/kinematics/SwerveDriveKinematics.h"
|
||||
#include "frc/kinematics/SwerveDriveOdometry.h"
|
||||
#include "units/time.h"
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -272,7 +273,7 @@ class SwerveDrivePoseEstimator {
|
||||
Pose2d Update(
|
||||
const Rotation2d& gyroAngle,
|
||||
const wpi::array<SwerveModulePosition, NumModules>& modulePositions) {
|
||||
return UpdateWithTime(units::microsecond_t(wpi::Now()), gyroAngle,
|
||||
return UpdateWithTime(wpi::math::MathSharedStore::GetTimestamp(), gyroAngle,
|
||||
modulePositions);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "units/time.h"
|
||||
#include "wpimath/MathShared.h"
|
||||
|
||||
namespace frc {
|
||||
/**
|
||||
@@ -45,7 +46,8 @@ class SlewRateLimiter {
|
||||
: m_positiveRateLimit{positiveRateLimit},
|
||||
m_negativeRateLimit{negativeRateLimit},
|
||||
m_prevVal{initialValue},
|
||||
m_prevTime{units::microsecond_t(wpi::Now())} {}
|
||||
m_prevTime{
|
||||
units::microsecond_t(wpi::math::MathSharedStore::GetTimestamp())} {}
|
||||
|
||||
/**
|
||||
* Creates a new SlewRateLimiter with the given positive rate limit and
|
||||
@@ -77,7 +79,7 @@ class SlewRateLimiter {
|
||||
* rate.
|
||||
*/
|
||||
Unit_t Calculate(Unit_t input) {
|
||||
units::second_t currentTime = units::microsecond_t(wpi::Now());
|
||||
units::second_t currentTime = wpi::math::MathSharedStore::GetTimestamp();
|
||||
units::second_t elapsedTime = currentTime - m_prevTime;
|
||||
m_prevVal +=
|
||||
std::clamp(input - m_prevVal, m_negativeRateLimit * elapsedTime,
|
||||
@@ -94,7 +96,7 @@ class SlewRateLimiter {
|
||||
*/
|
||||
void Reset(Unit_t value) {
|
||||
m_prevVal = value;
|
||||
m_prevTime = units::microsecond_t(wpi::Now());
|
||||
m_prevTime = wpi::math::MathSharedStore::GetTimestamp();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SymbolExports.h>
|
||||
|
||||
#include "units/time.h"
|
||||
|
||||
namespace wpi::math {
|
||||
|
||||
enum class MathUsageId {
|
||||
@@ -31,6 +33,7 @@ class WPILIB_DLLEXPORT MathShared {
|
||||
virtual void ReportWarningV(fmt::string_view format,
|
||||
fmt::format_args args) = 0;
|
||||
virtual void ReportUsage(MathUsageId id, int count) = 0;
|
||||
virtual units::second_t GetTimestamp() = 0;
|
||||
|
||||
template <typename S, typename... Args>
|
||||
inline void ReportError(const S& format, Args&&... args) {
|
||||
@@ -70,6 +73,10 @@ class WPILIB_DLLEXPORT MathSharedStore {
|
||||
static void ReportUsage(MathUsageId id, int count) {
|
||||
GetMathShared().ReportUsage(id, count);
|
||||
}
|
||||
|
||||
static units::second_t GetTimestamp() {
|
||||
return GetMathShared().GetTimestamp();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace wpi::math
|
||||
|
||||
Reference in New Issue
Block a user