Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
motor.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef _EM_MOTOR_H_
4#define _EM_MOTOR_H_
5
6/**
7 * @file motor.h
8 */
9
10#include <WString.h>
11
12#include <cstdint>
13
14#include "esp_arduino_version.h"
15
16namespace em {
17/**
18 * @~Chinese
19 * @class Motor
20 * @brief 两路PWM电机驱动类
21 */
22/**
23 * @~English
24 * @class Motor
25 * @brief A two-channel PWM motor driver class.
26 */
27class Motor {
28 public:
29 /**
30 * @~Chinese
31 * @brief PWM的分辨率,决定了PWM占空比可调节的精细程度,单位为位,这里设置为10位。
32 */
33 /**
34 * @~English
35 * @brief The resolution of PWM, which determines the fine-tuning level of the PWM duty cycle, in bits. Here it is set to 10
36 * bits.
37 */
38 static constexpr uint8_t kPwmResolution = 10;
39 /**
40 * @~Chinese
41 * @brief PWM的频率,单位为赫兹,这里设置为75000赫兹,用于控制电机驱动的PWM信号频率。
42 */
43 /**
44 * @~English
45 * @brief The frequency of PWM, in Hertz. Here it is set to 75000 Hz, used to control the frequency of the PWM signal for
46 * motor driving.
47 */
48 static constexpr uint8_t kPwmFrequency = 75000;
49
50 static_assert(kPwmResolution > 1);
51 /**
52 * @~Chinese
53 * @brief 根据PWM分辨率计算出的最大PWM占空比数值。
54 */
55 /**
56 * @~English
57 * @brief The maximum PWM duty cycle value calculated based on the PWM resolution.
58 */
59 static constexpr int16_t kMaxPwmDuty = (1 << kPwmResolution) - 1;
60
61 /**
62 * @~Chinese
63 * @brief 构造函数,用于创建一个 Motor 对象。
64 * @note 该构造函数仅在ESP32 Arduino Core版本大于等于3.0.0时有效,3.0.0以下版本请使用 @ref Motor(const uint8_t, const
65 * uint8_t, const uint8_t, const uint8_t)
66 * @param[in] positive_pin 电机正极引脚编号。
67 * @param[in] negative_pin 电机负极引脚编号。
68 */
69 /**
70 * @~English
71 * @brief Constructor for creating an Motor object.
72 * @note This constructor is only valid when the ESP32 Arduino Core version is greater than or equal to 3.0.0. For versions
73 * below 3.0.0, please use @ref Motor(const uint8_t, const uint8_t, const uint8_t, const uint8_t)
74 * @param[in] positive_pin The pin number of the motor's positive pole.
75 * @param[in] negative_pin The pin number of the motor's negative pole.
76 */
77 explicit Motor(const uint8_t positive_pin, const uint8_t negative_pin);
78
79 /**
80 * @~Chinese
81 * @brief 构造函数,用于创建一个 Motor 对象。
82 * @note 此类会使用ESP32的LED Control (LEDC)产生PWM波形驱动电机,在ESP32 Arduino Core版本小于3.0.0时,ESP32 Arduino
83 * Core不会管理LEDC通道,需要用户指定每个引脚对应的LEDC通道,
84 * 所以必须使用该构造函数构造对象,并指定电机正负极引脚对应的LEDC通道,每个pin只能对应一个LEDC通道,请勿重复使用LEDC通道,关于LEDC的说明请查阅官网:
85 * @ref https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
86 * @param[in] positive_pin 电机正极引脚编号。
87 * @param[in] positive_pin_ledc_channel 电机正极引脚对应的LED Control
88 * (LEDC)通道,共16个,范围0~15,每个pin只能对应一个通道,详情请查阅官方文档: @ref
89 * https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
90 * @param[in] negative_pin 电机负极引脚编号。
91 * @param[in] negative_pin_ledc_channel 电机负极引脚对应的LED Control
92 * (LEDC)通道,共16个,范围0~15,每个pin只能对应一个通道,详情请查阅官方文档: @ref
93 * https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
94 */
95 /**
96 * @~English
97 * @brief Constructor for creating an Motor object.
98 * @note This class will use the LED Control (LEDC) of ESP32 to generate PWM waveforms to driver the motor. When the ESP32
99 * Arduino Core version is less than 3.0.0, the ESP32 Arduino Core will not manage the LEDC channels, and the user needs to
100 * specify the LEDC channels corresponding to each pin. Therefore, it is necessary to use this constructor to construct the
101 * object and specify the LEDC channels corresponding to the positive and negative pins of the motor. Each pin can only
102 * correspond to one LEDC channel, and please do not reuse the LEDC channels. For the description of LEDC, please refer to the
103 * official website:
104 * @ref https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
105 * @param[in] positive_pin The pin number of the motor's positive pole.
106 * @param[in] positive_pin_ledc_channel The LED Control (LEDC) channel corresponding to the motor's positive pole pin, which
107 * has a total of 16 channels ranging from 0 to 15. Each pin can only correspond to one channel and is used to configure
108 * functions such as the PWM (Pulse Width Modulation) signal related to the positive pole pin of the motor. For more details,
109 * please refer to the official documentation: @ref https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
110 * @param[in] negative_pin The pin number of the motor's negative pole.
111 * @param[in] negative_pin_ledc_channel The LED Control (LEDC) channel corresponding to the motor's negative pole pin, which
112 * has a total of 16 channels ranging from 0 to 15. Each pin can only correspond to one channel and is used to configure
113 * functions such as the PWM (Pulse Width Modulation) signal related to the negative pole pin of the motor. For more details,
114 * please refer to the official documentation: @ref https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
115 */
116 explicit Motor(const uint8_t positive_pin,
117 const uint8_t positive_pin_ledc_channel,
118 const uint8_t negative_pin,
119 const uint8_t negative_pin_ledc_channel);
120
121 ~Motor() = default;
122
123 /**
124 * @~Chinese
125 * @brief 初始化。
126 */
127 /**
128 * @~English
129 * @brief Initialize.
130 */
131 void Init();
132
133 /**
134 * @~Chinese
135 * @brief 直接设置电机的PWM占空比。
136 * @param[in] pwm_duty PWM占空比(取值范围 -1023到1023)。正数代表正转,负数代表反转。
137 */
138 /**
139 * @~English
140 * @brief Set motor PWM directly.
141 * @param[in] pwm_duty The duty cycle of PWM (the value range is from -1023 to 1023). A positive number represents forward
142 * rotation, and a negative number represents revers
143 * e rotation.
144 */
145 void RunPwmDuty(const int16_t pwm_duty);
146
147 /**
148 * @~Chinese
149 * @brief 获取电机驱动器的PWM占空比。
150 * @return PWM占空比(取值范围 -1023到1023)。正数代表正转,负数代表反转。
151 */
152 /**
153 * @~English
154 * @brief Get the PWM pwm_duty cycle of the motor driver.
155 * @return The duty cycle of PWM (the value range is from -1023 to 1023). A positive number represents forward
156 * rotation, and a negative number represents reverse rotation.
157 */
158 int16_t PwmDuty() const;
159
160 /**
161 * @~Chinese
162 * @brief 停止电机运行。
163 */
164 /**
165 * @~English
166 * @brief Stop motor.
167 */
168 void Stop();
169
170 private:
171 const uint8_t positive_pin_ = 0xFF;
172 const uint8_t negative_pin_ = 0xFF;
173 const uint8_t positive_pin_ledc_channel_ = 0xFF;
174 const uint8_t negative_pin_ledc_channel_ = 0xFF;
175 int16_t pwm_duty_ = 0;
176};
177} // namespace em
178
179#endif
static constexpr uint8_t kPwmResolution
The resolution of PWM, which determines the fine-tuning level of the PWM duty cycle,...
Definition motor.h:38
void RunPwmDuty(const int16_t pwm_duty)
Set motor PWM directly.
Definition motor.cpp:49
static constexpr int16_t kMaxPwmDuty
The maximum PWM duty cycle value calculated based on the PWM resolution.
Definition motor.h:59
int16_t PwmDuty() const
Get the PWM pwm_duty cycle of the motor driver.
Definition motor.cpp:70
Motor(const uint8_t positive_pin, const uint8_t negative_pin)
Constructor for creating an Motor object.
Definition motor.cpp:12
void Stop()
Stop motor.
Definition motor.cpp:74
void Init()
Initialize.
Definition motor.cpp:27
static constexpr uint8_t kPwmFrequency
The frequency of PWM, in Hertz. Here it is set to 75000 Hz, used to control the frequency of the PWM ...
Definition motor.h:48