|
Emakefun Encoder Motor Arduino Lib 1.1.1
|
#include <encoder_motor.h>
Public Types | |
| enum | PhaseRelation : uint8_t { kAPhaseLeads , kBPhaseLeads } |
| Used to clarify the phase relationship between phase A and phase B of the encoder when the motor is rotating forward, so that the correct phase relationship can be used in operations such as pulse counting and subsequent speed calculation. More... | |
Public Member Functions | |
| EncoderMotor (const uint8_t positive_pin, const uint8_t negative_pin, const uint8_t a_pin, const uint8_t b_pin, const uint32_t ppr, const uint32_t reduction_ration, const PhaseRelation phase_relation) | |
| Constructor for creating an EncoderMotor object. | |
| EncoderMotor (const uint8_t positive_pin, const uint8_t positive_pin_ledc_channel, const uint8_t negative_pin, const uint8_t negative_pin_ledc_channel, const uint8_t a_pin, const uint8_t b_pin, const uint32_t ppr, const uint32_t reduction_ration, const PhaseRelation phase_relation) | |
| Constructor for creating an EncoderMotor object. | |
| void | Init () |
| Initialize. | |
| void | SetSpeedPid (const float p, const float i, const float d) |
| Set the parameters of the speed PID controller with the given Proportional (P), Integral (I), and Derivative (D) parameter values. | |
| void | GetSpeedPid (float *const p, float *const i, float *const d) const |
| Get the Proportional (P), Integral (I), and Derivative (D) parameter values of the speed PID controller through pointers. | |
| void | RunPwmDuty (const int16_t pwm_duty) |
| Set motor PWM directly. | |
| void | RunSpeed (const int16_t speed_rpm) |
| Run motor at speed setpoint. | |
| void | Stop () |
| Stop motor. | |
| int64_t | EncoderPulseCount () const |
| Get encoder pulse count. The count value is incremented by one during forward rotation and decremented by one during reverse rotation, counted at the falling edge of phase A. | |
| void | ResetPulseCount () |
| Reset the encoder pulse count to 0. | |
| int32_t | SpeedRpm () const |
| Get the current speed of the motor. | |
| int16_t | PwmDuty () const |
| Get the PWM pwm_duty cycle of the motor driver. | |
| int32_t | TargetRpm () const |
| Get the target speed of the motor in RPM. | |
Encoder Motor Class.
The main functions of this class are as follows:
Definition at line 50 of file encoder_motor.h.
| enum em::EncoderMotor::PhaseRelation : uint8_t |
Used to clarify the phase relationship between phase A and phase B of the encoder when the motor is rotating forward, so that the correct phase relationship can be used in operations such as pulse counting and subsequent speed calculation.
Definition at line 62 of file encoder_motor.h.
| em::EncoderMotor::EncoderMotor | ( | const uint8_t | positive_pin, |
| const uint8_t | negative_pin, | ||
| const uint8_t | a_pin, | ||
| const uint8_t | b_pin, | ||
| const uint32_t | ppr, | ||
| const uint32_t | reduction_ration, | ||
| const PhaseRelation | phase_relation ) |
Constructor for creating an EncoderMotor object.
| [in] | positive_pin | The pin number of the motor's positive pole. |
| [in] | negative_pin | The pin number of the motor's negative pole. |
| [in] | a_pin | The pin number of the encoder's A phase. |
| [in] | b_pin | The pin number of the encoder's B phase. |
| [in] | ppr | Pulses per revolution. |
| [in] | reduction_ration | Reduction ratio. |
| [in] | phase_relation | Phase relationship (A phase leads or B phase leads, referring to the situation when the motor is rotating forward), PhaseRelation. |
If the user is unsure about the value of the phase_relation parameter for the encoded motor they are using, they can use the example program detect_phase_relation.ino to help detect and determine the value of this parameter.
Definition at line 28 of file encoder_motor.cpp.
| em::EncoderMotor::EncoderMotor | ( | const uint8_t | positive_pin, |
| const uint8_t | positive_pin_ledc_channel, | ||
| const uint8_t | negative_pin, | ||
| const uint8_t | negative_pin_ledc_channel, | ||
| const uint8_t | a_pin, | ||
| const uint8_t | b_pin, | ||
| const uint32_t | ppr, | ||
| const uint32_t | reduction_ration, | ||
| const PhaseRelation | phase_relation ) |
Constructor for creating an EncoderMotor object.
| [in] | positive_pin | The pin number of the motor's positive pole. |
| [in] | positive_pin_ledc_channel | The 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_pin | The pin number of the motor's negative pole. |
| [in] | negative_pin_ledc_channel | The 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 |
| [in] | a_pin | The pin number of the encoder's A phase. |
| [in] | b_pin | The pin number of the encoder's B phase. |
| [in] | ppr | Pulses per revolution. |
| [in] | reduction_ration | Reduction ratio. |
| [in] | phase_relation | Phase relationship (A phase leads or B phase leads, referring to the situation when the motor is rotating forward), PhaseRelation. |
If the user is unsure about the value of the phase_relation parameter for the encoded motor they are using, they can use the example program detect_phase_relation.ino to help detect and determine the value of this parameter.
Definition at line 48 of file encoder_motor.cpp.
| em::EncoderMotor::~EncoderMotor | ( | ) |
Definition at line 110 of file encoder_motor.cpp.
| int64_t em::EncoderMotor::EncoderPulseCount | ( | ) | const |
Get encoder pulse count. The count value is incremented by one during forward rotation and decremented by one during reverse rotation, counted at the falling edge of phase A.
Definition at line 152 of file encoder_motor.cpp.
| void em::EncoderMotor::GetSpeedPid | ( | float *const | p, |
| float *const | i, | ||
| float *const | d ) const |
Get the Proportional (P), Integral (I), and Derivative (D) parameter values of the speed PID controller through pointers.
| [out] | p | Pointer used to get the value of the Proportional coefficient (P). After the function is executed, the memory location pointed to by this pointer will store the corresponding parameter value. |
| [out] | i | Pointer used to get the value of the Integral coefficient (I). After the function is executed, the memory location pointed to by this pointer will store the corresponding parameter value. |
| [out] | d | Pointer used to get the value of the Derivative coefficient (D). After the function is executed, the memory location pointed to by this pointer will store the corresponding parameter value. |
Definition at line 97 of file encoder_motor.cpp.
| void em::EncoderMotor::Init | ( | ) |
Initialize.
Definition at line 69 of file encoder_motor.cpp.
| int16_t em::EncoderMotor::PwmDuty | ( | ) | const |
Get the PWM pwm_duty cycle of the motor driver.
Definition at line 165 of file encoder_motor.cpp.
| void em::EncoderMotor::ResetPulseCount | ( | ) |
Reset the encoder pulse count to 0.
Definition at line 156 of file encoder_motor.cpp.
| void em::EncoderMotor::RunPwmDuty | ( | const int16_t | pwm_duty | ) |
Set motor PWM directly.
| [in] | pwm_duty | 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 116 of file encoder_motor.cpp.
| void em::EncoderMotor::RunSpeed | ( | const int16_t | speed_rpm | ) |
Run motor at speed setpoint.
| [in] | speed_rpm | Speed setpoint(RPM). |
Definition at line 128 of file encoder_motor.cpp.
| void em::EncoderMotor::SetSpeedPid | ( | const float | p, |
| const float | i, | ||
| const float | d ) |
Set the parameters of the speed PID controller with the given Proportional (P), Integral (I), and Derivative (D) parameter values.
| [in] | p | The value of the Proportional coefficient (P). |
| [in] | i | The value of the Integral coefficient (I). |
| [in] | d | The value of the Derivative coefficient (D). |
Definition at line 84 of file encoder_motor.cpp.
| int32_t em::EncoderMotor::SpeedRpm | ( | ) | const |
Get the current speed of the motor.
Definition at line 160 of file encoder_motor.cpp.
| void em::EncoderMotor::Stop | ( | ) |
Stop motor.
Definition at line 144 of file encoder_motor.cpp.
| int32_t em::EncoderMotor::TargetRpm | ( | ) | const |
Get the target speed of the motor in RPM.
Definition at line 170 of file encoder_motor.cpp.