[{"data":1,"prerenderedAt":185},["ShallowReactive",2],{"example-page-encoder-robot":3},{"id":4,"info":5,"parts":6,"wired":20,"refs":21,"nets":22,"moreNets":179,"code":180},"encoder-robot","Both gearmotors carry a Hall encoder on the motor shaft: 64 counts per motor turn, 1920 per wheel turn. The ESP32 counts channel A in an interrupt and reads B for the direction, 3.82 counts per millimetre on the 80 mm wheels. Straight on, the wheel that is ahead by some counts is slowed down, so the robot keeps its line; near the end it slows down and brakes on the mark; turning on the spot, each wheel rolls a quarter of the circle between the wheels. The tape square shows where it should go. Set KS to 0 in the sketch and see it drift, or change SIDE_MM and TURN_COUNTS.",[7,10,13,16,18],[8,9],"resistor",8,[11,12],"gearmotor_enc",2,[14,15],"battery",1,[17,15],"esp32_devkitc",[19,15],"l298n_module",false,{"A1":17,"BT1":14,"ML":11,"MR":11,"R2":8,"R4":8,"R6":8,"R8":8,"U3":19,"R1":8,"R3":8,"R5":8,"R7":8},[23,47,57,64,70,76,82,88,94,100,108,115,122,129,135,141,147,153,159,166,172],{"name":24,"pins":25},"0",[26,29,32,34,36,39,41,43,45],[27,28],"A1","GND",[30,31],"BT1","−",[33,28],"ML",[35,28],"MR",[37,38],"R2","2",[40,38],"R4",[42,38],"R6",[44,38],"R8",[46,28],"U3",{"name":48,"pins":49},"V5",[50,52,54,55],[27,51],"5V",[33,53],"VCC",[35,53],[46,56],"+5V",{"name":58,"pins":59},"VBAT",[60,62],[30,61],"+",[46,63],"+12V",{"name":65,"pins":66},"ENA",[67,69],[27,68],"GPIO25",[46,65],{"name":71,"pins":72},"ENB",[73,75],[27,74],"GPIO14",[46,71],{"name":77,"pins":78},"IN1",[79,81],[27,80],"GPIO26",[46,77],{"name":83,"pins":84},"IN2",[85,87],[27,86],"GPIO27",[46,83],{"name":89,"pins":90},"IN3",[91,93],[27,92],"GPIO32",[46,89],{"name":95,"pins":96},"IN4",[97,99],[27,98],"GPIO33",[46,95],{"name":101,"pins":102},"LA",[103,105],[33,104],"A",[106,107],"R1","1",{"name":109,"pins":110},"LA_3V3",[111,113,114],[27,112],"GPIO34",[106,38],[37,107],{"name":116,"pins":117},"LB",[118,120],[33,119],"B",[121,107],"R3",{"name":123,"pins":124},"LB_3V3",[125,127,128],[27,126],"GPIO35",[121,38],[40,107],{"name":130,"pins":131},"ML_A",[132,133],[33,61],[46,134],"OUT1",{"name":136,"pins":137},"ML_B",[138,139],[33,31],[46,140],"OUT2",{"name":142,"pins":143},"MR_A",[144,145],[35,31],[46,146],"OUT3",{"name":148,"pins":149},"MR_B",[150,151],[35,61],[46,152],"OUT4",{"name":154,"pins":155},"RA",[156,157],[35,119],[158,107],"R5",{"name":160,"pins":161},"RA_3V3",[162,164,165],[27,163],"GPIO36\u002FVP",[158,38],[42,107],{"name":167,"pins":168},"RB",[169,170],[35,104],[171,107],"R7",{"name":173,"pins":174},"RB_3V3",[175,177,178],[27,176],"GPIO39\u002FVN",[171,38],[44,107],0,{"file":181,"lang":182,"text":183,"lines":184,"more":20},"sketch.ino","cpp","\u002F\u002F Exact square with wheel encoders: each gearmotor has a Hall encoder on its motor shaft (64 counts\n\u002F\u002F per motor turn, 1920 per wheel turn at 30:1). The sketch counts channel A's edges in an interrupt\n\u002F\u002F (960 per wheel turn: 3.82 counts per mm on an 80 mm wheel) and reads B for the direction.\n\u002F\u002F\n\u002F\u002F Straight on: both wheels must turn the same, so the faster one is slowed by how many counts it is\n\u002F\u002F ahead (KS). Near the end it slows down, at the mark it brakes (the L298N shorts the motors). Turning on the spot: each wheel\n\u002F\u002F rolls a quarter of the circle between the wheels (170 mm apart: 133.5 mm, 510 counts). On a real\n\u002F\u002F robot the tyres slip a little: tune TURN_COUNTS until four turns make a full circle.\n\u002F\u002F\n\u002F\u002F Try it: change SIDE_MM or TURN_COUNTS, or set KS to 0 and watch it drift off the square.\n\nconstexpr int ENA = 25, IN1 = 26, IN2 = 27;     \u002F\u002F left motor\nconstexpr int ENB = 14, IN3 = 32, IN4 = 33;     \u002F\u002F right motor\nconstexpr int LA = 34, LB = 35;                 \u002F\u002F left encoder A \u002F B (through 10 k \u002F 20 k dividers)\nconstexpr int RA = 36, RB = 39;                 \u002F\u002F right encoder\n\nconstexpr float COUNTS_PER_MM = 960.0 \u002F (80 * PI);\nconstexpr int SIDE_MM = 500;\nconstexpr long TURN_COUNTS = 510;               \u002F\u002F 90°: a quarter of the 170 mm circle per wheel\nconstexpr float SPEED = 0.45;                   \u002F\u002F cruising PWM share\nconstexpr float MIN_SPEED = 0.18;               \u002F\u002F slower than this the motors stall\nconstexpr float KS = 0.01;                      \u002F\u002F straightness: PWM per count of difference\nconstexpr int PWM_BITS = 10, PWM_MAX = (1 \u003C\u003C PWM_BITS) - 1;\n\nvolatile long cntL = 0, cntR = 0;\n\u002F\u002F A changed: with B equal it went back, else forward\nvoid IRAM_ATTR isrL() { cntL += digitalRead(LA) == digitalRead(LB) ? -1 : 1; }\nvoid IRAM_ATTR isrR() { cntR += digitalRead(RA) == digitalRead(RB) ? -1 : 1; }\n\nvoid motor(int en, int a, int b, float u) {\n  u = constrain(u, -1.0f, 1.0f);\n  digitalWrite(a, u > 0 ? HIGH : LOW);\n  digitalWrite(b, u > 0 ? LOW : HIGH);\n  ledcWrite(en, (int)(fabsf(u) * PWM_MAX));\n}\nvoid drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); }\n\u002F\u002F brake: both inputs low with the bridge enabled short each motor (it stops at once instead of rolling on)\nvoid brake() {\n  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);\n  ledcWrite(ENA, PWM_MAX); ledcWrite(ENB, PWM_MAX);\n}\n\n\u002F\u002F move until each wheel has rolled `target` counts (dirL \u002F dirR: +1 forward, -1 back)\nvoid move(long target, int dirL, int dirR) {\n  long l0 = cntL, r0 = cntR;                                       \u002F\u002F 32-bit reads are atomic\n  unsigned long t0 = millis();\n  while (true) {\n    long l = (cntL - l0) * dirL, r = (cntR - r0) * dirR;\n    long done = (l + r) \u002F 2;\n    if (done >= target) break;\n    float ramp = min(1.0f, (millis() - t0) \u002F 600.0f);               \u002F\u002F soft start over 0.6 s\n    float brake = (target - done) \u002F (3.0f * COUNTS_PER_MM * 25);    \u002F\u002F slow down over the last 75 mm\n    float u = max(MIN_SPEED, SPEED * min(ramp, min(1.0f, brake)));\n    float k = KS * (l - r);                                         \u002F\u002F the wheel ahead slows down\n    drive(dirL * (u - k), dirR * (u + k));\n    delay(2);\n  }\n  brake();\n  delay(300);\n  drive(0, 0);\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);\n  for (int p : { LA, LB, RA, RB }) pinMode(p, INPUT);\n  attachInterrupt(digitalPinToInterrupt(LA), isrL, CHANGE);\n  attachInterrupt(digitalPinToInterrupt(RA), isrR, CHANGE);\n  ledcAttach(ENA, 1000, PWM_BITS);\n  ledcAttach(ENB, 1000, PWM_BITS);\n  drive(0, 0);\n  Serial.println(\"encoder robot ready\");\n}\n\nint side = 1;\n\nvoid loop() {\n  move(SIDE_MM * COUNTS_PER_MM, 1, 1);\n  Serial.printf(\"side %d: L %ld R %ld counts\\n\", side, cntL, cntR);\n  move(TURN_COUNTS, -1, 1);                                         \u002F\u002F left on the spot\n  side = side % 4 + 1;\n}",82,1796304400804]