From bb338d31be6bd5d71db175c6243401b71725b41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Thu, 28 Nov 2024 20:41:54 +0100 Subject: [PATCH] Phase shift right motor PWM signal by 180 degrees --- src/motion/Motion.h | 14 ++++++++++---- src/motion/Motor.cpp | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/motion/Motion.h b/src/motion/Motion.h index 2a7ed8a..84e395b 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -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; diff --git a/src/motion/Motor.cpp b/src/motion/Motor.cpp index 0c26a58..bc084ad 100644 --- a/src/motion/Motor.cpp +++ b/src/motion/Motor.cpp @@ -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");