deckle studioIm Editor öffnen

Genaues Quadrat mit Radencodern (ESP32, zwei 37D-Getriebemotoren mit Encoder)

Ein Zweirad-Roboter zählt die Umdrehungen seiner Räder und fährt ein genaues 50-cm-Quadrat.

  • L298N
  • Gearmotors with encoders
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.
Genaues Quadrat mit Radencodern (ESP32, zwei 37D-Getriebemotoren mit Encoder) in der Welt-Ansicht
In the World view

So funktioniert es

Beide Getriebemotoren tragen einen Hall-Encoder auf der Motorwelle: 64 Zählschritte pro Motorumdrehung, 1920 pro Radumdrehung. Der ESP32 zählt Kanal A per Interrupt und liest B für die Richtung, 3,82 Schritte pro Millimeter bei 80-mm-Rädern. Geradeaus wird das Rad gebremst, das einige Schritte voraus ist, so hält der Roboter die Spur; kurz vor dem Ziel wird er langsamer und bremst auf der Marke; beim Drehen auf der Stelle rollt jedes Rad ein Viertel des Kreises zwischen den Rädern. Das Quadrat aus Klebeband zeigt, wohin er fahren soll. Setz KS im Sketch auf 0 und sieh zu, wie er abdriftet, oder ändere SIDE_MM und TURN_COUNTS.

Verdrahtung

Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.

NetzPins
GNDA1 GNDBT1 −ML GNDMR GNDR2 2R4 2R6 2R8 2U3 GND
V5A1 5VML VCCMR VCCU3 +5V
VBATBT1 +U3 +12V
ENAA1 GPIO25U3 ENA
ENBA1 GPIO14U3 ENB
IN1A1 GPIO26U3 IN1
IN2A1 GPIO27U3 IN2
IN3A1 GPIO32U3 IN3
IN4A1 GPIO33U3 IN4
LAML AR1 1
LA_3V3A1 GPIO34R1 2R2 1
LBML BR3 1
LB_3V3A1 GPIO35R3 2R4 1
ML_AML +U3 OUT1
ML_BML −U3 OUT2
MR_AMR −U3 OUT3
MR_BMR +U3 OUT4
RAMR BR5 1
RA_3V3A1 GPIO36/VPR5 2R6 1
RBMR AR7 1
RB_3V3A1 GPIO39/VNR7 2R8 1

Bauteile

Firmware

sketch.ino · 82 Zeilen

// Exact square with wheel encoders: each gearmotor has a Hall encoder on its motor shaft (64 counts
// per motor turn, 1920 per wheel turn at 30:1). The sketch counts channel A's edges in an interrupt
// (960 per wheel turn: 3.82 counts per mm on an 80 mm wheel) and reads B for the direction.
//
// Straight on: both wheels must turn the same, so the faster one is slowed by how many counts it is
// ahead (KS). Near the end it slows down, at the mark it brakes (the L298N shorts the motors). Turning on the spot: each wheel
// rolls a quarter of the circle between the wheels (170 mm apart: 133.5 mm, 510 counts). On a real
// robot the tyres slip a little: tune TURN_COUNTS until four turns make a full circle.
//
// Try it: change SIDE_MM or TURN_COUNTS, or set KS to 0 and watch it drift off the square.

constexpr int ENA = 25, IN1 = 26, IN2 = 27;     // left motor
constexpr int ENB = 14, IN3 = 32, IN4 = 33;     // right motor
constexpr int LA = 34, LB = 35;                 // left encoder A / B (through 10 k / 20 k dividers)
constexpr int RA = 36, RB = 39;                 // right encoder

constexpr float COUNTS_PER_MM = 960.0 / (80 * PI);
constexpr int SIDE_MM = 500;
constexpr long TURN_COUNTS = 510;               // 90°: a quarter of the 170 mm circle per wheel
constexpr float SPEED = 0.45;                   // cruising PWM share
constexpr float MIN_SPEED = 0.18;               // slower than this the motors stall
constexpr float KS = 0.01;                      // straightness: PWM per count of difference
constexpr int PWM_BITS = 10, PWM_MAX = (1 << PWM_BITS) - 1;

volatile long cntL = 0, cntR = 0;
// A changed: with B equal it went back, else forward
void IRAM_ATTR isrL() { cntL += digitalRead(LA) == digitalRead(LB) ? -1 : 1; }
void IRAM_ATTR isrR() { cntR += digitalRead(RA) == digitalRead(RB) ? -1 : 1; }

void motor(int en, int a, int b, float u) {
  u = constrain(u, -1.0f, 1.0f);
  digitalWrite(a, u > 0 ? HIGH : LOW);
  digitalWrite(b, u > 0 ? LOW : HIGH);
  ledcWrite(en, (int)(fabsf(u) * PWM_MAX));
}
void drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); }
// brake: both inputs low with the bridge enabled short each motor (it stops at once instead of rolling on)
void brake() {
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
  ledcWrite(ENA, PWM_MAX); ledcWrite(ENB, PWM_MAX);
}

// move until each wheel has rolled `target` counts (dirL / dirR: +1 forward, -1 back)
void move(long target, int dirL, int dirR) {
  long l0 = cntL, r0 = cntR;                                       // 32-bit reads are atomic
  unsigned long t0 = millis();
  while (true) {
    long l = (cntL - l0) * dirL, r = (cntR - r0) * dirR;
    long done = (l + r) / 2;
    if (done >= target) break;
    float ramp = min(1.0f, (millis() - t0) / 600.0f);               // soft start over 0.6 s
    float brake = (target - done) / (3.0f * COUNTS_PER_MM * 25);    // slow down over the last 75 mm
    float u = max(MIN_SPEED, SPEED * min(ramp, min(1.0f, brake)));
    float k = KS * (l - r);                                         // the wheel ahead slows down
    drive(dirL * (u - k), dirR * (u + k));
    delay(2);
  }
  brake();
  delay(300);
  drive(0, 0);
}

void setup() {
  Serial.begin(115200);
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
  for (int p : { LA, LB, RA, RB }) pinMode(p, INPUT);
  attachInterrupt(digitalPinToInterrupt(LA), isrL, CHANGE);
  attachInterrupt(digitalPinToInterrupt(RA), isrR, CHANGE);
  ledcAttach(ENA, 1000, PWM_BITS);
  ledcAttach(ENB, 1000, PWM_BITS);
  drive(0, 0);
  Serial.println("encoder robot ready");
}

int side = 1;

void loop() {
  move(SIDE_MM * COUNTS_PER_MM, 1, 1);
  Serial.printf("side %d: L %ld R %ld counts\n", side, cntL, cntR);
  move(TURN_COUNTS, -1, 1);                                         // left on the spot
  side = side % 4 + 1;
}
Im Simulator öffnen

Weitere Beispiele: Roboter