WPILibC++ 2024.3.2
PWM.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 <stdint.h>
8
9#include "hal/Types.h"
10
11/**
12 * @defgroup hal_pwm PWM Output Functions
13 * @ingroup hal_capi
14 * @{
15 */
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * Initializes a PWM port.
23 *
24 * @param[in] portHandle the port to initialize
25 * @param[in] allocationLocation the location where the allocation is occurring
26 * (can be null)
27 * @param[out] status Error status variable. 0 on success.
28 * @return the created pwm handle
29 */
31 const char* allocationLocation,
32 int32_t* status);
33
34/**
35 * Frees a PWM port.
36 *
37 * @param[in] pwmPortHandle the pwm handle
38 * @param[out] status Error status variable. 0 on success.
39 */
40void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status);
41
42/**
43 * Checks if a pwm channel is valid.
44 *
45 * @param channel the channel to check
46 * @return true if the channel is valid, otherwise false
47 */
49
50/**
51 * Sets the configuration settings for the PWM channel.
52 *
53 * All values are in microseconds.
54 *
55 * @param[in] pwmPortHandle the PWM handle
56 * @param[in] maxPwm the maximum PWM value
57 * @param[in] deadbandMaxPwm the high range of the center deadband
58 * @param[in] centerPwm the center PWM value
59 * @param[in] deadbandMinPwm the low range of the center deadband
60 * @param[in] minPwm the minimum PWM value
61 * @param[out] status Error status variable. 0 on success.
62 */
64 int32_t maxPwm, int32_t deadbandMaxPwm,
65 int32_t centerPwm, int32_t deadbandMinPwm,
66 int32_t minPwm, int32_t* status);
67
68/**
69 * Gets the pwm configuration settings for the PWM channel.
70 *
71 * Values are in microseconds.
72 *
73 * @param[in] pwmPortHandle the PWM handle
74 * @param[in] maxPwm the maximum PWM value
75 * @param[in] deadbandMaxPwm the high range of the center deadband
76 * @param[in] centerPwm the center PWM value
77 * @param[in] deadbandMinPwm the low range of the center deadband
78 * @param[in] minPwm the minimum PWM value
79 * @param[out] status Error status variable. 0 on success.
80 */
82 int32_t* maxPwm, int32_t* deadbandMaxPwm,
83 int32_t* centerPwm, int32_t* deadbandMinPwm,
84 int32_t* minPwm, int32_t* status);
85
86/**
87 * Sets if the FPGA should output the center value if the input value is within
88 * the deadband.
89 *
90 * @param[in] pwmPortHandle the PWM handle
91 * @param[in] eliminateDeadband true to eliminate deadband, otherwise false
92 * @param[out] status Error status variable. 0 on success.
93 */
95 HAL_Bool eliminateDeadband, int32_t* status);
96
97/**
98 * Gets the current eliminate deadband value.
99 *
100 * @param[in] pwmPortHandle the PWM handle
101 * @param[out] status Error status variable. 0 on success.
102 * @return true if set, otherwise false
103 */
105 int32_t* status);
106
107/**
108 * Sets a PWM channel to the desired pulse width in microseconds.
109 *
110 *
111 * @param[in] pwmPortHandle the PWM handle
112 * @param[in] microsecondPulseTime the PWM value to set
113 * @param[out] status Error status variable. 0 on success.
114 */
116 int32_t microsecondPulseTime,
117 int32_t* status);
118
119/**
120 * Sets a PWM channel to the desired scaled value.
121 *
122 * The values range from -1 to 1 and the period is controlled by the PWM Period
123 * and MinHigh registers.
124 *
125 * @param[in] pwmPortHandle the PWM handle
126 * @param[in] speed the scaled PWM value to set
127 * @param[out] status Error status variable. 0 on success.
128 */
129void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
130 int32_t* status);
131
132/**
133 * Sets a PWM channel to the desired position value.
134 *
135 * The values range from 0 to 1 and the period is controlled by the PWM Period
136 * and MinHigh registers.
137 *
138 * @param[in] pwmPortHandle the PWM handle
139 * @param[in] position the positional PWM value to set
140 * @param[out] status Error status variable. 0 on success.
141 */
142void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double position,
143 int32_t* status);
144
145/**
146 * Sets a PWM channel to be disabled.
147 *
148 * The channel is disabled until the next time it is set. Note this is different
149 * from just setting a 0 speed, as this will actively stop all signaling on the
150 * channel.
151 *
152 * @param[in] pwmPortHandle the PWM handle.
153 * @param[out] status Error status variable. 0 on success.
154 */
155void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status);
156
157/**
158 * Gets the current microsecond pulse time from a PWM channel.
159 *
160 * @param[in] pwmPortHandle the PWM handle
161 * @param[out] status Error status variable. 0 on success.
162 * @return the current PWM microsecond pulse time
163 */
165 int32_t* status);
166
167/**
168 * Gets a scaled value from a PWM channel.
169 *
170 * The values range from -1 to 1.
171 *
172 * @param[in] pwmPortHandle the PWM handle
173 * @param[out] status Error status variable. 0 on success.
174 * @return the current speed PWM value
175 */
176double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status);
177
178/**
179 * Gets a position value from a PWM channel.
180 *
181 * The values range from 0 to 1.
182 *
183 * @param[in] pwmPortHandle the PWM handle
184 * @param[out] status Error status variable. 0 on success.
185 * @return the current positional PWM value
186 */
187double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status);
188
189/**
190 * Forces a PWM signal to go to 0 temporarily.
191 *
192 * @param[in] pwmPortHandle the PWM handle.
193 * @param[out] status Error status variable. 0 on success.
194 */
195void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status);
196
197/**
198 * Sets how how often the PWM signal is squelched, thus scaling the period.
199 *
200 * @param[in] pwmPortHandle the PWM handle.
201 * @param[in] squelchMask the 2-bit mask of outputs to squelch
202 * @param[out] status Error status variable. 0 on success.
203 */
204void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
205 int32_t* status);
206
207/**
208 * Sets the PWM output to be a continuous high signal while enabled.
209 *
210 * @param[in] pwmPortHandle the PWM handle.
211 * @param[out] status Error status variable. 0 on success.
212 */
213void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle, int32_t* status);
214
215/**
216 * Gets the loop timing of the PWM system.
217 *
218 * @param[out] status Error status variable. 0 on success.
219 * @return the loop time
220 */
221int32_t HAL_GetPWMLoopTiming(int32_t* status);
222
223/**
224 * Gets the pwm starting cycle time.
225 *
226 * This time is relative to the FPGA time.
227 *
228 * @param[out] status Error status variable. 0 on success.
229 * @return the pwm cycle start time
230 */
231uint64_t HAL_GetPWMCycleStartTime(int32_t* status);
232#ifdef __cplusplus
233} // extern "C"
234#endif
235/** @} */
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed, int32_t *status)
Sets a PWM channel to the desired scaled value.
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Gets a scaled value from a PWM channel.
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle, int32_t microsecondPulseTime, int32_t *status)
Sets a PWM channel to the desired pulse width in microseconds.
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Sets a PWM channel to be disabled.
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Forces a PWM signal to go to 0 temporarily.
void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Sets the PWM output to be a continuous high signal while enabled.
int32_t HAL_GetPWMLoopTiming(int32_t *status)
Gets the loop timing of the PWM system.
void HAL_SetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle, int32_t maxPwm, int32_t deadbandMaxPwm, int32_t centerPwm, int32_t deadbandMinPwm, int32_t minPwm, int32_t *status)
Sets the configuration settings for the PWM channel.
void HAL_GetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle, int32_t *maxPwm, int32_t *deadbandMaxPwm, int32_t *centerPwm, int32_t *deadbandMinPwm, int32_t *minPwm, int32_t *status)
Gets the pwm configuration settings for the PWM channel.
void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle, HAL_Bool eliminateDeadband, int32_t *status)
Sets if the FPGA should output the center value if the input value is within the deadband.
double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Gets a position value from a PWM channel.
HAL_Bool HAL_CheckPWMChannel(int32_t channel)
Checks if a pwm channel is valid.
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Frees a PWM port.
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask, int32_t *status)
Sets how how often the PWM signal is squelched, thus scaling the period.
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double position, int32_t *status)
Sets a PWM channel to the desired position value.
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle, const char *allocationLocation, int32_t *status)
Initializes a PWM port.
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Gets the current microsecond pulse time from a PWM channel.
uint64_t HAL_GetPWMCycleStartTime(int32_t *status)
Gets the pwm starting cycle time.
HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle, int32_t *status)
Gets the current eliminate deadband value.
int32_t HAL_Bool
Definition: Types.h:73
HAL_Handle HAL_PortHandle
Definition: Types.h:19
HAL_Handle HAL_DigitalHandle
Definition: Types.h:31