WPILibC++ 2023.4.3
DMASample.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <type_traits>
8
9#include <hal/AnalogInput.h>
10#include <hal/DMA.h>
11#include <units/time.h>
12
13#include "frc/AnalogInput.h"
14#include "frc/Counter.h"
15#include "frc/DMA.h"
16#include "frc/DutyCycle.h"
17#include "frc/Encoder.h"
18
19namespace frc {
20class DMASample : public HAL_DMASample {
21 public:
22 enum class DMAReadStatus {
23 kOk = HAL_DMA_OK,
24 kTimeout = HAL_DMA_TIMEOUT,
25 kError = HAL_DMA_ERROR
26 };
27
28 DMAReadStatus Update(const DMA* dma, units::second_t timeout,
29 int32_t* remaining, int32_t* status) {
30 return static_cast<DMAReadStatus>(
31 HAL_ReadDMA(dma->dmaHandle, this, timeout.value(), remaining, status));
32 }
33
34 uint64_t GetTime() const { return timeStamp; }
35
36 units::second_t GetTimeStamp() const {
37 return units::second_t{static_cast<double>(GetTime()) * 1.0e-6};
38 }
39
40 int32_t GetEncoderRaw(const Encoder* encoder, int32_t* status) const {
41 return HAL_GetDMASampleEncoderRaw(this, encoder->m_encoder, status);
42 }
43
44 double GetEncoderDistance(const Encoder* encoder, int32_t* status) const {
45 double val = GetEncoderRaw(encoder, status);
46 val *= encoder->DecodingScaleFactor();
47 val *= encoder->GetDistancePerPulse();
48 return val;
49 }
50
52 return HAL_GetDMASampleEncoderPeriodRaw(this, encoder->m_encoder, status);
53 }
54
55 int32_t GetCounter(const Counter* counter, int32_t* status) const {
56 return HAL_GetDMASampleCounter(this, counter->m_counter, status);
57 }
58
59 int32_t GetCounterPeriod(const Counter* counter, int32_t* status) const {
60 return HAL_GetDMASampleCounterPeriod(this, counter->m_counter, status);
61 }
62
63 bool GetDigitalSource(const DigitalSource* digitalSource,
64 int32_t* status) const {
66 this, digitalSource->GetPortHandleForRouting(), status);
67 }
68
70 int32_t* status) const {
71 return HAL_GetDMASampleAnalogInputRaw(this, analogInput->m_port, status);
72 }
73
74 double GetAnalogInputVoltage(const AnalogInput* analogInput,
75 int32_t* status) {
77 analogInput->m_port, GetAnalogInputRaw(analogInput, status), status);
78 }
79
81 int32_t* status) const {
82 return HAL_GetDMASampleAveragedAnalogInputRaw(this, analogInput->m_port,
83 status);
84 }
85
86 double GetAveragedAnalogInputVoltage(const AnalogInput* analogInput,
87 int32_t* status) {
89 analogInput->m_port, GetAveragedAnalogInputRaw(analogInput, status),
90 status);
91 }
92
93 void GetAnalogAccumulator(const AnalogInput* analogInput, int64_t* count,
94 int64_t* value, int32_t* status) const {
95 return HAL_GetDMASampleAnalogAccumulator(this, analogInput->m_port, count,
96 value, status);
97 }
98
100 int32_t* status) const {
101 return HAL_GetDMASampleDutyCycleOutputRaw(this, dutyCycle->m_handle,
102 status);
103 }
104
105 double GetDutyCycleOutput(const DutyCycle* dutyCycle, int32_t* status) {
106 return GetDutyCycleOutputRaw(dutyCycle, status) /
107 static_cast<double>(dutyCycle->GetOutputScaleFactor());
108 }
109};
110
111static_assert(std::is_standard_layout_v<frc::DMASample>,
112 "frc::DMASample must have standard layout");
113} // namespace frc
Analog input class.
Definition: AnalogInput.h:31
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:35
hal::Handle< HAL_CounterHandle > m_counter
Definition: Counter.h:433
Definition: DMA.h:20
Definition: DMASample.h:20
double GetEncoderDistance(const Encoder *encoder, int32_t *status) const
Definition: DMASample.h:44
double GetAveragedAnalogInputVoltage(const AnalogInput *analogInput, int32_t *status)
Definition: DMASample.h:86
void GetAnalogAccumulator(const AnalogInput *analogInput, int64_t *count, int64_t *value, int32_t *status) const
Definition: DMASample.h:93
int32_t GetCounter(const Counter *counter, int32_t *status) const
Definition: DMASample.h:55
int32_t GetCounterPeriod(const Counter *counter, int32_t *status) const
Definition: DMASample.h:59
uint64_t GetTime() const
Definition: DMASample.h:34
bool GetDigitalSource(const DigitalSource *digitalSource, int32_t *status) const
Definition: DMASample.h:63
int32_t GetEncoderRaw(const Encoder *encoder, int32_t *status) const
Definition: DMASample.h:40
double GetDutyCycleOutput(const DutyCycle *dutyCycle, int32_t *status)
Definition: DMASample.h:105
int32_t GetAveragedAnalogInputRaw(const AnalogInput *analogInput, int32_t *status) const
Definition: DMASample.h:80
int32_t GetDutyCycleOutputRaw(const DutyCycle *dutyCycle, int32_t *status) const
Definition: DMASample.h:99
int32_t GetEncoderPeriodRaw(const Encoder *encoder, int32_t *status) const
Definition: DMASample.h:51
DMAReadStatus
Definition: DMASample.h:22
double GetAnalogInputVoltage(const AnalogInput *analogInput, int32_t *status)
Definition: DMASample.h:74
units::second_t GetTimeStamp() const
Definition: DMASample.h:36
int32_t GetAnalogInputRaw(const AnalogInput *analogInput, int32_t *status) const
Definition: DMASample.h:69
DMAReadStatus Update(const DMA *dma, units::second_t timeout, int32_t *remaining, int32_t *status)
Definition: DMASample.h:28
DigitalSource Interface.
Definition: DigitalSource.h:22
virtual HAL_Handle GetPortHandleForRouting() const =0
Class to read a duty cycle PWM input.
Definition: DutyCycle.h:31
unsigned int GetOutputScaleFactor() const
Get the scale factor of the output.
Class to read quad encoders.
Definition: Encoder.h:41
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
Definition: core.h:1240
constexpr auto count() -> size_t
Definition: core.h:1204
double HAL_GetAnalogValueToVolts(HAL_AnalogInputHandle analogPortHandle, int32_t rawValue, int32_t *status)
Get the analog voltage from a raw value.
int32_t HAL_GetDMASampleDutyCycleOutputRaw(const HAL_DMASample *dmaSample, HAL_DutyCycleHandle dutyCycleHandle, int32_t *status)
Returns the raw duty cycle input ratio data from the sample.
enum HAL_DMAReadStatus HAL_ReadDMA(HAL_DMAHandle handle, HAL_DMASample *dmaSample, double timeoutSeconds, int32_t *remainingOut, int32_t *status)
Reads a DMA sample from the queue.
int32_t HAL_GetDMASampleAveragedAnalogInputRaw(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int32_t *status)
Returns the raw averaged analog data for an analog input from the sample.
HAL_Bool HAL_GetDMASampleDigitalSource(const HAL_DMASample *dmaSample, HAL_Handle dSourceHandle, int32_t *status)
Returns the state of a digital source from the sample.
int32_t HAL_GetDMASampleEncoderPeriodRaw(const HAL_DMASample *dmaSample, HAL_EncoderHandle encoderHandle, int32_t *status)
Returns the raw period data for an encoder from the sample.
int32_t HAL_GetDMASampleCounter(const HAL_DMASample *dmaSample, HAL_CounterHandle counterHandle, int32_t *status)
Returns the distance data for an counter from the sample.
int32_t HAL_GetDMASampleCounterPeriod(const HAL_DMASample *dmaSample, HAL_CounterHandle counterHandle, int32_t *status)
Returns the period data for an counter from the sample.
int32_t HAL_GetDMASampleAnalogInputRaw(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int32_t *status)
Returns the raw analog data for an analog input from the sample.
void HAL_GetDMASampleAnalogAccumulator(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int64_t *count, int64_t *value, int32_t *status)
Returns the analog accumulator data for an analog input from the sample.
int32_t HAL_GetDMASampleEncoderRaw(const HAL_DMASample *dmaSample, HAL_EncoderHandle encoderHandle, int32_t *status)
Returns the raw distance data for an encoder from the sample.
::uint64_t uint64_t
Definition: Meta.h:58
::int32_t int32_t
Definition: Meta.h:57
::int64_t int64_t
Definition: Meta.h:59
Definition: AprilTagFieldLayout.h:22
GHC_FS_API file_status status(const path &p, std::error_code &ec) noexcept
Definition: filesystem.hpp:4892
Buffer for containing all DMA data for a specific sample.
Definition: DMA.h:30
uint64_t timeStamp
Definition: DMA.h:33