Pilote DRV8231 basique en C++ compatible avec ESP-IDF & PlatformIO
Cette classe d’exemple utilise le pilote PWM LEDC de l’ESP-IDF pour contrôler un pilote de moteur DRV8231 (vous pourriez également utiliser le pilote MCPWM) pour contrôler un pilote de moteur DRV8231.
En-tête (DRV8231.hpp)
DRV8231.hpp
#pragma once
#include <cstdio>
#include <cstdint>
#include <driver/gpio.h>
#include <driver/ledc.h>
class DRV8231 {
public:
DRV8231(gpio_num_t pwmA, gpio_num_t pwmB, ledc_channel_t channelA, ledc_channel_t channelB);
void Start(uint16_t speed, bool reverse=false);
void Stop();
private:
gpio_num_t pwmA;
gpio_num_t pwmB;
ledc_channel_t channelA;
ledc_channel_t channelB;
};Source (DRV8231.cpp)
DRV8231.cpp
#include "DRV8231.hpp"
#include <algorithm>
DRV8231::DRV8231(gpio_num_t pwmA, gpio_num_t pwmB, ledc_channel_t channelA, ledc_channel_t channelB) {
// Stocker les numéros de broches
this->pwmA = pwmA;
this->pwmB = pwmB;
this->channelA = channelA;
this->channelB = channelB;
// Configurer le timer LEDC pour la génération PWM
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT, // Résolution 8 bits (0-255)
.timer_num = LEDC_TIMER_0,
.freq_hz = 20000, // Définir la fréquence à 100kHz
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
// Configurer le canal LEDC pour pwmA
ledc_channel_config_t ledc_channel_a = {
.gpio_num = pwmA,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = channelA,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 0, // Initialiser avec un cycle de service de 0
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_a));
// Configurer le canal LEDC pour pwmB
ledc_channel_config_t ledc_channel_b = {
.gpio_num = pwmB,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = channelB,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 0, // Initialiser avec un cycle de service de 0
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_b));
}
void DRV8231::Start(uint16_t speed, bool reverse) {
uint16_t duty = std::min(speed, (uint16_t)1023); // Limiter la vitesse à la plage 0-4095
// Définir la direction et le cycle de service
if (reverse) {
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, 1023);
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
} else {
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, 1023); // 100%
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
}
}
void DRV8231::Stop() {
// Définir les deux canaux à 100% de cycle de service (c.-à-d. frein)
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, 1023);
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, 1023);
ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
}Exemple d’utilisation
usage_example.cpp
#include "DRV8231.hpp"
DRV8231 motor(GPIO_NUM_7, GPIO_NUM_8, LEDC_CHANNEL_0, LEDC_CHANNEL_1);
extern "C" void app_main() {
while (true) {
// Initialiser le pilote DRV8231
motor.Start(512); // Démarrer le moteur à demi-vitesse
vTaskDelay(pdMS_TO_TICKS(2000)); // Exécuter pendant 2 secondes
motor.Stop(); // Arrêter le moteur
}
}Check out similar posts by category:
C/C++, Electronics, ESP-IDF
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow