Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
em::Motor Class Reference

A two-channel PWM motor driver class. More...

#include <motor.h>

Public Member Functions

 Motor (const uint8_t positive_pin, const uint8_t negative_pin)
 Constructor for creating an Motor object.
 
 Motor (const uint8_t positive_pin, const uint8_t positive_pin_ledc_channel, const uint8_t negative_pin, const uint8_t negative_pin_ledc_channel)
 Constructor for creating an Motor object.
 
void Init ()
 Initialize.
 
void RunPwmDuty (const int16_t pwm_duty)
 Set motor PWM directly.
 
int16_t PwmDuty () const
 Get the PWM pwm_duty cycle of the motor driver.
 
void Stop ()
 Stop motor.
 

Static Public Attributes

static constexpr uint8_t kPwmResolution = 10
 The resolution of PWM, which determines the fine-tuning level of the PWM duty cycle, in bits. Here it is set to 10 bits.
 
static constexpr uint8_t kPwmFrequency = 75000
 The frequency of PWM, in Hertz. Here it is set to 75000 Hz, used to control the frequency of the PWM signal for motor driving.
 
static constexpr int16_t kMaxPwmDuty = (1 << kPwmResolution) - 1
 The maximum PWM duty cycle value calculated based on the PWM resolution.
 

Detailed Description

A two-channel PWM motor driver class.

Examples
drive_dc_motor.ino.

Definition at line 27 of file motor.h.

Constructor & Destructor Documentation

◆ Motor() [1/2]

em::Motor::Motor ( const uint8_t positive_pin,
const uint8_t negative_pin )
explicit

Constructor for creating an Motor object.

Note
This constructor is only valid when the ESP32 Arduino Core version is greater than or equal to 3.0.0. For versions below 3.0.0, please use Motor(const uint8_t, const uint8_t, const uint8_t, const uint8_t)
Parameters
[in]positive_pinThe pin number of the motor's positive pole.
[in]negative_pinThe pin number of the motor's negative pole.

Definition at line 12 of file motor.cpp.

◆ Motor() [2/2]

em::Motor::Motor ( const uint8_t positive_pin,
const uint8_t positive_pin_ledc_channel,
const uint8_t negative_pin,
const uint8_t negative_pin_ledc_channel )
explicit

Constructor for creating an Motor object.

Note
This class will use the LED Control (LEDC) of ESP32 to generate PWM waveforms to driver the motor. When the ESP32 Arduino Core version is less than 3.0.0, the ESP32 Arduino Core will not manage the LEDC channels, and the user needs to specify the LEDC channels corresponding to each pin. Therefore, it is necessary to use this constructor to construct the object and specify the LEDC channels corresponding to the positive and negative pins of the motor. Each pin can only correspond to one LEDC channel, and please do not reuse the LEDC channels. For the description of LEDC, please refer to the official website: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
Parameters
[in]positive_pinThe pin number of the motor's positive pole.
[in]positive_pin_ledc_channelThe LED Control (LEDC) channel corresponding to the motor's positive pole pin, which has a total of 16 channels ranging from 0 to 15. Each pin can only correspond to one channel and is used to configure functions such as the PWM (Pulse Width Modulation) signal related to the positive pole pin of the motor. For more details, please refer to the official documentation: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
[in]negative_pinThe pin number of the motor's negative pole.
[in]negative_pin_ledc_channelThe LED Control (LEDC) channel corresponding to the motor's negative pole pin, which has a total of 16 channels ranging from 0 to 15. Each pin can only correspond to one channel and is used to configure functions such as the PWM (Pulse Width Modulation) signal related to the negative pole pin of the motor. For more details, please refer to the official documentation: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html

Definition at line 17 of file motor.cpp.

Member Function Documentation

◆ Init()

void em::Motor::Init ( )

Initialize.

Definition at line 27 of file motor.cpp.

◆ PwmDuty()

int16_t em::Motor::PwmDuty ( ) const

Get the PWM pwm_duty cycle of the motor driver.

Returns
The duty cycle of PWM (the value range is from -1023 to 1023). A positive number represents forward rotation, and a negative number represents reverse rotation.

Definition at line 70 of file motor.cpp.

◆ RunPwmDuty()

void em::Motor::RunPwmDuty ( const int16_t pwm_duty)

Set motor PWM directly.

Parameters
[in]pwm_dutyThe duty cycle of PWM (the value range is from -1023 to 1023). A positive number represents forward rotation, and a negative number represents revers e rotation.

Definition at line 49 of file motor.cpp.

◆ Stop()

void em::Motor::Stop ( )

Stop motor.

Definition at line 74 of file motor.cpp.

Member Data Documentation

◆ kMaxPwmDuty

int16_t em::Motor::kMaxPwmDuty = (1 << kPwmResolution) - 1
staticconstexpr

The maximum PWM duty cycle value calculated based on the PWM resolution.

Definition at line 59 of file motor.h.

◆ kPwmFrequency

uint8_t em::Motor::kPwmFrequency = 75000
staticconstexpr

The frequency of PWM, in Hertz. Here it is set to 75000 Hz, used to control the frequency of the PWM signal for motor driving.

Definition at line 48 of file motor.h.

◆ kPwmResolution

uint8_t em::Motor::kPwmResolution = 10
staticconstexpr

The resolution of PWM, which determines the fine-tuning level of the PWM duty cycle, in bits. Here it is set to 10 bits.

Definition at line 38 of file motor.h.


The documentation for this class was generated from the following files: