relative-position-move.ino

Unit 4-Encoder Motor Driver (DRV8837DSGR) · File : relative-position-move.ino · Language : arduino · Use case : Déplacer un moteur d'un nombre précis de pas (positionnement relatif) pour un bras robotique.

Uses position mode to move motor 2 by 1000 relative steps, useful for precise joint control.

The module calculates distance based on encoder ticks. The isBusy() function allows synchronizing the main code with the end of the physical movement. Check rotation direction according to wiring.

relative-position-move.ino

#include <M5Stack_MotorDriverUnit.h>

M5Stack_MotorDriverUnit motorDriver;

void setup() {
  M5.begin();
  motorDriver.begin();
  // Mode Position (Boucle fermée sur l'encodeur)
  motorDriver.setMode(2, MODE_POSITION);
  // Déplacer le moteur 2 de 1000 pas dans le sens horaire
  motorDriver.moveRelative(2, 1000, 30); // 30% de vitesse max
}

void loop() {
  // Attendre que le mouvement soit terminé
  while (motorDriver.isBusy(2)) {
    delay(10);
  }
  Serial.println("Move completed.");
  delay(2000);
  // Revenir à la position initiale
  motorDriver.moveRelative(2, -1000, 30);
  while (motorDriver.isBusy(2)) delay(10);
}

← Back to the board