{"id":693,"date":"2026-06-11T13:57:13","date_gmt":"2026-06-11T05:57:13","guid":{"rendered":"https:\/\/projectleon.net\/make\/blog\/self-driving-rc-deepracer-esp32-machine-learning-farsi\/"},"modified":"2026-06-11T13:57:14","modified_gmt":"2026-06-11T05:57:14","slug":"self-driving-rc-deepracer-esp32-machine-learning-farsi","status":"publish","type":"post","link":"https:\/\/projectleon.net\/make\/blog\/self-driving-rc-deepracer-esp32-machine-learning-farsi\/","title":{"rendered":"\u0633\u0627\u062e\u062a \u0645\u0627\u0634\u06cc\u0646 RC \u062e\u0648\u062f\u0631\u0627\u0646 \u0628\u0627 DeepRacer \u0648 ESP32: \u06cc\u0627\u062f\u06af\u06cc\u0631\u06cc \u0645\u0627\u0634\u06cc\u0646 \u0631\u0648\u06cc \u0645\u0633\u06cc\u0631 (\u0641\u0627\u0631\u0633\u06cc)"},"content":{"rendered":"<div class='hero-image-container' style='margin-bottom:28px;border-radius:14px;overflow:hidden;box-shadow:0 4px 18px rgba(0,0,0,0.08)'>\n<img src='https:\/\/projectleon.net\/make\/wp-content\/uploads\/2026\/06\/post130-self-driving.jpg' alt='\u0645\u0627\u0634\u06cc\u0646 RC \u062e\u0648\u062f\u0631\u0627\u0646 DeepRacer ESP32 OpenCV \u06cc\u0627\u062f\u06af\u06cc\u0631\u06cc \u0645\u0627\u0634\u06cc\u0646' style='width:100%;height:auto;display:block' loading='eager'\/>\n<\/div>\n<div class='beginner-intro' style='background:#f0f7ff;border-right:4px solid #2e5fb6;padding:16px 20px;border-radius:10px;margin-bottom:22px'>\n<h2 style='margin-top:0'>\u0645\u0627\u0634\u06cc\u0646 \u062e\u0648\u062f\u0631\u0627\u0646 DIY \u0686\u06cc\u0633\u062a\u061f (\u0645\u0628\u062a\u062f\u06cc)<\/h2>\n<p>\u06cc\u06a9 <strong>\u0645\u0627\u0634\u06cc\u0646 \u062e\u0648\u062f\u0631\u0627\u0646 DIY<\/strong> \u06cc\u06a9 \u0645\u0627\u0634\u06cc\u0646 RC \u0627\u0633\u062a \u06a9\u0647 \u0628\u0647 \u062c\u0627\u06cc \u062f\u0631\u06cc\u0627\u0641\u062a \u062f\u0633\u062a\u0648\u0631 \u0627\u0632 \u0631\u06cc\u0645\u0648\u062a\u060c \u0628\u0627 <strong>\u0628\u06cc\u0646\u0627\u06cc\u06cc \u0645\u0627\u0634\u06cc\u0646<\/strong> (Computer Vision) \u062e\u0648\u062f\u0634 \u0645\u0633\u06cc\u0631 \u0631\u0627 \u0645\u06cc\u200c\u0628\u06cc\u0646\u062f \u0648 \u062f\u0646\u0628\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f. \u0633\u0627\u062f\u0647\u200c\u062a\u0631\u06cc\u0646 \u0631\u0648\u0634 <strong>Line Following<\/strong> \u0627\u0633\u062a \u2014 \u062f\u0648\u0631\u0628\u06cc\u0646 \u06cc\u06a9 \u062e\u0637 \u0633\u0641\u06cc\u062f \u0631\u0648\u06cc \u0632\u0645\u06cc\u0646 \u0631\u0627 \u0645\u06cc\u200c\u0628\u06cc\u0646\u062f \u0648 \u0645\u0627\u0634\u06cc\u0646 \u0622\u0646 \u0631\u0627 \u062f\u0646\u0628\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/p>\n<p><strong>AWS DeepRacer<\/strong> \u06cc\u06a9 \u067e\u0644\u062a\u0641\u0631\u0645 \u062a\u062c\u0627\u0631\u06cc \u06f4\u06f0\u06f0 \u062f\u0644\u0627\u0631\u06cc \u0627\u0633\u062a \u06a9\u0647 \u0628\u0627 \u06cc\u0627\u062f\u06af\u06cc\u0631\u06cc \u062a\u0642\u0648\u06cc\u062a\u06cc (Reinforcement Learning) \u06a9\u0627\u0631 \u0645\u06cc\u200c\u06a9\u0646\u062f. \u0627\u0645\u0627 \u0628\u0627 \u06cc\u06a9 <strong>ESP32-CAM<\/strong> (\u06f5 \u062f\u0644\u0627\u0631) \u0648 <strong>OpenCV<\/strong> \u0631\u0648\u06cc Raspberry Pi \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u06cc\u06a9 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0634\u0627\u0628\u0647 \u0628\u0627 \u06f5\u06f0 \u062f\u0644\u0627\u0631 \u0633\u0627\u062e\u062a \u06a9\u0647 \u062e\u0637 \u0631\u0627 \u0628\u0627 PID \u062f\u0646\u0628\u0627\u0644 \u06a9\u0646\u062f \u2014 \u067e\u0627\u06cc\u0647 \u06cc\u0627\u062f\u06af\u06cc\u0631\u06cc \u0645\u0627\u0634\u06cc\u0646 \u062f\u0631 \u0631\u0628\u0627\u062a\u06cc\u06a9!<\/p>\n<\/div>\n<div style='border:1px solid #d8d8d8;padding:18px 20px;border-radius:12px;background:#fafafa;margin-bottom:22px'>\n<h2 style='margin-top:0'>\u0645\u062d\u0635\u0648\u0644\u06cc \u06a9\u0647 \u0645\u06cc\u200c\u0633\u0627\u0632\u06cc\u062f<\/h2>\n<p>\u06cc\u06a9 \u0645\u0627\u0634\u06cc\u0646 RC \u062e\u0648\u062f\u0631\u0627\u0646 Line Follower \u0628\u0627 OpenCV PID.<\/p>\n<ul>\n<li><strong>\u0647\u0632\u06cc\u0646\u0647:<\/strong> RC Car + RPi Zero + ESP32-CAM = \u06f5\u06f0 \u062f\u0644\u0627\u0631<\/li>\n<li><strong>\u0627\u0644\u06af\u0648\u0631\u06cc\u062a\u0645:<\/strong> Line Detection + PID Steering<\/li>\n<li><strong>\u067e\u0644\u062a\u0641\u0631\u0645:<\/strong> OpenCV Python \u0631\u0648\u06cc RPi<\/li>\n<\/ul>\n<\/div>\n<p><svg viewBox='0 0 860 200' width='100%' xmlns='http:\/\/www.w3.org\/2000\/svg' style='background:#111;border:1px solid #333;border-radius:12px;margin-bottom:22px'>\n<rect x='0' y='0' width='860' height='200' rx='12' fill='#0d1117'\/>\n<text x='430' y='18' text-anchor='middle' font-size='12' font-family='monospace' fill='#4fc3f7'>\u062a\u0635\u0648\u06cc\u0631 \u062f\u0648\u0631\u0628\u06cc\u0646 \u2014 \u062a\u0634\u062e\u06cc\u0635 \u062e\u0637 \u0628\u0627 OpenCV<\/text>\n<!-- \u062c\u0627\u062f\u0647 -->\n<rect x='30' y='30' width='800' height='150' rx='4' fill='#1a2a1a'\/>\n<!-- \u062e\u0637 -->\n<line x1='430' y1='30' x2='430' y2='180' stroke='#fff' stroke-width='8'\/>\n<!-- \u0645\u0631\u06a9\u0632 \u062a\u0635\u0648\u06cc\u0631 -->\n<line x1='430' y1='30' x2='430' y2='180' stroke='#ff5722' stroke-width='2' stroke-dasharray='5,3' opacity='0.6'\/>\n<!-- \u0645\u0627\u0634\u06cc\u0646 -->\n<rect x='400' y='130' width='60' height='35' rx='4' fill='#e53935'\/>\n<text x='430' y='152' text-anchor='middle' font-size='10' font-family='monospace' fill='#fff'>CAR<\/text>\n<!-- \u062e\u0637\u0627\u06cc \u0627\u0646\u062d\u0631\u0627\u0641 -->\n<line x1='430' y1='115' x2='450' y2='100' stroke='#ff9800' stroke-width='2'\/>\n<text x='460' y='98' font-size='9' font-family='monospace' fill='#ff9800'>error=20px<\/text>\n<!-- Region of Interest -->\n<rect x='150' y='100' width='560' height='80' rx='2' fill='none' stroke='#4fc3f7' stroke-width='1.5' stroke-dasharray='4,3'\/>\n<text x='180' y='115' font-size='9' font-family='monospace' fill='#4fc3f7'>ROI<\/text>\n<text x='430' y='192' text-anchor='middle' font-size='11' font-family='Tahoma' fill='#888'>OpenCV: threshold \u2192 findContours \u2192 centroid \u2192 PID steering<\/text>\n<\/svg><\/p>\n<h2>\u06a9\u062f Python \u2014 Line Following \u0628\u0627 PID<\/h2>\n<pre style='background:#1e1e1e;color:#d4d4d4;padding:14px;border-radius:10px;overflow-x:auto;font-size:13px'><code class='language-python'>\nimport cv2, numpy as np\nimport serial, time\n\n# \u0627\u0631\u062a\u0628\u0627\u0637 \u0628\u0627 ESP32-CAM (MJPEG) \u0648 Arduino (\u0645\u0648\u062a\u0648\u0631):\ncap = cv2.VideoCapture(\"http:\/\/192.168.1.100\/stream\")\nmotor_serial = serial.Serial('\/dev\/ttyUSB0', 115200)\n\n# PID Constants:\nKp, Ki, Kd = 0.4, 0.002, 0.15\nprev_error = integral = 0\nCENTER_X = 320  # \u0646\u0635\u0641 \u0639\u0631\u0636 \u06f6\u06f4\u06f0\n\ndef find_line_center(frame):\n    \"\"\"\u067e\u06cc\u062f\u0627 \u06a9\u0631\u062f\u0646 \u0645\u0631\u06a9\u0632 \u062e\u0637 \u0633\u0641\u06cc\u062f \u062f\u0631 ROI\"\"\"\n    roi = frame[300:400, :, :]  # \u0641\u0642\u0637 \u0642\u0633\u0645\u062a \u067e\u0627\u06cc\u06cc\u0646\n    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)\n    _, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)\n    M = cv2.moments(thresh)\n    if M[\"m00\"] > 5000:\n        cx = int(M[\"m10\"] \/ M[\"m00\"])\n        return cx\n    return None\n\ndef pid_steer(error):\n    global prev_error, integral\n    integral += error\n    derivative = error - prev_error\n    output = Kp*error + Ki*integral + Kd*derivative\n    prev_error = error\n    return int(max(-45, min(45, output)))  # \u0632\u0627\u0648\u06cc\u0647 \u0641\u0631\u0645\u0627\u0646\n\nBASE_SPEED = 60  # 0-100\n\nwhile True:\n    ret, frame = cap.read()\n    if not ret: continue\n\n    cx = find_line_center(frame)\n    if cx is not None:\n        error = cx - CENTER_X\n        steer = pid_steer(error)\n        cmd = f\"S:{BASE_SPEED},T:{steer}\\n\"\n        motor_serial.write(cmd.encode())\n\n        cv2.circle(frame, (cx, 350), 8, (0,255,0), -1)\n        cv2.line(frame, (CENTER_X,0), (CENTER_X,480), (255,0,0), 1)\n    else:\n        motor_serial.write(b\"S:0,T:0\\n\")  # \u062e\u0637 \u0627\u0632 \u062f\u0633\u062a \u0631\u0641\u062a\n\n    cv2.imshow(\"Self Driving\", frame)\n    if cv2.waitKey(1) == ord('q'): break\n<\/code><\/pre>\n<h2>\u06a9\u062f Arduino \u2014 \u062f\u0631\u06cc\u0627\u0641\u062a \u062f\u0633\u062a\u0648\u0631 \u0641\u0631\u0645\u0627\u0646<\/h2>\n<pre style='background:#1e1e1e;color:#d4d4d4;padding:14px;border-radius:10px;overflow-x:auto;font-size:13px'><code class='language-cpp'>\n#include &lt;Servo.h&gt;\nServo steering;\nconst int M_F=6, M_B=7;\n\nvoid setup() {\n  Serial.begin(115200);\n  steering.attach(9);\n  pinMode(M_F,OUTPUT); pinMode(M_B,OUTPUT);\n}\n\nvoid loop() {\n  if(Serial.available()) {\n    String line = Serial.readStringUntil('\\n');\n    \/\/ \u0641\u0631\u0645\u062a: \"S:60,T:-20\"\n    int si = line.indexOf(\"S:\"); int ti = line.indexOf(\",T:\");\n    if(si>=0 && ti>0) {\n      int spd = line.substring(si+2,ti).toInt();\n      int turn = line.substring(ti+3).toInt();\n\n      \/\/ \u062d\u0631\u06a9\u062a \u0645\u0648\u062a\u0648\u0631:\n      if(spd>0){ digitalWrite(M_F,1); digitalWrite(M_B,0); }\n      else      { digitalWrite(M_F,0); digitalWrite(M_B,0); }\n\n      \/\/ \u0641\u0631\u0645\u0627\u0646 \u0633\u0631\u0648 (\u06f9\u06f0 = \u0645\u0633\u062a\u0642\u06cc\u0645\u060c \u00b1\u06f4\u06f5):\n      steering.write(90 + turn);\n    }\n  }\n}\n<\/code><\/pre>\n<h2>\u0686\u06a9\u200c\u0644\u06cc\u0633\u062a \u062e\u0648\u062f\u0631\u0627\u0646<\/h2>\n<table style='width:100%;border-collapse:collapse;margin-bottom:18px'>\n<thead>\n<tr style='background:#e8f0fe'>\n<th style='padding:9px;border:1px solid #ddd;text-align:right'>\u0628\u0631\u0631\u0633\u06cc<\/th>\n<th style='padding:9px;border:1px solid #ddd;text-align:right'>\u0631\u0648\u0634<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style='padding:8px;border:1px solid #ddd'>\u062e\u0637 \u0633\u0641\u06cc\u062f \u0631\u0648\u06cc \u0632\u0645\u06cc\u0646 \u062a\u0627\u0631\u06cc\u06a9<\/td>\n<td style='padding:8px;border:1px solid #ddd'>\u06a9\u0627\u063a\u0630 A4 \u06cc\u0627 \u0646\u0648\u0627\u0631 \u0633\u0641\u06cc\u062f<\/td>\n<\/tr>\n<tr style='background:#fafafa'>\n<td style='padding:8px;border:1px solid #ddd'>Threshold \u062f\u0631\u0633\u062a \u062a\u0646\u0638\u06cc\u0645\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>\u0628\u0627 trackbar (cv2.createTrackbar)<\/td>\n<\/tr>\n<tr>\n<td style='padding:8px;border:1px solid #ddd'>PID \u0627\u0648\u0633\u06cc\u0644\u0647 \u0646\u0645\u06cc\u200c\u0632\u0646\u062f\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>Kp \u0631\u0627 \u06a9\u0645 \u06a9\u0646\u06cc\u062f<\/td>\n<\/tr>\n<tr style='background:#fafafa'>\n<td style='padding:8px;border:1px solid #ddd'>\u062a\u0623\u062e\u06cc\u0631 \u0648\u06cc\u062f\u06cc\u0648 \u0632\u06cc\u0631 \u06f1\u06f0\u06f0ms\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>cap.set(CAP_PROP_BUFFERSIZE,1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><svg viewBox='0 0 860 200' width='100%' xmlns='http:\/\/www.w3.org\/2000\/svg' style='background:#fff;border:1px solid #e0e0e0;border-radius:12px;margin-bottom:22px'>\n<rect x='40' y='75' width='155' height='60' rx='12' fill='#f7c6c6' stroke='#a03030' stroke-width='2'\/>\n<rect x='245' y='75' width='155' height='60' rx='12' fill='#8bd3dd' stroke='#1f6f78' stroke-width='2'\/>\n<rect x='450' y='75' width='155' height='60' rx='12' fill='#f7d794' stroke='#a06d00' stroke-width='2'\/>\n<rect x='655' y='75' width='165' height='60' rx='12' fill='#b7e4a0' stroke='#3a7a1e' stroke-width='2'\/>\n<line x1='195' y1='105' x2='245' y2='105' stroke='#555' stroke-width='2.5' stroke-dasharray='6,3'\/>\n<line x1='400' y1='105' x2='450' y2='105' stroke='#555' stroke-width='2.5'\/>\n<line x1='605' y1='105' x2='655' y2='105' stroke='#555' stroke-width='2.5'\/>\n<text x='117' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>ESP32-CAM<\/text>\n<text x='117' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>MJPEG WiFi<\/text>\n<text x='322' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>RPi OpenCV<\/text>\n<text x='322' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>PID Line<\/text>\n<text x='527' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>Arduino<\/text>\n<text x='527' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>\u0641\u0631\u0645\u0627\u0646 \u0645\u0648\u062a\u0648\u0631<\/text>\n<text x='737' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>\u0645\u0627\u0634\u06cc\u0646 \u062e\u0648\u062f\u0631\u0627\u0646<\/text>\n<text x='737' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>\u062f\u0646\u0628\u0627\u0644 \u062e\u0637<\/text>\n<text x='430' y='175' text-anchor='middle' font-size='13' font-family='Tahoma' fill='#666'>\u062e\u0648\u062f\u0631\u0627\u0646 \u2014 ESP32-CAM \u2192 OpenCV PID \u2192 Arduino \u2192 \u062f\u0646\u0628\u0627\u0644 \u062e\u0637<\/text>\n<\/svg><\/p>\n<h2>\u0648\u06cc\u062f\u0626\u0648\u0647\u0627\u06cc \u0622\u0645\u0648\u0632\u0634\u06cc<\/h2>\n<div style='display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:18px;margin:20px 0'>\n<iframe width='100%' height='315' src='https:\/\/www.youtube.com\/embed\/Cd-oRQnT-s8' title='OpenCV Line Following Robot Python PID' frameborder='0' allow='accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture' allowfullscreen><\/iframe><br \/>\n<iframe width='100%' height='315' src='https:\/\/www.youtube.com\/embed\/3p_gfnZbGRg' title='Self Driving Car DIY OpenCV Raspberry Pi' frameborder='0' allow='accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture' allowfullscreen><\/iframe>\n<\/div>\n<h2>\u0645\u0646\u0627\u0628\u0639 \u0628\u06cc\u0634\u062a\u0631<\/h2>\n<ul>\n<li><a href='https:\/\/www.aparat.com\/result\/%D9%85%D8%A7%D8%B4%DB%8C%D9%86_%D8%AE%D9%88%D8%AF%D8%B1%D8%A7%D9%86_OpenCV' target='_blank'>\u0622\u067e\u0627\u0631\u0627\u062a: \u0645\u0627\u0634\u06cc\u0646 \u062e\u0648\u062f\u0631\u0627\u0646 OpenCV \u0641\u0627\u0631\u0633\u06cc<\/a><\/li>\n<li><a href='https:\/\/aws.amazon.com\/deepracer\/' target='_blank'>AWS DeepRacer: Reinforcement Learning Platform<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u0622\u0645\u0648\u0632\u0634 \u0633\u0627\u062e\u062a \u0645\u0627\u0634\u06cc\u0646 RC \u062e\u0648\u062f\u0631\u0627\u0646 \u0628\u0627 ESP32-CAM\u060c OpenCV\u060c \u0648 \u06cc\u06a9 \u0634\u0628\u06a9\u0647 \u0639\u0635\u0628\u06cc \u0633\u0627\u062f\u0647\u061b \u062a\u0634\u062e\u06cc\u0635 \u062e\u0637 \u0645\u0633\u06cc\u0631\u060c PID \u0631\u0648\u06cc \u062e\u0637\u060c \u0648 \u0645\u0642\u0627\u06cc\u0633\u0647 \u0628\u0627 \u067e\u0644\u062a\u0641\u0631\u0645 AWS DeepRacer.<\/p>\n","protected":false},"author":1,"featured_media":692,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[16,20],"tags":[488,63,349,73,50,490,487,489],"class_list":["post-693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ugv","category-projects-2","tag-deepracer","tag-esp32-cam","tag-opencv","tag-pid","tag-50","tag-490","tag-487","tag-489"],"_links":{"self":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":1,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/693\/revisions\/694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/media\/692"}],"wp:attachment":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}