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