[{"data":1,"prerenderedAt":144},["ShallowReactive",2],{"example-page-avoider":3},{"id":4,"info":5,"parts":6,"wired":21,"refs":22,"nets":23,"moreNets":138,"code":139},"avoider","A classic two-wheel robot: two gearmotors with wheels under the back of the board, a ball caster at the front, a 3S pack, an L298N and an ESP32. The HC-SR04 at the front edge measures the room: its beam (the blue cone) hits the walls and boxes, and the echo comes back after the sound's real travel time. Closer than 30 cm, the robot stops, backs off and turns away. Move the boxes and walls in the World while it runs, put it in a corner, or change SPEED and STOP_CM in the sketch.",[7,10,12,15,17,19],[8,9],"gearmotor",2,[11,9],"resistor",[13,14],"battery",1,[16,14],"esp32_devkitc",[18,14],"hcsr04",[20,14],"l298n_module",false,{"A1":16,"BT1":13,"R2":11,"U2":18,"U3":20,"R1":11,"ML":8,"MR":8},[24,40,49,56,63,70,76,82,88,94,100,106,113,119,126,132],{"name":25,"pins":26},"0",[27,30,33,36,38],[28,29],"A1","GND",[31,32],"BT1","−",[34,35],"R2","2",[37,29],"U2",[39,29],"U3",{"name":41,"pins":42},"V5",[43,45,47],[28,44],"5V",[37,46],"VCC",[39,48],"+5V",{"name":50,"pins":51},"VBAT",[52,54],[31,53],"+",[39,55],"+12V",{"name":57,"pins":58},"ECHO",[59,62],[60,61],"R1","1",[37,57],{"name":64,"pins":65},"ECHO_3V3",[66,68,69],[28,67],"GPIO18",[60,35],[34,61],{"name":71,"pins":72},"ENA",[73,75],[28,74],"GPIO25",[39,71],{"name":77,"pins":78},"ENB",[79,81],[28,80],"GPIO14",[39,77],{"name":83,"pins":84},"IN1",[85,87],[28,86],"GPIO26",[39,83],{"name":89,"pins":90},"IN2",[91,93],[28,92],"GPIO27",[39,89],{"name":95,"pins":96},"IN3",[97,99],[28,98],"GPIO32",[39,95],{"name":101,"pins":102},"IN4",[103,105],[28,104],"GPIO33",[39,101],{"name":107,"pins":108},"ML_A",[109,111],[110,53],"ML",[39,112],"OUT1",{"name":114,"pins":115},"ML_B",[116,117],[110,32],[39,118],"OUT2",{"name":120,"pins":121},"MR_A",[122,124],[123,32],"MR",[39,125],"OUT3",{"name":127,"pins":128},"MR_B",[129,130],[123,53],[39,131],"OUT4",{"name":133,"pins":134},"TRIG",[135,137],[28,136],"GPIO5",[37,133],0,{"file":140,"lang":141,"text":142,"lines":143,"more":21},"sketch.ino","cpp","\u002F\u002F Obstacle-avoiding robot: an ESP32 drives two gearmotors through an L298N and looks ahead with an\n\u002F\u002F HC-SR04. It drives straight on; when something is closer than 30 cm it stops, backs off a little,\n\u002F\u002F turns away (alternately left and right) and drives on. Every measurement goes to the serial console.\n\u002F\u002F\n\u002F\u002F The HC-SR04 runs on 5 V: its ECHO comes through a 1 kΩ \u002F 2 kΩ divider to the 3.3 V input.\n\u002F\u002F Every change of speed or direction is ramped (RAMP_MS): the 30:1 gearmotors are strong, a sudden\n\u002F\u002F full start makes the robot rear up, a sudden reverse tips it back.\n\u002F\u002F Try it: move the boxes in the World, put the robot in a corner, change SPEED or STOP_CM.\n\n\u002F\u002F pins\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 TRIG = 5, ECHO = 18;              \u002F\u002F HC-SR04\n\n\u002F\u002F behaviour\nconstexpr float SPEED = 0.45;                   \u002F\u002F driving speed: share of full PWM\nconstexpr float TURN = 0.40;                    \u002F\u002F turning on the spot\nconstexpr float STOP_CM = 30;                   \u002F\u002F closer than this: avoid\nconstexpr int BACK_MS = 350, TURN_MS = 450;     \u002F\u002F how long to back off and to turn\nconstexpr int RAMP_MS = 250;                    \u002F\u002F soft changes: from one command to the next in this time\nconstexpr int PWM_BITS = 10, PWM_MAX = (1 \u003C\u003C PWM_BITS) - 1;\n\n\u002F\u002F a signed command -1..1 for one motor: direction pins and PWM duty\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}\nfloat curL = 0, curR = 0;\nvoid drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); curL = left; curR = right; }\n\u002F\u002F from the present command to the new one in steps of 10 ms over ms milliseconds\nvoid rampTo(float left, float right, int ms) {\n  int n = max(1, ms \u002F 10);\n  float l0 = curL, r0 = curR;\n  for (int k = 1; k \u003C= n; k++) { drive(l0 + (left - l0) * k \u002F n, r0 + (right - r0) * k \u002F n); delay(10); }\n}\n\n\u002F\u002F one measurement: the echo's length is the sound's way there and back (343 m\u002Fs); no echo within\n\u002F\u002F 30 ms (beyond 4 m): nothing ahead\nfloat distanceCm() {\n  digitalWrite(TRIG, LOW); delayMicroseconds(2);\n  digitalWrite(TRIG, HIGH); delayMicroseconds(10);\n  digitalWrite(TRIG, LOW);\n  unsigned long us = pulseIn(ECHO, HIGH, 30000);\n  return us ? us * 0.0343f \u002F 2 : 999;\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);\n  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);\n  ledcAttach(ENA, 1000, PWM_BITS);\n  ledcAttach(ENB, 1000, PWM_BITS);\n  drive(0, 0);\n  Serial.println(\"obstacle robot ready\");\n}\n\nbool turnLeft = true;\n\nvoid loop() {\n  float d = distanceCm();\n  if (d \u003C STOP_CM) {\n    Serial.printf(\"obstacle at %.0f cm: back off and turn %s\\n\", d, turnLeft ? \"left\" : \"right\");\n    rampTo(0, 0, RAMP_MS);\n    rampTo(-SPEED, -SPEED, RAMP_MS); delay(BACK_MS);\n    rampTo(0, 0, RAMP_MS);\n    if (turnLeft) rampTo(-TURN, TURN, RAMP_MS); else rampTo(TURN, -TURN, RAMP_MS);\n    delay(TURN_MS);\n    rampTo(0, 0, RAMP_MS);\n    turnLeft = !turnLeft;\n  } else {\n    if (curL != SPEED || curR != SPEED) rampTo(SPEED, SPEED, 2 * RAMP_MS);\n    static uint32_t lastPrint = 0;\n    if (millis() - lastPrint > 500) { lastPrint = millis(); if (d \u003C 999) Serial.printf(\"free ahead: %.0f cm\\n\", d); else Serial.println(\"free ahead: no echo\"); }\n  }\n  delay(60);   \u002F\u002F the data sheet's measurement cycle: 60 ms, so an old echo is not taken for a new one\n}",78,1796304400768]