Phase shift right motor PWM signal by 180 degrees

This commit is contained in:
Phillip Kühne 2024-11-28 20:41:54 +01:00
parent 87b9fbe66f
commit bb338d31be
2 changed files with 13 additions and 6 deletions

View File

@ -22,11 +22,12 @@
#define CHANNEL_LEFT LEDC_CHANNEL_3
#define CHANNEL_RIGHT LEDC_CHANNEL_4
#define DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
#define PHASE_180_DEG (1 << (DUTY_RES))/2 // (2**DUTY_RES)/2 Set phase to 180 degrees
#define FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
#define DEFAULT_BASE_VALUE 3900
class Motor{
public:
Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel);
Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel, int phase=0);
/**
* @brief Initializes the motor
@ -52,14 +53,19 @@ class Motor{
uint8_t pin;
ledc_timer_t timer;
ledc_channel_t channel;
uint16_t duty;
// The phase of the pwm signal, expressed as a number betweeen 0 and
// the maximum value representable by the pwm timer resolution.
int phase;
};
class Motion{
protected:
static inline uint16_t RIGHT_MOTOR_DUTY = DEFAULT_BASE_VALUE;
static inline uint16_t LEFT_MOTOR_DUTY = DEFAULT_BASE_VALUE;
static inline int LEFT_MOTOR_PHASE = 0;
static inline int RIGHT_MOTOR_PHASE = PHASE_180_DEG;
static const int MOTOR_RIGHT_PIN = 11;
static const int MOTOR_LEFT_PIN = 12;
static void moveTask(void * args);
@ -75,8 +81,8 @@ protected:
public:
//Instances of the motors, so they can also be used from outside to set values for the motors directly.
static inline Motor left = Motor(MOTOR_LEFT_PIN,TIMER,CHANNEL_LEFT);
static inline Motor right = Motor(MOTOR_RIGHT_PIN,TIMER,CHANNEL_RIGHT);
static inline Motor left = Motor(MOTOR_LEFT_PIN,TIMER,CHANNEL_LEFT,LEFT_MOTOR_PHASE);
static inline Motor right = Motor(MOTOR_RIGHT_PIN,TIMER,CHANNEL_RIGHT,RIGHT_MOTOR_PHASE);
//MotionDetection instance, for motion Correction and user (access with dezibot.motion.detection)
static inline MotionDetection detection;

View File

@ -1,10 +1,11 @@
#include "Motion.h"
Motor::Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel){
Motor::Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel, int phase){
this->pin = pin;
this->channel = channel;
this->timer = timer;
this->duty = 0;
this->phase = phase;
};
void Motor::begin(void){
@ -16,7 +17,7 @@ void Motor::begin(void){
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = this->timer,
.duty = 0, // Set duty to 0%
.hpoint = 0
.hpoint = this->phase
};
ledc_channel_config(&channelConfig);
Serial.println("Motor begin done");