wpilibc: Add overloads for units (#1815)

Add unit-taking overloads to the following classes:
- IterativeRobotBase
- LinearFilter
- Notifier
- TimedRobot
- Timer (HasPeriodPassed only)
- frc2::PIDController

The corresponding non-units-taking functions have been deprecated.

The return value of TimedRobot::GetPeriod() was updated.
This is a breaking change, users should use to<double> to get the value in seconds.

Other return values, e.g. Timer::Get(), have NOT been updated due to much wider use.
This commit is contained in:
Prateek Machiraju
2019-08-17 00:56:48 -04:00
committed by Peter Johnson
parent f1d71da8a9
commit c07ac23532
15 changed files with 136 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -7,6 +7,9 @@
#pragma once
#include <units/units.h>
#include <wpi/deprecated.h>
#include "frc/RobotBase.h"
#include "frc/Watchdog.h"
@@ -141,8 +144,16 @@ class IterativeRobotBase : public RobotBase {
*
* @param period Period in seconds.
*/
WPI_DEPRECATED("Use ctor with unit-safety instead.")
explicit IterativeRobotBase(double period);
/**
* Constructor for IterativeRobotBase.
*
* @param period Period.
*/
explicit IterativeRobotBase(units::second_t period);
virtual ~IterativeRobotBase() = default;
IterativeRobotBase(IterativeRobotBase&&) = default;

View File

@@ -10,6 +10,7 @@
#include <initializer_list>
#include <vector>
#include <units/units.h>
#include <wpi/ArrayRef.h>
#include "frc/circular_buffer.h"
@@ -99,7 +100,8 @@ class LinearFilter {
* @param period The period in seconds between samples taken by the
* user.
*/
static LinearFilter SinglePoleIIR(double timeConstant, double period);
static LinearFilter SinglePoleIIR(double timeConstant,
units::second_t period);
/**
* Creates a first-order high-pass filter of the form:<br>
@@ -112,7 +114,7 @@ class LinearFilter {
* @param period The period in seconds between samples taken by the
* user.
*/
static LinearFilter HighPass(double timeConstant, double period);
static LinearFilter HighPass(double timeConstant, units::second_t period);
/**
* Creates a K-tap FIR moving average filter of the form:<br>

View File

@@ -15,6 +15,8 @@
#include <utility>
#include <hal/Types.h>
#include <units/units.h>
#include <wpi/deprecated.h>
#include <wpi/mutex.h>
#include "frc/ErrorBase.h"
@@ -58,8 +60,18 @@ class Notifier : public ErrorBase {
*
* @param delay Seconds to wait before the handler is called.
*/
WPI_DEPRECATED("Use unit-safe StartSingle method instead.")
void StartSingle(double delay);
/**
* Register for single event notification.
*
* A timer event is queued for a single event after the specified delay.
*
* @param delay Amount of time to wait before the handler is called.
*/
void StartSingle(units::second_t delay);
/**
* Register for periodic event notification.
*
@@ -70,8 +82,21 @@ class Notifier : public ErrorBase {
* @param period Period in seconds to call the handler starting one period
* after the call to this method.
*/
WPI_DEPRECATED("Use unit-safe StartPeriodic method instead.")
void StartPeriodic(double period);
/**
* Register for periodic event notification.
*
* A timer event is queued for periodic event notification. Each time the
* interrupt occurs, the event will be immediately requeued for the same time
* interval.
*
* @param period Period to call the handler starting one period
* after the call to this method.
*/
void StartPeriodic(units::second_t period);
/**
* Stop timer events from occuring.
*

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -8,6 +8,8 @@
#pragma once
#include <hal/Types.h>
#include <units/units.h>
#include <wpi/deprecated.h>
#include "frc/ErrorBase.h"
#include "frc/IterativeRobotBase.h"
@@ -25,7 +27,7 @@ namespace frc {
*/
class TimedRobot : public IterativeRobotBase, public ErrorBase {
public:
static constexpr double kDefaultPeriod = 0.02;
static constexpr units::second_t kDefaultPeriod = 20_ms;
/**
* Provide an alternate "main loop" via StartCompetition().
@@ -35,14 +37,22 @@ class TimedRobot : public IterativeRobotBase, public ErrorBase {
/**
* Get the time period between calls to Periodic() functions.
*/
double GetPeriod() const;
units::second_t GetPeriod() const;
/**
* Constructor for TimedRobot.
*
* @param period Period in seconds.
*/
explicit TimedRobot(double period = kDefaultPeriod);
WPI_DEPRECATED("Use constructor with unit-safety instead.")
explicit TimedRobot(double period);
/**
* Constructor for TimedRobot.
*
* @param period Period.
*/
explicit TimedRobot(units::second_t period = kDefaultPeriod);
~TimedRobot() override;

View File

@@ -7,6 +7,7 @@
#pragma once
#include <units/units.h>
#include <wpi/deprecated.h>
#include <wpi/mutex.h>
@@ -101,8 +102,19 @@ class Timer {
* @param period The period to check for (in seconds).
* @return True if the period has passed.
*/
WPI_DEPRECATED("Use unit-safe HasPeriodPassed method instead.")
bool HasPeriodPassed(double period);
/**
* Check if the period specified has passed and if it has, advance the start
* time by that period. This is useful to decide if it's time to do periodic
* work without drifting later by the time it took to get around to checking.
*
* @param period The period to check for.
* @return True if the period has passed.
*/
bool HasPeriodPassed(units::second_t period);
/**
* Return the FPGA system clock time in seconds.
*

View File

@@ -10,6 +10,8 @@
#include <functional>
#include <limits>
#include <units/units.h>
#include "frc/smartdashboard/SendableBase.h"
namespace frc2 {
@@ -30,7 +32,8 @@ class PIDController : public frc::SendableBase {
* @param period The period between controller updates in seconds. The
* default is 0.02 seconds.
*/
PIDController(double Kp, double Ki, double Kd, double period = 0.02);
PIDController(double Kp, double Ki, double Kd,
units::second_t period = 20_ms);
~PIDController() override = default;
@@ -97,7 +100,7 @@ class PIDController : public frc::SendableBase {
*
* @return The period of the controller.
*/
double GetPeriod() const;
units::second_t GetPeriod() const;
/**
* Returns the current controller output.