3#ifndef _EM_ENCODER_MOTOR_H_
4#define _EM_ENCODER_MOTOR_H_
14#include <condition_variable>
19#include "esp_arduino_version.h"
120 const uint8_t negative_pin,
124 const uint32_t reduction_ration,
181 const uint8_t positive_pin_ledc_channel,
182 const uint8_t negative_pin,
183 const uint8_t negative_pin_ledc_channel,
187 const uint32_t reduction_ration,
217 void SetSpeedPid(
const float p,
const float i,
const float d);
237 void GetSpeedPid(
float*
const p,
float*
const i,
float*
const d)
const;
262 void RunSpeed(
const int16_t speed_rpm);
335 static void OnPinAFalling(
void* self);
337 void OnPinAFalling();
343 void DeleteThread(std::thread*& thread);
349 float integral = 0.0;
350 float max_integral = 0.0;
353 mutable std::mutex mutex_;
354 std::condition_variable condition_;
355 std::thread* update_rpm_thread_ =
nullptr;
356 std::thread* driving_thread_ =
nullptr;
358 const uint8_t pin_a_ = 0;
359 const uint8_t pin_b_ = 0;
360 const double total_ppr_ = 0;
361 const uint8_t b_level_at_a_falling_edge_ = 0;
363 int64_t previous_pulse_count_ = 0;
364 std::atomic<int64_t> pulse_count_;
365 std::chrono::system_clock::time_point last_update_speed_time_ = std::chrono::time_point<std::chrono::system_clock>::min();
366 int32_t speed_rpm_ = 0;
367 int32_t target_speed_rpm_ = 0.0;
int16_t PwmDuty() const
Get the PWM pwm_duty cycle of the motor driver.
void RunSpeed(const int16_t speed_rpm)
Run motor at speed setpoint.
void ResetPulseCount()
Reset the encoder pulse count to 0.
PhaseRelation
Used to clarify the phase relationship between phase A and phase B of the encoder when the motor is r...
@ kBPhaseLeads
Represents the situation where phase B leads phase A when the motor is rotating forward.
@ kAPhaseLeads
Represents the situation where phase A leads phase B when the motor is rotating forward.
void RunPwmDuty(const int16_t pwm_duty)
Set motor PWM directly.
int64_t EncoderPulseCount() const
Get encoder pulse count. The count value is incremented by one during forward rotation and decremente...
int32_t TargetRpm() const
Get the target speed of the motor in RPM.
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),...
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 controll...
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.
int32_t SpeedRpm() const
Get the current speed of the motor.