Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
motor.cpp
Go to the documentation of this file.
1/**
2 * @file motor.cpp
3 */
4
5#include "motor.h"
6
7#include <Arduino.h>
8
9namespace em {
10
11#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
12Motor::Motor(const uint8_t positive_pin, const uint8_t negative_pin)
13 : positive_pin_(positive_pin), negative_pin_(negative_pin) {
14}
15#endif
16
17Motor::Motor(const uint8_t positive_pin,
18 const uint8_t positive_pin_ledc_channel,
19 const uint8_t negative_pin,
20 const uint8_t negative_pin_ledc_channel)
21 : positive_pin_(positive_pin),
22 negative_pin_(negative_pin),
23 positive_pin_ledc_channel_(positive_pin_ledc_channel),
24 negative_pin_ledc_channel_(negative_pin_ledc_channel) {
25}
26
28#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
29 if (positive_pin_ledc_channel_ != 0xFF) {
30 ledcAttachChannel(positive_pin_, kPwmFrequency, kPwmResolution, positive_pin_ledc_channel_);
31 } else {
32 ledcAttach(positive_pin_, kPwmFrequency, kPwmResolution);
33 }
34
35 if (negative_pin_ledc_channel_ != 0xFF) {
36 ledcAttachChannel(negative_pin_, kPwmFrequency, kPwmResolution, negative_pin_ledc_channel_);
37 } else {
38 ledcAttach(negative_pin_, kPwmFrequency, kPwmResolution);
39 }
40#else
41 ledcSetup(positive_pin_ledc_channel_, kPwmFrequency, kPwmResolution);
42 ledcSetup(negative_pin_ledc_channel_, kPwmFrequency, kPwmResolution);
43 ledcAttachPin(positive_pin_, positive_pin_ledc_channel_);
44 ledcAttachPin(negative_pin_, negative_pin_ledc_channel_);
45#endif
46 Stop();
47}
48
49void Motor::RunPwmDuty(const int16_t pwm_duty) {
50 pwm_duty_ = constrain(pwm_duty, -kMaxPwmDuty, kMaxPwmDuty);
51 if (pwm_duty_ >= 0) {
52#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
53 ledcWrite(positive_pin_, pwm_duty_);
54 ledcWrite(negative_pin_, 0);
55#else
56 ledcWrite(positive_pin_ledc_channel_, pwm_duty_);
57 ledcWrite(negative_pin_ledc_channel_, 0);
58#endif
59 } else {
60#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
61 ledcWrite(positive_pin_, 0);
62 ledcWrite(negative_pin_, -pwm_duty_);
63#else
64 ledcWrite(positive_pin_ledc_channel_, 0);
65 ledcWrite(negative_pin_ledc_channel_, -pwm_duty_);
66#endif
67 }
68}
69
70int16_t Motor::PwmDuty() const {
71 return pwm_duty_;
72}
73
75 pwm_duty_ = 0;
76#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
77 ledcWrite(positive_pin_, kMaxPwmDuty);
78 ledcWrite(negative_pin_, kMaxPwmDuty);
79#else
80 ledcWrite(positive_pin_ledc_channel_, kMaxPwmDuty);
81 ledcWrite(negative_pin_ledc_channel_, kMaxPwmDuty);
82#endif
83}
84
85} // namespace em
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