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