m5stack-encoder-motor-closed-loop.ino

M5Stack Encoder Motor Driver Unit (DRV8837DSGR) · File : m5stack-encoder-motor-closed-loop.ino · Language : C++ (Arduino) · Use case : Mettre en œuvre un contrôle en boucle fermée pour maintenir une vitesse cible constante malgré les charges.

Configuring PID/closed-loop mode and reading actual speed.

This example activates closed-loop control mode by writing to the mode register. It then sets a target speed and reads the current speed via the integrated encoder. Register addresses are indicative; the official M5Stack library handles complex PID parameters. The unit's AB encoder provides feedback data to the STM32G031.

m5stack-encoder-motor-closed-loop.ino

#include <Wire.h>

#define I2C_ADDRESS 0x2F
#define REG_TARGET_SPEED 0x0B // Exemple registre cible
#define REG_CURRENT_SPEED 0x0C // Exemple registre lecture

void setup() {
  Serial.begin(115200);
  Wire.begin();
  
  // Configurer le mode boucle fermée
  // Note: Utiliser les fonctions de la bibliothèque M5Stack-Encoder-Motor-Driver si disponible
  // Sinon, écriture manuelle aux registres de configuration
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(0x00); // Registre de mode
  Wire.write(0x01); // Activer boucle fermée
  Wire.endTransmission();
}

void loop() {
  uint8_t targetSpeed = 100;
  uint8_t currentSpeed = 0;
  
  // Définir la vitesse cible
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(REG_TARGET_SPEED);
  Wire.write(targetSpeed);
  Wire.endTransmission();
  
  // Lecture de la vitesse actuelle (pour debug)
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(REG_CURRENT_SPEED);
  Wire.endTransmission(false);
  Wire.requestFrom(I2C_ADDRESS, 1);
  if(Wire.available()) {
    currentSpeed = Wire.read();
    Serial.print("Target: "); Serial.print(targetSpeed);
    Serial.print(" Current: "); Serial.println(currentSpeed);
  }
  delay(500);
}

← Back to the board