Emakefun DM11 Arduino 库 1.0.4
载入中...
搜索中...
未找到
dm11.h
浏览该文件的文档.
1#pragma once
2
3#ifndef _EM_DM11_H_
4#define _EM_DM11_H_
5
6/**
7 * @file dm11.h
8 */
9
10#include <WString.h>
11#include <Wire.h>
12#include <stdint.h>
13
14namespace em {
15/**
16 * @~Chinese
17 * @class Dm11
18 * @brief DM11是使用I2C协议驱动4路PWM从而驱动2路电机的模块,默认I2C地址为0x15。
19 */
20/**
21 * @~English
22 * @class Dm11
23 * @brief DM11 is a module that uses the I2C protocol to drive four PWM channels, which in turn drive two motors. The default
24 * I2C address is 0x15.
25 */
26class Dm11 {
27 public:
28 /**
29 * @~Chinese
30 * @brief 默认I2C地址。
31 */
32 /**
33 * @~English
34 * @brief Default I2C address.
35 */
36 static constexpr uint8_t kDefaultI2cAddress = 0x15;
37
38 /**
39 * @~Chinese
40 * @brief 最小PWM波频率。
41 */
42 /**
43 * @~English
44 * @brief Minimum PWM wave frequency.
45 */
46 static constexpr uint16_t kMinFrequencyHz = 1;
47
48 /**
49 * @~Chinese
50 * @brief 最大PWM波频率。
51 */
52 /**
53 * @~English
54 * @brief Maximum PWM wave frequency.
55 */
56 static constexpr uint16_t kMaxFrequencyHz = 10000;
57
58 /**
59 * @~Chinese
60 * @brief 最大PWM占空比。
61 */
62 /**
63 * @~English
64 * @brief Maximum PWM duty cycle.
65 */
66 static constexpr uint16_t kMaxPwmDuty = 4095;
67
68 /**
69 * @~Chinese
70 * @enum ErrorCode
71 * @brief 错误码。
72 */
73 /**
74 * @~English
75 * @enum ErrorCode
76 * @brief Error codes.
77 */
78 enum ErrorCode : uint32_t {
79 /**
80 * @~Chinese
81 * @brief 成功。
82 */
83 /**
84 * @~English
85 * @brief Success.
86 */
87 kOK = 0,
88
89 /**
90 * @~Chinese
91 * @brief I2C数据太长,无法装入传输缓冲区。
92 */
93 /**
94 * @~English
95 * @brief I2C data too long to fit in transmit buffer.
96 */
98
99 /**
100 * @~Chinese
101 * @brief 在I2C发送地址时收到NACK。
102 */
103 /**
104 * @~English
105 * @brief NACK received on I2C transmit of address.
106 */
108
109 /**
110 * @~Chinese
111 * @brief 在I2C发送数据时收到NACK。
112 */
113 /**
114 * @~English
115 * @brief NACK received on I2C transmit of data.
116 */
118
119 /**
120 * @~Chinese
121 * @brief 其他I2C错误。
122 */
123 /**
124 * @~English
125 * @brief Other I2C errors.
126 */
128
129 /**
130 * @~Chinese
131 * @brief I2C通讯超时。
132 */
133 /**
134 * @~English
135 * @brief I2C communication timed out.
136 */
138
139 /**
140 * @~Chinese
141 * @brief 参数错误。
142 */
143 /**
144 * @~English
145 * @brief Invalid parameter.
146 */
148
149 /**
150 * @~Chinese
151 * @brief 未知错误。
152 */
153 /**
154 * @~English
155 * @brief Unknown error.
156 */
158 };
159
160 /**
161 * @~Chinese
162 * @enum PwmChannel
163 * @brief PWM通道。
164 */
165 /**
166 * @~English
167 * @enum PwmChannel
168 * @brief PWM channel.
169 */
170 enum PwmChannel : uint8_t {
171 /**
172 * @~Chinese
173 * @brief PWM通道0。
174 */
175 /**
176 * @~English
177 * @brief PWM channel 0.
178 */
180 /**
181 * @~Chinese
182 * @brief PWM通道1。
183 */
184 /**
185 * @~English
186 * @brief PWM channel 1.
187 */
189 /**
190 * @~Chinese
191 * @brief PWM通道2。
192 */
193 /**
194 * @~English
195 * @brief PWM channel 2.
196 */
198 /**
199 * @~Chinese
200 * @brief PWM通道3。
201 */
202 /**
203 * @~English
204 * @brief PWM channel 3.
205 */
207 /**
208 * @~Chinese
209 * @brief PWM通道数。
210 */
211 /**
212 * @~English
213 * @brief PWM channel 4.
214 */
216 };
217
218 /**
219 * @~Chinese
220 * @brief 构造函数,指定 I2C 地址和 TwoWire 对象。
221 * @param i2c_address I2C 地址,默认为 @ref kDefaultI2cAddress 。
222 * @param wire TwoWire 对象引用,默认为 Wire。
223 */
224 /**
225 * @~English
226 * @brief Constructor, specifying I2C address and TwoWire object.
227 * @param i2c_address I2C address, defaults to @ref kDefaultI2cAddress.
228 * @param wire TwoWire object reference, defaults to Wire.
229 */
230 explicit Dm11(const uint8_t i2c_address = kDefaultI2cAddress, TwoWire& wire = Wire);
231
232 /**
233 * @~Chinese
234 * @brief 构造函数,使用默认 I2C 地址和指定的 TwoWire 对象。
235 * @param wire TwoWire 对象引用。
236 */
237 /**
238 * @~English
239 * @brief Constructor using the default I2C address and a specified TwoWire object.
240 * @param wire TwoWire object reference.
241 */
242 explicit Dm11(TwoWire& wire) : Dm11(kDefaultI2cAddress, wire) {
243 }
244
245 /**
246 * @~Chinese
247 * @brief 初始化函数。
248 * @param[in] frequency_hz PWM波频率,单位HZ,范围:1 ~ 10000,默认为1000。
249 * @return 返回值请参考 @ref ErrorCode 。
250 */
251 /**
252 * @~English
253 * @brief Initialization function.
254 * @param[in] frequency_hz PWM wave frequency in Hz, range: 1 ~ 10000, default is 1000.
255 * @return Return value please refer to @ref ErrorCode.
256 */
257 ErrorCode Init(const uint16_t frequency_hz = 1000);
258
259 /**
260 * @~Chinese
261 * @brief 设置指定通道的 PWM 占空比。
262 * @param[in] pwm_channel PWM通道,参考 @ref PwmChannel
263 * @param[in] duty PWM占空比,范围:0 ~ 4095。
264 * @return 返回值请参考 @ref ErrorCode 。
265 */
266 /**
267 * @~English
268 * @brief Set the PWM duty cycle for a specified channel.
269 * @param[in] pwm_channel PWM channel, @ref PwmChannel
270 * @param[in] duty PWM duty cycle, range: 0 ~ 4095.
271 * @return Return value please refer to @ref ErrorCode.
272 */
273 ErrorCode PwmDuty(const PwmChannel pwm_channel, uint16_t duty);
274
275 private:
276 Dm11(const Dm11&) = delete;
277 Dm11& operator=(const Dm11&) = delete;
278
279 const uint8_t i2c_address_ = kDefaultI2cAddress;
280 TwoWire& wire_ = Wire;
281};
282} // namespace em
283#endif
DM11是使用I2C协议驱动4路PWM从而驱动2路电机的模块,默认I2C地址为0x15。
定义 dm11.h:26
static constexpr uint16_t kMaxFrequencyHz
最大PWM波频率。
定义 dm11.h:56
PwmChannel
PWM通道。
定义 dm11.h:170
@ kPwmChannelNum
PWM通道数。
定义 dm11.h:215
@ kPwmChannel2
PWM通道2。
定义 dm11.h:197
@ kPwmChannel0
PWM通道0。
定义 dm11.h:179
@ kPwmChannel3
PWM通道3。
定义 dm11.h:206
@ kPwmChannel1
PWM通道1。
定义 dm11.h:188
static constexpr uint16_t kMaxPwmDuty
最大PWM占空比。
定义 dm11.h:66
Dm11(const uint8_t i2c_address=kDefaultI2cAddress, TwoWire &wire=Wire)
构造函数,指定 I2C 地址和 TwoWire 对象。
static constexpr uint16_t kMinFrequencyHz
最小PWM波频率。
定义 dm11.h:46
ErrorCode
错误码。
定义 dm11.h:78
@ kI2cReceivedNackOnTransmitOfData
在I2C发送数据时收到NACK。
定义 dm11.h:117
@ kI2cOtherError
其他I2C错误。
定义 dm11.h:127
@ kOK
成功。
定义 dm11.h:87
@ kInvalidParameter
参数错误。
定义 dm11.h:147
@ kI2cReceivedNackOnTransmitOfAddress
在I2C发送地址时收到NACK。
定义 dm11.h:107
@ kI2cTimeout
I2C通讯超时。
定义 dm11.h:137
@ kUnknownError
未知错误。
定义 dm11.h:157
@ kI2cDataTooLongToFitInTransmitBuffer
I2C数据太长,无法装入传输缓冲区。
定义 dm11.h:97
ErrorCode PwmDuty(const PwmChannel pwm_channel, uint16_t duty)
设置指定通道的 PWM 占空比。
ErrorCode Init(const uint16_t frequency_hz=1000)
初始化函数。
Dm11(TwoWire &wire)
构造函数,使用默认 I2C 地址和指定的 TwoWire 对象。
定义 dm11.h:242
static constexpr uint8_t kDefaultI2cAddress
默认I2C地址。
定义 dm11.h:36