[wpilib] Add new counter implementations (#2447)

This commit is contained in:
Thad House
2021-11-23 20:33:36 -08:00
committed by GitHub
parent b156db400d
commit 3aa54fa027
15 changed files with 1052 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
// 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
namespace frc {
enum class EdgeConfiguration {
kNone = 0,
kRisingEdge = 0x1,
kFallingEdge = 0x2,
kBoth = 0x3
};
} // namespace frc

View File

@@ -0,0 +1,79 @@
// 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/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "EdgeConfiguration.h"
namespace frc {
class DigitalSource;
/** Counter using external direction. */
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() override;
ExternalDirectionCounter(ExternalDirectionCounter&&) = default;
ExternalDirectionCounter& operator=(ExternalDirectionCounter&&) = default;
/**
* Gets the current count.
*
* @return The current count.
*/
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 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> m_handle;
int32_t m_index = 0;
};
} // namespace frc

View File

@@ -0,0 +1,125 @@
// 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/Types.h>
#include <units/angular_velocity.h>
#include <units/frequency.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
namespace frc {
class DigitalSource;
/**
* Tachometer for getting rotational speed from a device.
*/
class Tachometer : public wpi::Sendable,
public wpi::SendableHelper<Tachometer> {
public:
/**
* Constructs a new tachometer.
*
* @param source The source.
*/
explicit Tachometer(DigitalSource& source);
/**
* Constructs a new tachometer.
*
* @param source The source.
*/
explicit Tachometer(std::shared_ptr<DigitalSource> source);
~Tachometer() override;
Tachometer(Tachometer&&) = default;
Tachometer& operator=(Tachometer&&) = default;
/**
* Gets the tachometer frequency.
*
* @return Current frequency.
*/
units::hertz_t GetFrequency() const;
/**
* Gets the tachometer period.
*
* @return Current period.
*/
units::second_t GetPeriod() const;
/**
* Gets the number of edges per revolution.
*
* @return Edges per revolution.
*/
int GetEdgesPerRevolution() const;
/**
* Sets the number of edges per revolution.
*
* @param edges Edges per revolution.
*/
void SetEdgesPerRevolution(int edges);
/**
* Gets the current tachometer revolutions per minute.
*
* SetEdgesPerRevolution must be set with a non 0 value for this to work.
*
* @return Current RPM.
*/
units::revolutions_per_minute_t GetRevolutionsPerMinute() const;
/**
* Gets if the tachometer is stopped.
*
* @return True if the tachometer is stopped.
*/
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.
*
* @param maxPeriod The max period.
*/
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> m_handle;
int m_edgesPerRevolution;
int32_t m_index;
};
} // namespace frc

View File

@@ -0,0 +1,85 @@
// 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/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "EdgeConfiguration.h"
namespace frc {
class DigitalSource;
/** Up Down Counter. */
class UpDownCounter : public wpi::Sendable,
public wpi::SendableHelper<UpDownCounter> {
public:
/**
* Constructs a new UpDown Counter.
*
* @param upSource The up count source (can be null).
* @param downSource The down count source (can be null).
*/
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() override;
UpDownCounter(UpDownCounter&&) = default;
UpDownCounter& operator=(UpDownCounter&&) = default;
/**
* Gets the current count.
*
* @return The current count.
*/
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.
*
* @param configuration The up source configuration.
*/
void SetUpEdgeConfiguration(EdgeConfiguration configuration);
/**
* Sets the configuration for the down source.
*
* @param configuration The down source configuration.
*/
void SetDownEdgeConfiguration(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> m_handle;
int32_t m_index = 0;
};
} // namespace frc