WPILibC++ 2023.4.3
DigitalOutput.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 <hal/Types.h>
8#include <units/time.h>
11
12#include "frc/DigitalSource.h"
13
14namespace frc {
15
16/**
17 * Class to write to digital outputs.
18 *
19 * Write values to the digital output channels. Other devices implemented
20 * elsewhere will allocate channels automatically so for those devices it
21 * shouldn't be done here.
22 */
24 public wpi::Sendable,
25 public wpi::SendableHelper<DigitalOutput> {
26 public:
27 /**
28 * Create an instance of a digital output.
29 *
30 * Create a digital output given a channel.
31 *
32 * @param channel The digital channel 0-9 are on-board, 10-25 are on the MXP
33 * port
34 */
35 explicit DigitalOutput(int channel);
36
37 ~DigitalOutput() override;
38
41
42 /**
43 * Set the value of a digital output.
44 *
45 * Set the value of a digital output to either one (true) or zero (false).
46 *
47 * @param value 1 (true) for high, 0 (false) for disabled
48 */
49 void Set(bool value);
50
51 /**
52 * Gets the value being output from the Digital Output.
53 *
54 * @return the state of the digital output.
55 */
56 bool Get() const;
57
58 // Digital Source Interface
59 /**
60 * @return The HAL Handle to the specified source.
61 */
63
64 /**
65 * @return The type of analog trigger output to be used. 0 for Digitals
66 */
68
69 /**
70 * Is source an AnalogTrigger
71 */
72 bool IsAnalogTrigger() const override;
73
74 /**
75 * @return The GPIO channel number that this object represents.
76 */
77 int GetChannel() const override;
78
79 /**
80 * Output a single pulse on the digital output line.
81 *
82 * Send a single pulse on the digital output line where the pulse duration is
83 * specified in seconds. Maximum of 65535 microseconds.
84 *
85 * @param pulseLength The pulse length in seconds
86 */
87 void Pulse(units::second_t pulseLength);
88
89 /**
90 * Determine if the pulse is still going.
91 *
92 * Determine if a previously started pulse is still going.
93 */
94 bool IsPulsing() const;
95
96 /**
97 * Change the PWM frequency of the PWM output on a Digital Output line.
98 *
99 * The valid range is from 0.6 Hz to 19 kHz. The frequency resolution is
100 * logarithmic.
101 *
102 * There is only one PWM frequency for all digital channels.
103 *
104 * @param rate The frequency to output all digital output PWM signals.
105 */
106 void SetPWMRate(double rate);
107
108 /**
109 * Enable a PWM PPS (Pulse Per Second) Output on this line.
110 *
111 * Allocate one of the 6 DO PWM generator resources from this module.
112 *
113 * Supply the duty-cycle to output.
114 *
115 * The resolution of the duty cycle is 8-bit.
116 *
117 * @param dutyCycle The duty-cycle to start generating. [0..1]
118 */
119 void EnablePPS(double dutyCycle);
120
121 /**
122 * Enable a PWM Output on this line.
123 *
124 * Allocate one of the 6 DO PWM generator resources from this module.
125 *
126 * Supply the initial duty-cycle to output so as to avoid a glitch when first
127 * starting.
128 *
129 * The resolution of the duty cycle is 8-bit for low frequencies (1kHz or
130 * less) but is reduced the higher the frequency of the PWM signal is.
131 *
132 * @param initialDutyCycle The duty-cycle to start generating. [0..1]
133 */
134 void EnablePWM(double initialDutyCycle);
135
136 /**
137 * Change this line from a PWM output back to a static Digital Output line.
138 *
139 * Free up one of the 6 DO PWM generator resources that were in use.
140 */
142
143 /**
144 * Change the duty-cycle that is being generated on the line.
145 *
146 * The resolution of the duty cycle is 8-bit for low frequencies (1kHz or
147 * less) but is reduced the higher the frequency of the PWM signal is.
148 *
149 * @param dutyCycle The duty-cycle to change to. [0..1]
150 */
151 void UpdateDutyCycle(double dutyCycle);
152
153 /**
154 * Indicates this output is used by a simulated device.
155 *
156 * @param device simulated device handle
157 */
159
160 void InitSendable(wpi::SendableBuilder& builder) override;
161
162 private:
163 int m_channel;
164 hal::Handle<HAL_DigitalHandle> m_handle;
165 hal::Handle<HAL_DigitalPWMHandle> m_pwmGenerator;
166};
167
168} // namespace frc
Class to write to digital outputs.
Definition: DigitalOutput.h:25
DigitalOutput(int channel)
Create an instance of a digital output.
bool IsAnalogTrigger() const override
Is source an AnalogTrigger.
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this output is used by a simulated device.
void EnablePWM(double initialDutyCycle)
Enable a PWM Output on this line.
DigitalOutput(DigitalOutput &&)=default
bool IsPulsing() const
Determine if the pulse is still going.
AnalogTriggerType GetAnalogTriggerTypeForRouting() const override
void EnablePPS(double dutyCycle)
Enable a PWM PPS (Pulse Per Second) Output on this line.
HAL_Handle GetPortHandleForRouting() const override
void DisablePWM()
Change this line from a PWM output back to a static Digital Output line.
~DigitalOutput() override
void SetPWMRate(double rate)
Change the PWM frequency of the PWM output on a Digital Output line.
bool Get() const
Gets the value being output from the Digital Output.
void Set(bool value)
Set the value of a digital output.
int GetChannel() const override
void UpdateDutyCycle(double dutyCycle)
Change the duty-cycle that is being generated on the line.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
DigitalOutput & operator=(DigitalOutput &&)=default
void Pulse(units::second_t pulseLength)
Output a single pulse on the digital output line.
DigitalSource Interface.
Definition: DigitalSource.h:22
Definition: core.h:1240
Definition: SendableBuilder.h:18
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
int32_t HAL_Handle
Definition: Types.h:17
HAL_Handle HAL_SimDeviceHandle
Definition: Types.h:53
Definition: AprilTagFieldLayout.h:22
AnalogTriggerType
Definition: AnalogTriggerType.h:9