WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
PWM.hpp
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 "wpi/hal/PWM.h"
10#include "wpi/hal/Types.hpp"
11#include "wpi/units/time.hpp"
14
15namespace wpi {
16class AddressableLED;
17
18/**
19 * Class implements the PWM generation in the FPGA.
20 *
21 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
22 * are mapped to the microseconds to keep the pulse high, with a range of 0
23 * (off) to 4096. Changes are immediately sent to the FPGA, and the update
24 * occurs at the next FPGA cycle (5.05ms). There is no delay.
25 */
27 public:
28 friend class AddressableLED;
29 /**
30 * Represents the output period in microseconds.
31 */
33 /**
34 * PWM pulses occur every 5 ms
35 */
37 /**
38 * PWM pulses occur every 10 ms
39 */
41 /**
42 * PWM pulses occur every 20 ms
43 */
45 };
46
47 /**
48 * Allocate a PWM given a channel number.
49 *
50 * Checks channel value range and allocates the appropriate channel.
51 * The allocation is only done to help users ensure that they don't double
52 * assign channels.
53 *
54 * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the
55 * MXP port
56 * @param registerSendable If true, adds this instance to SendableRegistry
57 */
58 explicit PWM(int channel, bool registerSendable = true);
59
60 PWM(PWM&&) = default;
61 PWM& operator=(PWM&&) = default;
62
63 /**
64 * Free the PWM channel.
65 *
66 * Free the resource associated with the PWM channel and set the value to 0.
67 */
68 ~PWM() override;
69
70 /**
71 * Set the PWM pulse time directly to the hardware.
72 *
73 * Write a microsecond value to a PWM channel.
74 *
75 * @param time Microsecond PWM value.
76 */
77 void SetPulseTime(wpi::units::microsecond_t time);
78
79 /**
80 * Get the PWM pulse time directly from the hardware.
81 *
82 * Read a microsecond value from a PWM channel.
83 *
84 * @return Microsecond PWM control value.
85 */
86 wpi::units::microsecond_t GetPulseTime() const;
87
88 /**
89 * Temporarily disables the PWM output. The next set call will re-enable
90 * the output.
91 */
93
94 /**
95 * Sets the PWM output period.
96 *
97 * @param mult The output period to apply to this channel
98 */
100
101 int GetChannel() const;
102
103 /**
104 * Indicates this input is used by a simulated device.
105 *
106 * @param device simulated device handle
107 */
109
110 protected:
112
113 private:
114 int m_channel;
116};
117
118} // namespace wpi
A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
Definition AddressableLED.hpp:31
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
OutputPeriod
Represents the output period in microseconds.
Definition PWM.hpp:32
@ kOutputPeriod_10Ms
PWM pulses occur every 10 ms.
Definition PWM.hpp:40
@ kOutputPeriod_5Ms
PWM pulses occur every 5 ms.
Definition PWM.hpp:36
@ kOutputPeriod_20Ms
PWM pulses occur every 20 ms.
Definition PWM.hpp:44
~PWM() override
Free the PWM channel.
void SetPulseTime(wpi::units::microsecond_t time)
Set the PWM pulse time directly to the hardware.
void SetDisabled()
Temporarily disables the PWM output.
void SetOutputPeriod(OutputPeriod mult)
Sets the PWM output period.
PWM & operator=(PWM &&)=default
PWM(int channel, bool registerSendable=true)
Allocate a PWM given a channel number.
friend class AddressableLED
Definition PWM.hpp:28
wpi::units::microsecond_t GetPulseTime() const
Get the PWM pulse time directly from the hardware.
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this input is used by a simulated device.
int GetChannel() const
PWM(PWM &&)=default
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:53
Definition CvSource.hpp:15