2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2020-07-15 23:48:09 -07:00
|
|
|
#include "frc/simulation/CallbackStore.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
namespace frc {
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
class Encoder;
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
namespace sim {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to control a simulated encoder.
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
class EncoderSim {
|
|
|
|
|
public:
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from an Encoder object.
|
|
|
|
|
*
|
|
|
|
|
* @param encoder Encoder to simulate
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
explicit EncoderSim(const Encoder& encoder);
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an EncoderSim for a digital input channel. Encoders take two
|
|
|
|
|
* channels, so either one may be specified.
|
|
|
|
|
*
|
|
|
|
|
* @param channel digital input channel
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
* @throws NoSuchElementException if no Encoder is configured for that channel
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static EncoderSim CreateForChannel(int channel);
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an EncoderSim for a simulated index.
|
|
|
|
|
* The index is incremented for each simulated Encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param index simulator index
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static EncoderSim CreateForIndex(int index);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the Initialized property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the Initialized
|
|
|
|
|
* property is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the Initialized value of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return true if initialized
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
bool GetInitialized() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the Initialized value of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param initialized the new value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetInitialized(bool initialized);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the count property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the count
|
|
|
|
|
* property is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterCountCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the count of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the count
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
int GetCount() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the count of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param count the new count
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetCount(int count);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the period is
|
|
|
|
|
* changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterPeriodCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the encoder period
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
double GetPeriod() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the encoder period.
|
|
|
|
|
*
|
|
|
|
|
* @param period the new period
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetPeriod(double period);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be called whenever the encoder is reset.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterResetCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Check if the encoder has been reset.
|
|
|
|
|
*
|
|
|
|
|
* @return true if reset
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
bool GetReset() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the reset property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param reset the new value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetReset(bool reset);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run whenever the max period of the encoder is
|
|
|
|
|
* changed.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterMaxPeriodCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the max period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the max period of the encoder
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
double GetMaxPeriod() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the max period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param maxPeriod the new value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetMaxPeriod(double maxPeriod);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the direction
|
|
|
|
|
* is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterDirectionCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the direction of the encoder
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
bool GetDirection() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param direction the new direction
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetDirection(bool direction);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the reverse direction.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the reverse
|
|
|
|
|
* direction is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterReverseDirectionCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the reverse direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the reverse direction of the encoder
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
bool GetReverseDirection() const;
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the reverse direction.
|
|
|
|
|
*
|
|
|
|
|
* @param reverseDirection the new value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetReverseDirection(bool reverseDirection);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the samples-to-average value of this encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the
|
|
|
|
|
* samples-to-average is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterSamplesToAverageCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the samples-to-average value.
|
|
|
|
|
*
|
|
|
|
|
* @return the samples-to-average value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
int GetSamplesToAverage() const;
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the samples-to-average value.
|
|
|
|
|
*
|
|
|
|
|
* @param samplesToAverage the new value
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetSamplesToAverage(int samplesToAverage);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the distance per pulse value of this encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the
|
|
|
|
|
* distance per pulse is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
|
|
|
|
* @return the CallbackStore object associated with this callback
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
[[nodiscard]]
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterDistancePerPulseCallback(
|
2020-07-15 23:48:09 -07:00
|
|
|
NotifyCallback callback, bool initialNotify);
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the distance per pulse of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the encoder distance per pulse
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
double GetDistancePerPulse() const;
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the encoder distance per pulse.
|
|
|
|
|
*
|
|
|
|
|
* @param distancePerPulse the new distance per pulse
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetDistancePerPulse(double distancePerPulse);
|
2018-07-12 23:11:26 -04:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Resets all simulation data for this encoder.
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void ResetData();
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the encoder distance.
|
|
|
|
|
*
|
|
|
|
|
* @param distance the new distance
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetDistance(double distance);
|
2020-04-03 08:33:38 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the distance of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the encoder distance
|
|
|
|
|
*/
|
2023-09-18 18:18:18 +03:00
|
|
|
double GetDistance() const;
|
2020-04-03 08:33:38 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the rate of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param rate the new rate
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
void SetRate(double rate);
|
2020-04-03 08:33:38 -07:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the rate of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the rate of change
|
|
|
|
|
*/
|
2023-09-18 18:18:18 +03:00
|
|
|
double GetRate() const;
|
2020-04-03 08:33:38 -07:00
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
private:
|
2020-07-04 10:10:43 -07:00
|
|
|
explicit EncoderSim(int index) : m_index{index} {}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
int m_index;
|
|
|
|
|
};
|
|
|
|
|
} // namespace sim
|
|
|
|
|
} // namespace frc
|