mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
[hal, wpilib] Add initial systemcore counter implementation (#7723)
This commit is contained in:
@@ -1,469 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Counter.h>
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/AnalogTrigger.h"
|
||||
#include "frc/CounterBase.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class DigitalGlitchFilter;
|
||||
|
||||
/**
|
||||
* Class for counting the number of ticks on a digital input channel.
|
||||
*
|
||||
* This is a general purpose class for counting repetitive events. It can return
|
||||
* the number of counts, the period of the most recent cycle, and detect when
|
||||
* the signal being counted has stopped by supplying a maximum cycle time.
|
||||
*
|
||||
* All counters will immediately start counting - Reset() them if you need them
|
||||
* to be zeroed before use.
|
||||
*/
|
||||
class Counter : public CounterBase,
|
||||
public wpi::Sendable,
|
||||
public wpi::SendableHelper<Counter> {
|
||||
public:
|
||||
enum Mode {
|
||||
kTwoPulse = 0,
|
||||
kSemiperiod = 1,
|
||||
kPulseLength = 2,
|
||||
kExternalDirection = 3
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an instance of a counter where no sources are selected.
|
||||
*
|
||||
* They all must be selected by calling functions to specify the up source and
|
||||
* the down source independently.
|
||||
*
|
||||
* This creates a ChipObject counter and initializes status variables
|
||||
* appropriately.
|
||||
*
|
||||
* The counter will start counting immediately.
|
||||
*
|
||||
* @param mode The counter mode
|
||||
*/
|
||||
explicit Counter(Mode mode = kTwoPulse);
|
||||
|
||||
/**
|
||||
* Create an instance of a Counter object.
|
||||
*
|
||||
* Create an up-Counter instance given a channel.
|
||||
*
|
||||
* The counter will start counting immediately.
|
||||
*
|
||||
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
||||
* 10-25 are on the MXP
|
||||
*/
|
||||
explicit Counter(int channel);
|
||||
|
||||
/**
|
||||
* Create an instance of a counter from a Digital Source (such as a Digital
|
||||
* Input).
|
||||
*
|
||||
* This is used if an existing digital input is to be shared by multiple other
|
||||
* objects such as encoders or if the Digital Source is not a Digital Input
|
||||
* channel (such as an Analog %Trigger).
|
||||
*
|
||||
* The counter will start counting immediately.
|
||||
* @param source A pointer to the existing DigitalSource object. It will be
|
||||
* set as the Up Source.
|
||||
*/
|
||||
explicit Counter(DigitalSource* source);
|
||||
|
||||
/**
|
||||
* Create an instance of a counter from a Digital Source (such as a Digital
|
||||
* Input).
|
||||
*
|
||||
* This is used if an existing digital input is to be shared by multiple other
|
||||
* objects such as encoders or if the Digital Source is not a Digital Input
|
||||
* channel (such as an Analog %Trigger).
|
||||
*
|
||||
* The counter will start counting immediately.
|
||||
*
|
||||
* @param source A pointer to the existing DigitalSource object. It will be
|
||||
* set as the Up Source.
|
||||
*/
|
||||
explicit Counter(std::shared_ptr<DigitalSource> source);
|
||||
|
||||
/**
|
||||
* Create an instance of a Counter object.
|
||||
*
|
||||
* Create an instance of a simple up-Counter given an analog trigger.
|
||||
* Use the trigger state output from the analog trigger.
|
||||
*
|
||||
* The counter will start counting immediately.
|
||||
*
|
||||
* @param trigger The reference to the existing AnalogTrigger object.
|
||||
*/
|
||||
explicit Counter(const AnalogTrigger& trigger);
|
||||
|
||||
/**
|
||||
* Create an instance of a Counter object.
|
||||
*
|
||||
* Creates a full up-down counter given two Digital Sources.
|
||||
*
|
||||
* @param encodingType The quadrature decoding mode (1x or 2x)
|
||||
* @param upSource The pointer to the DigitalSource to set as the up
|
||||
* source
|
||||
* @param downSource The pointer to the DigitalSource to set as the down
|
||||
* source
|
||||
* @param inverted True to invert the output (reverse the direction)
|
||||
*/
|
||||
Counter(EncodingType encodingType, DigitalSource* upSource,
|
||||
DigitalSource* downSource, bool inverted);
|
||||
|
||||
/**
|
||||
* Create an instance of a Counter object.
|
||||
*
|
||||
* Creates a full up-down counter given two Digital Sources.
|
||||
*
|
||||
* @param encodingType The quadrature decoding mode (1x or 2x)
|
||||
* @param upSource The pointer to the DigitalSource to set as the up
|
||||
* source
|
||||
* @param downSource The pointer to the DigitalSource to set as the down
|
||||
* source
|
||||
* @param inverted True to invert the output (reverse the direction)
|
||||
*/
|
||||
Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
|
||||
std::shared_ptr<DigitalSource> downSource, bool inverted);
|
||||
|
||||
Counter(Counter&&) = default;
|
||||
Counter& operator=(Counter&&) = default;
|
||||
|
||||
~Counter() override;
|
||||
|
||||
/**
|
||||
* Set the up source for the counter as a digital input channel.
|
||||
*
|
||||
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
||||
* 10-25 are on the MXP
|
||||
*/
|
||||
void SetUpSource(int channel);
|
||||
|
||||
/**
|
||||
* Set the up counting source to be an analog trigger.
|
||||
*
|
||||
* @param analogTrigger The analog trigger object that is used for the Up
|
||||
* Source
|
||||
* @param triggerType The analog trigger output that will trigger the
|
||||
* counter.
|
||||
*/
|
||||
void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
|
||||
|
||||
/**
|
||||
* Set the up counting source to be an analog trigger.
|
||||
*
|
||||
* @param analogTrigger The analog trigger object that is used for the Up
|
||||
* Source
|
||||
* @param triggerType The analog trigger output that will trigger the
|
||||
* counter.
|
||||
*/
|
||||
void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
|
||||
AnalogTriggerType triggerType);
|
||||
|
||||
void SetUpSource(DigitalSource* source);
|
||||
|
||||
/**
|
||||
* Set the source object that causes the counter to count up.
|
||||
*
|
||||
* Set the up counting DigitalSource.
|
||||
*
|
||||
* @param source Pointer to the DigitalSource object to set as the up source
|
||||
*/
|
||||
void SetUpSource(std::shared_ptr<DigitalSource> source);
|
||||
|
||||
/**
|
||||
* Set the source object that causes the counter to count up.
|
||||
*
|
||||
* Set the up counting DigitalSource.
|
||||
*
|
||||
* @param source Reference to the DigitalSource object to set as the up source
|
||||
*/
|
||||
void SetUpSource(DigitalSource& source);
|
||||
|
||||
/**
|
||||
* Set the edge sensitivity on an up counting source.
|
||||
*
|
||||
* Set the up source to either detect rising edges or falling edges or both.
|
||||
*
|
||||
* @param risingEdge True to trigger on rising edges
|
||||
* @param fallingEdge True to trigger on falling edges
|
||||
*/
|
||||
void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
|
||||
|
||||
/**
|
||||
* Disable the up counting source to the counter.
|
||||
*/
|
||||
void ClearUpSource();
|
||||
|
||||
/**
|
||||
* Set the down counting source to be a digital input channel.
|
||||
*
|
||||
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
||||
* 10-25 are on the MXP
|
||||
*/
|
||||
void SetDownSource(int channel);
|
||||
|
||||
/**
|
||||
* Set the down counting source to be an analog trigger.
|
||||
*
|
||||
* @param analogTrigger The analog trigger object that is used for the Down
|
||||
* Source
|
||||
* @param triggerType The analog trigger output that will trigger the
|
||||
* counter.
|
||||
*/
|
||||
void SetDownSource(AnalogTrigger* analogTrigger,
|
||||
AnalogTriggerType triggerType);
|
||||
|
||||
/**
|
||||
* Set the down counting source to be an analog trigger.
|
||||
*
|
||||
* @param analogTrigger The analog trigger object that is used for the Down
|
||||
* Source
|
||||
* @param triggerType The analog trigger output that will trigger the
|
||||
* counter.
|
||||
*/
|
||||
void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
|
||||
AnalogTriggerType triggerType);
|
||||
|
||||
/**
|
||||
* Set the source object that causes the counter to count down.
|
||||
*
|
||||
* Set the down counting DigitalSource.
|
||||
*
|
||||
* @param source Pointer to the DigitalSource object to set as the down source
|
||||
*/
|
||||
void SetDownSource(DigitalSource* source);
|
||||
|
||||
/**
|
||||
* Set the source object that causes the counter to count down.
|
||||
*
|
||||
* Set the down counting DigitalSource.
|
||||
*
|
||||
* @param source Reference to the DigitalSource object to set as the down
|
||||
* source
|
||||
*/
|
||||
void SetDownSource(DigitalSource& source);
|
||||
|
||||
void SetDownSource(std::shared_ptr<DigitalSource> source);
|
||||
|
||||
/**
|
||||
* Set the edge sensitivity on a down counting source.
|
||||
*
|
||||
* Set the down source to either detect rising edges or falling edges.
|
||||
*
|
||||
* @param risingEdge True to trigger on rising edges
|
||||
* @param fallingEdge True to trigger on falling edges
|
||||
*/
|
||||
void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
|
||||
|
||||
/**
|
||||
* Disable the down counting source to the counter.
|
||||
*/
|
||||
void ClearDownSource();
|
||||
|
||||
/**
|
||||
* Set standard up / down counting mode on this counter.
|
||||
*
|
||||
* Up and down counts are sourced independently from two inputs.
|
||||
*/
|
||||
void SetUpDownCounterMode();
|
||||
|
||||
/**
|
||||
* Set external direction mode on this counter.
|
||||
*
|
||||
* Counts are sourced on the Up counter input.
|
||||
* The Down counter input represents the direction to count.
|
||||
*/
|
||||
void SetExternalDirectionMode();
|
||||
|
||||
/**
|
||||
* Set Semi-period mode on this counter.
|
||||
*
|
||||
* Counts up on both rising and falling edges.
|
||||
*/
|
||||
void SetSemiPeriodMode(bool highSemiPeriod);
|
||||
|
||||
/**
|
||||
* Configure the counter to count in up or down based on the length of the
|
||||
* input pulse.
|
||||
*
|
||||
* This mode is most useful for direction sensitive gear tooth sensors.
|
||||
*
|
||||
* @param threshold The pulse length beyond which the counter counts the
|
||||
* opposite direction. Units are seconds.
|
||||
*/
|
||||
void SetPulseLengthMode(double threshold);
|
||||
|
||||
/**
|
||||
* Set the Counter to return reversed sensing on the direction.
|
||||
*
|
||||
* This allows counters to change the direction they are counting in the case
|
||||
* of 1X and 2X quadrature encoding only. Any other counter mode isn't
|
||||
* supported.
|
||||
*
|
||||
* @param reverseDirection true if the value counted should be negated.
|
||||
*/
|
||||
void SetReverseDirection(bool reverseDirection);
|
||||
|
||||
/**
|
||||
* Set the Samples to Average which specifies the number of samples of the
|
||||
* timer to average when calculating the period. Perform averaging to account
|
||||
* for mechanical imperfections or as oversampling to increase resolution.
|
||||
*
|
||||
* @param samplesToAverage The number of samples to average from 1 to 127.
|
||||
*/
|
||||
void SetSamplesToAverage(int samplesToAverage);
|
||||
|
||||
/**
|
||||
* Get the Samples to Average which specifies the number of samples of the
|
||||
* timer to average when calculating the period.
|
||||
*
|
||||
* Perform averaging to account for mechanical imperfections or as
|
||||
* oversampling to increase resolution.
|
||||
*
|
||||
* @return The number of samples being averaged (from 1 to 127)
|
||||
*/
|
||||
int GetSamplesToAverage() const;
|
||||
|
||||
int GetFPGAIndex() const;
|
||||
|
||||
/**
|
||||
* Set the distance per pulse for this counter. This sets the multiplier used
|
||||
* to determine the distance driven based on the count value from the encoder.
|
||||
* Set this value based on the Pulses per Revolution and factor in any gearing
|
||||
* reductions. This distance can be in any units you like, linear or angular.
|
||||
*
|
||||
* @param distancePerPulse The scale factor that will be used to convert
|
||||
* pulses to useful units.
|
||||
*/
|
||||
void SetDistancePerPulse(double distancePerPulse);
|
||||
|
||||
/**
|
||||
* Read the current scaled counter value. Read the value at this instant,
|
||||
* scaled by the distance per pulse (defaults to 1).
|
||||
*
|
||||
* @return The distance since the last reset
|
||||
*/
|
||||
double GetDistance() const;
|
||||
|
||||
/**
|
||||
* Get the current rate of the Counter. Read the current rate of the counter
|
||||
* accounting for the distance per pulse value. The default value for distance
|
||||
* per pulse (1) yields units of pulses per second.
|
||||
*
|
||||
* @return The rate in units/sec
|
||||
*/
|
||||
double GetRate() const;
|
||||
|
||||
// CounterBase interface
|
||||
/**
|
||||
* Read the current counter value.
|
||||
*
|
||||
* Read the value at this instant. It may still be running, so it reflects the
|
||||
* current value. Next time it is read, it might have a different value.
|
||||
*/
|
||||
int Get() const override;
|
||||
|
||||
/**
|
||||
* Reset the Counter to zero.
|
||||
*
|
||||
* Set the counter value to zero. This doesn't effect the running state of the
|
||||
* counter, just sets the current value to zero.
|
||||
*/
|
||||
void Reset() override;
|
||||
|
||||
/**
|
||||
* Get the Period of the most recent count.
|
||||
*
|
||||
* Returns the time interval of the most recent count. This can be used for
|
||||
* velocity calculations to determine shaft speed.
|
||||
*
|
||||
* @returns The period between the last two pulses in units of seconds.
|
||||
*/
|
||||
units::second_t GetPeriod() const override;
|
||||
|
||||
/**
|
||||
* Set the maximum period where the device is still considered "moving".
|
||||
*
|
||||
* Sets the maximum period where the device is considered moving. This value
|
||||
* is used to determine the "stopped" state of the counter using the
|
||||
* GetStopped method.
|
||||
*
|
||||
* @param maxPeriod The maximum period where the counted device is considered
|
||||
* moving in seconds.
|
||||
*/
|
||||
void SetMaxPeriod(units::second_t maxPeriod) final;
|
||||
|
||||
/**
|
||||
* Select whether you want to continue updating the event timer output when
|
||||
* there are no samples captured.
|
||||
*
|
||||
* The output of the event timer has a buffer of periods that are averaged and
|
||||
* posted to a register on the FPGA. When the timer detects that the event
|
||||
* source has stopped (based on the MaxPeriod) the buffer of samples to be
|
||||
* averaged is emptied. If you enable the update when empty, you will be
|
||||
* notified of the stopped source and the event time will report 0 samples.
|
||||
* If you disable update when empty, the most recent average will remain on
|
||||
* the output until a new sample is acquired. You will never see 0 samples
|
||||
* output (except when there have been no events since an FPGA reset) and you
|
||||
* will likely not see the stopped bit become true (since it is updated at the
|
||||
* end of an average and there are no samples to average).
|
||||
*
|
||||
* @param enabled True to enable update when empty
|
||||
*/
|
||||
void SetUpdateWhenEmpty(bool enabled);
|
||||
|
||||
/**
|
||||
* Determine if the clock is stopped.
|
||||
*
|
||||
* Determine if the clocked input is stopped based on the MaxPeriod value set
|
||||
* using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the
|
||||
* device (and counter) are assumed to be stopped and it returns true.
|
||||
*
|
||||
* @return Returns true if the most recent counter period exceeds the
|
||||
* MaxPeriod value set by SetMaxPeriod.
|
||||
*/
|
||||
bool GetStopped() const override;
|
||||
|
||||
/**
|
||||
* The last direction the counter value changed.
|
||||
*
|
||||
* @return The last direction the counter value changed.
|
||||
*/
|
||||
bool GetDirection() const override;
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
protected:
|
||||
/// Makes the counter count up.
|
||||
std::shared_ptr<DigitalSource> m_upSource;
|
||||
|
||||
/// Makes the counter count down.
|
||||
std::shared_ptr<DigitalSource> m_downSource;
|
||||
|
||||
/// The FPGA counter object
|
||||
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_counter;
|
||||
|
||||
private:
|
||||
/// The index of this counter.
|
||||
int m_index = 0;
|
||||
|
||||
/// Distance of travel for each tick.
|
||||
double m_distancePerPulse = 1;
|
||||
|
||||
friend class DigitalGlitchFilter;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/Counter.h"
|
||||
#include "frc/CounterBase.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -1,198 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/length.h>
|
||||
#include <units/time.h>
|
||||
#include <units/velocity.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/Counter.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class DigitalInput;
|
||||
class DigitalOutput;
|
||||
|
||||
/**
|
||||
* Ultrasonic rangefinder class.
|
||||
*
|
||||
* The Ultrasonic rangefinder measures absolute distance based on the round-trip
|
||||
* time of a ping generated by the controller. These sensors use two
|
||||
* transducers, a speaker and a microphone both tuned to the ultrasonic range. A
|
||||
* common ultrasonic sensor, the Daventech SRF04 requires a short pulse to be
|
||||
* generated on a digital channel. This causes the chirp to be emitted. A second
|
||||
* line becomes high as the ping is transmitted and goes low when the echo is
|
||||
* received. The time that the line is high determines the round trip distance
|
||||
* (time of flight).
|
||||
*/
|
||||
class Ultrasonic : public wpi::Sendable,
|
||||
public wpi::SendableHelper<Ultrasonic> {
|
||||
public:
|
||||
/**
|
||||
* Create an instance of the Ultrasonic Sensor.
|
||||
*
|
||||
* This is designed to support the Daventech SRF04 and Vex ultrasonic sensors.
|
||||
*
|
||||
* @param pingChannel The digital output channel that sends the pulse to
|
||||
* initiate the sensor sending the ping.
|
||||
* @param echoChannel The digital input channel that receives the echo. The
|
||||
* length of time that the echo is high represents the
|
||||
* round trip time of the ping, and the distance.
|
||||
*/
|
||||
Ultrasonic(int pingChannel, int echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
* channel and a DigitalOutput for the ping channel.
|
||||
*
|
||||
* @param pingChannel The digital output object that starts the sensor doing a
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
*/
|
||||
Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
* channel and a DigitalOutput for the ping channel.
|
||||
*
|
||||
* @param pingChannel The digital output object that starts the sensor doing a
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
*/
|
||||
Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
* channel and a DigitalOutput for the ping channel.
|
||||
*
|
||||
* @param pingChannel The digital output object that starts the sensor doing a
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
*/
|
||||
Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
|
||||
std::shared_ptr<DigitalInput> echoChannel);
|
||||
|
||||
~Ultrasonic() override;
|
||||
|
||||
Ultrasonic(Ultrasonic&&) = default;
|
||||
Ultrasonic& operator=(Ultrasonic&&) = default;
|
||||
|
||||
/**
|
||||
* Returns the echo channel.
|
||||
*
|
||||
* @return The echo channel.
|
||||
*/
|
||||
int GetEchoChannel() const;
|
||||
|
||||
/**
|
||||
* Single ping to ultrasonic sensor.
|
||||
*
|
||||
* Send out a single ping to the ultrasonic sensor. This only works if
|
||||
* automatic (round robin) mode is disabled. A single ping is sent out, and
|
||||
* the counter should count the semi-period when it comes in. The counter is
|
||||
* reset to make the current value invalid.
|
||||
*/
|
||||
void Ping();
|
||||
|
||||
/**
|
||||
* Check if there is a valid range measurement.
|
||||
*
|
||||
* The ranges are accumulated in a counter that will increment on each edge of
|
||||
* the echo (return) signal. If the count is not at least 2, then the range
|
||||
* has not yet been measured, and is invalid.
|
||||
*/
|
||||
bool IsRangeValid() const;
|
||||
|
||||
/**
|
||||
* Turn Automatic mode on/off.
|
||||
*
|
||||
* When in Automatic mode, all sensors will fire in round robin, waiting a set
|
||||
* time between each sensor.
|
||||
*
|
||||
* @param enabling Set to true if round robin scheduling should start for all
|
||||
* the ultrasonic sensors. This scheduling method assures that
|
||||
* the sensors are non-interfering because no two 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.
|
||||
*/
|
||||
static void SetAutomaticMode(bool enabling);
|
||||
|
||||
/**
|
||||
* Get the range from the ultrasonic sensor.
|
||||
*
|
||||
* @return Range of the target returned from the ultrasonic sensor. If there
|
||||
* is no valid value yet, i.e. at least one measurement hasn't
|
||||
* completed, then return 0.
|
||||
*/
|
||||
units::meter_t GetRange() const;
|
||||
|
||||
bool IsEnabled() const;
|
||||
|
||||
void SetEnabled(bool enable);
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Initialize the Ultrasonic Sensor.
|
||||
*
|
||||
* This is the common code that initializes the ultrasonic sensor given that
|
||||
* there are two digital I/O channels allocated. If the system was running in
|
||||
* automatic mode (round robin) when the new sensor is added, it is stopped,
|
||||
* the sensor is added, then automatic mode is restored.
|
||||
*/
|
||||
void Initialize();
|
||||
|
||||
/**
|
||||
* Background task that goes through the list of ultrasonic sensors and pings
|
||||
* each one in turn. The counter is configured to read the timing of the
|
||||
* returned echo pulse.
|
||||
*
|
||||
* DANGER WILL ROBINSON, DANGER WILL ROBINSON:
|
||||
* This code runs as a task and assumes that none of the ultrasonic sensors
|
||||
* will change while it's running. Make sure to disable automatic mode before
|
||||
* touching the list.
|
||||
*/
|
||||
static void UltrasonicChecker();
|
||||
|
||||
// Time (usec) for the ping trigger pulse.
|
||||
static constexpr auto kPingTime = 10_us;
|
||||
|
||||
// Max time (ms) between readings.
|
||||
static constexpr auto kMaxUltrasonicTime = 0.1_s;
|
||||
static constexpr auto kSpeedOfSound = 1130_fps;
|
||||
|
||||
// Thread doing the round-robin automatic sensing
|
||||
static std::thread m_thread;
|
||||
|
||||
// Ultrasonic sensors
|
||||
static std::vector<Ultrasonic*> m_sensors;
|
||||
|
||||
// Automatic round-robin mode
|
||||
static std::atomic<bool> m_automaticEnabled;
|
||||
|
||||
std::shared_ptr<DigitalOutput> m_pingChannel;
|
||||
std::shared_ptr<DigitalInput> m_echoChannel;
|
||||
bool m_enabled = false;
|
||||
Counter m_counter;
|
||||
|
||||
hal::SimDevice m_simDevice;
|
||||
hal::SimBoolean m_simRangeValid;
|
||||
hal::SimDouble m_simRange;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -9,13 +9,9 @@ namespace frc {
|
||||
* Edge configuration.
|
||||
*/
|
||||
enum class EdgeConfiguration {
|
||||
/// No edge configuration (neither rising nor falling).
|
||||
kNone = 0,
|
||||
/// Rising edge configuration.
|
||||
kRisingEdge = 0x1,
|
||||
kRisingEdge = 0,
|
||||
/// Falling edge configuration.
|
||||
kFallingEdge = 0x2,
|
||||
/// Both rising and falling edges configuration.
|
||||
kBoth = 0x3
|
||||
kFallingEdge = 1,
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Counter.h>
|
||||
#include <hal/Types.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "EdgeConfiguration.h"
|
||||
|
||||
namespace frc {
|
||||
class DigitalSource;
|
||||
|
||||
/** Counter using external direction.
|
||||
*
|
||||
* <p>This counts on an edge from one digital input and the whether it counts
|
||||
* up or down based on the state of a second digital input.
|
||||
*
|
||||
*/
|
||||
class ExternalDirectionCounter
|
||||
: public wpi::Sendable,
|
||||
public wpi::SendableHelper<ExternalDirectionCounter> {
|
||||
public:
|
||||
/**
|
||||
* Constructs a new ExternalDirectionCounter.
|
||||
*
|
||||
* @param countSource The source for counting.
|
||||
* @param directionSource The source for selecting count direction.
|
||||
*/
|
||||
ExternalDirectionCounter(DigitalSource& countSource,
|
||||
DigitalSource& directionSource);
|
||||
|
||||
/**
|
||||
* Constructs a new ExternalDirectionCounter.
|
||||
*
|
||||
* @param countSource The source for counting.
|
||||
* @param directionSource The source for selecting count direction.
|
||||
*/
|
||||
ExternalDirectionCounter(std::shared_ptr<DigitalSource> countSource,
|
||||
std::shared_ptr<DigitalSource> directionSource);
|
||||
|
||||
ExternalDirectionCounter(ExternalDirectionCounter&&) = default;
|
||||
ExternalDirectionCounter& operator=(ExternalDirectionCounter&&) = default;
|
||||
|
||||
~ExternalDirectionCounter() override = default;
|
||||
|
||||
/**
|
||||
* Gets the current count.
|
||||
*
|
||||
* @return The current count.
|
||||
*/
|
||||
int GetCount() const;
|
||||
|
||||
/**
|
||||
* Sets to reverse the counter direction.
|
||||
*
|
||||
* @param reverseDirection True to reverse counting direction.
|
||||
*/
|
||||
void SetReverseDirection(bool reverseDirection);
|
||||
|
||||
/** Resets the current count. */
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Sets the edge configuration for counting.
|
||||
*
|
||||
* @param configuration The counting edge configuration.
|
||||
*/
|
||||
void SetEdgeConfiguration(EdgeConfiguration configuration);
|
||||
|
||||
protected:
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<DigitalSource> m_countSource;
|
||||
std::shared_ptr<DigitalSource> m_directionSource;
|
||||
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
|
||||
int32_t m_index = 0;
|
||||
};
|
||||
} // namespace frc
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "EdgeConfiguration.h"
|
||||
|
||||
namespace frc {
|
||||
class DigitalSource;
|
||||
|
||||
@@ -32,22 +34,23 @@ class Tachometer : public wpi::Sendable,
|
||||
/**
|
||||
* Constructs a new tachometer.
|
||||
*
|
||||
* @param source The source.
|
||||
* @param channel The DIO Channel.
|
||||
* @param configuration Edge configuration
|
||||
*/
|
||||
explicit Tachometer(DigitalSource& source);
|
||||
|
||||
/**
|
||||
* Constructs a new tachometer.
|
||||
*
|
||||
* @param source The source.
|
||||
*/
|
||||
explicit Tachometer(std::shared_ptr<DigitalSource> source);
|
||||
Tachometer(int channel, EdgeConfiguration configuration);
|
||||
|
||||
Tachometer(Tachometer&&) = default;
|
||||
Tachometer& operator=(Tachometer&&) = default;
|
||||
|
||||
~Tachometer() override = default;
|
||||
|
||||
/**
|
||||
* Sets the configuration for the channel.
|
||||
*
|
||||
* @param configuration The channel configuration.
|
||||
*/
|
||||
void SetEdgeConfiguration(EdgeConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Gets the tachometer frequency.
|
||||
*
|
||||
@@ -101,20 +104,6 @@ class Tachometer : public wpi::Sendable,
|
||||
*/
|
||||
bool GetStopped() const;
|
||||
|
||||
/**
|
||||
* Gets the number of sample to average.
|
||||
*
|
||||
* @return Samples to average.
|
||||
*/
|
||||
int GetSamplesToAverage() const;
|
||||
|
||||
/**
|
||||
* Sets the number of samples to average.
|
||||
*
|
||||
* @param samples Samples to average.
|
||||
*/
|
||||
void SetSamplesToAverage(int samples);
|
||||
|
||||
/**
|
||||
* Sets the maximum period before the tachometer is considered stopped.
|
||||
*
|
||||
@@ -122,20 +111,12 @@ class Tachometer : public wpi::Sendable,
|
||||
*/
|
||||
void SetMaxPeriod(units::second_t maxPeriod);
|
||||
|
||||
/**
|
||||
* Sets if to update when empty.
|
||||
*
|
||||
* @param updateWhenEmpty True to update when empty.
|
||||
*/
|
||||
void SetUpdateWhenEmpty(bool updateWhenEmpty);
|
||||
|
||||
protected:
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<DigitalSource> m_source;
|
||||
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
|
||||
int m_edgesPerRevolution;
|
||||
int32_t m_index;
|
||||
int32_t m_channel;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -28,19 +28,10 @@ class UpDownCounter : public wpi::Sendable,
|
||||
/**
|
||||
* Constructs a new UpDown Counter.
|
||||
*
|
||||
* @param upSource The up count source (can be null).
|
||||
* @param downSource The down count source (can be null).
|
||||
* @param channel The DIO channel
|
||||
* @param configuration Edge configuration
|
||||
*/
|
||||
UpDownCounter(DigitalSource& upSource, DigitalSource& downSource);
|
||||
|
||||
/**
|
||||
* Constructs a new UpDown Counter.
|
||||
*
|
||||
* @param upSource The up count source (can be null).
|
||||
* @param downSource The down count source (can be null).
|
||||
*/
|
||||
UpDownCounter(std::shared_ptr<DigitalSource> upSource,
|
||||
std::shared_ptr<DigitalSource> downSource);
|
||||
UpDownCounter(int channel, EdgeConfiguration configuration);
|
||||
|
||||
UpDownCounter(UpDownCounter&&) = default;
|
||||
UpDownCounter& operator=(UpDownCounter&&) = default;
|
||||
@@ -54,38 +45,21 @@ class UpDownCounter : public wpi::Sendable,
|
||||
*/
|
||||
int GetCount() const;
|
||||
|
||||
/**
|
||||
* Sets to revert the counter direction.
|
||||
*
|
||||
* @param reverseDirection True to reverse counting direction.
|
||||
*/
|
||||
void SetReverseDirection(bool reverseDirection);
|
||||
|
||||
/** Resets the current count. */
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Sets the configuration for the up source.
|
||||
* Sets the configuration for the channel.
|
||||
*
|
||||
* @param configuration The up source configuration.
|
||||
* @param configuration The channel configuration.
|
||||
*/
|
||||
void SetUpEdgeConfiguration(EdgeConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Sets the configuration for the down source.
|
||||
*
|
||||
* @param configuration The down source configuration.
|
||||
*/
|
||||
void SetDownEdgeConfiguration(EdgeConfiguration configuration);
|
||||
void SetEdgeConfiguration(EdgeConfiguration configuration);
|
||||
|
||||
protected:
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
void InitUpDownCounter();
|
||||
std::shared_ptr<DigitalSource> m_upSource;
|
||||
std::shared_ptr<DigitalSource> m_downSource;
|
||||
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
|
||||
int32_t m_index = 0;
|
||||
int32_t m_channel;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/length.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Ultrasonic;
|
||||
|
||||
namespace sim {
|
||||
|
||||
/**
|
||||
* Class to control a simulated {@link Ultrasonic}.
|
||||
*/
|
||||
class UltrasonicSim {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param ultrasonic The real ultrasonic to simulate
|
||||
*/
|
||||
explicit UltrasonicSim(const Ultrasonic& ultrasonic);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param ping unused.
|
||||
* @param echo the ultrasonic's echo channel.
|
||||
*/
|
||||
UltrasonicSim(int ping, int echo);
|
||||
|
||||
/**
|
||||
* Sets if the range measurement is valid.
|
||||
*
|
||||
* @param valid True if valid
|
||||
*/
|
||||
void SetRangeValid(bool valid);
|
||||
|
||||
/**
|
||||
* Sets the range measurement.
|
||||
*
|
||||
* @param range The range.
|
||||
*/
|
||||
void SetRange(units::inch_t range);
|
||||
|
||||
private:
|
||||
hal::SimBoolean m_simRangeValid;
|
||||
hal::SimDouble m_simRange;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user