WPILibC++ 2024.3.2
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 {
20/**
21 * DMA sample.
22 */
23class DMASample : public HAL_DMASample {
24 public:
25 /**
26 * DMA read status.
27 */
28 enum class DMAReadStatus {
29 /// OK status.
30 kOk = HAL_DMA_OK,
31 /// Timeout status.
32 kTimeout = HAL_DMA_TIMEOUT,
33 /// Error status.
34 kError = HAL_DMA_ERROR
35 };
36
37 /**
38 * Retrieves a new DMA sample.
39 *
40 * @param dma DMA object.
41 * @param timeout Timeout for retrieval.
42 * @param remaining Number of remaining samples.
43 * @param status DMA read status.
44 */
45 DMAReadStatus Update(const DMA* dma, units::second_t timeout,
46 int32_t* remaining, int32_t* status) {
47 return static_cast<DMAReadStatus>(
48 HAL_ReadDMA(dma->dmaHandle, this, timeout.value(), remaining, status));
49 }
50
51 /**
52 * Returns the DMA sample time in microseconds.
53 *
54 * @return The DMA sample time in microseconds.
55 */
56 uint64_t GetTime() const { return timeStamp; }
57
58 /**
59 * Returns the DMA sample timestamp.
60 *
61 * @return The DMA sample timestamp.
62 */
63 units::second_t GetTimeStamp() const {
64 return units::second_t{static_cast<double>(GetTime()) * 1.0e-6};
65 }
66
67 /**
68 * Returns raw encoder value from DMA.
69 *
70 * @param encoder Encoder used for DMA.
71 * @param status DMA read status.
72 * @return Raw encoder value from DMA.
73 */
74 int32_t GetEncoderRaw(const Encoder* encoder, int32_t* status) const {
75 return HAL_GetDMASampleEncoderRaw(this, encoder->m_encoder, status);
76 }
77
78 /**
79 * Returns encoder distance from DMA.
80 *
81 * @param encoder Encoder used for DMA.
82 * @param status DMA read status.
83 * @return Encoder distance from DMA.
84 */
85 double GetEncoderDistance(const Encoder* encoder, int32_t* status) const {
86 double val = GetEncoderRaw(encoder, status);
87 val *= encoder->DecodingScaleFactor();
88 val *= encoder->GetDistancePerPulse();
89 return val;
90 }
91
92 /**
93 * Returns raw encoder period from DMA.
94 *
95 * @param encoder Encoder used for DMA.
96 * @param status DMA read status.
97 * @return Raw encoder period from DMA.
98 */
99 int32_t GetEncoderPeriodRaw(const Encoder* encoder, int32_t* status) const {
100 return HAL_GetDMASampleEncoderPeriodRaw(this, encoder->m_encoder, status);
101 }
102
103 /**
104 * Returns counter value from DMA.
105 *
106 * @param counter Counter used for DMA.
107 * @param status DMA read status.
108 * @return Counter value from DMA.
109 */
110 int32_t GetCounter(const Counter* counter, int32_t* status) const {
111 return HAL_GetDMASampleCounter(this, counter->m_counter, status);
112 }
113
114 /**
115 * Returns counter period from DMA.
116 *
117 * @param counter Counter used for DMA.
118 * @param status DMA read status.
119 * @return Counter period from DMA.
120 */
121 int32_t GetCounterPeriod(const Counter* counter, int32_t* status) const {
122 return HAL_GetDMASampleCounterPeriod(this, counter->m_counter, status);
123 }
124
125 /**
126 * Returns digital source value from DMA.
127 *
128 * @param digitalSource DigitalSource used for DMA.
129 * @param status DMA read status.
130 * @return DigitalSource value from DMA.
131 */
132 bool GetDigitalSource(const DigitalSource* digitalSource,
133 int32_t* status) const {
135 this, digitalSource->GetPortHandleForRouting(), status);
136 }
137
138 /**
139 * Returns raw analog input value from DMA.
140 *
141 * @param analogInput AnalogInput used for DMA.
142 * @param status DMA read status.
143 * @return Raw analog input value from DMA.
144 */
145 int32_t GetAnalogInputRaw(const AnalogInput* analogInput,
146 int32_t* status) const {
147 return HAL_GetDMASampleAnalogInputRaw(this, analogInput->m_port, status);
148 }
149
150 /**
151 * Returns analog input voltage from DMA.
152 *
153 * @param analogInput AnalogInput used for DMA.
154 * @param status DMA read status.
155 * @return Analog input voltage from DMA.
156 */
157 double GetAnalogInputVoltage(const AnalogInput* analogInput,
158 int32_t* status) {
160 analogInput->m_port, GetAnalogInputRaw(analogInput, status), status);
161 }
162
163 /**
164 * Returns averaged analog input raw value from DMA.
165 *
166 * @param analogInput AnalogInput used for DMA.
167 * @param status DMA read status.
168 * @return Averaged analog input raw value from DMA.
169 */
170 int32_t GetAveragedAnalogInputRaw(const AnalogInput* analogInput,
171 int32_t* status) const {
172 return HAL_GetDMASampleAveragedAnalogInputRaw(this, analogInput->m_port,
173 status);
174 }
175
176 /**
177 * Returns averaged analog input voltage from DMA.
178 *
179 * @param analogInput AnalogInput used for DMA.
180 * @param status DMA read status.
181 * @return Averaged analog input voltage from DMA.
182 */
184 int32_t* status) {
186 analogInput->m_port, GetAveragedAnalogInputRaw(analogInput, status),
187 status);
188 }
189
190 /**
191 * Returns analog accumulator value from DMA.
192 *
193 * @param analogInput AnalogInput used for DMA.
194 * @param count Accumulator sample count.
195 * @param value Accumulator value.
196 * @param status DMA read status.
197 */
198 void GetAnalogAccumulator(const AnalogInput* analogInput, int64_t* count,
199 int64_t* value, int32_t* status) const {
200 return HAL_GetDMASampleAnalogAccumulator(this, analogInput->m_port, count,
201 value, status);
202 }
203
204 /**
205 * Returns raw duty cycle output from DMA.
206 *
207 * @param dutyCycle DutyCycle used for DMA.
208 * @param status DMA read status.
209 * @return Raw duty cycle output from DMA.
210 */
211 int32_t GetDutyCycleOutputRaw(const DutyCycle* dutyCycle,
212 int32_t* status) const {
213 return HAL_GetDMASampleDutyCycleOutputRaw(this, dutyCycle->m_handle,
214 status);
215 }
216
217 /**
218 * Returns duty cycle output (0-1) from DMA.
219 *
220 * @param dutyCycle DutyCycle used for DMA.
221 * @param status DMA read status.
222 * @return Duty cycle output (0-1) from DMA.
223 */
224 double GetDutyCycleOutput(const DutyCycle* dutyCycle, int32_t* status) {
225 return GetDutyCycleOutputRaw(dutyCycle, status) /
226 static_cast<double>(dutyCycle->GetOutputScaleFactor());
227 }
228};
229
230static_assert(std::is_standard_layout_v<frc::DMASample>,
231 "frc::DMASample must have standard layout");
232} // 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
The FPGA counter object.
Definition: Counter.h:461
Class for configuring Direct Memory Access (DMA) of FPGA inputs.
Definition: DMA.h:23
DMA sample.
Definition: DMASample.h:23
double GetEncoderDistance(const Encoder *encoder, int32_t *status) const
Returns encoder distance from DMA.
Definition: DMASample.h:85
double GetAveragedAnalogInputVoltage(const AnalogInput *analogInput, int32_t *status)
Returns averaged analog input voltage from DMA.
Definition: DMASample.h:183
void GetAnalogAccumulator(const AnalogInput *analogInput, int64_t *count, int64_t *value, int32_t *status) const
Returns analog accumulator value from DMA.
Definition: DMASample.h:198
int32_t GetCounter(const Counter *counter, int32_t *status) const
Returns counter value from DMA.
Definition: DMASample.h:110
int32_t GetCounterPeriod(const Counter *counter, int32_t *status) const
Returns counter period from DMA.
Definition: DMASample.h:121
uint64_t GetTime() const
Returns the DMA sample time in microseconds.
Definition: DMASample.h:56
bool GetDigitalSource(const DigitalSource *digitalSource, int32_t *status) const
Returns digital source value from DMA.
Definition: DMASample.h:132
int32_t GetEncoderRaw(const Encoder *encoder, int32_t *status) const
Returns raw encoder value from DMA.
Definition: DMASample.h:74
double GetDutyCycleOutput(const DutyCycle *dutyCycle, int32_t *status)
Returns duty cycle output (0-1) from DMA.
Definition: DMASample.h:224
int32_t GetAveragedAnalogInputRaw(const AnalogInput *analogInput, int32_t *status) const
Returns averaged analog input raw value from DMA.
Definition: DMASample.h:170
int32_t GetDutyCycleOutputRaw(const DutyCycle *dutyCycle, int32_t *status) const
Returns raw duty cycle output from DMA.
Definition: DMASample.h:211
int32_t GetEncoderPeriodRaw(const Encoder *encoder, int32_t *status) const
Returns raw encoder period from DMA.
Definition: DMASample.h:99
DMAReadStatus
DMA read status.
Definition: DMASample.h:28
double GetAnalogInputVoltage(const AnalogInput *analogInput, int32_t *status)
Returns analog input voltage from DMA.
Definition: DMASample.h:157
units::second_t GetTimeStamp() const
Returns the DMA sample timestamp.
Definition: DMASample.h:63
int32_t GetAnalogInputRaw(const AnalogInput *analogInput, int32_t *status) const
Returns raw analog input value from DMA.
Definition: DMASample.h:145
DMAReadStatus Update(const DMA *dma, units::second_t timeout, int32_t *remaining, int32_t *status)
Retrieves a new DMA sample.
Definition: DMASample.h:45
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:40
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
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.
@ HAL_DMA_ERROR
Definition: DMA.h:22
@ HAL_DMA_TIMEOUT
Definition: DMA.h:21
@ HAL_DMA_OK
Definition: DMA.h:20
constexpr auto count() -> size_t
Definition: core.h:1203
Definition: AprilTagPoseEstimator.h:15
Buffer for containing all DMA data for a specific sample.
Definition: DMA.h:28
uint64_t timeStamp
Definition: DMA.h:31